u-boot的Makefile分析之顶层config.mk

时间:2022-01-01 01:50:25
版本信息:
u-boot-2010-06
顶层目录下的config.mk文件主要完成如下功能的配置:
1、确定生成可执行文件过程中需要的各种工具,如编译器(arm-linux-gcc)、连接器(arm-linux-ld)、反汇编器(arm-linux-objdump)等
2、确定CPU、板相关的配置文件,存在于各个目录下的config.mk
3、确定编译、链接、转换等过程的操作选项

4、根据步骤3确定的编译连接选项生成需要的文件

config.mk完整内容及必要注释如下
:config.mk文件注释符改为/* 注释内容 */

ifneq ($(OBJTREE),$(SRCTREE))
	ifeq ($(CURDIR),$(SRCTREE))
		dir :=
	else
		dir := $(subst $(SRCTREE)/,,$(CURDIR))
	endif

	obj := $(if $(dir),$(OBJTREE)/$(dir)/,$(OBJTREE)/)
	src := $(if $(dir),$(SRCTREE)/$(dir)/,$(SRCTREE)/)

	$(shell mkdir -p $(obj))
else
	obj :=
	src :=
endif
/* obj = 空,src = 空
 * dir = 空
 */
 
/* clean the slate ... */
PLATFORM_RELFLAGS =
PLATFORM_CPPFLAGS =
PLATFORM_LDFLAGS =

/* HOSTCFLAGS = -Wall -Wstrict-prototypes -O2 -fomit-frame-pointer
 * -Wall: 打印出编译时所有的错误或警告信息
 * -Wstrict-prototypes: 编译时,若产生与数据类型不相符的问题,打印出提示或警告信息。当在不同体系结构间移植时,加上该选项可避免很多错误
 * -O: 编译代码时的优化等级,共有五种:-O0、-O1、-O2、-O3和-Os
 * -fomit-frame-pointer: 对于不需要帧指针的函数,不要在寄存器中保存帧指针
 * 代码优化时打开-fomit-frame-pointer,函数调用时不保存frame指针,也就不能用backtrace()来查看函数栈调用
 * backtrace()系列函数见[http://blog.csdn.net/u013686019/article/details/42128771](Linux中backtrace()系列函数的应用实例)
 */
HOSTCFLAGS	= -Wall -Wstrict-prototypes -O2 -fomit-frame-pointer \
		  $(HOSTCPPFLAGS)
/* HOSTSTRIP = strip 
 * strip能清除执行文件中不必要的标示符及调试信息,可减小文件大小而不影响正常使用,、
 * 与压缩不同的是,文件一旦strip后就不能恢复原样
 * strip后的文件不包含调试信息
 */
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.
 */
ifeq ($(HOSTOS),darwin)
	......
else
	HOSTCC		= gcc
endif

ifeq ($(HOSTOS),cygwin)
	......
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.
 */
HOSTCFLAGS_NOPED = $(filter-out -pedantic,$(HOSTCFLAGS))
/* -pedantic: 当GCC在编译不符合ANSI/ISO C语言标准的源代码时,如果在编译指令中加上了-pedantic选项
 * 那么源程序中使用了扩展语法的地方将产生相应的警告信息
 */
HOSTCFLAGS	+= -pedantic

#########################################################################
/* Option checker (courtesy linux kernel) to ensure
 * only supported compiler options are used
 * cc-option变量保存了一个测试编译选项的命令,其他地方会经常用call函数来调用它,测试编译选项
 * if $(CC) $(CFLAGS) $(1) -S -o /dev/null -xc /dev/null > /dev/null 2>&1;then
		echo "$(1)";
   else
		echo "$(2)";
   fi;
 * -S:编译后立即结束,不进行汇编等操作
 * -o /dev/null : 生成文件到/dev/null,即不生成任何编译结果,要编译的文件也为空
 * -xc: 指定按c语言编译
 * 用此语句如:call cc-option,-a,-b 则如果支持-a选项则返回-a否则返回-b
 */
cc-option = $(shell if $(CC) $(CFLAGS) $(1) -S -o /dev/null -xc /dev/null \
		> /dev/null 2>&1; then echo "$(1)"; else echo "$(2)"; fi ;)

/* Include the make variables (CC, etc...) */
AS	= $(CROSS_COMPILE)as	/* 汇编工具 */
LD	= $(CROSS_COMPILE)ld	/* 链接工具 */
CC	= $(CROSS_COMPILE)gcc	/* 编译工具 */
CPP	= $(CC) -E				/* 预处理   */
AR	= $(CROSS_COMPILE)ar	/* 归档工具 */
NM	= $(CROSS_COMPILE)nm	/* 列出object文件中的符号 */
LDR	= $(CROSS_COMPILE)ldr
STRIP	= $(CROSS_COMPILE)strip
OBJCOPY = $(CROSS_COMPILE)objcopy /* 转换可执行文件格式工具 */
OBJDUMP = $(CROSS_COMPILE)objdump /* 反汇编工具 */
RANLIB	= $(CROSS_COMPILE)RANLIB  /* 产生归档文件索引 */

#########################################################################
/* Load generated board configuration */
/* sinclude:
 * 在Makefile中可使用"sinclude"代替"include",用来忽略由于包含文件不存在或者无法创建时的错误
 */
sinclude $(OBJTREE)/include/autoconf.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=arch/$(ARCH)/cpu/$(CPU)
ifneq ($(SRCTREE)/$(CPUDIR),$(wildcard $(SRCTREE)/$(CPUDIR)))
CPUDIR=arch/$(ARCH)/cpu
endif
/* CPUDIR=arch/arm/cpu/arm920t */

/* include architecture dependend rules: arch/arm/config.mk */
sinclude $(TOPDIR)/arch/$(ARCH)/config.mk
/* include  CPU	specific rules: arch/arm/cpu/arm920t/config.mk */
sinclude $(TOPDIR)/$(CPUDIR)/config.mk
ifdef	SOC
	/* include  SoC	specific rules: arch/arm/cpu/arm920t/s3c24x0/config.mk */
	sinclude $(TOPDIR)/$(CPUDIR)/$(SOC)/config.mk
endif
ifdef	VENDOR
	BOARDDIR = $(VENDOR)/$(BOARD)
else
	BOARDDIR = $(BOARD)
endif
/* BOARDDIR = samsung/smdk2410 */

ifdef	BOARD
	/* include board specific rules: board/samsung/smdk2410/config.mk */
	sinclude $(TOPDIR)/board/$(BOARDDIR)/config.mk
endif

#########################################################################
ifneq (,$(findstring s,$(MAKEFLAGS)))
ARFLAGS = cr
else
ARFLAGS = crv
endif
RELFLAGS= $(PLATFORM_RELFLAGS)
DBGFLAGS= -g # -DDEBUG
OPTFLAGS= -Os #-fomit-frame-pointer

/* LDSCRIPT = arch/arm/cpu/arm920t/u-boot.lds,在在文件arch/arm/config.mk中赋值 */
ifndef LDSCRIPT
	#LDSCRIPT := $(TOPDIR)/board/$(BOARDDIR)/u-boot.lds.debug
	ifeq ($(CONFIG_NAND_U_BOOT),y)
		LDSCRIPT := $(TOPDIR)/board/$(BOARDDIR)/u-boot-nand.lds
	else
		LDSCRIPT := $(TOPDIR)/board/$(BOARDDIR)/u-boot.lds
	endif
endif
/* 段之间的空隙用0xff填充 */
OBJCFLAGS += --gap-fill=0xff

gccincdir := $(shell $(CC) -print-file-name=include)

/* CPPFLAGS变量综合了DBGFLAGS,OPTFLAGS,RELFLAGS编译选项,并定义了__KERBEL__
 * -D: 设置宏定义__KERNEL__
 */
CPPFLAGS := $(DBGFLAGS) $(OPTFLAGS) $(RELFLAGS)		\
	-D__KERNEL__
ifneq ($(TEXT_BASE),)
CPPFLAGS += -DTEXT_BASE=$(TEXT_BASE)
endif

ifneq ($(RESET_VECTOR_ADDRESS),)
CPPFLAGS += -DRESET_VECTOR_ADDRESS=$(RESET_VECTOR_ADDRESS)
endif

ifneq ($(OBJTREE),$(SRCTREE))
CPPFLAGS += -I$(OBJTREE)/include2 -I$(OBJTREE)/include
endif

CPPFLAGS += -I$(TOPDIR)/include
CPPFLAGS += -fno-builtin -ffreestanding -nostdinc	\
	-isystem $(gccincdir) -pipe $(PLATFORM_CPPFLAGS)

ifdef BUILD_TAG
CFLAGS := $(CPPFLAGS) -Wall -Wstrict-prototypes \
	-DBUILD_TAG='"$(BUILD_TAG)"'
else
CFLAGS := $(CPPFLAGS) -Wall -Wstrict-prototypes
endif

CFLAGS += $(call cc-option,-fno-stack-protector)

/* $(CPPFLAGS) sets -g, which causes gcc to pass a suitable -g<format> */
/* option to the assembler. */
AFLAGS_DEBUG :=

AFLAGS := $(AFLAGS_DEBUG) -D__ASSEMBLY__ $(CPPFLAGS)

LDFLAGS += -Bstatic -T $(obj)u-boot.lds $(PLATFORM_LDFLAGS)
ifneq ($(TEXT_BASE),)
LDFLAGS += -Ttext $(TEXT_BASE)
endif
/* LDFLAGS = -Bstatic -T u-boot.lds -Ttext 0x33F80000 */
/* 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 */

/* 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.
 */
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

#########################################################################
export	HOSTCC HOSTCFLAGS HOSTLDFLAGS PEDCFLAGS HOSTSTRIP CROSS_COMPILE \
	AS LD CC CPP AR NM STRIP OBJCOPY OBJDUMP MAKE
export	TEXT_BASE PLATFORM_CPPFLAGS PLATFORM_RELFLAGS CPPFLAGS CFLAGS AFLAGS

#########################################################################
/* 下面几行规定了各种文件的编译时用到的编译选项 */
/* Allow boards to use custom optimize flags on a per dir/file basis */
BCURDIR = $(subst $(SRCTREE)/,,$(CURDIR:$(obj)%=%))
/* BCURDIR = 顶层目录 */
$(obj)%.s:	%.S
	$(CPP) $(AFLAGS) $(AFLAGS_$(BCURDIR)/$(@F)) $(AFLAGS_$(BCURDIR)) \
		-o $@ $<
$(obj)%.o:	%.S
	$(CC)  $(AFLAGS) $(AFLAGS_$(BCURDIR)/$(@F)) $(AFLAGS_$(BCURDIR)) \
		-o $@ $< -c
$(obj)%.o:	%.c
	$(CC)  $(CFLAGS) $(CFLAGS_$(BCURDIR)/$(@F)) $(CFLAGS_$(BCURDIR)) \
		-o $@ $< -c
$(obj)%.i:	%.c
	$(CPP) $(CFLAGS) $(CFLAGS_$(BCURDIR)/$(@F)) $(CFLAGS_$(BCURDIR)) \
		-o $@ $< -c
$(obj)%.s:	%.c
	$(CC)  $(CFLAGS) $(CFLAGS_$(BCURDIR)/$(@F)) $(CFLAGS_$(BCURDIR)) \
		-o $@ $< -c -S