标签:makefile u-boot samsung arm9
由顶层Makefile文件,梳理U-Boot的编译流程。
小技巧:
在大型Makefile中,很多时候需要确定某个变量的值,一个小方法就是:
$(shell echo "VARIABLE0_VAL = $(VARIABLE0_VAL)" > v_file.txt ) $(shell echo "VARIABLE1_VAL = $(VARIABLE1_VAL)" >> v_file.txt )
1、首先,确定make过程中需要的变量(Makefile的注视符改为/* */)。
VERSION = 2010 PATCHLEVEL = 06 SUBLEVEL = EXTRAVERSION = ifneq "$(SUBLEVEL)" "" U_BOOT_VERSION = $(VERSION).$(PATCHLEVEL).$(SUBLEVEL)$(EXTRAVERSION) else /* U_BOOT_VERSION = 2010.06 */ U_BOOT_VERSION = $(VERSION).$(PATCHLEVEL)$(EXTRAVERSION) endif /* obj变量为空 */ TIMESTAMP_FILE = $(obj)include/timestamp_autogenerated.h VERSION_FILE = $(obj)include/version_autogenerated.h /* HOSTARCH:获取主机的硬件架构 * "sed -e"表示后面跟的是命令脚本,"s/xxx/yyy/"表示从打印信息中查找"xxx"然后替换为"yyy","."为通配符。 * uname -m获取主机架构信息的命令 * 执行流程: * uname -m结果为"i686",符合"s/i.86/i386/"命令脚本,"i686"被替换为"i386" * 最终结果:HOSTARCH = i386 */ HOSTARCH := $(shell uname -m | sed -e s/i.86/i386/ -e s/sun4u/sparc64/ -e s/arm.*/arm/ -e s/sa110/arm/ -e s/ppc64/powerpc/ -e s/ppc/powerpc/ -e s/macppc/powerpc/) /* -s, print the kernel name * [:upper:], all upper case letters * [:lower:], all lower case letters * 执行流程: * uname -s得到内核名"Linux",tr把字串的大写转换成小写 * 最终结果:HOSTOS = linux */ HOSTOS := $(shell uname -s | tr '[:upper:]' '[:lower:]' | sed -e 's/\(cygwin\).*/cygwin/') /* Set shell to bash if possible, otherwise fall back to sh * 最终结果:SHELL = /bin/bash */ SHELL := $(shell if [ -x "$$BASH" ]; then echo $$BASH; else if [ -x /bin/bash ]; then echo /bin/bash; else echo sh; fi; fi) /* HOSTARCH HOSTOS SHELL 是编译u-boot的主机的信息 */ export HOSTARCH HOSTOS SHELL /* Deal with colliding definitions from tcsh etc. */ VENDOR= /* Allow for silent builds */ ifeq (,$(findstring s,$(MAKEFLAGS))) XECHO = echo else XECHO = : endif /* U-boot支持在其他目录编译处理目标文件,这样可以保持源代码洁净 * 指定目录的方式有2种: * 1) 执行make命令时附加"-O dir"项 --'make O=/tmp/build all' * 2) 设置环境变量BUILD_DIR --'export BUILD_DIR=/tmp/build'; 'make' */ ifdef O /* origin函数的作用是告知某个变量来自与那里,它并不改变变量。 * origin函数有如下几种情况 * 1) 若从未定义过变量O,$(origin O)返回'undefined' * 2) 若变量O为环境变量,$(origin O)返回'environment' * 3) 若变量O为默认定义,$(origin O)返回'default' * 4) 若变量O定义在Makefile中,$(origin O)返回'file' * 5) 若变量O来自命令行,$(origin O)返回'command line' * 6) 若变量O被重新定义过,$(origin O)返回'override' * 7) 若变量O是自动变量,如"$@""$<"等,$(origin O)返回'automatic' */ ifeq ("$(origin O)", "command line") BUILD_DIR := $(O) endif endif /* 若BUILD_DIR不为空,赋值给变量saved-output */ ifneq ($(BUILD_DIR),) saved-output := $(BUILD_DIR) /* Attempt to create a output directory. * -d FILE:FILE exists and is a directory */ $(shell [ -d ${BUILD_DIR} ] || mkdir -p ${BUILD_DIR}) /* Verify if it was successful. */ BUILD_DIR := $(shell cd $(BUILD_DIR) && /bin/pwd) $(if $(BUILD_DIR),,$(error output directory "$(saved-output)" does not exist)) endif # ifneq ($(BUILD_DIR),) /* CURDIR = . * OBJTREE = . * SRCTREE = . * TOPDIR = . * LNDIR = . */ OBJTREE := $(if $(BUILD_DIR),$(BUILD_DIR),$(CURDIR)) SRCTREE := $(CURDIR) TOPDIR := $(SRCTREE) LNDIR := $(OBJTREE) export TOPDIR SRCTREE OBJTREE /* MKCONFIG = ./mkconfig * mkconfig的分析见上篇:《u-boot的Makefile分析之mkconfig》 * http://blog.csdn.net/u013686019/article/details/42295291 */ MKCONFIG := $(SRCTREE)/mkconfig export MKCONFIG ifneq ($(OBJTREE),$(SRCTREE)) REMOTE_BUILD := 1 export REMOTE_BUILD endif /* $(obj) and (src) are defined in config.mk but here in main Makefile * we also need them before config.mk is included which is the case for * some targets like unconfig, clean, clobber, distclean, etc. */ ifneq ($(OBJTREE),$(SRCTREE)) obj := $(OBJTREE)/ src := $(SRCTREE)/ else obj := src := endif export obj src /* Make sure CDPATH settings don't interfere */ unexport CDPATH ######################################################################### /* The "tools" are needed early, so put this first * Don't include stuff already done in $(LIBS) */ SUBDIRS = tools examples/standalone examples/api /* .PHONY用来标明一个伪目标 * 何为"伪目标"?就是我们定义了一个目标,但这个目标不依赖具体的文件 * "伪目标"有何作用?假设Makefile所在的目录有一个文件,其名字和Makefile中的一个目标相同;由于该文件没有依赖任何文件,所以它一直是最新的,根据make的编译规则,与文件同名的目标将不会被执行 */ .PHONY : $(SUBDIRS) ifeq ($(obj)include/config.mk,$(wildcard $(obj)include/config.mk)) /* Include autoconf.mk before config.mk so that the config options are available * to all top level build files. We need the dummy all: target to prevent the * dependency target in autoconf.mk.dep from being the default. */ all: sinclude $(obj)include/autoconf.mk.dep sinclude $(obj)include/autoconf.mk include $(obj)include/config.mk /* arm arm920t smdk2410 samsung s3c24x0 */ export ARCH CPU BOARD VENDOR SOC /* 该ifeq不会被执行,CROSS_COMPILE ?= arm-linux-在文件arch/arm/config.mk中赋值 */ ifeq ($(HOSTARCH),$(ARCH)) CROSS_COMPILE ?= endif $(shell echo "HOSTARCH = $(HOSTARCH)" > v_file.txt ) $(shell echo "ARCH = $(ARCH)" >> v_file.txt ) $(shell echo "CROSS_COMPILE= $(CROSS_COMPILE)" >> v_file.txt ) /* load other configuration */ include $(TOPDIR)/config.mk
/* U-Boot objects....order is important (i.e. start must be first) /* OBJS = $(CPUDIR)/start.o
/* addprefix,加前缀函数 * 功能:把前缀加到中的每个单词后面 * 示例:$(addprefix src/,foo bar)返回值是“src/foo src/bar” */ OBJS := $(addprefix $(obj),$(OBJS)) LIBS = lib/libgeneric.a LIBS += lib/lzma/liblzma.a LIBS += lib/lzo/liblzo.a LIBS += $(shell if [ -f board/$(VENDOR)/common/Makefile ]; then echo "board/$(VENDOR)/common/lib$(VENDOR).a"; fi) LIBS += $(CPUDIR)/lib$(CPU).a ifdef SOC LIBS += $(CPUDIR)/$(SOC)/lib$(SOC).a endif LIBS += arch/$(ARCH)/lib/lib$(ARCH).a LIBS += fs/cramfs/libcramfs.a fs/fat/libfat.a fs/fdos/libfdos.a fs/jffs2/libjffs2.a fs/reiserfs/libreiserfs.a fs/ext2/libext2fs.a fs/yaffs2/libyaffs2.a fs/ubifs/libubifs.a LIBS += net/libnet.a LIBS += disk/libdisk.a LIBS += drivers/bios_emulator/libatibiosemu.a LIBS += drivers/block/libblock.a LIBS += drivers/dma/libdma.a LIBS += drivers/fpga/libfpga.a LIBS += drivers/gpio/libgpio.a LIBS += drivers/hwmon/libhwmon.a LIBS += drivers/i2c/libi2c.a LIBS += drivers/input/libinput.a LIBS += drivers/misc/libmisc.a LIBS += drivers/mmc/libmmc.a LIBS += drivers/mtd/libmtd.a LIBS += drivers/mtd/nand/libnand.a LIBS += drivers/mtd/onenand/libonenand.a LIBS += drivers/mtd/ubi/libubi.a LIBS += drivers/mtd/spi/libspi_flash.a LIBS += drivers/net/libnet.a LIBS += drivers/net/phy/libphy.a LIBS += drivers/pci/libpci.a LIBS += drivers/pcmcia/libpcmcia.a LIBS += drivers/power/libpower.a LIBS += drivers/spi/libspi.a LIBS += drivers/rtc/librtc.a LIBS += drivers/serial/libserial.a LIBS += drivers/twserial/libtws.a LIBS += drivers/usb/gadget/libusb_gadget.a LIBS += drivers/usb/host/libusb_host.a LIBS += drivers/usb/musb/libusb_musb.a LIBS += drivers/usb/phy/libusb_phy.a LIBS += drivers/video/libvideo.a LIBS += drivers/watchdog/libwatchdog.a LIBS += common/libcommon.a LIBS += lib/libfdt/libfdt.a LIBS += api/libapi.a LIBS += post/libpost.a LIBS := $(addprefix $(obj),$(LIBS)) .PHONY : $(LIBS) $(TIMESTAMP_FILE) $(VERSION_FILE) /* BOARDDIR = samsung/smdk2410 */ LIBBOARD = board/$(BOARDDIR)/lib$(BOARD).a LIBBOARD := $(addprefix $(obj),$(LIBBOARD))
$(OBJS): depend $(MAKE) -C $(CPUDIR) $(if $(REMOTE_BUILD),$@,$(notdir $@)) $(LIBS): depend $(SUBDIRS) $(MAKE) -C $(dir $(subst $(obj),,$@)) $(LIBBOARD): depend $(LIBS) $(MAKE) -C $(dir $(subst $(obj),,$@)) $(SUBDIRS): depend $(MAKE) -C $@ all $(LDSCRIPT): depend $(MAKE) -C $(dir $@) $(notdir $@) $(obj)u-boot.lds: $(LDSCRIPT) $(CPP) $(CPPFLAGS) $(LDPPFLAGS) -ansi -D__ASSEMBLY__ -P - <$^ >$@ $(NAND_SPL): $(TIMESTAMP_FILE) $(VERSION_FILE) $(obj)include/autoconf.mk $(MAKE) -C nand_spl/board/$(BOARDDIR) all $(U_BOOT_NAND): $(NAND_SPL) $(obj)u-boot.bin cat $(obj)nand_spl/u-boot-spl-16k.bin $(obj)u-boot.bin > $(obj)u-boot-nand.bin $(ONENAND_IPL): $(TIMESTAMP_FILE) $(VERSION_FILE) $(obj)include/autoconf.mk $(MAKE) -C onenand_ipl/board/$(BOARDDIR) all $(U_BOOT_ONENAND): $(ONENAND_IPL) $(obj)u-boot.bin cat $(ONENAND_BIN) $(obj)u-boot.bin > $(obj)u-boot-onenand.bin $(VERSION_FILE): @( printf '#define U_BOOT_VERSION "U-Boot %s%s"\n' "$(U_BOOT_VERSION)" '$(shell $(TOPDIR)/tools/setlocalversion $(TOPDIR))' ) > $@.tmp @cmp -s $@ $@.tmp && rm -f $@.tmp || mv -f $@.tmp $@ $(TIMESTAMP_FILE): @LC_ALL=C date +'#define U_BOOT_DATE "%b %d %C%y"' > $@ @LC_ALL=C date +'#define U_BOOT_TIME "%T"' >> $@ gdbtools: $(MAKE) -C tools/gdb all || exit 1 updater: $(MAKE) -C tools/updater all || exit 1 env: $(MAKE) -C tools/env all MTD_VERSION=${MTD_VERSION} || exit 1 # Explicitly make _depend in subdirs containing multiple targets to prevent # parallel sub-makes creating .depend files simultaneously. depend dep: $(TIMESTAMP_FILE) $(VERSION_FILE) $(obj)include/autoconf.mk for dir in $(SUBDIRS) $(CPUDIR) $(dir $(LDSCRIPT)) ; do $(MAKE) -C $$dir _depend ; done TAG_SUBDIRS = $(SUBDIRS) TAG_SUBDIRS += $(dir $(__LIBS)) TAG_SUBDIRS += include tags ctags: ctags -w -o $(obj)ctags `find $(TAG_SUBDIRS) -name '*.[chS]' -print` etags: etags -a -o $(obj)etags `find $(TAG_SUBDIRS) -name '*.[chS]' -print` cscope: find $(TAG_SUBDIRS) -name '*.[chS]' -print > cscope.files cscope -b -q -k SYSTEM_MAP = $(NM) $1 | grep -v '\(compiled\)\|\(\.o$$\)\|\( [aUw] \)\|\(\.\.ng$$\)\|\(LASH[RL]DI\)' | LC_ALL=C sort $(obj)System.map: $(obj)u-boot @$(call SYSTEM_MAP,$<) > $(obj)System.map # # Auto-generate the autoconf.mk file (which is included by all makefiles) # # This target actually generates 2 files; autoconf.mk and autoconf.mk.dep. # the dep file is only include in this top level makefile to determine when # to regenerate the autoconf.mk file. $(obj)include/autoconf.mk.dep: $(obj)include/config.h include/common.h @$(XECHO) Generating $@ ; set -e ; : Generate the dependancies ; $(CC) -x c -DDO_DEPS_ONLY -M $(HOSTCFLAGS) $(CPPFLAGS) -MQ $(obj)include/autoconf.mk include/common.h > $@ $(obj)include/autoconf.mk: $(obj)include/config.h @$(XECHO) Generating $@ ; set -e ; : Extract the config macros ; $(CPP) $(CFLAGS) -DDO_DEPS_ONLY -dM include/common.h | sed -n -f tools/scripts/define2mk.sed > $@.tmp && mv $@.tmp $@ ######################################################################### else # !config.mk all $(obj)u-boot.hex $(obj)u-boot.srec $(obj)u-boot.bin $(obj)u-boot.img $(obj)u-boot.dis $(obj)u-boot $(filter-out tools,$(SUBDIRS)) $(TIMESTAMP_FILE) $(VERSION_FILE) gdbtools updater env depend dep tags ctags etags cscope $(obj)System.map: @echo "System not configured - see README" >&2 @ exit 1 tools: $(MAKE) -C tools tools-all: $(MAKE) -C tools HOST_TOOLS_ALL=y endif # config.mk .PHONY : CHANGELOG CHANGELOG: git log --no-merges U-Boot-1_1_5.. | unexpand -a | sed -e 's/\s\s*$$//' > $@ include/license.h: tools/bin2header COPYING cat COPYING | gzip -9 -c | ./tools/bin2header license_gzip > include/license.h ######################################################################### unconfig: @rm -f $(obj)include/config.h $(obj)include/config.mk $(obj)board/*/config.tmp $(obj)board/*/*/config.tmp $(obj)include/autoconf.mk $(obj)include/autoconf.mk.dep %: %_config $(MAKE) ######################################################################### smdk2410_config : unconfig @$(MKCONFIG) $(@:_config=) arm arm920t smdk2410 samsung s3c24x0
5、并根据需要转换成其他格式的可执行文件,如二进制格式、S_Record格式等:
/* Add GCC lib */ ifdef USE_PRIVATE_LIBGCC ifeq ("$(USE_PRIVATE_LIBGCC)", "yes") PLATFORM_LIBGCC = -L $(OBJTREE)/arch/$(ARCH)/lib -lgcc else PLATFORM_LIBGCC = -L $(USE_PRIVATE_LIBGCC) -lgcc endif else PLATFORM_LIBGCC = -L $(shell dirname `$(CC) $(CFLAGS) -print-libgcc-file-name`) -lgcc endif PLATFORM_LIBS += $(PLATFORM_LIBGCC) export PLATFORM_LIBS /* Special flags for CPP when processing the linker script. * Pass the version down so we can handle backwards compatibility * on the fly. */ LDPPFLAGS += -include $(TOPDIR)/include/u-boot/u-boot.lds.h $(shell $(LD) --version | sed -ne 's/GNU ld version \([0-9][0-9]*\)\.\([0-9][0-9]*\).*/-DLD_MAJOR=\1 -DLD_MINOR=\2/p') ifeq ($(CONFIG_NAND_U_BOOT),y) NAND_SPL = nand_spl U_BOOT_NAND = $(obj)u-boot-nand.bin endif /* subst函数: * $(subst from, to, text),在文本"text"中用"to"代替每一处的"from" */ __OBJS := $(subst $(obj),,$(OBJS)) __LIBS := $(subst $(obj),,$(LIBS)) $(subst $(obj),,$(LIBBOARD)) ######################################################################### /* Always append ALL so that arch config.mk's can add custom ones */ ALL += $(obj)u-boot.srec $(obj)u-boot.bin $(obj)System.map $(U_BOOT_NAND) $(U_BOOT_ONENAND) all: $(ALL) /* 在顶层的config.mk * AS = $(CROSS_COMPILE)as * LD = $(CROSS_COMPILE)ld * 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 */ /* 根据生成的u-boot转换成其他格式的可执行文件 */ $(obj)u-boot.hex: $(obj)u-boot $(OBJCOPY) ${OBJCFLAGS} -O ihex $< $@ $(obj)u-boot.srec: $(obj)u-boot $(OBJCOPY) -O srec $< $@ $(obj)u-boot.bin: $(obj)u-boot $(OBJCOPY) ${OBJCFLAGS} -O binary $< $@ $(obj)u-boot.ldr: $(obj)u-boot $(CREATE_LDR_ENV) $(LDR) -T $(CONFIG_BFIN_CPU) -c $@ $< $(LDR_FLAGS) $(obj)u-boot.ldr.hex: $(obj)u-boot.ldr $(OBJCOPY) ${OBJCFLAGS} -O ihex $< $@ -I binary $(obj)u-boot.ldr.srec: $(obj)u-boot.ldr $(OBJCOPY) ${OBJCFLAGS} -O srec $< $@ -I binary $(obj)u-boot.img: $(obj)u-boot.bin $(obj)tools/mkimage -A $(ARCH) -T firmware -C none -a $(TEXT_BASE) -e 0 -n $(shell sed -n -e 's/.*U_BOOT_VERSION//p' $(VERSION_FILE) | sed -e 's/"[ ]*$$/ for $(BOARD) board"/') -d $< $@ $(obj)u-boot.imx: $(obj)u-boot.bin $(obj)tools/mkimage -n $(IMX_CONFIG) -T imximage -e $(TEXT_BASE) -d $< $@ $(obj)u-boot.kwb: $(obj)u-boot.bin $(obj)tools/mkimage -n $(KWD_CONFIG) -T kwbimage -a $(TEXT_BASE) -e $(TEXT_BASE) -d $< $@ $(obj)u-boot.sha1: $(obj)u-boot.bin $(obj)tools/ubsha1 $(obj)u-boot.bin $(obj)u-boot.dis: $(obj)u-boot $(OBJDUMP) -d $< > $@ GEN_UBOOT = UNDEF_SYM=`$(OBJDUMP) -x $(LIBBOARD) $(LIBS) | sed -n -e 's/.*\($(SYM_PREFIX)__u_boot_cmd_.*\)/-u\1/p'|sort|uniq`; cd $(LNDIR) && $(LD) $(LDFLAGS) $$UNDEF_SYM $(__OBJS) --start-group $(__LIBS) --end-group $(PLATFORM_LIBS) -Map u-boot.map -o u-boot $(obj)u-boot: depend $(SUBDIRS) $(OBJS) $(LIBBOARD) $(LIBS) $(LDSCRIPT) $(obj)u-boot.lds $(GEN_UBOOT) ifeq ($(CONFIG_KALLSYMS),y) smap=`$(call SYSTEM_MAP,u-boot) | awk '$$2 ~ /[tTwW]/ {printf $$1 $$3 "\\\\000"}'` ; $(CC) $(CFLAGS) -DSYSTEM_MAP="\"$${smap}\"" -c common/system_map.c -o $(obj)common/system_map.o $(GEN_UBOOT) $(obj)common/system_map.o endif
标签:makefile u-boot samsung arm9
原文地址:http://blog.csdn.net/u013686019/article/details/42365657