Skip to content

Commit f73edc8

Browse files
committed
kbuild: unify two modpost invocations
Currently, modpost is executed twice; first for vmlinux, second for modules. This commit merges them. Current build flow ================== 1) build obj-y and obj-m objects 2) link vmlinux.o 3) modpost for vmlinux 4) link vmlinux 5) modpost for modules 6) link modules (*.ko) The build steps 1) through 6) are serialized, that is, modules are built after vmlinux. You do not get benefits of parallel builds when scripts/link-vmlinux.sh is being run. New build flow ============== 1) build obj-y and obj-m objects 2) link vmlinux.o 3) modpost for vmlinux and modules 4a) link vmlinux 4b) link modules (*.ko) In the new build flow, modpost is invoked just once. vmlinux and modules are built in parallel. One exception is CONFIG_DEBUG_INFO_BTF_MODULES=y, where modules depend on vmlinux. Signed-off-by: Masahiro Yamada <[email protected]> Tested-by: Nick Desaulniers <[email protected]> Reviewed-by: Nicolas Schier <[email protected]>
1 parent 9c5a0ac commit f73edc8

File tree

4 files changed

+54
-76
lines changed

4 files changed

+54
-76
lines changed

Makefile

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,7 +1150,7 @@ cmd_link-vmlinux = \
11501150
$(CONFIG_SHELL) $< "$(LD)" "$(KBUILD_LDFLAGS)" "$(LDFLAGS_vmlinux)"; \
11511151
$(if $(ARCH_POSTLINK), $(MAKE) -f $(ARCH_POSTLINK) $@, true)
11521152

1153-
vmlinux: scripts/link-vmlinux.sh vmlinux.o $(KBUILD_LDS) FORCE
1153+
vmlinux: scripts/link-vmlinux.sh vmlinux.o $(KBUILD_LDS) modpost FORCE
11541154
+$(call if_changed_dep,link-vmlinux)
11551155

11561156
targets := vmlinux
@@ -1426,7 +1426,13 @@ endif
14261426
# Build modules
14271427
#
14281428

1429-
modules: $(if $(KBUILD_BUILTIN),vmlinux) modules_prepare
1429+
# *.ko are usually independent of vmlinux, but CONFIG_DEBUG_INFOBTF_MODULES
1430+
# is an exception.
1431+
ifdef CONFIG_DEBUG_INFO_BTF_MODULES
1432+
modules: vmlinux
1433+
endif
1434+
1435+
modules: modules_prepare
14301436

14311437
# Target to prepare building external modules
14321438
modules_prepare: prepare
@@ -1739,8 +1745,12 @@ ifdef CONFIG_MODULES
17391745
$(MODORDER): $(build-dir)
17401746
@:
17411747

1742-
modules: modules_check
1743-
$(Q)$(MAKE) -f $(srctree)/scripts/Makefile.modpost
1748+
# KBUILD_MODPOST_NOFINAL can be set to skip the final link of modules.
1749+
# This is solely useful to speed up test compiles.
1750+
modules: modpost
1751+
ifneq ($(KBUILD_MODPOST_NOFINAL),1)
1752+
$(Q)$(MAKE) -f $(srctree)/scripts/Makefile.modfinal
1753+
endif
17441754

17451755
PHONY += modules_check
17461756
modules_check: $(MODORDER)
@@ -1771,6 +1781,11 @@ KBUILD_MODULES :=
17711781

17721782
endif # CONFIG_MODULES
17731783

1784+
PHONY += modpost
1785+
modpost: $(if $(single-build),, $(if $(KBUILD_BUILTIN), vmlinux.o)) \
1786+
$(if $(KBUILD_MODULES), modules_check)
1787+
$(Q)$(MAKE) -f $(srctree)/scripts/Makefile.modpost
1788+
17741789
# Single targets
17751790
# ---------------------------------------------------------------------------
17761791
# To build individual files in subdirectories, you can do like this:
@@ -1790,16 +1805,19 @@ single-ko := $(sort $(filter %.ko, $(MAKECMDGOALS)))
17901805
single-no-ko := $(filter-out $(single-ko), $(MAKECMDGOALS)) \
17911806
$(foreach x, o mod, $(patsubst %.ko, %.$x, $(single-ko)))
17921807

1793-
$(single-ko): single_modpost
1808+
$(single-ko): single_modules
17941809
@:
17951810
$(single-no-ko): $(build-dir)
17961811
@:
17971812

17981813
# Remove MODORDER when done because it is not the real one.
1799-
PHONY += single_modpost
1800-
single_modpost: $(single-no-ko) modules_prepare
1814+
PHONY += single_modules
1815+
single_modules: $(single-no-ko) modules_prepare
18011816
$(Q){ $(foreach m, $(single-ko), echo $(extmod_prefix)$m;) } > $(MODORDER)
18021817
$(Q)$(MAKE) -f $(srctree)/scripts/Makefile.modpost
1818+
ifneq ($(KBUILD_MODPOST_NOFINAL),1)
1819+
$(Q)$(MAKE) -f $(srctree)/scripts/Makefile.modfinal
1820+
endif
18031821
$(Q)rm -f $(MODORDER)
18041822

18051823
single-goals := $(addprefix $(build-dir)/, $(single-no-ko))

scripts/Makefile.modfinal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ if_changed_except = $(if $(call newer_prereqs_except,$(2))$(cmd-check), \
5555
printf '%s\n' 'cmd_$@ := $(make-cmd)' > $(dot-target).cmd, @:)
5656

5757
# Re-generate module BTFs if either module's .ko or vmlinux changed
58-
$(modules): %.ko: %.o %.mod.o scripts/module.lds $(if $(KBUILD_BUILTIN),vmlinux) FORCE
58+
$(modules): %.ko: %.o %.mod.o scripts/module.lds $(and $(CONFIG_DEBUG_INFO_BTF_MODULES),$(KBUILD_BUILTIN),vmlinux) FORCE
5959
+$(call if_changed_except,ld_ko_o,vmlinux)
6060
ifdef CONFIG_DEBUG_INFO_BTF_MODULES
6161
+$(if $(newer-prereqs),$(call cmd,btf_ko))

scripts/Makefile.modpost

Lines changed: 28 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@
3232
# Step 4 is solely used to allow module versioning in external modules,
3333
# where the CRC of each module is retrieved from the Module.symvers file.
3434

35-
# KBUILD_MODPOST_NOFINAL can be set to skip the final link of modules.
36-
# This is solely useful to speed up test compiles
37-
3835
PHONY := __modpost
3936
__modpost:
4037

@@ -45,24 +42,23 @@ MODPOST = scripts/mod/modpost \
4542
$(if $(CONFIG_MODVERSIONS),-m) \
4643
$(if $(CONFIG_MODULE_SRCVERSION_ALL),-a) \
4744
$(if $(CONFIG_SECTION_MISMATCH_WARN_ONLY),,-E) \
45+
$(if $(KBUILD_NSDEPS),-d $(MODULES_NSDEPS)) \
46+
$(if $(CONFIG_MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS)$(KBUILD_NSDEPS),-N) \
4847
-o $@
4948

50-
ifdef MODPOST_VMLINUX
51-
52-
quiet_cmd_modpost = MODPOST $@
53-
cmd_modpost = $(MODPOST) $<
54-
55-
vmlinux.symvers: vmlinux.o
56-
$(call cmd,modpost)
49+
# 'make -i -k' ignores compile errors, and builds as many modules as possible.
50+
ifneq ($(findstring i,$(filter-out --%,$(MAKEFLAGS))),)
51+
MODPOST += -n
52+
endif
5753

58-
__modpost: vmlinux.symvers
54+
ifeq ($(KBUILD_EXTMOD),)
5955

6056
# Generate the list of in-tree objects in vmlinux
6157
# ---------------------------------------------------------------------------
6258

6359
# This is used to retrieve symbol versions generated by genksyms.
6460
ifdef CONFIG_MODVERSIONS
65-
vmlinux.symvers: .vmlinux.objs
61+
vmlinux.symvers Module.symvers: .vmlinux.objs
6662
endif
6763

6864
# Ignore libgcc.a
@@ -83,24 +79,12 @@ targets += .vmlinux.objs
8379
.vmlinux.objs: $(KBUILD_VMLINUX_OBJS) $(KBUILD_VMLINUX_LIBS) FORCE
8480
$(call if_changed,vmlinux_objs)
8581

86-
else
87-
88-
ifeq ($(KBUILD_EXTMOD),)
89-
90-
input-symdump := vmlinux.symvers
91-
output-symdump := modules-only.symvers
92-
93-
quiet_cmd_cat = GEN $@
94-
cmd_cat = cat $(real-prereqs) > $@
95-
96-
ifneq ($(wildcard vmlinux.symvers),)
97-
98-
__modpost: Module.symvers
99-
Module.symvers: vmlinux.symvers modules-only.symvers FORCE
100-
$(call if_changed,cat)
101-
102-
targets += Module.symvers
82+
vmlinux.o-if-present := $(wildcard vmlinux.o)
83+
output-symdump := vmlinux.symvers
10384

85+
ifdef KBUILD_MODULES
86+
output-symdump := $(if $(vmlinux.o-if-present), Module.symvers, modules-only.symvers)
87+
missing-input := $(filter-out $(vmlinux.o-if-present),vmlinux.o)
10488
endif
10589

10690
else
@@ -112,56 +96,35 @@ src := $(obj)
11296
# Include the module's Makefile to find KBUILD_EXTRA_SYMBOLS
11397
include $(or $(wildcard $(src)/Kbuild), $(src)/Makefile)
11498

115-
# modpost option for external modules
116-
MODPOST += -e
117-
118-
input-symdump := Module.symvers $(KBUILD_EXTRA_SYMBOLS)
99+
module.symvers-if-present := $(wildcard Module.symvers)
119100
output-symdump := $(KBUILD_EXTMOD)/Module.symvers
101+
missing-input := $(filter-out $(module.symvers-if-present), Module.symvers)
120102

121-
endif
122-
123-
existing-input-symdump := $(wildcard $(input-symdump))
124-
125-
# modpost options for modules (both in-kernel and external)
126-
MODPOST += \
127-
$(addprefix -i ,$(existing-input-symdump)) \
128-
$(if $(KBUILD_NSDEPS),-d $(MODULES_NSDEPS)) \
129-
$(if $(CONFIG_MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS)$(KBUILD_NSDEPS),-N)
130-
131-
# 'make -i -k' ignores compile errors, and builds as many modules as possible.
132-
ifneq ($(findstring i,$(filter-out --%,$(MAKEFLAGS))),)
133-
MODPOST += -n
134-
endif
103+
MODPOST += -e $(addprefix -i ,$(module.symvers-if-present) $(KBUILD_EXTRA_SYMBOLS))
135104

136-
# Clear VPATH to not search for *.symvers in $(srctree). Check only $(objtree).
137-
VPATH :=
138-
$(input-symdump):
139-
@echo >&2 'WARNING: Symbol version dump "$@" is missing.'
140-
@echo >&2 ' Modules may not have dependencies or modversions.'
141-
@echo >&2 ' You may get many unresolved symbol warnings.'
105+
endif # ($(KBUILD_EXTMOD),)
142106

143-
# KBUILD_MODPOST_WARN can be set to avoid error out in case of undefined symbols
144-
ifneq ($(KBUILD_MODPOST_WARN)$(filter-out $(existing-input-symdump), $(input-symdump)),)
107+
ifneq ($(KBUILD_MODPOST_WARN)$(missing-input),)
145108
MODPOST += -w
146109
endif
147110

111+
modorder-if-needed := $(if $(KBUILD_MODULES), $(MODORDER))
112+
148113
# Read out modules.order to pass in modpost.
149114
# Otherwise, allmodconfig would fail with "Argument list too long".
150115
quiet_cmd_modpost = MODPOST $@
151-
cmd_modpost = sed 's/ko$$/o/' $< | $(MODPOST) -T -
152-
153-
$(output-symdump): $(MODORDER) $(input-symdump) FORCE
154-
$(call if_changed,modpost)
116+
cmd_modpost = \
117+
$(if $(missing-input), \
118+
echo >&2 "WARNING: $(missing-input) is missing."; \
119+
echo >&2 " Modules may not have dependencies or modversions."; \
120+
echo >&2 " You may get many unresolved symbol warnings.";) \
121+
sed 's/ko$$/o/' $(or $(modorder-if-needed), /dev/null) | $(MODPOST) $(vmlinux.o-if-present) -T -
155122

156123
targets += $(output-symdump)
124+
$(output-symdump): $(modorder-if-needed) $(vmlinux.o-if-present) $(moudle.symvers-if-present) FORCE
125+
$(call if_changed,modpost)
157126

158127
__modpost: $(output-symdump)
159-
ifneq ($(KBUILD_MODPOST_NOFINAL),1)
160-
$(Q)$(MAKE) -f $(srctree)/scripts/Makefile.modfinal
161-
endif
162-
163-
endif
164-
165128
PHONY += FORCE
166129
FORCE:
167130

scripts/link-vmlinux.sh

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,6 @@ if [ "$1" = "clean" ]; then
214214
exit 0
215215
fi
216216

217-
# modpost vmlinux.o to check for section mismatches
218-
${MAKE} -f "${srctree}/scripts/Makefile.modpost" MODPOST_VMLINUX=1
219-
220217
info MODINFO modules.builtin.modinfo
221218
${OBJCOPY} -j .modinfo -O binary vmlinux.o modules.builtin.modinfo
222219
info GEN modules.builtin

0 commit comments

Comments
 (0)