• 二、编译第一步 make xxx_defconfig


    2.1 顶层make defconfig规则

      make xxx_defconfig 的执行主要分成三个部分:

    1. 执行 make -f ./scripts/Makefile.build obj=scripts/basic,编译生成 scripts/basic/fixdep 工具
    2. 执行 make -f ./scripts/Makefile.build obj=scripts/kconfig rpi_3_32b_defconfig 编译生成 scripts/kconfig/conf 工具
    3. 执行 scripts/kconfig/conf --defconfig=arch/../configs/xxx_defconfig Kconfig生成最终的 .config 配置文件 

      执行 make xxx_defconfig 命令时,u-boot 根目录下的 Makefile 中有唯一的规则匹配目标:

      代码第 467 到 478 行

    复制代码
     1 # ===========================================================================
     2 # *config targets only - make sure prerequisites are updated, and descend
     3 # in scripts/kconfig to make the *config target
     4 
     5 KBUILD_DEFCONFIG := sandbox_defconfig
     6 export KBUILD_DEFCONFIG KBUILD_KCONFIG
     7 
     8 config: scripts_basic outputmakefile FORCE
     9     $(Q)$(MAKE) $(build)=scripts/kconfig $@
    10 
    11 %config: scripts_basic outputmakefile FORCE
    12     $(Q)$(MAKE) $(build)=scripts/kconfig $@
    复制代码

      注释意思为,仅限 *config 目标,确保先决条件已经更新,并在 scripts/kconfig 下创建 *config 目标。上面有两个变量 config 和 %config,% 符号为通配符,对应所有的 xxxconfig 目标,前面已经说过。我们的 make xxx_defconfig 就对应 %config,我们并没有执行 make config 命令。

      Makefile中几种变量赋值运算符:

    • =      :最简单的赋值 
    • :=     :一般也是赋值 
      • 以上这两个大部分情况下效果是一样的,但是有时候不一样。
      • 用 = 赋值的变量,在被解析时他的值取决于最后一次赋值时的值,所以看变量引用的值时不能只往前面看,还要往后面看。
      • 用 := 来赋值的,则是就地直接解析,只用往前看即可。  
    • ?=     : 如果变量前面并没有赋值过则执行这条赋值,如果前面已经赋值过了则本行被忽略。
    • +=   用来给一个已经赋值的变量接续赋值,意思就是把这次的值加到原来的值的后面,有点类似于strcat     
      • 在shell makefile等文件中,可以认为所有变量都是字符串,+= 就相当于给字符串 strcat 接续内容
      • +=续接的内容和原来的内容之间会自动加一个空格隔开

    2.1.1 代码执行到%config 的条件  

      先往上分析下这段代码的执行条件: ifeq ($(config-targets),1),代码在415 到 447 行

    复制代码
     1 # To make sure we do not include .config for any of the *config targets
     2 # catch them early, and hand them over to scripts/kconfig/Makefile
     3 # It is allowed to specify more targets when calling make, including
     4 # mixing *config targets and build targets.
     5 # For example 'make oldconfig all'.
     6 # Detect when mixed targets is specified, and make a second invocation
     7 # of make so .config is not included in this case either (for *config).
     8 
     9 version_h := include/generated/version_autogenerated.h
    10 timestamp_h := include/generated/timestamp_autogenerated.h
    11 
    12 no-dot-config-targets := clean clobber mrproper distclean 
    13              help %docs check% coccicheck 
    14              ubootversion backup tests
    15 
    16 config-targets := 0
    17 mixed-targets  := 0
    18 dot-config     := 1
    19 
    20 ifneq ($(filter $(no-dot-config-targets), $(MAKECMDGOALS)),)
    21     ifeq ($(filter-out $(no-dot-config-targets), $(MAKECMDGOALS)),)
    22         dot-config := 0
    23     endif
    24 endif
    25 
    26 ifeq ($(KBUILD_EXTMOD),)
    27         ifneq ($(filter config %config,$(MAKECMDGOALS)),)
    28                 config-targets := 1
    29                 ifneq ($(words $(MAKECMDGOALS)),1)
    30                         mixed-targets := 1
    31                 endif
    32         endif
    33 endif
    复制代码

      代码注释内容:为了确保我们不包含任何 * config 目标的 .config,请尽早捕获它们,并将它们交给 scripts / kconfig / Makefile。调用make 时允许指定更多目标,包括混合 * config 目标和构建目标。例如 'make oldconfig all' 。检测何时指定了混合目标,并进行make的第二次调用,因此 .config不包含在这种情况下(对于* config)。

    • version_h:版本号文件,此文件是自动生成的
    • timestamp_h:时间戳文件,此文件是自动生成的
    • no-dot-config-targets:指代的是那些和 .config 没有关系的目标
    • config-targets:配置目标,初始值设置为0
    • mixed-targets:混合目标,初始值设置为0
    • dot-config:初始值设置为1

      变量 MAKECMDGOALS:make 在执行时会设置一个特殊变量 -- "MAKECMDGOALS" ,该变量记录了命令行参数指定的终极目标列表,没有通过参数指定终极目标时此变量为空。该变量仅限于用在特殊场合(比如判断),在 Makefile 中最好不要对它进行重新定义。

      我们执行  make xxx_defconfig 的时候,MAKECMDGOALS 变量的值就为 xxx_defconfig。

      filter 函数 和 filter-out 函数:

    复制代码
    1 $(filter PATTERN…,TEXT)
    2 函数名称:  过滤函数— filter。
    3 函数功能:  过滤掉字串“ TEXT”中所有不符合模式“ PATTERN”的单词,保留所
    4         有符合此模式的单词。可以使用多个模式。模式中一般需要包含模式字
    5        符“ %”。存在多个模式时,模式表达式之间使用空格分割。
    6 返回值:空格分割的“ TEXT”字串中所有符合模式“ PATTERN”的字串。
    7 函数说明:“ filter”函数可以用来去除一个变量中的某些字符串
    复制代码
    1 $(filter-out PATTERN...,TEXT)
    2 函数名称:  反过滤函数— filter-out
    3 函数功能:  和“ filter”函数实现的功能相反。过滤掉字串“ TEXT”中所有符合模式“ PATTERN”的单词,保留所有不符合此模式的单词。
            可以有多个模式。存在多个模式时,模式表达式之间使用空格分割 4 返回值:  空格分割的“ TEXT”字串中所有不符合模式“ PATTERN”的字串。 5 函数说明:  “ filter-out”函数也可以用来去除一个变量中的某些字符串,(实现和“ filter”函数相反)

      代码执行的过程就为,如果 过滤掉 MAKECMDGOALS 不符合 no-dot-config-targets 后结果不为空,则执行分支语句。很显然过滤后为空,则不执行分支语句,dot-config 依然 值为1。

      接着执行下一条 ifeq 语句,对 KBUILD_EXTMOD 进行判定。KBUILD_EXTMODE 的赋值地方在代码 182 到 191 行处:

    复制代码
     1 # Use make M=dir to specify directory of external module to build
     2 # Old syntax make ... SUBDIRS=$PWD is still supported
     3 # Setting the environment variable KBUILD_EXTMOD take precedence
     4 ifdef SUBDIRS
     5   KBUILD_EXTMOD ?= $(SUBDIRS)
     6 endif
     7 
     8 ifeq ("$(origin M)", "command line")
     9   KBUILD_EXTMOD := $(M)
    10 endif
    复制代码

      由注释可以知道,SUBDIRS 这个变量是通过执行 make 的时候传进来的,我们并没有执行此项,所以未定义SUBDIRS,第一个分支不会去走。第二个 if 语句为 ifeq 语句,这里使用 origin 函数。

      origin 函数不是操作变量(即它的参数),它只是获取此变量(参数)相关的信息,告诉我们这个变量的出处(定义方式)。

      那么 ifeq 语句可以理解为 如果make传入的命令行变量存在且是M,那么,变量KBUILD_EXTMOD变为变量M的值。

      第一阶段中我们并没有传入 M 值,则 KBUILD_EXTMOD 值为空

      继续回到此小节主代码处,当前执行 KBUILD_EXTMOD 的判定,此处满足 ifeq  条件,开始执行分支语句,分支语句同样是一个判断,首先过滤掉 MAKECMDGOALS 不符合 config 和 %config 模式的字符串,然后返回 xxx_defconfig ,xxx_defconfig 再与 空进行比较,if 语句为 ifneq ,很显然, filter  语句不为空,与空进行比较,满足 ifneq 执行语句。

      此处将 config-targets 重新赋值为 1;赋值完后,进行  ifneq  条件判断,再次涉及  makefile 的函数——words。

      

      显然我们的传入的单词数据为1,与1相等,则不执行分支,即 mixed-targets 不进行重新赋值,依然为0。

      再代码进行到 ifeq ($(config-targets),1) 时候,先进行了 ifeq ($(mixed-targets),1)分支,如果 mixed-targets 则执行另一条分支,就不会再执行ifeq ($(config-targets),1) 了。我们这里执行的ifeq ($(mixed-targets),1) 的 else中的分支语句。

      到此处,ifeq ($(config-targets),1) 是否会执行已经分析完毕。

      当前我们已经知道的变量的值为:

    • MAKECMDGOALS = xxx_defconfig
    • KBUILD_EXTMOD =
    • version_h := include/generated/version_autogenerated.h
    • timestamp_h := include/generated/timestamp_autogenerated.h
    • no-dot-config-targets := clean clobber mrproper distclean help %docs check% coccicheck ubootversion backup tests
    • config-targets := 1
    • mixed-targets := 0
    • dot-config := 1

    2.1.2 %config分析

      ifeq ($(config-targets),1) 中也由else分支,我们从上面的小节可以知道,else 分支不会去执行。所以很多代码可以忽略了

      

      从这里可以知道,此处此处选择语句一直执行到 1655 行,466~480 行才是我们需要分析的。

      471 到 472 行

    1 KBUILD_DEFCONFIG := sandbox_defconfig
    2 export KBUILD_DEFCONFIG KBUILD_KCONFIG

      这里定义了两个环境变量:

    • KBUILD_DEFCONFIG = sandbox_defconfig  
    • KBUILD_KCONFIG 为空

      继续执行474 到 475 行

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

      此处目标没有匹配,不会去执行

      继续执行477 478 行

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

      %config 依赖scripts_basic outputmakefile  FORCE

    (1)依赖 FORCE

      FORCE 的定义在 1748 和 1749 行

    1 PHONY += FORCE
    2 FORCE:

      FORCE被定义为一个空目标。如果一个目标添加 FORCE 依赖,每次编译都会去先去执行 FORCE(实际上什么都不做),然后运行命令更新目标,这样就能确保目标每次都会被更新。

    (2)依赖 scripts_basic

      392 - 402 行

    复制代码
     1 # ===========================================================================
     2 # Rules shared between *config targets and build targets
     3 
     4 # Basic helpers built in scripts/
     5 PHONY += scripts_basic
     6 scripts_basic:
     7     $(Q)$(MAKE) $(build)=scripts/basic
     8     $(Q)rm -f .tmp_quiet_recordmcount
     9 
    10 # To avoid any implicit rule to kick in, define an empty command.
    11 scripts/basic/%: scripts_basic ;
    复制代码

       Q = @,MAKE = make,build 变量的定义在 scripts/Kbuild.include 文件中

      主Makefile 在327-329 行包含 scripts/Kbuild.include 文件

    1 # We need some generic definitions (do not try to remake the file).
    2 scripts/Kbuild.include: ;
    3 include scripts/Kbuild.include

      build 变量在 scripts/Kbuild.include 在177 - 181 行定义

    1 ###
    2 # Shorthand for $(Q)$(MAKE) -f scripts/Makefile.build obj=
    3 # Usage:
    4 # $(Q)$(MAKE) $(build)=dir
    5 build := -f $(srctree)/scripts/Makefile.build obj

      srctree 定义在主 Makefile 中202-212 行

    复制代码
     1 ifeq ($(KBUILD_SRC),)
     2         # building in the source tree
     3         srctree := .
     4 else
     5         ifeq ($(KBUILD_SRC)/,$(dir $(CURDIR)))
     6                 # building in a subdirectory of the source tree
     7                 srctree := ..
     8         else
     9                 srctree := $(KBUILD_SRC)
    10         endif
    11 endif
    复制代码

      KBUILD_SRC (构建的源码目录) 在执行 make 命令的时候并没有传入,设为空,则srctree 为当前 uboot 源码的根目录

      scripts/Kbuild.include 在177 - 181 行的展开为:build := -f ./scripts/Makefile.build obj

      主 Makefile 中 scripts_basic 的展开为:

    1 scripts_basic:
    2      make -f ./scripts/Makefile.build obj=scripts/basic  # 根据传入的 obj 参数显示的执行 ./scripts/Makefile.build 文件
    3      rm -f .tmp_quiet_recordmcount

      ./scripts/Makefile.build 文件之后再分析。

    (3)outputmakefile 依赖

      404 - 413 行

    复制代码
     1 PHONY += outputmakefile
     2 # outputmakefile generates a Makefile in the output directory, if using a
     3 # separate output directory. This allows convenient use of make in the
     4 # output directory.
     5 outputmakefile:
     6 ifneq ($(KBUILD_SRC),)
     7     $(Q)ln -fsn $(srctree) source
     8     $(Q)$(CONFIG_SHELL) $(srctree)/scripts/mkmakefile 
     9         $(srctree) $(objtree) $(VERSION) $(PATCHLEVEL)
    10 endif
    复制代码

      KBUILD_SRC 为空,所以ifneq 中的语句不会执行。 outputmakefile 为空

      重新回到 %config 处

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

      依据前面的条件,展开表达式为:

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

    2.2 总结

    2.2.1 变量

    • MAKECMDGOALS = xxx_defconfig
    • KBUILD_EXTMOD =
    • version_h := include/generated/version_autogenerated.h
    • timestamp_h := include/generated/timestamp_autogenerated.h
    • no-dot-config-targets := clean clobber mrproper distclean help %docs check% coccicheck ubootversion backup tests
    • config-targets := 1
    • mixed-targets := 0
    • dot-config := 1
    • KBUILD_SRC =
    • build := -f ./scripts/Makefile.build obj

    2.2.2 环境变量

    • KBUILD_DEFCONFIG := sandbox_defconfig
    • KBUILD_KCONFIG =

    2.2.3 需要进行分析的地方

    (1)scripts_basic 目标执行的命令

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

    (2)%config 目标执行的命令

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

  • 相关阅读:
    构建之法第九、十章读后感
    构建之法第七章读后感
    构建之法五、六章读后感
    构建之法第四章读后感
    一组阶段小记之再读构建之法
    打印控件
    TTS语音
    VS2005通过网络连接CE设备进行调试开发
    Windows XP下安装WinCE6.0开发环境
    利用VS2005创建WINCE 6.0 平台
  • 原文地址:https://www.cnblogs.com/idyllcheung/p/11206629.html
Copyright © 2020-2023  润新知