1. 程式人生 > 其它 >linux kernel makefile 分析 - 5

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 614
else 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