一、知识准备
1.看下这个人写的博客,讲到了u-boot用到的所有Makefile规则,实用且短。
2.执行make命令:首先,Makefile 中变量和函数的展开 (除规则命令行中的变量和函数以外),是在 make读取 makefile 文件时进行的;然后,对目标处进行操作,这个目标可以是目标文件也可以是个伪目标(标签)。
二、u-boot顶层makefile分析
1.定义一些有关版本信息的变量,其实就是字符串替换
#前面是注释
24 VERSION = 2009 25 PATCHLEVEL = 11 26 SUBLEVEL = 27 EXTRAVERSION = 28 ifneq "$(SUBLEVEL)" "" 29 U_BOOT_VERSION = $(VERSION).$(PATCHLEVEL).$(SUBLEVEL)$(EXTRAVERSION) 30 else 31 U_BOOT_VERSION = $(VERSION).$(PATCHLEVEL)$(EXTRAVERSION) 32 endif 33 TIMESTAMP_FILE = $(obj)include/timestamp_autogenerated.h 34 VERSION_FILE = $(obj)include/version_autogenerated.h
|
2.定义了变量,存放了主机的一些信息
35 #$(shell xxx)的意思是在shell中执行xxx命令
#sed -e的意思,就是表示后面跟的是一串命令脚本,s/abc/def/的命令表达
#式,就是表示要从标准输入中,查找到内容为abc的,然后替换成def 36 HOSTARCH := $(shell uname -m | \ 37 sed -e s/i.86/i386/ \ 38 -e s/sun4u/sparc64/ \ 39 -e s/arm.*/arm/ \ 40 -e s/sa110/arm/ \ 41 -e s/powerpc/ppc/ \ 42 -e s/ppc64/ppc/ \ 43 -e s/macppc/ppc/) 44 #uname -s 查看内核版本 45 HOSTOS := $(shell uname -s | tr '[:upper:]' '[:lower:]' | \ 46 sed -e 's/\(cygwin\).*/cygwin/') 47 48 # Set shell to bash if possible, otherwise fall back to sh 49 SHELL := $(shell if [ -x "$$BASH" ]; then echo $$BASH; \ 50 else if [ -x /bin/bash ]; then echo /bin/bash; \ 51 else echo sh; fi; fi) 52 53 export HOSTARCH HOSTOS SHELL 54 55 # Deal with colliding definitions from tcsh etc. 56 VENDOR= 57 58 ######################################################################### 59 # Allow for silent builds 60 ifeq (,$(findstring s,$(MAKEFLAGS))) 61 XECHO = echo 62 else 63 XECHO = : 64 endif 65
|
3. 支持生成的目标代码和源代码不在同一个目录下这个功能
#66-87是一些注释 #88-103,主要用于支持生成的目标文件与源代码不在同一个目录下
#88检查是否用O指定了编译目录,如果指定了则把值存入变量BUILD_DIR 88 ifdef O 89 ifeq ("$(origin O)", "command line") 90 BUILD_DIR := $(O) 91 endif 92 endif 93 94 ifneq ($(BUILD_DIR),) 95 saved-output := $(BUILD_DIR) 96 #98行,选项“-d”用来判断目录是否存在 97 # Attempt to create a output directory. 98 $(shell [ -d ${BUILD_DIR} ] || mkdir -p ${BUILD_DIR}) 99 #检查BUILD_DIR是否创建成功 100 # Verify if it was successful. 101 BUILD_DIR := $(shell cd $(BUILD_DIR) && /bin/pwd)
#102行,if(a,b,c)如果a为真则执行b否则执行c,endif
102 $(if $(BUILD_DIR),,$(error output directory "$(saved-output)" does not exist)) 103 endif # ifneq ($(BUILD_DIR),)
|
4.定义了4个变量,存放有关目录的路径。OBJIREE表示目标代码的路径,并赋值为当前目录;
SRCTREE表示源代码的路径,并赋值为当前目录;TOPDIR表示顶层目录,并赋值为源代码所
在的目录的路径;LINDIR为链接目录,并赋值为目标代码所在目录的路径。
104 105 OBJTREE := $(if $(BUILD_DIR),$(BUILD_DIR),$(CURDIR)) 106 SRCTREE := $(CURDIR) 107 TOPDIR := $(SRCTREE) 108 LNDIR := $(OBJTREE) 109 export TOPDIR SRCTREE OBJTREE
|
5. 定义变量MKCONFIG,存放mkconfig这个文件的执行路径。
110 111 MKCONFIG := $(SRCTREE)/mkconfig 112 export MKCONFIG
|
6. 测试目标代码和源代码是不是在同一个目录里面,不是变量REMOTE_BUILD为1
113 114 ifneq ($(OBJTREE),$(SRCTREE)) 115 REMOTE_BUILD := 1 116 export REMOTE_BUILD 117 endif
|
7. 测试目标代码和源代码是不是在同一个目录,如果不在同一个目录,那么obj就存放自己修改过的目标代码路径,如果在同一个目录,则赋值为空,表示就在当前目录里面。
118 119 # $(obj) and (src) are defined in config.mk but here in main Makefile 120 # we also need them before config.mk is included which is the case for 121 # some targets like unconfig, clean, clobber, distclean, etc. 122 ifneq ($(OBJTREE),$(SRCTREE)) 123 obj := $(OBJTREE)/ 124 src := $(SRCTREE)/ 125 else 126 obj := 127 src := 128 endif 129 export obj src 130
131 # Make sure CDPATH settings don't interfere 132 unexport CDPATH
|
8. 如果变量ARCH==powerpc,ARCH=pcc.(ARCH在这之前没有定义啊???)
133 134 ############################################################# 135 136 ifeq ($(ARCH),powerpc) 137 ARCH = ppc 138 endif
|
9. 定义变量SUBDIRS,把tools这个目录包含在里面,注意这个变量在哪里使用
139 140 # The "tools" are needed early, so put this first 141 # Don't include stuff already done in $(LIBS) 142 SUBDIRS = tools \ 143 examples/standalone \ 144 examples/api 145 146 .PHONY : $(SUBDIRS)
|
10. 148行'wildcard' 的函 数,它有一个参数,功能是展开成一列所有符合由其参数描述的文件名,文件间以空格间隔。此句是判断$(obj)include/config.mk是否存在。『当调用make _config后将生成config.mk』---(来源于longhaihai'blog)。
152行出现了第一个目标all.158行包含进来了执行make boardname_config产生的文件
config.mk.这个文件里面定义了ARCH,SOC,CPU,BOARD等。
148 ifeq ($(obj)include/config.mk,$(wildcard $(obj)include/config.mk)) 149 150 # Include autoconf.mk before config.mk so that the config options are available 151 # to all top level build files. We need the dummy all: target to prevent the 152 # dependency target in autoconf.mk.dep from being the default. 153 all: 154 sinclude $(obj)include/autoconf.mk.dep 155 sinclude $(obj)include/autoconf.mk 156 157 # load ARCH, BOARD, and CPU configuration 158 include $(obj)include/config.mk 159 export ARCH CPU BOARD VENDOR SOC
|
11. 162行比较目标主机和宿主机的体系结构是否一样,来决定是否交叉编译。注意CROSS_COMPILE在以后哪里用到了。
160 161 # set default to nothing for native builds 162 ifeq ($(HOSTARCH),$(ARCH)) 163 CROSS_COMPILE ?= 164 endif 165 166 # load other configuration 167 include $(TOPDIR)/config.mk
|
12. 定义OBJS,如果cpu是arm920t,那么最后obj的值为:cpu/arm920t/start.o
168 169 ############################################################## 170 # U-Boot objects....order is important (i.e. start must be first) 171 172 OBJS = cpu/$(CPU)/start.o 173 ifeq ($(CPU),i386) 174 OBJS += cpu/$(CPU)/start16.o 175 OBJS += cpu/$(CPU)/resetvec.o 176 endif 177 ifeq ($(CPU),ppc4xx) 178 OBJS += cpu/$(CPU)/resetvec.o 179 endif 180 ifeq ($(CPU),mpc85xx) 181 OBJS += cpu/$(CPU)/resetvec.o 182 endif 183 184 OBJS := $(addprefix $(obj),$(OBJS))
|
13. LIBS,包含了后缀为.a的文件,这种文件是在进到到各个目录执行make产生的库文件,链接时需要这些文件。链接的文件涉及到了lib_generic目录,cpu目录,lib_arm目录(以arm为例子),fs目录,net目录,disk目录,drivers目录。从下面这段代码就可以清楚的知道,整个u-boot究竟需要什么文件,由哪些文件组成。
185 186 LIBS = lib_generic/libgeneric.a 187 LIBS += lib_generic/lzma/liblzma.a 188 LIBS += lib_generic/lzo/liblzo.a 189 LIBS += $(shell if [ -f board/$(VENDOR)/common/Makefile ]; then echo \ 190 "board/$(VENDOR)/common/lib$(VENDOR).a"; fi) 191 LIBS += cpu/$(CPU)/lib$(CPU).a 192 ifdef SOC 193 LIBS += cpu/$(CPU)/$(SOC)/lib$(SOC).a 194 endif 195 ifeq ($(CPU),ixp) 196 LIBS += cpu/ixp/npe/libnpe.a 197 endif 198 LIBS += lib_$(ARCH)/lib$(ARCH).a 199 LIBS += fs/cramfs/libcramfs.a fs/fat/libfat.a fs/fdos/libfdos.a fs/jffs2/libjffs2.a \ 200 fs/reiserfs/libreiserfs.a fs/ext2/libext2fs.a fs/yaffs2/libyaffs2.a \ 201 fs/ubifs/libubifs.a 202 LIBS += net/libnet.a 203 LIBS += disk/libdisk.a 204 LIBS += drivers/bios_emulator/libatibiosemu.a 205 LIBS += drivers/block/libblock.a 206 LIBS += drivers/dma/libdma.a 207 LIBS += drivers/fpga/libfpga.a 208 LIBS += drivers/gpio/libgpio.a 209 LIBS += drivers/hwmon/libhwmon.a 210 LIBS += drivers/i2c/libi2c.a 211 LIBS += drivers/input/libinput.a 212 LIBS += drivers/misc/libmisc.a 213 LIBS += drivers/mmc/libmmc.a 214 LIBS += drivers/mtd/libmtd.a 215 LIBS += drivers/mtd/nand/libnand.a 216 LIBS += drivers/mtd/onenand/libonenand.a 217 LIBS += drivers/mtd/ubi/libubi.a 218 LIBS += drivers/mtd/spi/libspi_flash.a 219 LIBS += drivers/net/libnet.a 220 LIBS += drivers/net/phy/libphy.a 221 LIBS += drivers/net/sk98lin/libsk98lin.a 222 LIBS += drivers/pci/libpci.a 223 LIBS += drivers/pcmcia/libpcmcia.a 224 LIBS += drivers/power/libpower.a 225 LIBS += drivers/spi/libspi.a 226 ifeq ($(CPU),mpc83xx) 227 LIBS += drivers/qe/qe.a 228 endif 229 ifeq ($(CPU),mpc85xx) 230 LIBS += drivers/qe/qe.a 231 LIBS += cpu/mpc8xxx/ddr/libddr.a 232 LIBS += cpu/mpc8xxx/lib8xxx.a 233 TAG_SUBDIRS += cpu/mpc8xxx 234 endif 235 ifeq ($(CPU),mpc86xx) 236 LIBS += cpu/mpc8xxx/ddr/libddr.a 237 LIBS += cpu/mpc8xxx/lib8xxx.a 238 TAG_SUBDIRS += cpu/mpc8xxx 240 LIBS += drivers/rtc/librtc.a 241 LIBS += drivers/serial/libserial.a 242 LIBS += drivers/twserial/libtws.a 243 LIBS += drivers/usb/gadget/libusb_gadget.a 244 LIBS += drivers/usb/host/libusb_host.a 245 LIBS += drivers/usb/musb/libusb_musb.a 246 LIBS += drivers/video/libvideo.a 247 LIBS += drivers/watchdog/libwatchdog.a 248 LIBS += common/libcommon.a 249 LIBS += libfdt/libfdt.a 250 LIBS += api/libapi.a 251 LIBS += post/libpost.a 252 253 LIBS := $(addprefix $(obj),$(LIBS)) 254 .PHONY : $(LIBS) $(TIMESTAMP_FILE) $(VERSION_FILE)
|
此时的目标代码为:obj = OBJS + LIBS.
14. 256-257,是具体的电路板涉及到的库文件,执行完这两句后,obj=OBJS+LIBS+LIBBOARD
255 256 LIBBOARD = board/$(BOARDDIR)/lib$(BOARD).a 257 LIBBOARD := $(addprefix $(obj),$(LIBBOARD)) 258 259 # Add GCC lib 260 ifdef USE_PRIVATE_LIBGCC 261 ifeq ("$(USE_PRIVATE_LIBGCC)", "yes") 262 PLATFORM_LIBGCC = -L $(OBJTREE)/lib_$(ARCH) -lgcc 263 else 264 PLATFORM_LIBGCC = -L $(USE_PRIVATE_LIBGCC) -lgcc 265 endif 266 else 267 PLATFORM_LIBGCC = -L $(shell dirname `$(CC) $(CFLAGS) -print-libgcc-file-name`) -lgcc 268 endif 269 PLATFORM_LIBS += $(PLATFORM_LIBGCC) 270 export PLATFORM_LIBS
|
15. 包含u-boot.lds.h,如果定义从Nandflash启动,就定义变量U_BOOT_ONENAND = $(obj)u-boot-onenand.bin.
271 272 # Special flags for CPP when processing the linker script. 273 # Pass the version down so we can handle backwards compatibility 274 # on the fly. 275 LDPPFLAGS += \ 276 -include $(TOPDIR)/include/u-boot/u-boot.lds.h \ 277 $(shell $(LD) --version | \ 278 sed -ne 's/GNU ld version \([0-9][0-9]*\)\.\([0-9][0-9]*\).*/-DLD_MAJOR=\1 -DLD_MINOR=\2/p') 279 280 ifeq ($(CONFIG_NAND_U_BOOT),y) 281 NAND_SPL = nand_spl 282 U_BOOT_NAND = $(obj)u-boot-nand.bin 283 endif 284 285 ifeq ($(CONFIG_ONENAND_U_BOOT),y) 286 ONENAND_IPL = onenand_ipl 287 U_BOOT_ONENAND = $(obj)u-boot-onenand.bin 288 ONENAND_BIN ?= $(obj)onenand_ipl/onenand-ipl-2k.bin 289 endif 290 291 __OBJS := $(subst $(obj),,$(OBJS)) 292 __LIBS := $(subst $(obj),,$(LIBS)) $(subst $(obj),,$(LIBBOARD))
|
16. 关注ALL
293 294 ############################################################## 295 ############################################################## 296 297 # Always append ALL so that arch config.mk
298 ALL += $(obj)u-boot.srec $(obj)u-boot.bin $(obj)System.map $(U_BOOT_NAND) $(U_BOOT_ONENAND)
|
17. 当输入命令 make all,讲寻找到此次的伪目标all,all依赖于 $(ALL),ALL由5部分组成。那么接下去就分别对这5部分进行递归的处理。(贴出来格式变了,最好自己看源码)。看到308行,
(1)目标文件u-boot.bin依赖于 $(obj)u-boot目标,可以在344行找到;
(1.1)$(obj)u-boot依赖于:depend $(SUBDIRS) $(OBJS) $(LIBBOARD) $(LIBS) $(LDSCRIPT) $(obj)u-boot.lds;
(1.1.1)depend;402行。执行的操作为:进入到$(SUBDIRS),生成各个子目
录的.depend文件。通过调用各个子目录的make _depend。
(1.1.2)$(SUBDIRS);363行。依赖depend。执行操作为:对各个子目录执行
makefile文件。
(1.1.3) $(OBJS);354行。依赖depend。执行:
$(MAKE) -C cpu/$(CPU) $(if $(REMOTE_BUILD),$@,$(notdir $@))
如果cpu为arm920t,编译文件cpu/arm920t。
(1.1.4) $(LIBS) ;357行。依赖$(SUBDIRS).执行操作为:
$(MAKE) -C $(dir $(subst $(obj),,$@)),
dir为GNU内嵌函数为取目录操作,比如$(dir a/b/c) 结果为
a/b/;subst 也是GNU内嵌函数$(subst from,to,text)表示在text
中把form 字符串替换为字符串to。subst资料上面是这样讲的。但是
在此处,我认为是:$(subst text, to, from)。总之,这样理解
吧:在LIBS包含的n个路径下面执行make
(1.1.5) $(LIBBOARD);360行。依赖depend,$(LIBS)。执行:
$(MAKE) -C $(dir $(subst $(obj),,$@))。
再在之前,$(LIBS)已经执行完毕,现在obj只剩下了:LIBBOAED路径
集合了。在这些路径集合下面执行make。
(1.1.6) $(LDSCRIPT);336行。依赖depend。执行:
$(MAKE) -C $(dir $@) $(notdir $@),notdir为取文件名函数,
比如,$(notdir a/my.o)结果为my.o。
(1.1.7) $(obj)u-boot.lds。369行。依赖$(LDSCRIPT).执行:
$(CPP) $(CPPFLAGS) $(LDPPFLAGS) -ansi -D__ASSEMBLY__ -P - <$^ >$@
生成链接文件。
300 all: $(ALL) 301 302 $(obj)u-boot.hex: $(obj)u-boot 303 $(OBJCOPY) ${OBJCFLAGS} -O ihex $< $@ 304 305 $(obj)u-boot.srec: $(obj)u-boot 306 $(OBJCOPY) -O srec $< $@ 307 308 $(obj)u-boot.bin: $(obj)u-boot 309 $(OBJCOPY) ${OBJCFLAGS} -O binary $< $@ 310 311 $(obj)u-boot.ldr: $(obj)u-boot 312 $(obj)tools/envcrc --binary > $(obj)env-ldr.o 313 $(LDR) -T $(CONFIG_BFIN_CPU) -c $@ $< $(LDR_FLAGS) 314 315 $(obj)u-boot.ldr.hex: $(obj)u-boot.ldr 316 $(OBJCOPY) ${OBJCFLAGS} -O ihex $< $@ -I binary 317 318 $(obj)u-boot.ldr.srec: $(obj)u-boot.ldr 319 $(OBJCOPY) ${OBJCFLAGS} -O srec $< $@ -I binary 320 321 $(obj)u-boot.img: $(obj)u-boot.bin 322 ./tools/mkimage -A $(ARCH) -T firmware -C none \ 323 -a $(TEXT_BASE) -e 0 \ 324 -n $(shell sed -n -e 's/.*U_BOOT_VERSION//p' $(VERSION_FILE) | \ 325 sed -e 's/"[ ]*$$/ for $(BOARD) board"/') \ 326 -d $< $@ 327 328 $(obj)u-boot.kwb: $(obj)u-boot.bin 329 $(obj)tools/mkimage -n $(KWD_CONFIG) -T kwbimage \ 330 -a $(TEXT_BASE) -e $(TEXT_BASE) -d $< $@ 331 332 $(obj)u-boot.sha1: $(obj)u-boot.bin 333 $(obj)tools/ubsha1 $(obj)u-boot.bin 334 335 $(obj)u-boot.dis: $(obj)u-boot 336 $(OBJDUMP) -d $< > $@ 337 338 GEN_UBOOT = \ 339 UNDEF_SYM=`$(OBJDUMP) -x $(LIBBOARD) $(LIBS) | \ 340 sed -n -e 's/.*\($(SYM_PREFIX)__u_boot_cmd_.*\)/-u\1/p'|sort|uniq`;\ 341 cd $(LNDIR) && $(LD) $(LDFLAGS) $$UNDEF_SYM $(__OBJS) \ 342 --start-group $(__LIBS) --end-group $(PLATFORM_LIBS) \ 343 -Map u-boot.map -o u-boot 344 $(obj)u-boot: depend $(SUBDIRS) $(OBJS) $(LIBBOARD) $(LIBS) $(LDSCRIPT) $(obj)u-boot.lds 345 $(GEN_UBOOT) 346 ifeq ($(CONFIG_KALLSYMS),y) 347 smap=`$(call SYSTEM_MAP,u-boot) | \ 348 awk '$$2 ~ /[tTwW]/ {printf $$1 $$3 "` ; \ 349 $(CC) $(CFLAGS) -DSYSTEM_MAP="\"$${smap}\"" \ 350 -c common/system_map.c -o $(obj)common/system_map.o 351 $(GEN_UBOOT) $(obj)common/system_map.o 352 endif 353 354 $(OBJS): depend 355 $(MAKE) -C cpu/$(CPU) $(if $(REMOTE_BUILD),$@,$(notdir $@)) 356 357 $(LIBS): depend $(SUBDIRS) 358 $(MAKE) -C $(dir $(subst $(obj),,$@)) 359 360 $(LIBBOARD): depend $(LIBS) 361 $(MAKE) -C $(dir $(subst $(obj),,$@)) 362 363 $(SUBDIRS): depend 364 $(MAKE) -C $@ all 365 366 $(LDSCRIPT): depend 367 $(MAKE) -C $(dir $@) $(notdir $@) 368 369 $(obj)u-boot.lds: $(LDSCRIPT) 370 $(CPP) $(CPPFLAGS) $(LDPPFLAGS) -ansi -D__ASSEMBLY__ -P - <$^ >$@ 371 372 $(NAND_SPL): $(TIMESTAMP_FILE) $(VERSION_FILE) $(obj)include/autoconf.mk 373 $(MAKE) -C nand_spl/board/$(BOARDDIR) all 374 375 $(U_BOOT_NAND): $(NAND_SPL) $(obj)u-boot.bin 376 cat $(obj)nand_spl/u-boot-spl-16k.bin $(obj)u-boot.bin > $(obj)u-boot-nand.bin 377 378 $(ONENAND_IPL): $(TIMESTAMP_FILE) $(VERSION_FILE) $(obj)include/autoconf.mk 379 $(MAKE) -C onenand_ipl/board/$(BOARDDIR) all
|
380 381 $(U_BOOT_ONENAND): $(ONENAND_IPL) $(obj)u-boot.bin 382 cat $(ONENAND_BIN) $(obj)u-boot.bin > $(obj)u-boot-onenand.bin 383 384 $(VERSION_FILE): 385 @( printf '#define U_BOOT_VERSION "U-Boot %s%s"\n' "$(U_BOOT_VERSION)" \ 386 '$(shell $(TOPDIR)/tools/setlocalversion $(TOPDIR))' ) > $@.tmp 387 @cmp -s $@ $@.tmp && rm -f $@.tmp || mv -f $@.tmp $@ 388 389 $(TIMESTAMP_FILE): 390 @date +'#define U_BOOT_DATE "%b %d %C%y"' > $@ 391 @date +'#define U_BOOT_TIME "%T"' >> $@ 392 393 gdbtools: 394 $(MAKE) -C tools/gdb all || exit 1 395 396 updater: 397 $(MAKE) -C tools/updater all || exit 1 398 399 env: 400 $(MAKE) -C tools/env all MTD_VERSION=${MTD_VERSION} || exit 1 401 402 depend dep: $(TIMESTAMP_FILE) $(VERSION_FILE) $(obj)include/autoconf.mk 403 for dir in $(SUBDIRS) ; do $(MAKE) -C $$dir _depend ; done
|
18. 至此,u-boot的整个框架已经在前面分析完了。剩下的代码就是一些细节的问题。不列出。
19. 执行make smdk2410_config 是怎么执行的?
是怎么把6个参数传递给MKCONFIG的?执行,make smdk2410_coinfig, 指定了目标为smdk2410_config 。 那么就寻找Makefile中的伪目录sdmk2410_config。在3047行。
smdk2410依赖于unconfig。所以看看unconfig是什么样,unconfig在499行,用于删除以前make时候产生的垃圾文件。
再回到3048行,$(MKCONFIG)是前面定义的执行路径。$(@:_config=) 的结果为smdk2410,所以传入的参数为:smdk2410 arm arm920t smdk2410 samsung s3c24x0。刚好与上一篇分析MKCONFIG的博客吻合。
3047 smdk2410_config : unconfig 3048 @$(MKCONFIG) $(@:_config=) arm arm920t smdk2410 samsung s3c24x0
|
499 unconfig: 500 @rm -f $(obj)include/config.h $(obj)include/config.mk \ 501 $(obj)board/*/config.tmp $(obj)board/*/*/config.tmp \ 502 $(obj)include/autoconf.mk $(obj)include/autoconf.mk.dep
|
参考博客:http://blog.mcuol.com/lvembededsys/Category.htm?ID=216
zzzppp.cublog.cn
后记:Makefile是非常烦且复杂的规则。本文将不定时更新,以加入对u-boot
新的理解。下一篇将写u-boot的链接过程。
本文难免有谬误,欢迎指出,共同进步!
欢迎转载,但是注明出处。