1*4882a593Smuzhiyun# =========================================================================== 2*4882a593Smuzhiyun# Module versions 3*4882a593Smuzhiyun# =========================================================================== 4*4882a593Smuzhiyun# 5*4882a593Smuzhiyun# Stage one of module building created the following: 6*4882a593Smuzhiyun# a) The individual .o files used for the module 7*4882a593Smuzhiyun# b) A <module>.o file which is the .o files above linked together 8*4882a593Smuzhiyun# c) A <module>.mod file in $(MODVERDIR)/, listing the name of the 9*4882a593Smuzhiyun# the preliminary <module>.o file, plus all .o files 10*4882a593Smuzhiyun 11*4882a593Smuzhiyun# Stage 2 is handled by this file and does the following 12*4882a593Smuzhiyun# 1) Find all modules from the files listed in $(MODVERDIR)/ 13*4882a593Smuzhiyun# 2) modpost is then used to 14*4882a593Smuzhiyun# 3) create one <module>.mod.c file pr. module 15*4882a593Smuzhiyun# 4) create one Module.symvers file with CRC for all exported symbols 16*4882a593Smuzhiyun# 5) compile all <module>.mod.c files 17*4882a593Smuzhiyun# 6) final link of the module to a <module.ko> file 18*4882a593Smuzhiyun 19*4882a593Smuzhiyun# Step 3 is used to place certain information in the module's ELF 20*4882a593Smuzhiyun# section, including information such as: 21*4882a593Smuzhiyun# Version magic (see include/linux/vermagic.h for full details) 22*4882a593Smuzhiyun# - Kernel release 23*4882a593Smuzhiyun# - SMP is CONFIG_SMP 24*4882a593Smuzhiyun# - PREEMPT is CONFIG_PREEMPT 25*4882a593Smuzhiyun# - GCC Version 26*4882a593Smuzhiyun# Module info 27*4882a593Smuzhiyun# - Module version (MODULE_VERSION) 28*4882a593Smuzhiyun# - Module alias'es (MODULE_ALIAS) 29*4882a593Smuzhiyun# - Module license (MODULE_LICENSE) 30*4882a593Smuzhiyun# - See include/linux/module.h for more details 31*4882a593Smuzhiyun 32*4882a593Smuzhiyun# Step 4 is solely used to allow module versioning in external modules, 33*4882a593Smuzhiyun# where the CRC of each module is retrieved from the Module.symvers file. 34*4882a593Smuzhiyun 35*4882a593Smuzhiyun# KBUILD_MODPOST_WARN can be set to avoid error out in case of undefined 36*4882a593Smuzhiyun# symbols in the final module linking stage 37*4882a593Smuzhiyun# KBUILD_MODPOST_NOFINAL can be set to skip the final link of modules. 38*4882a593Smuzhiyun# This is solely useful to speed up test compiles 39*4882a593SmuzhiyunPHONY := _modpost 40*4882a593Smuzhiyun_modpost: __modpost 41*4882a593Smuzhiyun 42*4882a593Smuzhiyuninclude include/config/auto.conf 43*4882a593Smuzhiyuninclude scripts/Kbuild.include 44*4882a593Smuzhiyun 45*4882a593Smuzhiyun# When building external modules load the Kbuild file to retrieve EXTRA_SYMBOLS info 46*4882a593Smuzhiyunifneq ($(KBUILD_EXTMOD),) 47*4882a593Smuzhiyun 48*4882a593Smuzhiyun# set src + obj - they may be used when building the .mod.c file 49*4882a593Smuzhiyunobj := $(KBUILD_EXTMOD) 50*4882a593Smuzhiyunsrc := $(obj) 51*4882a593Smuzhiyun 52*4882a593Smuzhiyun# Include the module's Makefile to find KBUILD_EXTRA_SYMBOLS 53*4882a593Smuzhiyuninclude $(if $(wildcard $(KBUILD_EXTMOD)/Kbuild), \ 54*4882a593Smuzhiyun $(KBUILD_EXTMOD)/Kbuild, $(KBUILD_EXTMOD)/Makefile) 55*4882a593Smuzhiyunendif 56*4882a593Smuzhiyun 57*4882a593Smuzhiyuninclude scripts/Makefile.lib 58*4882a593Smuzhiyun 59*4882a593Smuzhiyunkernelsymfile := $(objtree)/Module.symvers 60*4882a593Smuzhiyunmodulesymfile := $(firstword $(KBUILD_EXTMOD))/Module.symvers 61*4882a593Smuzhiyun 62*4882a593Smuzhiyun# Step 1), find all modules listed in $(MODVERDIR)/ 63*4882a593SmuzhiyunMODLISTCMD := find $(MODVERDIR) -name '*.mod' | xargs -r grep -h '\.ko$$' | sort -u 64*4882a593Smuzhiyun__modules := $(shell $(MODLISTCMD)) 65*4882a593Smuzhiyunmodules := $(patsubst %.o,%.ko, $(wildcard $(__modules:.ko=.o))) 66*4882a593Smuzhiyun 67*4882a593Smuzhiyun# Stop after building .o files if NOFINAL is set. Makes compile tests quicker 68*4882a593Smuzhiyun_modpost: $(if $(KBUILD_MODPOST_NOFINAL), $(modules:.ko:.o),$(modules)) 69*4882a593Smuzhiyun 70*4882a593Smuzhiyun# Step 2), invoke modpost 71*4882a593Smuzhiyun# Includes step 3,4 72*4882a593Smuzhiyunmodpost = scripts/mod/modpost \ 73*4882a593Smuzhiyun $(if $(CONFIG_MODVERSIONS),-m) \ 74*4882a593Smuzhiyun $(if $(CONFIG_MODULE_SRCVERSION_ALL),-a,) \ 75*4882a593Smuzhiyun $(if $(KBUILD_EXTMOD),-i,-o) $(kernelsymfile) \ 76*4882a593Smuzhiyun $(if $(KBUILD_EXTMOD),-I $(modulesymfile)) \ 77*4882a593Smuzhiyun $(if $(KBUILD_EXTMOD),$(addprefix -e ,$(KBUILD_EXTRA_SYMBOLS))) \ 78*4882a593Smuzhiyun $(if $(KBUILD_EXTMOD),-o $(modulesymfile)) \ 79*4882a593Smuzhiyun $(if $(CONFIG_DEBUG_SECTION_MISMATCH),,-S) \ 80*4882a593Smuzhiyun $(if $(CONFIG_SECTION_MISMATCH_WARN_ONLY),,-E) \ 81*4882a593Smuzhiyun $(if $(KBUILD_EXTMOD)$(KBUILD_MODPOST_WARN),-w) 82*4882a593Smuzhiyun 83*4882a593SmuzhiyunMODPOST_OPT=$(subst -i,-n,$(filter -i,$(MAKEFLAGS))) 84*4882a593Smuzhiyun 85*4882a593Smuzhiyun# We can go over command line length here, so be careful. 86*4882a593Smuzhiyunquiet_cmd_modpost = MODPOST $(words $(filter-out vmlinux FORCE, $^)) modules 87*4882a593Smuzhiyun cmd_modpost = $(MODLISTCMD) | sed 's/\.ko$$/.o/' | $(modpost) $(MODPOST_OPT) -s -T - 88*4882a593Smuzhiyun 89*4882a593SmuzhiyunPHONY += __modpost 90*4882a593Smuzhiyun__modpost: $(modules:.ko=.o) FORCE 91*4882a593Smuzhiyun $(call cmd,modpost) $(wildcard vmlinux) 92*4882a593Smuzhiyun 93*4882a593Smuzhiyunquiet_cmd_kernel-mod = MODPOST $@ 94*4882a593Smuzhiyun cmd_kernel-mod = $(modpost) $@ 95*4882a593Smuzhiyun 96*4882a593Smuzhiyunvmlinux.o: FORCE 97*4882a593Smuzhiyun $(call cmd,kernel-mod) 98*4882a593Smuzhiyun 99*4882a593Smuzhiyun# Declare generated files as targets for modpost 100*4882a593Smuzhiyun$(symverfile): __modpost ; 101*4882a593Smuzhiyun$(modules:.ko=.mod.c): __modpost ; 102*4882a593Smuzhiyun 103*4882a593Smuzhiyun 104*4882a593Smuzhiyun# Step 5), compile all *.mod.c files 105*4882a593Smuzhiyun 106*4882a593Smuzhiyun# modname is set to make c_flags define KBUILD_MODNAME 107*4882a593Smuzhiyunmodname = $(notdir $(@:.mod.o=)) 108*4882a593Smuzhiyun 109*4882a593Smuzhiyunquiet_cmd_cc_o_c = CC $@ 110*4882a593Smuzhiyun cmd_cc_o_c = $(CC) $(c_flags) $(KBUILD_CFLAGS_MODULE) $(CFLAGS_MODULE) \ 111*4882a593Smuzhiyun -c -o $@ $< 112*4882a593Smuzhiyun 113*4882a593Smuzhiyun$(modules:.ko=.mod.o): %.mod.o: %.mod.c FORCE 114*4882a593Smuzhiyun $(call if_changed_dep,cc_o_c) 115*4882a593Smuzhiyun 116*4882a593Smuzhiyuntargets += $(modules:.ko=.mod.o) 117*4882a593Smuzhiyun 118*4882a593Smuzhiyun# Step 6), final link of the modules 119*4882a593Smuzhiyunquiet_cmd_ld_ko_o = LD [M] $@ 120*4882a593Smuzhiyun cmd_ld_ko_o = $(LD) -r $(LDFLAGS) \ 121*4882a593Smuzhiyun $(KBUILD_LDFLAGS_MODULE) $(LDFLAGS_MODULE) \ 122*4882a593Smuzhiyun -o $@ $(filter-out FORCE,$^) 123*4882a593Smuzhiyun 124*4882a593Smuzhiyun$(modules): %.ko :%.o %.mod.o FORCE 125*4882a593Smuzhiyun $(call if_changed,ld_ko_o) 126*4882a593Smuzhiyun 127*4882a593Smuzhiyuntargets += $(modules) 128*4882a593Smuzhiyun 129*4882a593Smuzhiyun 130*4882a593Smuzhiyun# Add FORCE to the prequisites of a target to force it to be always rebuilt. 131*4882a593Smuzhiyun# --------------------------------------------------------------------------- 132*4882a593Smuzhiyun 133*4882a593SmuzhiyunPHONY += FORCE 134*4882a593Smuzhiyun 135*4882a593SmuzhiyunFORCE: 136*4882a593Smuzhiyun 137*4882a593Smuzhiyun# Read all saved command lines and dependencies for the $(targets) we 138*4882a593Smuzhiyun# may be building above, using $(if_changed{,_dep}). As an 139*4882a593Smuzhiyun# optimization, we don't need to read them if the target does not 140*4882a593Smuzhiyun# exist, we will rebuild anyway in that case. 141*4882a593Smuzhiyun 142*4882a593Smuzhiyuntargets := $(wildcard $(sort $(targets))) 143*4882a593Smuzhiyuncmd_files := $(wildcard $(foreach f,$(targets),$(dir $(f)).$(notdir $(f)).cmd)) 144*4882a593Smuzhiyun 145*4882a593Smuzhiyunifneq ($(cmd_files),) 146*4882a593Smuzhiyun include $(cmd_files) 147*4882a593Smuzhiyunendif 148*4882a593Smuzhiyun 149*4882a593Smuzhiyun 150*4882a593Smuzhiyun# Declare the contents of the .PHONY variable as phony. We keep that 151*4882a593Smuzhiyun# information in a variable se we can use it in if_changed and friends. 152*4882a593Smuzhiyun 153*4882a593Smuzhiyun.PHONY: $(PHONY) 154