顶层目录下的config.mk文件分析

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了顶层目录下的config.mk文件分析相关的知识,希望对你有一定的参考价值。

顶层目录下的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文件分析的主要内容,如果未能解决你的问题,请参考以下文章

u-boot顶层目录config.mk分析

顶层Makefile分析

嵌入式开发(S5PV210)——u-boot的顶层config.mk分析

config.mk 文件详细分析

主Makefile配置文件config.mk的分析

uboot主Makefile分析(t配置和编译过程详解)