第十一章 U-boot 顶层 Makefile 详解 (make xxx_defconfig 过程)

11.2.13 make xxx_defconfig 过程

        在编译 uboot 之前要使用“make xxx_defconfig”命令来配置 uboot,在顶层 Makefile 中有如下代码:

475 # To make sure we do not include .config for any of the *config
476 # targets catch them early, and hand them over to
477 # scripts/kconfig/Makefile It is allowed to specify more targets
478 # when calling make, including mixing *config targets and build
479 # targets.For example 'make oldconfig all'.
480 # Detect when mixed targets is specified, and make a second
481 # invocation of make so .config is not included in this case either
        (for *config).
482
483 version_h := include/generated/version_autogenerated.h
484 timestamp_h := include/generated/timestamp_autogenerated.h
485 defaultenv_h := include/generated/defaultenv_autogenerated.h
486
487 no-dot-config-targets := clean clobber mrproper distclean \
488 help %docs check% coccicheck \
489 ubootversion backup tests check qcheck
490
491 config-targets := 0
492 mixed-targets := 0
493 dot-config := 1
494
495 ifneq ($(filter $(no-dot-config-targets), $(MAKECMDGOALS)),)
496    ifeq ($(filter-out $(no-dot-config-targets), $(MAKECMDGOALS)),)
497        dot-config := 0
498    endif
499 endif
500
501 ifeq ($(KBUILD_EXTMOD),)
502    ifneq ($(filter config %config,$(MAKECMDGOALS)),)
503         config-targets := 1
504         ifneq ($(words $(MAKECMDGOALS)),1)
505            mixed-targets := 1
506            endif
507    endif
508 endif
509
510 ifeq ($(mixed-targets),1)
511 # ==============================================================
512 # We're called with mixed targets (*config and build targets).
513 # Handle them one by one.
514
515 PHONY += $(MAKECMDGOALS) __build_one_by_one
516
517 $(filter-out __build_one_by_one, $(MAKECMDGOALS)):
__build_one_by_one
518    @:
519
520 __build_one_by_one:
521 $(Q)set -e; \
522 for i in $(MAKECMDGOALS); do \
523    $(MAKE) -f $(srctree)/Makefile $$i; \
524    done
525
526 else
527 ifeq ($(config-targets),1)
528 # ================================================================
529 # *config targets only - make sure prerequisites are updated, and
530 # descend in scripts/kconfig to make the *config target
531
532 KBUILD_DEFCONFIG := sandbox_defconfig
533 export KBUILD_DEFCONFIG KBUILD_KCONFIG
534
535 config: scripts_basic outputmakefile FORCE
536    $(Q)$(MAKE) $(build)=scripts/kconfig $@
537
538 %config: scripts_basic outputmakefile FORCE
539    $(Q)$(MAKE) $(build)=scripts/kconfig $@
540
541 else
542 # ================================================================
543 # Build targets only - this includes vmlinux, arch specific
544 # targets, clean targets and others. In general all targets except
    *config targets.
545
546 # Additional helpers built in scripts/
547 # Carefully list dependencies so we do not try to build scripts
548 # twice in parallel
549 PHONY += scripts
550 scripts: scripts_basic include/config/auto.conf
551    $(Q)$(MAKE) $(build)=$(@)
552
553 ifeq ($(dot-config),1)
554 # Read in config
555 -include include/config/auto.conf
......

        第 483 行定义了变量 version_h,这变量保存版本号文件,此文件是自动生成的。文件include/generated/version_autogenerated.h。
        第 484 行定义了变量 timestamp_h,此变量保存时间戳文件,此文件也是自动生成的。文件
include/generated/timestamp_autogenerated.h。
        第 487 行定义了变量 no-dot-config-targets。
        第 491 行定义了变量 config-targets,初始值为 0。
        第 492 行定义了变量 mixed-targets,初始值为 0。
        第 493 行定义了变量 dot-config,初始值为 1。
        第 495 行将 MAKECMDGOALS 中不符合 no-dot-config-targets 的部分过滤掉,剩下的如果
不为空的话条件就成立。 MAKECMDGOALS 是 make 的一个环境变量,这个变量会保存你所指
定的终极目标列表。如执行“make mx6ull_alientek_emmc_defconfig”,那么MAKECMDGOALS就为 mx6ull_alientek_emmc_defconfig。很明显过滤后为空,所以条件不成立,变量 dot-config 依旧为 1。
        第 501 行判断 KBUILD_EXTMOD 是否为空,如果 KBUILD_EXTMOD 为空的话条件成立,经过前面的分析,我们知道 KBUILD_EXTMOD 为空,所以条件成立。
        第 502 行将 MAKECMDGOALS 中不符合“config”和“%config”的部分过滤掉,如果剩下的部分不为空条件就成立,很明显此处条件成立,变量 config-targets=1。
        第 504 行统计 MAKECMDGOALS 中的单词个数,如果不为 1 的话条件成立。此处调用Makefile 中的 words 函数来统计单词个数,words 函数格式如下:

$(words <text>)

        很明显,MAKECMDGOALS 的单词个数是 1 个,所以条件不成立,mixed-targets 继续为0。综上所述,这些变量值如下:

config-targets = 1
mixed-targets = 0
dot-config = 1

        第 510 行如果变量 mixed-targets 为 1 的话条件成立,很明显,条件不成立。
        第 527 行如果变量 config-targets 为 1 的话条件成立,很明显,条件成立,执行这个分支。
        第 535 行,没有目标与之匹配,所以不执行。
        第 538 行,有目标与之匹配,当输入“make xxx_defconfig”的时候就会匹配到%config 目标,目标“%config”依赖于 scripts_basic、outputmakefile 和 FORCE。FORCE 在顶层 Makefile后面有如下定义:

2166 PHONY += FORCE
2167 FORCE:

        可以看出 FORCE 是没有规则和依赖的,所以每次都会重新生成 FORCE。当 FORCE 作为其他目标的依赖时,由于 FORCE 总是被更新过的,因此依赖所在的规则总是会执行的。依赖scripts_basic 和 outputmakefile 在顶层 Makefile 中的内容如下:

455 # Basic helpers built in scripts/
456 PHONY += scripts_basic
457 scripts_basic:
458     $(Q)$(MAKE) $(build)=scripts/basic
459     $(Q)rm -f .tmp_quiet_recordmcount
460
461 # To avoid any implicit rule to kick in, define an empty command.
462 scripts/basic/%: scripts_basic ;
463
464 PHONY += outputmakefile
465 # outputmakefile generates a Makefile in the output directory, if
466 # using a separate output directory. This allows convenient use of
467 # make in the output directory.
468 outputmakefile:
469 ifneq ($(KBUILD_SRC),)
470     $(Q)ln -fsn $(srctree) source
471     $(Q)$(CONFIG_SHELL) $(srctree)/scripts/mkmakefile \
472         $(srctree) $(objtree) $(VERSION) $(PATCHLEVEL)
473 endif

        第 469 行 , 判断 KBUILD_SRC 是 否 为空 , 只有变 量 KBUILD_SRC 不空时,outputmakefile 才有意义,经过我们前面的分析 KBUILD_SRC 为空,所以 outputmakefile 无效。只有 scripts_basic 是有效的。
        第 457~459 行是 scripts_basic 的规则,其对应的命令用到了变量 Q、MAKE 和 build,其中

Q=@或为空
MAKE=make

        变量 build 是在 scripts/Kbuild.include 文件中有定义:

178 ###
179 # Shorthand for $(Q)$(MAKE) -f scripts/Makefile.build obj=
180 # Usage:
181 # $(Q)$(MAKE) $(build)=dir
182 build := -f $(srctree)/scripts/Makefile.build obj

        从示例代码 11.2.13.4 可以看出 build=-f $(srctree)/scripts/Makefile.build obj,经过前面的分析可知,变量 srctree 为”.”,因此:

build=-f ./scripts/Makefile.build obj

        scripts_basic 展开以后如下:

scripts_basic:
@make -f ./scripts/Makefile.build obj=scripts/basic //也可以没有@,视配置而定
@rm -f . tmp_quiet_recordmcount
//也可以没有@

        scripts_basic 会调用文件./scripts/Makefile.build,这个我们后面再分析。接着回到示例代码 中的%config 处,内容如下:

%config: scripts_basic outputmakefile FORCE
$(Q)$(MAKE) $(build)=scripts/kconfig $@

将命令展开就是:

@make -f ./scripts/Makefile.build obj=scripts/kconfig xxx_defconfig

        同样也跟文件./scripts/Makefile.build 有关,我们后面再分析此文件。使用如下命令配置 uboot,并观察其配置过程:

make stm32mp15_atk_defconfig V=1

配置过程如图 所示:

 从图 11.2.13.1 可以看出,我们的分析是正确的,接下来就要结合下面两行命令重点分析一下文件 scripts/Makefile.build。
1、scripts_basic 目标对应的命令
                @make -f ./scripts/Makefile.build obj=scripts/basic
2、%config 目标对应的命令
                @make -f ./scripts/Makefile.build obj=scripts/kconfig xxx_defconfig

  • 13
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值