blob: 10d9a902f8513547ad2e29c5f89b789228328194 [file] [log] [blame]
Masahiro Yamada9b9a3f22019-08-15 01:06:23 +09001# SPDX-License-Identifier: GPL-2.0-only
2# ===========================================================================
3# Module final link
4# ===========================================================================
5
6PHONY := __modfinal
7__modfinal:
8
Andrii Nakryiko5f9ae912020-11-09 17:19:30 -08009include include/config/auto.conf
Masahiro Yamada9b9a3f22019-08-15 01:06:23 +090010include $(srctree)/scripts/Kbuild.include
11
Sami Tolvanen850ded42021-08-16 11:05:19 -070012# for c_flags and mod-prelink-ext
Masahiro Yamada9b9a3f22019-08-15 01:06:23 +090013include $(srctree)/scripts/Makefile.lib
14
Yifan Hongb7a492a2021-11-08 23:08:14 +000015mixed-build-prefix = $(if $(KBUILD_MIXED_TREE),$(KBUILD_MIXED_TREE)/)
16
Masahiro Yamada9b9a3f22019-08-15 01:06:23 +090017# find all modules listed in modules.order
18modules := $(sort $(shell cat $(MODORDER)))
19
20__modfinal: $(modules)
21 @:
22
Masahiro Yamadaeb27ea52019-08-19 17:58:43 +090023# modname and part-of-module are set to make c_flags define proper module flags
Masahiro Yamada9b9a3f22019-08-15 01:06:23 +090024modname = $(notdir $(@:.mod.o=))
Masahiro Yamadaeb27ea52019-08-19 17:58:43 +090025part-of-module = y
Masahiro Yamada9b9a3f22019-08-15 01:06:23 +090026
27quiet_cmd_cc_o_c = CC [M] $@
Sami Tolvanencf68fff2021-04-08 11:28:26 -070028 cmd_cc_o_c = $(CC) $(filter-out $(CC_FLAGS_CFI), $(c_flags)) -c -o $@ $<
Masahiro Yamada9b9a3f22019-08-15 01:06:23 +090029
30%.mod.o: %.mod.c FORCE
31 $(call if_changed_dep,cc_o_c)
32
33ARCH_POSTLINK := $(wildcard $(srctree)/arch/$(SRCARCH)/Makefile.postlink)
34
35quiet_cmd_ld_ko_o = LD [M] $@
Sami Tolvanenb1a1a1a2020-04-13 16:10:13 -070036 cmd_ld_ko_o += \
Masahiro Yamada9b9a3f22019-08-15 01:06:23 +090037 $(LD) -r $(KBUILD_LDFLAGS) \
38 $(KBUILD_LDFLAGS_MODULE) $(LDFLAGS_MODULE) \
Masahiro Yamada596b0472020-09-08 13:27:08 +090039 -T scripts/module.lds -o $@ $(filter %.o, $^); \
Masahiro Yamada9b9a3f22019-08-15 01:06:23 +090040 $(if $(ARCH_POSTLINK), $(MAKE) -f $(ARCH_POSTLINK) $@, true)
41
Andrii Nakryiko5f9ae912020-11-09 17:19:30 -080042quiet_cmd_btf_ko = BTF [M] $@
Andrii Nakryikoe732b532020-11-20 23:08:28 -080043 cmd_btf_ko = \
Yifan Hongb7a492a2021-11-08 23:08:14 +000044 if [ -f $(mixed-build-prefix)vmlinux ]; then \
Greg Kroah-Hartman4204bff2022-09-24 10:03:40 +020045 LLVM_OBJCOPY="$(OBJCOPY)" $(PAHOLE) -J $(PAHOLE_FLAGS) --btf_base $(mixed-build-prefix)vmlinux $@; \
Andrii Nakryikoe732b532020-11-20 23:08:28 -080046 else \
Yifan Hongb7a492a2021-11-08 23:08:14 +000047 printf "Skipping BTF generation for %s due to unavailability of $(mixed-build-prefix)vmlinux\n" $@ 1>&2; \
Andrii Nakryikoe732b532020-11-20 23:08:28 -080048 fi;
Andrii Nakryiko5f9ae912020-11-09 17:19:30 -080049
50# Same as newer-prereqs, but allows to exclude specified extra dependencies
51newer_prereqs_except = $(filter-out $(PHONY) $(1),$?)
52
53# Same as if_changed, but allows to exclude specified extra dependencies
54if_changed_except = $(if $(call newer_prereqs_except,$(2))$(cmd-check), \
55 $(cmd); \
56 printf '%s\n' 'cmd_$@ := $(make-cmd)' > $(dot-target).cmd, @:)
57
Sami Tolvanendc5723b2020-12-11 10:46:19 -080058
Andrii Nakryiko5f9ae912020-11-09 17:19:30 -080059# Re-generate module BTFs if either module's .ko or vmlinux changed
Yifan Hongb7a492a2021-11-08 23:08:14 +000060$(modules): %.ko: %$(mod-prelink-ext).o %.mod.o scripts/module.lds $(if $(KBUILD_BUILTIN),$(mixed-build-prefix)vmlinux) FORCE
61 +$(call if_changed_except,ld_ko_o,$(mixed-build-prefix)vmlinux)
Andrii Nakryiko5f9ae912020-11-09 17:19:30 -080062ifdef CONFIG_DEBUG_INFO_BTF_MODULES
63 +$(if $(newer-prereqs),$(call cmd,btf_ko))
64endif
Masahiro Yamada9b9a3f22019-08-15 01:06:23 +090065
66targets += $(modules) $(modules:.ko=.mod.o)
67
68# Add FORCE to the prequisites of a target to force it to be always rebuilt.
69# ---------------------------------------------------------------------------
70
71PHONY += FORCE
72FORCE:
73
74# Read all saved command lines and dependencies for the $(targets) we
75# may be building above, using $(if_changed{,_dep}). As an
76# optimization, we don't need to read them if the target does not
77# exist, we will rebuild anyway in that case.
78
79existing-targets := $(wildcard $(sort $(targets)))
80
81-include $(foreach f,$(existing-targets),$(dir $(f)).$(notdir $(f)).cmd)
82
83.PHONY: $(PHONY)