config.mk主要功能是配置单板信息和编译环境,将会被u-boot的makefile所调用,本文仍然侧重于句法分析。
######################################################################### # 脚本所在的目录通常在U-boot源码顶层目录下,所以CURDIR = SRCTREE,所以dir = 空 ifeq ($(CURDIR),$(SRCTREE)) dir := else dir := $(subst $(SRCTREE)/,,$(CURDIR)) endif # 不相等的情况下才会建立相应的目录,这里不会被执行 ifneq ($(OBJTREE),$(SRCTREE)) # Create object files for SPL in a separate directory ifeq ($(CONFIG_SPL_BUILD),y) obj := $(if $(dir),$(SPLTREE)/$(dir)/,$(SPLTREE)/) else obj := $(if $(dir),$(OBJTREE)/$(dir)/,$(OBJTREE)/) endif src := $(if $(dir),$(SRCTREE)/$(dir)/,$(SRCTREE)/) $(shell mkdir -p $(obj)) else # Create object files for SPL in a separate directory # CONFIG_SPL_BUILD = y的时候才会建立执行的存放编译文件的目录,这里也不执行 ifeq ($(CONFIG_SPL_BUILD),y) obj := $(if $(dir),$(SPLTREE)/$(dir)/,$(SPLTREE)/) $(shell mkdir -p $(obj)) else # 所以最后的结果是obj为空,src也为空 obj := endif src := endif # clean the slate ... # 清除标志 PLATFORM_RELFLAGS = PLATFORM_CPPFLAGS = PLATFORM_LDFLAGS = ######################################################################### # HOSTCPPFLAGS的值为空,HOSTCFLAGS = -Wall -Wstrict-prototypes -O2 # -fomit-frame-pointer HOSTCFLAGS = -Wall -Wstrict-prototypes -O2 -fomit-frame-pointer $(HOSTCPPFLAGS) HOSTSTRIP = strip # Mac OS X / Darwin's C preprocessor is Apple specific. It # generates numerous errors and warnings. We want to bypass it # and use GNU C's cpp. To do this we pass the -traditional-cpp # option to the compiler. Note that the -traditional-cpp flag # DOES NOT have the same semantics as GNU C's flag, all it does # is invoke the GNU preprocessor in stock ANSI/ISO C fashion. # # Apple's linker is similar, thanks to the new 2 stage linking # multiple symbol definitions are treated as errors, hence the # -multiply_defined suppress option to turn off this error. # # 如果HOSTOS是darwin体系,则做专门处理 ifeq ($(HOSTOS),darwin) # get major and minor product version (e.g. '10' and '6' for Snow Leopard) DARWIN_MAJOR_VERSION = $(shell sw_vers -productVersion | cut -f 1 -d '.') DARWIN_MINOR_VERSION = $(shell sw_vers -productVersion | cut -f 2 -d '.') os_x_before = $(shell if [ $(DARWIN_MAJOR_VERSION) -le $(1) -a $(DARWIN_MINOR_VERSION) -le $(2) ] ; then echo "$(3)"; else echo "$(4)"; fi ;) # Snow Leopards build environment has no longer restrictions as described above HOSTCC = $(call os_x_before, 10, 5, "cc", "gcc") HOSTCFLAGS += $(call os_x_before, 10, 4, "-traditional-cpp") HOSTLDFLAGS += $(call os_x_before, 10, 5, "-multiply_defined suppress") else # 除了darwin外其他都采用gcc HOSTCC = gcc endif ifeq ($(HOSTOS),cygwin) HOSTCFLAGS += -ansi endif # We build some files with extra pedantic flags to try to minimize things # that won't build on some weird host compiler -- though there are lots of # exceptions for files that aren't complaint. # filter-out为反过滤函数,HOSTCFLAGS中的"-pedantic"全部删掉 # HOSTCFLAGS_NOPED = -Wall -Wstrict-prototypes -O2 -fomit-frame-pointer # HOSTCFLAGS = -Wall -Wstrict-prototypes -O2 -fomit-frame-pointer # -pedantic HOSTCFLAGS_NOPED = $(filter-out -pedantic,$(HOSTCFLAGS)) HOSTCFLAGS += -pedantic ######################################################################### # # Option checker, gcc version (courtesy linux kernel) to ensure # only supported compiler options are used # # 选项检查器,确保支持编译选项的唯一可用的gcc版本 # cc-option-sys=空 cc-option=空 # strip函数的作用是去掉字符串的空格,call函数作用是把它后面的参数一次赋值给$(1),$(2),... # findstring函数是从字符串中超找指定内容,$(findstring $1,$(CC_OPTIONS)) # 就是从CC_OPTIONS从查找$(1)的内容,找到则返回$(1),找不到则返回空 CC_OPTIONS_CACHE_FILE := $(OBJTREE)/include/generated/cc_options.mk CC_TEST_OFILE := $(OBJTREE)/include/generated/cc_test_file.o -include $(CC_OPTIONS_CACHE_FILE) ######################################################################### # cc-option用于检查编译器CC是否支持某选项。将2个选项作为参数传递给cc-option函数,该函数# 调用CC编译器检查参数1是否支持,若支持则函数返回参数1,否则返回参数2 (因此CC编译器必须支# 持参数1或参数2,若两个都不支持则会编译出错)。可以像下面这样调用cc-option函数,并将支持# 的选项添加到FLAGS中: FLAGS +=$(call cc-option,option1,option2) cc-option-sys = $(shell mkdir -p $(dir $(CC_TEST_OFILE)); if $(CC) $(CFLAGS) $(1) -S -xc /dev/null -o $(CC_TEST_OFILE) > /dev/null 2>&1; then echo 'CC_OPTIONS += $(strip $1)' >> $(CC_OPTIONS_CACHE_FILE); echo "$(1)"; fi) ifeq ($(CONFIG_CC_OPT_CACHE_DISABLE),y) cc-option = $(strip $(if $(call cc-option-sys,$1),$1,$2)) else cc-option = $(strip $(if $(findstring $1,$(CC_OPTIONS)),$1, $(if $(call cc-option-sys,$1),$1,$2))) endif ######################################################################### # cc-version = 0404 # binutils-version = 0221 cc-version = $(shell $(SHELL) $(SRCTREE)/tools/gcc-version.sh $(CC)) binutils-version = $(shell $(SHELL) $(SRCTREE)/tools/binutils-version.sh $(AS)) ######################################################################### # Include the make variables (CC, etc...) # AS = as AS = $(CROSS_COMPILE)as ######################################################################### # 链接器固定使用GNU ld # LD = ld LD = $(shell if $(CROSS_COMPILE)ld.bfd -v > /dev/null 2>&1; then echo "$(CROSS_COMPILE)ld.bfd"; else echo "$(CROSS_COMPILE)ld"; fi;) ######################################################################### # 定义交叉工具链集 # CC = gcc # CPP = gcc -E # AR = ar # NM = nm # LDR = ldr # STRIP = strip # OBJCOPY = objcopy # OBJDUMP = objdump # RANLIB = RANLIB # DTC = dtc CC = $(CROSS_COMPILE)gcc CPP = $(CC) -E AR = $(CROSS_COMPILE)ar NM = $(CROSS_COMPILE)nm LDR = $(CROSS_COMPILE)ldr STRIP = $(CROSS_COMPILE)strip OBJCOPY = $(CROSS_COMPILE)objcopy OBJDUMP = $(CROSS_COMPILE)objdump RANLIB = $(CROSS_COMPILE)RANLIB DTC = dtc ######################################################################### # 加载产生的单板配置文件autoconf.mk和config.mk,均在include目录下 # config.mk中内容是ARCH CPU BOARD VENDOR SOC5个变量信息 # autoconf.mk为单板配置宏的集合,打开/关闭 用 y/n表示 sinclude $(OBJTREE)/include/autoconf.mk sinclude $(OBJTREE)/include/config.mk ######################################################################### # Some architecture config.mk files need to know what CPUDIR is set to, # so calculate CPUDIR before including ARCH/SOC/CPU config.mk files. # Check if arch/$ARCH/cpu/$CPU exists, otherwise assume arch/$ARCH/cpu contains # CPU-specific code. # 翻译上面这段英文: # 需要知道CPUDIR的设置来查找一些架构的config.mk文件,所以在加载ARCH/SOC/config.mk和 # ARCH/CPU/config.mk之前要设置好CPUDIR路径。检查目录arch/$ARCH/cpu/$CPU是否存在, # 否则认为arch/$ARCH/cpu目录下包含有CPU相关的代码。翻译完毕! # 看看ifneq的执行: # 首先这里CPUDIR = arch/$(ARCH)/cpu/$(CPU) = arch/arm/cpu/armv7 # wildcard函数用来匹配 arch/arm/cpu/armv7这一目录,意思就是如果不存在这个目录 # 则CPUDIR=arch/$(ARCH)/cpu=arch/arm/cpu,否则就保持采用 # CPUDIR=arch/arm/cpu/armv7 CPUDIR=arch/$(ARCH)/cpu/$(CPU) ifneq ($(SRCTREE)/$(CPUDIR),$(wildcard $(SRCTREE)/$(CPUDIR))) CPUDIR=arch/$(ARCH)/cpu endif ######################################################################### # 加载 $(TOPDIR)/arch/arm/config.mk # 加载 $(TOPDIR)/arch/arm/config.mk sinclude $(TOPDIR)/arch/$(ARCH)/config.mk # 加载架构编译选项文件 sinclude $(TOPDIR)/arch/arm/cpu/armv7/config.mk # 加载CPU相关编译选项文件 ######################################################################### # 定义有SOC = s5pc1xx,所以加载$(TOPDIR)/arch/arm/cpu/armv7/s5pc1xx/config.mk ifdef SOC sinclude $(TOPDIR)/$(CPUDIR)/$(SOC)/config.mk # include SoC specific rules endif ######################################################################### # 定义有VENDOR = samsung,所以BOARDDIR = samsung/smdkc100 ifdef VENDOR BOARDDIR = $(VENDOR)/$(BOARD) else BOARDDIR = $(BOARD) endif ######################################################################### # 定义有BOARD = smdkc100,所以加载$(TOPDIR)/board/samsung/smdkc100/config.mk ifdef BOARD sinclude $(TOPDIR)/board/$(BOARDDIR)/config.mk # 加载单板编译选项文件 endif ######################################################################### # 我们不会在任何地方使用ARFLAGS了,所以有人不更新$(AR)就试图处理旧代码的时候就会报错 # ARFLAGS = $(error update your Makefile to use cmd_link_o_target and not AR) # RELFLAGS=$(PLATFORM_RELFLAGS)=-fno-common -ffixed-r8 -msoft-float # DBGFLAGS = -g # OPTFLAGS = -Os # OBJCFLAGS = --gap-fill=0xff # gccincdir = $(shell $(CC) -print-file-name=include)=交叉工具链include目录在宿# 主机中的位置CPPFLAGS = -g -Os -fno-common -ffixed-r8 -msoft-float -D__KERNEL__ ARFLAGS = $(error update your Makefile to use cmd_link_o_target and not AR) RELFLAGS= $(PLATFORM_RELFLAGS) DBGFLAGS= -g # -DDEBUG OPTFLAGS= -Os # -fomit-frame-pointer OBJCFLAGS += --gap-fill=0xff gccincdir := $(shell $(CC) -print-file-name=include) CPPFLAGS := $(DBGFLAGS) $(OPTFLAGS) $(RELFLAGS) -D__KERNEL__ # Enable garbage collection of un-used sections for SPL ifeq ($(CONFIG_SPL_BUILD),y) CPPFLAGS += -ffunction-sections -fdata-sections LDFLAGS_FINAL += --gc-sections endif ########################################################## # U-Boot编译时将使用TEXT_BASE作为代码段连接的起始地址 # CONFIG_SYS_TEXT_BASE不为空,所以CPPFLAGS会多出一项: # -DCONFIG_SYS_TEXT_BASE=0x34800000 ifneq ($(CONFIG_SYS_TEXT_BASE),) CPPFLAGS += -DCONFIG_SYS_TEXT_BASE=$(CONFIG_SYS_TEXT_BASE) endif ifneq ($(CONFIG_SPL_TEXT_BASE),) CPPFLAGS += -DCONFIG_SPL_TEXT_BASE=$(CONFIG_SPL_TEXT_BASE) endif ifneq ($(CONFIG_SPL_PAD_TO),) CPPFLAGS += -DCONFIG_SPL_PAD_TO=$(CONFIG_SPL_PAD_TO) endif ifeq ($(CONFIG_SPL_BUILD),y) CPPFLAGS += -DCONFIG_SPL_BUILD endif ifneq ($(RESET_VECTOR_ADDRESS),) CPPFLAGS += -DRESET_VECTOR_ADDRESS=$(RESET_VECTOR_ADDRESS) endif ifneq ($(OBJTREE),$(SRCTREE)) CPPFLAGS += -I$(OBJTREE)/include2 -I$(OBJTREE)/include endif ########################################################## # 添加-I$(TOPDIR)/include路径到CPPFLAGS中,还有 # -fno-builtin -ffreestanding -nostdinc -isystem $(gccincdir) # -pipe $(PLATFORM_CPPFLAGS)也添加进CPPFLAGS中 CPPFLAGS += -I$(TOPDIR)/include CPPFLAGS += -fno-builtin -ffreestanding -nostdinc -isystem $(gccincdir) -pipe $(PLATFORM_CPPFLAGS) ########################################################## # 没有定义BUILD_TAG,执行else分支,赋值CFLAGS ifdef BUILD_TAG CFLAGS := $(CPPFLAGS) -Wall -Wstrict-prototypes -DBUILD_TAG='"$(BUILD_TAG)"' else CFLAGS := $(CPPFLAGS) -Wall -Wstrict-prototypes endif ########################################################## # 利用call函数,使得$1=cc-option ,$2=-fno-stack-protector # CFLAGS_SSP= $1$2 = -fno-stack-protector,然后把CFLAGS_SSP # 追加给CFLAGS CFLAGS_SSP := $(call cc-option,-fno-stack-protector) CFLAGS += $(CFLAGS_SSP) # Some toolchains enable security related warning flags by default, # but they don't make much sense in the u-boot world, so disable them. # 上面英文意思是一些工具链默认使用了一些安全警告选项,但是在U-boot的世界中我们不必要在意 # 太多这些警告,所以禁用它们。 # CFLAGS_WARN = -Wno-format-nonliteral -Wno-format-security,然后追加 # 到编译选项变量CFLAGS中 CFLAGS_WARN := $(call cc-option,-Wno-format-nonliteral) $(call cc-option,-Wno-format-security) CFLAGS += $(CFLAGS_WARN) # Report stack usage if supported # 如果支持的话开启报告堆栈使用状态,通过参数-fstack-usage开启,追加到CFLAGS中 CFLAGS_STACK := $(call cc-option,-fstack-usage) CFLAGS += $(CFLAGS_STACK) # $(CPPFLAGS) sets -g, which causes gcc to pass a suitable -g<format> # option to the assembler. # CPPFLAGS设置了-g参数, AFLAGS_DEBUG := # turn jbsr into jsr for m68k ifeq ($(ARCH),m68k) ifeq ($(findstring 3.4,$(shell $(CC) --version)),3.4) AFLAGS_DEBUG := -Wa,-gstabs,-S endif endif AFLAGS := $(AFLAGS_DEBUG) -D__ASSEMBLY__ $(CPPFLAGS) LDFLAGS += $(PLATFORM_LDFLAGS) LDFLAGS_FINAL += -Bstatic # 设置U-boot的链接选项 LDFLAGS_u-boot += -T $(obj)u-boot.lds $(LDFLAGS_FINAL) ifneq ($(CONFIG_SYS_TEXT_BASE),) LDFLAGS_u-boot += -Ttext $(CONFIG_SYS_TEXT_BASE) endif # 设置U-boot的链接选项,当uboot是采用spl时的情况 LDFLAGS_u-boot-spl += -T $(obj)u-boot-spl.lds $(LDFLAGS_FINAL) ifneq ($(CONFIG_SPL_TEXT_BASE),) LDFLAGS_u-boot-spl += -Ttext $(CONFIG_SPL_TEXT_BASE) endif # Location of a usable BFD library, where we define "usable" as # "built for ${HOST}, supports ${TARGET}". Sensible values are # - When cross-compiling: the root of the cross-environment # - Linux/ppc (native): /usr # - NetBSD/ppc (native): you lose ... (must extract these from the # binutils build directory, plus the native and U-Boot include # files don't like each other) # # So far, this is used only by tools/gdb/Makefile. # 非arm架构设置,不执行 ifeq ($(HOSTOS),darwin) BFD_ROOT_DIR = /usr/local/tools else ifeq ($(HOSTARCH),$(ARCH)) # native BFD_ROOT_DIR = /usr else #BFD_ROOT_DIR = /LinuxPPC/CDK # Linux/i386 #BFD_ROOT_DIR = /usr/pkg/cross # NetBSD/i386 BFD_ROOT_DIR = /opt/powerpc endif endif ######################################################################### # 设置各种编译选项的环境变量供Makefile使用 export HOSTCC HOSTCFLAGS HOSTLDFLAGS PEDCFLAGS HOSTSTRIP CROSS_COMPILE AS LD CC CPP AR NM STRIP OBJCOPY OBJDUMP MAKE export CONFIG_SYS_TEXT_BASE PLATFORM_CPPFLAGS PLATFORM_RELFLAGS CPPFLAGS CFLAGS AFLAGS ######################################################################### # Allow boards to use custom optimize flags on a per dir/file basis # BCURDIR = U-boot源码顶层目录,其他的太长了就不打出来了 BCURDIR = $(subst $(SRCTREE)/,,$(CURDIR:$(obj)%=%)) ALL_AFLAGS = $(AFLAGS) $(AFLAGS_$(BCURDIR)/$(@F)) $(AFLAGS_$(BCURDIR)) ALL_CFLAGS = $(CFLAGS) $(CFLAGS_$(BCURDIR)/$(@F)) $(CFLAGS_$(BCURDIR)) EXTRA_CPPFLAGS = $(CPPFLAGS_$(BCURDIR)/$(@F)) $(CPPFLAGS_$(BCURDIR)) ALL_CFLAGS += $(EXTRA_CPPFLAGS) ########################################################################### # The _DEP version uses the $< file target (for dependency generation) # See rules.mk # 指定隐含的编译规则,例如:根据以上的定义,以“.s”结尾的目标文件将根据第一条规则由同名但后 # 缀为“.S”的源文件来生成,若不存在“.S”结尾的同名文件则根据最后一条规则由同名的“.c”文件生 # 成。 EXTRA_CPPFLAGS_DEP = $(CPPFLAGS_$(BCURDIR)/$(addsuffix .o,$(basename $<))) $(CPPFLAGS_$(BCURDIR)) $(obj)%.s: %.S $(CPP) $(ALL_AFLAGS) -o $@ $< $(obj)%.o: %.S $(CC) $(ALL_AFLAGS) -o $@ $< -c $(obj)%.o: %.c $(CC) $(ALL_CFLAGS) -o $@ $< -c $(obj)%.i: %.c $(CPP) $(ALL_CFLAGS) -o $@ $< -c $(obj)%.s: %.c $(CC) $(ALL_CFLAGS) -o $@ $< -c -S ######################################################################### # If the list of objects to link is empty, just create an empty built-in.o # $1为空,if返回空,cmd_link_o_target = rm -f; arm-linux-ar rcs cmd_link_o_target = $(if $(strip $1), $(LD) $(LDFLAGS) -r -o $@ $1, rm -f $@; $(AR) rcs $@ ) #########################################################################