1# SPDX-License-Identifier: GPL-2.0-only 2# =========================================================================== 3# Module final link 4# =========================================================================== 5 6PHONY := __modfinal 7__modfinal: 8 9include include/config/auto.conf 10include $(srctree)/scripts/Kbuild.include 11 12# for c_flags and mod-prelink-ext 13include $(srctree)/scripts/Makefile.lib 14 15mixed-build-prefix = $(if $(KBUILD_MIXED_TREE),$(KBUILD_MIXED_TREE)/) 16 17# find all modules listed in modules.order 18modules := $(sort $(shell cat $(MODORDER))) 19 20__modfinal: $(modules) 21 @: 22 23# modname and part-of-module are set to make c_flags define proper module flags 24modname = $(notdir $(@:.mod.o=)) 25part-of-module = y 26 27quiet_cmd_cc_o_c = CC [M] $@ 28 cmd_cc_o_c = $(CC) $(filter-out $(CC_FLAGS_CFI) $(CFLAGS_GCOV), $(c_flags)) -c -o $@ $< 29 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] $@ 36 cmd_ld_ko_o += \ 37 $(LD) -r $(KBUILD_LDFLAGS) \ 38 $(KBUILD_LDFLAGS_MODULE) $(LDFLAGS_MODULE) \ 39 -T scripts/module.lds -o $@ $(filter %.o, $^); \ 40 $(if $(ARCH_POSTLINK), $(MAKE) -f $(ARCH_POSTLINK) $@, true) 41 42quiet_cmd_btf_ko = BTF [M] $@ 43 cmd_btf_ko = \ 44 if [ -f $(mixed-build-prefix)vmlinux ]; then \ 45 LLVM_OBJCOPY="$(OBJCOPY)" $(PAHOLE) -J $(PAHOLE_FLAGS) --btf_base $(mixed-build-prefix)vmlinux $@; \ 46 else \ 47 printf "Skipping BTF generation for %s due to unavailability of $(mixed-build-prefix)vmlinux\n" $@ 1>&2; \ 48 fi; 49 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 58 59# Re-generate module BTFs if either module's .ko or vmlinux changed 60$(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) 62ifdef CONFIG_DEBUG_INFO_BTF_MODULES 63 +$(if $(newer-prereqs),$(call cmd,btf_ko)) 64endif 65 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) 84