linux kernel makefile 分析 - 5

上一篇: https://www.cnblogs.com/zhangzhiwei122/p/16027368.html

背景说明

版本:

5.10.0 - 下面分析中 使用的行号,都是 参考 这个 版本的 Makefile 。

在线浏览: https://lxr.missinglinkelectronics.com/linux/Makefile

 

使用场景:

在源码文件夹下面建立一个build 文件夹,然后使用 O=build 

mkdir build

make O=build

 

606 ~ 616 __all 的依赖子目标

1、604 else 进入,表示不是  config 目标。而是一个 build target 。

2、611 ~ 616  https://www.cnblogs.com/zhangzhiwei122/p/16025859.html 中的 Makefile 的首个目标(即默认目标) __all 的依赖子目标设置。

    如果是编译外部模块,KBUILD_EXTMOD 不为空,  __all 依赖 modules 目标

     如果是编译 内核 代码, KBUILD_EXTMOD 为空, __all 依赖 all 。

 604else #!config-build
 605# ===========================================================================
 606# Build targets only - this includes vmlinux, arch specific targets, clean
 607# targets and others. In general all targets except *config targets.
 608
 609# If building an external module we do not care about the all: rule
 610# but instead __all depend on modules
 611PHONY += all
 612ifeq ($(KBUILD_EXTMOD),)
 613__all: all
 614else
 615__all: modules
 616endif

 

modules 子目标

https://www.cnblogs.com/zhangzhiwei122/p/16027368.html 中, 1104 ~ 1683  和  1684 ~ 1747 这两部分是对立的( make 可见1104 ~ 1683  时,对  1684 ~ 1747就不可见。)

所以,modules 在 这两块里面都有 定义。

 

1104 ~ 1683 编译内核代码时的modules 目标

  这一块 时 make 编译 内核源码时可见。有 根据 内核配置(make menuconfig) 中是否 配置了支持 内核模块  (CONFIG_MODULES ) 分为两部分。

1375  ~  1446 之间是  配置了 CONFIG_MODULES 时的处理, 1379 让all 依赖 modules, 1395  定义modules 目标更新规则

1447 ~ 1460 之间是 没有配置 CONFIG_MODULES 时的处理,1452 定义modules modules_install 目标的更新规则,即打印错误 “The present kernel configuration has modules disabled

1374
1375ifdef CONFIG_MODULES
1376
1377# By default, build modules as well
1378
1379all: modules


1394PHONY += modules
1395modules: $(if $(KBUILD_BUILTIN),vmlinux) modules_check modules_prepare
1396        $(Q)$(MAKE) -f $(srctree)/scripts/Makefile.modpost


1446else # CONFIG_MODULES
1447
1448# Modules not configured
1449# ---------------------------------------------------------------------------
1450
1451PHONY += modules modules_install
1452modules modules_install:
1453        @echo >&2
1454        @echo >&2 "The present kernel configuration has modules disabled."
1455        @echo >&2 "Type 'make config' and enable loadable module support."
1456        @echo >&2 "Then build a kernel with module support enabled."
1457        @echo >&2
1458        @exit 1
1459
1460endif # CONFIG_MODULES

 

 

1684 ~ 1747 编译外部模块时的modules 目标

1708 行定义 modules 目标更新规则,启动sub make, 指定makefile 为 scripts/Makefile.modpost

1683else # KBUILD_EXTMOD
1684


1707PHONY += modules
1708modules: $(MODORDER)
1709        $(Q)$(MAKE) -f $(srctree)/scripts/Makefile.modpost
1710


1747endif # KBUILD_EXTMOD

 

618 ~ 641 & 1973 ~ 1975 built-in modules or both

KBUILD_BUILTIN  KBUILD_MODULES 配置

621 ~ 622 定义默认值。 built-in 默认为 1, modules 默认不做。

624 ~ 627 更新 built-in 的值。如果命令行只指定了 一个 modules 目标,则 将 built-in 设置为空

629 ~ 639 定义两种设置 modules 为1 的情况。 a: make  <whatever>  modules;  (make 后面跟 modules + 其他目标)b : make  (即make 后面不跟任何目标)

 621KBUILD_MODULES :=
 622KBUILD_BUILTIN := 1
 623
 624# If we have only "make modules", don't compile built-in objects.
 625ifeq ($(MAKECMDGOALS),modules)
 626  KBUILD_BUILTIN :=
 627endif
 628
 629# If we have "make <whatever> modules", compile modules
 630# in addition to whatever we do anyway.
 631# Just "make" or "make all" shall build modules as well
 632
 633ifneq ($(filter all modules nsdeps %compile_commands.json clang-%,$(MAKECMDGOALS)),)
 634  KBUILD_MODULES := 1
 635endif
 636
 637ifeq ($(MAKECMDGOALS),)
 638  KBUILD_MODULES := 1
 639endif
 640
 641export KBUILD_MODULES KBUILD_BUILTIN
 
 

1142 ~ 1151 之间(这是编译内核代码可见部分),如果 配置了CONFIG_TRIM_UNUSED_KSYMS 从vmlinux 中剔除 不使用的 符号。这个feature 需要 编译内核,所以设置 modules

1142ifdef CONFIG_TRIM_UNUSED_KSYMS
1143# For the kernel to actually contain only the needed exported symbols,
1144# we have to build modules as well to determine what those symbols are.
1145# (this can be evaluated only once include/config/auto.conf has been included)
1146KBUILD_MODULES := 1
1147

1151endif

1702 ~ 1704 之间(这是 编译外部模块可见部分),覆盖之前的设置,built-in 为空,modules 为 1 。

1702# We are always building only modules.
1703KBUILD_BUILTIN :=
1704KBUILD_MODULES := 1

1783  (single-build 可见部分),设置 modules 为 1 。

1793 ~ 1795 , 如果 CONFIG_MODULES 没有配置,则覆盖  modules 为空。

 

KBUILD_BUILTIN   KBUILD_MODULES 用途

在scripts/Makefile.build 中

484 ~ 488 行,定义 __build 目标的依赖。如果定义 了  KBUILD_BUILTIN 依赖就增加 targets-for-builtin ; 如果定义了 KBUILD_MODULES 依赖就增加 targets-for-modules

   8PHONY := __build
   9__build:
   
 454ifdef single-build
 455
 469__build: $(curdir-single) $(single-subdirs)
 481
 482else
 483
 484__build: $(if $(KBUILD_BUILTIN), $(targets-for-builtin)) \
 485         $(if $(KBUILD_MODULES), $(targets-for-modules)) \
 486         $(subdir-ym) $(always-y)
 487        @:
 488
 489endif
 

 

下一篇:

编译单个目标 (举例 make vmlinux)时,需要 auto.conf 时怎么处理

https://www.cnblogs.com/zhangzhiwei122/p/16029312.html

 

posted @ 2022-03-20 10:37  张志伟122  阅读(443)  评论(0编辑  收藏  举报