1. 程式人生 > >頂層目錄下的config.mk文件分析

頂層目錄下的config.mk文件分析

option cpu 宏定義 stc 過程 數據 mit 目錄 iso

頂層目錄下的config.mk文件主要完成如下功能的配置:
1、確定生成可執行文件過程中需要的各種工具,如編譯器(arm-linux-gcc)、連接器(arm-linux-ld)、反匯編器(arm-linux-objdump)等
2、確定CPU、板相關的配置文件,存在於各個目錄下的config.mk
3、確定編譯、鏈接、轉換等過程的操作選項

4、根據步驟3確定的編譯連接選項生成需要的文件

config.mk完整內容及必要註釋如下
:config.mk文件註釋符改為/* 註釋內容 */

  1 ifneq ($(OBJTREE),$(SRCTREE))
  2     ifeq ($(CURDIR),$(SRCTREE))
3 dir := 4 else 5 dir := $(subst $(SRCTREE)/,,$(CURDIR)) 6 endif 7 8 obj := $(if $(dir),$(OBJTREE)/$(dir)/,$(OBJTREE)/) 9 src := $(if $(dir),$(SRCTREE)/$(dir)/,$(SRCTREE)/) 10 11 $(shell mkdir -p $(obj)) 12 else 13 obj := 14 src := 15 endif 16
/* obj = 空,src = 空 17 * dir = 空 18 */ 19 20 /* clean the slate ... */ 21 PLATFORM_RELFLAGS = 22 PLATFORM_CPPFLAGS = 23 PLATFORM_LDFLAGS = 24 25 /* HOSTCFLAGS = -Wall -Wstrict-prototypes -O2 -fomit-frame-pointer 26 * -Wall: 打印出編譯時所有的錯誤或警告信息 27 * -Wstrict-prototypes: 編譯時,若產生與數據類型不相符的問題,打印出提示或警告信息。當在不同體系結構間移植時,加上該選項可避免很多錯誤
28 * -O: 編譯代碼時的優化等級,共有五種:-O0、-O1、-O2、-O3和-Os 29 * -fomit-frame-pointer: 對於不需要幀指針的函數,不要在寄存器中保存幀指針 30 * 代碼優化時打開-fomit-frame-pointer,函數調用時不保存frame指針,也就不能用backtrace()來查看函數棧調用 31 * backtrace()系列函數見[http://blog.csdn.net/u013686019/article/details/42128771](Linux中backtrace()系列函數的應用實例) 32 */ 33 HOSTCFLAGS = -Wall -Wstrict-prototypes -O2 -fomit-frame-pointer 34 $(HOSTCPPFLAGS) 35 /* HOSTSTRIP = strip 36 * strip能清除執行文件中不必要的標示符及調試信息,可減小文件大小而不影響正常使用,、 37 * 與壓縮不同的是,文件一旦strip後就不能恢復原樣 38 * strip後的文件不包含調試信息 39 */ 40 HOSTSTRIP = strip 41 42 /* 43 * Mac OS X / Darwin‘s C preprocessor is Apple specific. It 44 * generates numerous errors and warnings. We want to bypass it 45 * and use GNU C‘s cpp. To do this we pass the -traditional-cpp 46 * option to the compiler. Note that the -traditional-cpp flag 47 * DOES NOT have the same semantics as GNU C‘s flag, all it does 48 * is invoke the GNU preprocessor in stock ANSI/ISO C fashion. 49 * 50 * Apple‘s linker is similar, thanks to the new 2 stage linking 51 * multiple symbol definitions are treated as errors, hence the 52 * -multiply_defined suppress option to turn off this error. 53 */ 54 ifeq ($(HOSTOS),darwin) 55 ...... 56 else 57 HOSTCC = gcc 58 endif 59 60 ifeq ($(HOSTOS),cygwin) 61 ...... 62 endif 63 64 /* We build some files with extra pedantic flags to try to minimize things 65 * that won‘t build on some weird host compiler -- though there are lots of 66 * exceptions for files that aren‘t complaint. 67 */ 68 HOSTCFLAGS_NOPED = $(filter-out -pedantic,$(HOSTCFLAGS)) 69 /* -pedantic: 當GCC在編譯不符合ANSI/ISO C語言標準的源代碼時,如果在編譯指令中加上了-pedantic選項 70 * 那麽源程序中使用了擴展語法的地方將產生相應的警告信息 71 */ 72 HOSTCFLAGS += -pedantic 73 74 ######################################################################### 75 /* Option checker (courtesy linux kernel) to ensure 76 * only supported compiler options are used 77 * cc-option變量保存了一個測試編譯選項的命令,其他地方會經常用call函數來調用它,測試編譯選項 78 * if $(CC) $(CFLAGS) $(1) -S -o /dev/null -xc /dev/null > /dev/null 2>&1;then 79 echo "$(1)"; 80 else 81 echo "$(2)"; 82 fi; 83 * -S:編譯後立即結束,不進行<a href="http://www.it165.net/pro/yysam/" target="_blank" class="keylink">匯編</a>等操作 84 * -o /dev/null : 生成文件到/dev/null,即不生成任何編譯結果,要編譯的文件也為空 85 * -xc: 指定按c語言編譯 86 * 用此語句如:call cc-option,-a,-b 則如果支持-a選項則返回-a否則返回-b 87 */ 88 cc-option = $(shell if $(CC) $(CFLAGS) $(1) -S -o /dev/null -xc /dev/null 89 > /dev/null 2>&1; then echo "$(1)"; else echo "$(2)"; fi ;) 90 91 /* Include the make variables (CC, etc...) */ 92 AS = $(CROSS_COMPILE)as /* 匯編工具 */ 93 LD = $(CROSS_COMPILE)ld /* 鏈接工具 */ 94 CC = $(CROSS_COMPILE)gcc /* 編譯工具 */ 95 CPP = $(CC) -E /* 預處理 */ 96 AR = $(CROSS_COMPILE)ar /* 歸檔工具 */ 97 NM = $(CROSS_COMPILE)nm /* 列出object文件中的符號 */ 98 LDR = $(CROSS_COMPILE)ldr 99 STRIP = $(CROSS_COMPILE)strip 100 OBJCOPY = $(CROSS_COMPILE)objcopy /* 轉換可執行文件格式工具 */ 101 OBJDUMP = $(CROSS_COMPILE)objdump /* 反匯編工具 */ 102 RANLIB = $(CROSS_COMPILE)RANLIB /* 產生歸檔文件索引 */ 103 104 ######################################################################### 105 /* Load generated board configuration */ 106 /* sinclude: 107 * 在Makefile中可使用"sinclude"代替"include",用來忽略由於包含文件不存在或者無法創建時的錯誤 108 */ 109 sinclude $(OBJTREE)/include/autoconf.mk 110 111 /* Some architecture config.mk files need to know what CPUDIR is set to, 112 * so calculate CPUDIR before including ARCH/SOC/CPU config.mk files. 113 * Check if arch/$ARCH/cpu/$CPU exists, otherwise assume arch/$ARCH/cpu contains 114 * CPU-specific code. 115 */ 116 CPUDIR=arch/$(ARCH)/cpu/$(CPU) 117 ifneq ($(SRCTREE)/$(CPUDIR),$(wildcard $(SRCTREE)/$(CPUDIR))) 118 CPUDIR=arch/$(ARCH)/cpu 119 endif 120 /* CPUDIR=arch/arm/cpu/arm920t */ 121 122 /* include architecture dependend rules: arch/arm/config.mk */ 123 sinclude $(TOPDIR)/arch/$(ARCH)/config.mk 124 /* include CPU specific rules: arch/arm/cpu/arm920t/config.mk */ 125 sinclude $(TOPDIR)/$(CPUDIR)/config.mk 126 ifdef SOC 127 /* include SoC specific rules: arch/arm/cpu/arm920t/s3c24x0/config.mk */ 128 sinclude $(TOPDIR)/$(CPUDIR)/$(SOC)/config.mk 129 endif 130 ifdef VENDOR 131 BOARDDIR = $(VENDOR)/$(BOARD) 132 else 133 BOARDDIR = $(BOARD) 134 endif 135 /* BOARDDIR = samsung/smdk2410 */ 136 137 ifdef BOARD 138 /* include board specific rules: board/samsung/smdk2410/config.mk */ 139 sinclude $(TOPDIR)/board/$(BOARDDIR)/config.mk 140 endif 141 142 ######################################################################### 143 ifneq (,$(findstring s,$(MAKEFLAGS))) 144 ARFLAGS = cr 145 else 146 ARFLAGS = crv 147 endif 148 RELFLAGS= $(PLATFORM_RELFLAGS) 149 DBGFLAGS= -g # -DDEBUG 150 OPTFLAGS= -Os #-fomit-frame-pointer 151 152 /* LDSCRIPT = arch/arm/cpu/arm920t/u-boot.lds,在在文件arch/arm/config.mk中賦值 */ 153 ifndef LDSCRIPT 154 #LDSCRIPT := $(TOPDIR)/board/$(BOARDDIR)/u-boot.lds.debug 155 ifeq ($(CONFIG_NAND_U_BOOT),y) 156 LDSCRIPT := $(TOPDIR)/board/$(BOARDDIR)/u-boot-nand.lds 157 else 158 LDSCRIPT := $(TOPDIR)/board/$(BOARDDIR)/u-boot.lds 159 endif 160 endif 161 /* 段之間的空隙用0xff填充 */ 162 OBJCFLAGS += --gap-fill=0xff 163 164 gccincdir := $(shell $(CC) -print-file-name=include) 165 166 /* CPPFLAGS變量綜合了DBGFLAGS,OPTFLAGS,RELFLAGS編譯選項,並定義了__KERBEL__ 167 * -D: 設置宏定義__KERNEL__ 168 */ 169 CPPFLAGS := $(DBGFLAGS) $(OPTFLAGS) $(RELFLAGS) 170 -D__KERNEL__ 171 ifneq ($(TEXT_BASE),) 172 CPPFLAGS += -DTEXT_BASE=$(TEXT_BASE) 173 endif 174 175 ifneq ($(RESET_VECTOR_ADDRESS),) 176 CPPFLAGS += -DRESET_VECTOR_ADDRESS=$(RESET_VECTOR_ADDRESS) 177 endif 178 179 ifneq ($(OBJTREE),$(SRCTREE)) 180 CPPFLAGS += -I$(OBJTREE)/include2 -I$(OBJTREE)/include 181 endif 182 183 CPPFLAGS += -I$(TOPDIR)/include 184 CPPFLAGS += -fno-builtin -ffreestanding -nostdinc 185 -isystem $(gccincdir) -pipe $(PLATFORM_CPPFLAGS) 186 187 ifdef BUILD_TAG 188 CFLAGS := $(CPPFLAGS) -Wall -Wstrict-prototypes 189 -DBUILD_TAG="$(BUILD_TAG)" 190 else 191 CFLAGS := $(CPPFLAGS) -Wall -Wstrict-prototypes 192 endif 193 194 CFLAGS += $(call cc-option,-fno-stack-protector) 195 196 /* $(CPPFLAGS) sets -g, which causes gcc to pass a suitable -g<format> */ 197 /* option to the assembler. */ 198 AFLAGS_DEBUG := 199 200 AFLAGS := $(AFLAGS_DEBUG) -D__ASSEMBLY__ $(CPPFLAGS) 201 202 LDFLAGS += -Bstatic -T $(obj)u-boot.lds $(PLATFORM_LDFLAGS) 203 ifneq ($(TEXT_BASE),) 204 LDFLAGS += -Ttext $(TEXT_BASE) 205 endif 206 /* LDFLAGS = -Bstatic -T u-boot.lds -Ttext 0x33F80000 */ 207 /* CFLAGS = -g -Os -fno-common -ffixed-r8 -msoft-float -D__KERNEL__ -DTEXT_BASE=0x33F80000 -I/u-boot-2010.06/include -fno-builtin -ffreestanding -nostdinc -isystem /usr/local/arm/4.2.2-eabi/usr/bin-ccache/../lib/gcc/arm-unknown-linux-gnueabi/4.2.2/include -pipe -DCONFIG_ARM -D__ARM__ -marm -mabi=aapcs-linux -mno-thumb-interwork -march=armv4 -Wall -Wstrict-prototypes -fno-stack-protector */ 208 209 /* Location of a usable BFD library, where we define "usable" as 210 * "built for ${HOST}, supports ${TARGET}". Sensible values are 211 * - When cross-compiling: the root of the cross-environment 212 * - Linux/ppc (native): /usr 213 * - NetBSD/ppc (native): you lose ... (must extract these from the 214 * binutils build directory, plus the native and U-Boot include 215 * files don‘t like each other) 216 * 217 * So far, this is used only by tools/gdb/Makefile. 218 */ 219 ifeq ($(HOSTOS),darwin) 220 BFD_ROOT_DIR = /usr/local/tools 221 else 222 ifeq ($(HOSTARCH),$(ARCH)) 223 /* native */ 224 BFD_ROOT_DIR = /usr 225 else 226 /* BFD_ROOT_DIR = /LinuxPPC/CDK # Linux/i386 */ 227 /* BFD_ROOT_DIR = /usr/pkg/cross # NetBSD/i386 */ 228 BFD_ROOT_DIR = /opt/powerpc 229 endif 230 endif 231 232 ######################################################################### 233 export HOSTCC HOSTCFLAGS HO<a href="http://www.it165.net/pro/" target="_blank" class="keylink">STL</a>DFLAGS PEDCFLAGS HOSTSTRIP CROSS_COMPILE 234 AS LD CC CPP AR NM STRIP OBJCOPY OBJDUMP MAKE 235 export TEXT_BASE PLATFORM_CPPFLAGS PLATFORM_RELFLAGS CPPFLAGS CFLAGS AFLAGS 236 237 ######################################################################### 238 /* 下面幾行規定了各種文件的編譯時用到的編譯選項 */ 239 /* Allow boards to use custom optimize flags on a per dir/file basis */ 240 BCURDIR = $(subst $(SRCTREE)/,,$(CURDIR:$(obj)%=%)) 241 /* BCURDIR = 頂層目錄 */ 242 $(obj)%.s: %.S 243 $(CPP) $(AFLAGS) $(AFLAGS_$(BCURDIR)/$(@F)) $(AFLAGS_$(BCURDIR)) 244 -o [email protected] $< 245 $(obj)%.o: %.S 246 $(CC) $(AFLAGS) $(AFLAGS_$(BCURDIR)/$(@F)) $(AFLAGS_$(BCURDIR)) 247 -o [email protected] $< -c 248 $(obj)%.o: %.c 249 $(CC) $(CFLAGS) $(CFLAGS_$(BCURDIR)/$(@F)) $(CFLAGS_$(BCURDIR)) 250 -o [email protected] $< -c 251 $(obj)%.i: %.c 252 $(CPP) $(CFLAGS) $(CFLAGS_$(BCURDIR)/$(@F)) $(CFLAGS_$(BCURDIR)) 253 -o [email protected] $< -c 254 $(obj)%.s: %.c 255 $(CC) $(CFLAGS) $(CFLAGS_$(BCURDIR)/$(@F)) $(CFLAGS_$(BCURDIR)) 256 -o [email protected] $< -c -S

頂層目錄下的config.mk文件分析