Back to home page

OSCL-LXR

 
 

    


0001 # SPDX-License-Identifier: GPL-2.0
0002 # ==========================================================================
0003 # Building
0004 # ==========================================================================
0005 
0006 src := $(obj)
0007 
0008 PHONY := __build
0009 __build:
0010 
0011 # Init all relevant variables used in kbuild files so
0012 # 1) they have correct type
0013 # 2) they do not inherit any value from the environment
0014 obj-y :=
0015 obj-m :=
0016 lib-y :=
0017 lib-m :=
0018 always-y :=
0019 always-m :=
0020 targets :=
0021 subdir-y :=
0022 subdir-m :=
0023 EXTRA_AFLAGS   :=
0024 EXTRA_CFLAGS   :=
0025 EXTRA_CPPFLAGS :=
0026 EXTRA_LDFLAGS  :=
0027 asflags-y  :=
0028 ccflags-y  :=
0029 cppflags-y :=
0030 ldflags-y  :=
0031 
0032 subdir-asflags-y :=
0033 subdir-ccflags-y :=
0034 
0035 # Read auto.conf if it exists, otherwise ignore
0036 -include include/config/auto.conf
0037 
0038 include $(srctree)/scripts/Kbuild.include
0039 include $(srctree)/scripts/Makefile.compiler
0040 
0041 # The filename Kbuild has precedence over Makefile
0042 kbuild-dir := $(if $(filter /%,$(src)),$(src),$(srctree)/$(src))
0043 include $(or $(wildcard $(kbuild-dir)/Kbuild),$(kbuild-dir)/Makefile) 
0044 
0045 include $(srctree)/scripts/Makefile.lib
0046 
0047 # Do not include hostprogs rules unless needed.
0048 # $(sort ...) is used here to remove duplicated words and excessive spaces.
0049 hostprogs := $(sort $(hostprogs))
0050 ifneq ($(hostprogs),)
0051 include $(srctree)/scripts/Makefile.host
0052 endif
0053 
0054 # Do not include userprogs rules unless needed.
0055 # $(sort ...) is used here to remove duplicated words and excessive spaces.
0056 userprogs := $(sort $(userprogs))
0057 ifneq ($(userprogs),)
0058 include $(srctree)/scripts/Makefile.userprogs
0059 endif
0060 
0061 ifndef obj
0062 $(warning kbuild: Makefile.build is included improperly)
0063 endif
0064 
0065 ifeq ($(need-modorder),)
0066 ifneq ($(obj-m),)
0067 $(warning $(patsubst %.o,'%.ko',$(obj-m)) will not be built even though obj-m is specified.)
0068 $(warning You cannot use subdir-y/m to visit a module Makefile. Use obj-y/m instead.)
0069 endif
0070 endif
0071 
0072 # ===========================================================================
0073 
0074 # subdir-builtin and subdir-modorder may contain duplications. Use $(sort ...)
0075 subdir-builtin := $(sort $(filter %/built-in.a, $(real-obj-y)))
0076 subdir-modorder := $(sort $(filter %/modules.order, $(obj-m)))
0077 
0078 targets-for-builtin := $(extra-y)
0079 
0080 ifneq ($(strip $(lib-y) $(lib-m) $(lib-)),)
0081 targets-for-builtin += $(obj)/lib.a
0082 endif
0083 
0084 ifdef need-builtin
0085 targets-for-builtin += $(obj)/built-in.a
0086 endif
0087 
0088 targets-for-modules := $(foreach x, o mod $(if $(CONFIG_TRIM_UNUSED_KSYMS), usyms), \
0089                                 $(patsubst %.o, %.$x, $(filter %.o, $(obj-m))))
0090 
0091 ifdef need-modorder
0092 targets-for-modules += $(obj)/modules.order
0093 endif
0094 
0095 targets += $(targets-for-builtin) $(targets-for-modules)
0096 
0097 # Linus' kernel sanity checking tool
0098 ifeq ($(KBUILD_CHECKSRC),1)
0099   quiet_cmd_checksrc       = CHECK   $<
0100         cmd_checksrc       = $(CHECK) $(CHECKFLAGS) $(c_flags) $<
0101 else ifeq ($(KBUILD_CHECKSRC),2)
0102   quiet_cmd_force_checksrc = CHECK   $<
0103         cmd_force_checksrc = $(CHECK) $(CHECKFLAGS) $(c_flags) $<
0104 endif
0105 
0106 ifneq ($(KBUILD_EXTRA_WARN),)
0107   cmd_checkdoc = $(srctree)/scripts/kernel-doc -none $<
0108 endif
0109 
0110 # Compile C sources (.c)
0111 # ---------------------------------------------------------------------------
0112 
0113 quiet_cmd_cc_s_c = CC $(quiet_modtag)  $@
0114       cmd_cc_s_c = $(CC) $(filter-out $(DEBUG_CFLAGS) $(CC_FLAGS_LTO), $(c_flags)) -fverbose-asm -S -o $@ $<
0115 
0116 $(obj)/%.s: $(src)/%.c FORCE
0117         $(call if_changed_dep,cc_s_c)
0118 
0119 quiet_cmd_cpp_i_c = CPP $(quiet_modtag) $@
0120 cmd_cpp_i_c       = $(CPP) $(c_flags) -o $@ $<
0121 
0122 $(obj)/%.i: $(src)/%.c FORCE
0123         $(call if_changed_dep,cpp_i_c)
0124 
0125 genksyms = scripts/genksyms/genksyms            \
0126         $(if $(1), -T $(2))                     \
0127         $(if $(KBUILD_PRESERVE), -p)            \
0128         -r $(or $(wildcard $(2:.symtypes=.symref)), /dev/null)
0129 
0130 # These mirror gensymtypes_S and co below, keep them in synch.
0131 cmd_gensymtypes_c = $(CPP) -D__GENKSYMS__ $(c_flags) $< | $(genksyms)
0132 
0133 quiet_cmd_cc_symtypes_c = SYM $(quiet_modtag) $@
0134       cmd_cc_symtypes_c = $(call cmd_gensymtypes_c,true,$@) >/dev/null
0135 
0136 $(obj)/%.symtypes : $(src)/%.c FORCE
0137         $(call cmd,cc_symtypes_c)
0138 
0139 # LLVM assembly
0140 # Generate .ll files from .c
0141 quiet_cmd_cc_ll_c = CC $(quiet_modtag)  $@
0142       cmd_cc_ll_c = $(CC) $(c_flags) -emit-llvm -S -o $@ $<
0143 
0144 $(obj)/%.ll: $(src)/%.c FORCE
0145         $(call if_changed_dep,cc_ll_c)
0146 
0147 # C (.c) files
0148 # The C file is compiled and updated dependency information is generated.
0149 # (See cmd_cc_o_c + relevant part of rule_cc_o_c)
0150 
0151 is-single-obj-m = $(and $(part-of-module),$(filter $@, $(obj-m)),y)
0152 
0153 # When a module consists of a single object, there is no reason to keep LLVM IR.
0154 # Make $(LD) covert LLVM IR to ELF here.
0155 ifdef CONFIG_LTO_CLANG
0156 cmd_ld_single_m = $(if $(is-single-obj-m), ; $(LD) $(ld_flags) -r -o $(tmp-target) $@; mv $(tmp-target) $@)
0157 endif
0158 
0159 quiet_cmd_cc_o_c = CC $(quiet_modtag)  $@
0160       cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $< \
0161                 $(cmd_ld_single_m) \
0162                 $(cmd_objtool)
0163 
0164 ifdef CONFIG_MODVERSIONS
0165 # When module versioning is enabled the following steps are executed:
0166 # o compile a <file>.o from <file>.c
0167 # o if <file>.o doesn't contain a __ksymtab version, i.e. does
0168 #   not export symbols, it's done.
0169 # o otherwise, we calculate symbol versions using the good old
0170 #   genksyms on the preprocessed source and dump them into the .cmd file.
0171 # o modpost will extract versions from that file and create *.c files that will
0172 #   be compiled and linked to the kernel and/or modules.
0173 
0174 gen_symversions =                                                               \
0175         if $(NM) $@ 2>/dev/null | grep -q __ksymtab; then                       \
0176                 $(call cmd_gensymtypes_$(1),$(KBUILD_SYMTYPES),$(@:.o=.symtypes)) \
0177                         >> $(dot-target).cmd;                                   \
0178         fi
0179 
0180 cmd_gen_symversions_c = $(call gen_symversions,c)
0181 
0182 endif
0183 
0184 ifdef CONFIG_FTRACE_MCOUNT_USE_RECORDMCOUNT
0185 # compiler will not generate __mcount_loc use recordmcount or recordmcount.pl
0186 ifdef BUILD_C_RECORDMCOUNT
0187 ifeq ("$(origin RECORDMCOUNT_WARN)", "command line")
0188   RECORDMCOUNT_FLAGS = -w
0189 endif
0190 # Due to recursion, we must skip empty.o.
0191 # The empty.o file is created in the make process in order to determine
0192 # the target endianness and word size. It is made before all other C
0193 # files, including recordmcount.
0194 sub_cmd_record_mcount =                                 \
0195         if [ $(@) != "scripts/mod/empty.o" ]; then      \
0196                 $(objtree)/scripts/recordmcount $(RECORDMCOUNT_FLAGS) "$(@)";   \
0197         fi;
0198 recordmcount_source := $(srctree)/scripts/recordmcount.c \
0199                     $(srctree)/scripts/recordmcount.h
0200 else
0201 sub_cmd_record_mcount = perl $(srctree)/scripts/recordmcount.pl "$(ARCH)" \
0202         "$(if $(CONFIG_CPU_BIG_ENDIAN),big,little)" \
0203         "$(if $(CONFIG_64BIT),64,32)" \
0204         "$(OBJDUMP)" "$(OBJCOPY)" "$(CC) $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS)" \
0205         "$(LD) $(KBUILD_LDFLAGS)" "$(NM)" "$(RM)" "$(MV)" \
0206         "$(if $(part-of-module),1,0)" "$(@)";
0207 recordmcount_source := $(srctree)/scripts/recordmcount.pl
0208 endif # BUILD_C_RECORDMCOUNT
0209 cmd_record_mcount = $(if $(findstring $(strip $(CC_FLAGS_FTRACE)),$(_c_flags)), \
0210         $(sub_cmd_record_mcount))
0211 endif # CONFIG_FTRACE_MCOUNT_USE_RECORDMCOUNT
0212 
0213 # 'OBJECT_FILES_NON_STANDARD := y': skip objtool checking for a directory
0214 # 'OBJECT_FILES_NON_STANDARD_foo.o := 'y': skip objtool checking for a file
0215 # 'OBJECT_FILES_NON_STANDARD_foo.o := 'n': override directory skip for a file
0216 
0217 is-standard-object = $(if $(filter-out y%, $(OBJECT_FILES_NON_STANDARD_$(basetarget).o)$(OBJECT_FILES_NON_STANDARD)n),y)
0218 
0219 $(obj)/%.o: objtool-enabled = $(if $(is-standard-object),$(if $(delay-objtool),$(is-single-obj-m),y))
0220 
0221 ifdef CONFIG_TRIM_UNUSED_KSYMS
0222 cmd_gen_ksymdeps = \
0223         $(CONFIG_SHELL) $(srctree)/scripts/gen_ksymdeps.sh $@ >> $(dot-target).cmd
0224 endif
0225 
0226 cmd_check_local_export = $(srctree)/scripts/check-local-export $@
0227 
0228 define rule_cc_o_c
0229         $(call cmd_and_fixdep,cc_o_c)
0230         $(call cmd,gen_ksymdeps)
0231         $(call cmd,check_local_export)
0232         $(call cmd,checksrc)
0233         $(call cmd,checkdoc)
0234         $(call cmd,gen_objtooldep)
0235         $(call cmd,gen_symversions_c)
0236         $(call cmd,record_mcount)
0237 endef
0238 
0239 define rule_as_o_S
0240         $(call cmd_and_fixdep,as_o_S)
0241         $(call cmd,gen_ksymdeps)
0242         $(call cmd,check_local_export)
0243         $(call cmd,gen_objtooldep)
0244         $(call cmd,gen_symversions_S)
0245 endef
0246 
0247 # Built-in and composite module parts
0248 $(obj)/%.o: $(src)/%.c $(recordmcount_source) FORCE
0249         $(call if_changed_rule,cc_o_c)
0250         $(call cmd,force_checksrc)
0251 
0252 # To make this rule robust against "Argument list too long" error,
0253 # ensure to add $(obj)/ prefix by a shell command.
0254 cmd_mod = printf '%s\n' $(call real-search, $*.o, .o, -objs -y -m) | \
0255         $(AWK) '!x[$$0]++ { print("$(obj)/"$$0) }' > $@
0256 
0257 $(obj)/%.mod: FORCE
0258         $(call if_changed,mod)
0259 
0260 # List module undefined symbols
0261 cmd_undefined_syms = $(NM) $< | sed -n 's/^  *U //p' > $@
0262 
0263 $(obj)/%.usyms: $(obj)/%.o FORCE
0264         $(call if_changed,undefined_syms)
0265 
0266 quiet_cmd_cc_lst_c = MKLST   $@
0267       cmd_cc_lst_c = $(CC) $(c_flags) -g -c -o $*.o $< && \
0268                      $(CONFIG_SHELL) $(srctree)/scripts/makelst $*.o \
0269                                      System.map $(OBJDUMP) > $@
0270 
0271 $(obj)/%.lst: $(src)/%.c FORCE
0272         $(call if_changed_dep,cc_lst_c)
0273 
0274 # Compile assembler sources (.S)
0275 # ---------------------------------------------------------------------------
0276 
0277 # .S file exports must have their C prototypes defined in asm/asm-prototypes.h
0278 # or a file that it includes, in order to get versioned symbols. We build a
0279 # dummy C file that includes asm-prototypes and the EXPORT_SYMBOL lines from
0280 # the .S file (with trailing ';'), and run genksyms on that, to extract vers.
0281 #
0282 # This is convoluted. The .S file must first be preprocessed to run guards and
0283 # expand names, then the resulting exports must be constructed into plain
0284 # EXPORT_SYMBOL(symbol); to build our dummy C file, and that gets preprocessed
0285 # to make the genksyms input.
0286 #
0287 # These mirror gensymtypes_c and co above, keep them in synch.
0288 cmd_gensymtypes_S =                                                         \
0289    { echo "\#include <linux/kernel.h>" ;                                    \
0290      echo "\#include <asm/asm-prototypes.h>" ;                              \
0291     $(CPP) $(a_flags) $< |                                                  \
0292      grep "\<___EXPORT_SYMBOL\>" |                                          \
0293      sed 's/.*___EXPORT_SYMBOL[[:space:]]*\([a-zA-Z0-9_]*\)[[:space:]]*,.*/EXPORT_SYMBOL(\1);/' ; } | \
0294     $(CPP) -D__GENKSYMS__ $(c_flags) -xc - | $(genksyms)
0295 
0296 quiet_cmd_cc_symtypes_S = SYM $(quiet_modtag) $@
0297       cmd_cc_symtypes_S = $(call cmd_gensymtypes_S,true,$@) >/dev/null
0298 
0299 $(obj)/%.symtypes : $(src)/%.S FORCE
0300         $(call cmd,cc_symtypes_S)
0301 
0302 
0303 quiet_cmd_cpp_s_S = CPP $(quiet_modtag) $@
0304 cmd_cpp_s_S       = $(CPP) $(a_flags) -o $@ $<
0305 
0306 $(obj)/%.s: $(src)/%.S FORCE
0307         $(call if_changed_dep,cpp_s_S)
0308 
0309 quiet_cmd_as_o_S = AS $(quiet_modtag)  $@
0310       cmd_as_o_S = $(CC) $(a_flags) -c -o $@ $< $(cmd_objtool)
0311 
0312 ifdef CONFIG_ASM_MODVERSIONS
0313 
0314 # versioning matches the C process described above, with difference that
0315 # we parse asm-prototypes.h C header to get function definitions.
0316 
0317 cmd_gen_symversions_S = $(call gen_symversions,S)
0318 
0319 endif
0320 
0321 $(obj)/%.o: $(src)/%.S FORCE
0322         $(call if_changed_rule,as_o_S)
0323 
0324 targets += $(filter-out $(subdir-builtin), $(real-obj-y))
0325 targets += $(filter-out $(subdir-modorder), $(real-obj-m))
0326 targets += $(real-dtb-y) $(lib-y) $(always-y) $(MAKECMDGOALS)
0327 
0328 # Linker scripts preprocessor (.lds.S -> .lds)
0329 # ---------------------------------------------------------------------------
0330 quiet_cmd_cpp_lds_S = LDS     $@
0331       cmd_cpp_lds_S = $(CPP) $(cpp_flags) -P -U$(ARCH) \
0332                              -D__ASSEMBLY__ -DLINKER_SCRIPT -o $@ $<
0333 
0334 $(obj)/%.lds: $(src)/%.lds.S FORCE
0335         $(call if_changed_dep,cpp_lds_S)
0336 
0337 # ASN.1 grammar
0338 # ---------------------------------------------------------------------------
0339 quiet_cmd_asn1_compiler = ASN.1   $(basename $@).[ch]
0340       cmd_asn1_compiler = $(objtree)/scripts/asn1_compiler $< \
0341                                 $(basename $@).c $(basename $@).h
0342 
0343 $(obj)/%.asn1.c $(obj)/%.asn1.h: $(src)/%.asn1 $(objtree)/scripts/asn1_compiler
0344         $(call cmd,asn1_compiler)
0345 
0346 # Build the compiled-in targets
0347 # ---------------------------------------------------------------------------
0348 
0349 # To build objects in subdirs, we need to descend into the directories
0350 $(subdir-builtin): $(obj)/%/built-in.a: $(obj)/% ;
0351 $(subdir-modorder): $(obj)/%/modules.order: $(obj)/% ;
0352 
0353 #
0354 # Rule to compile a set of .o files into one .a file (without symbol table)
0355 #
0356 # To make this rule robust against "Argument list too long" error,
0357 # remove $(obj)/ prefix, and restore it by a shell command.
0358 
0359 quiet_cmd_ar_builtin = AR      $@
0360       cmd_ar_builtin = rm -f $@; \
0361         $(if $(real-prereqs), printf "$(obj)/%s " $(patsubst $(obj)/%,%,$(real-prereqs)) | xargs) \
0362         $(AR) cDPrST $@
0363 
0364 $(obj)/built-in.a: $(real-obj-y) FORCE
0365         $(call if_changed,ar_builtin)
0366 
0367 #
0368 # Rule to create modules.order file
0369 #
0370 # Create commands to either record .ko file or cat modules.order from
0371 # a subdirectory
0372 # Add $(obj-m) as the prerequisite to avoid updating the timestamp of
0373 # modules.order unless contained modules are updated.
0374 
0375 cmd_modules_order = { $(foreach m, $(real-prereqs), \
0376         $(if $(filter %/modules.order, $m), cat $m, echo $(patsubst %.o,%.ko,$m));) :; } \
0377         | $(AWK) '!x[$$0]++' - > $@
0378 
0379 $(obj)/modules.order: $(obj-m) FORCE
0380         $(call if_changed,modules_order)
0381 
0382 #
0383 # Rule to compile a set of .o files into one .a file (with symbol table)
0384 #
0385 
0386 $(obj)/lib.a: $(lib-y) FORCE
0387         $(call if_changed,ar)
0388 
0389 quiet_cmd_ld_multi_m = LD [M]  $@
0390       cmd_ld_multi_m = $(LD) $(ld_flags) -r -o $@ @$(patsubst %.o,%.mod,$@) $(cmd_objtool)
0391 
0392 define rule_ld_multi_m
0393         $(call cmd_and_savecmd,ld_multi_m)
0394         $(call cmd,gen_objtooldep)
0395 endef
0396 
0397 $(multi-obj-m): objtool-enabled := $(delay-objtool)
0398 $(multi-obj-m): part-of-module := y
0399 $(multi-obj-m): %.o: %.mod FORCE
0400         $(call if_changed_rule,ld_multi_m)
0401 $(call multi_depend, $(multi-obj-m), .o, -objs -y -m)
0402 
0403 targets := $(filter-out $(PHONY), $(targets))
0404 
0405 # Add intermediate targets:
0406 # When building objects with specific suffix patterns, add intermediate
0407 # targets that the final targets are derived from.
0408 intermediate_targets = $(foreach sfx, $(2), \
0409                                 $(patsubst %$(strip $(1)),%$(sfx), \
0410                                         $(filter %$(strip $(1)), $(targets))))
0411 # %.asn1.o <- %.asn1.[ch] <- %.asn1
0412 # %.dtb.o <- %.dtb.S <- %.dtb <- %.dts
0413 # %.lex.o <- %.lex.c <- %.l
0414 # %.tab.o <- %.tab.[ch] <- %.y
0415 targets += $(call intermediate_targets, .asn1.o, .asn1.c .asn1.h) \
0416            $(call intermediate_targets, .dtb.o, .dtb.S .dtb) \
0417            $(call intermediate_targets, .lex.o, .lex.c) \
0418            $(call intermediate_targets, .tab.o, .tab.c .tab.h)
0419 
0420 # Build
0421 # ---------------------------------------------------------------------------
0422 
0423 ifdef single-build
0424 
0425 KBUILD_SINGLE_TARGETS := $(filter $(obj)/%, $(KBUILD_SINGLE_TARGETS))
0426 
0427 curdir-single := $(sort $(foreach x, $(KBUILD_SINGLE_TARGETS), \
0428                         $(if $(filter $(x) $(basename $(x)).o, $(targets)), $(x))))
0429 
0430 # Handle single targets without any rule: show "Nothing to be done for ..." or
0431 # "No rule to make target ..." depending on whether the target exists.
0432 unknown-single := $(filter-out $(addsuffix /%, $(subdir-ym)), \
0433                         $(filter-out $(curdir-single), $(KBUILD_SINGLE_TARGETS)))
0434 
0435 single-subdirs := $(foreach d, $(subdir-ym), \
0436                         $(if $(filter $(d)/%, $(KBUILD_SINGLE_TARGETS)), $(d)))
0437 
0438 __build: $(curdir-single) $(single-subdirs)
0439 ifneq ($(unknown-single),)
0440         $(Q)$(MAKE) -f /dev/null $(unknown-single)
0441 endif
0442         @:
0443 
0444 ifeq ($(curdir-single),)
0445 # Nothing to do in this directory. Do not include any .*.cmd file for speed-up
0446 targets :=
0447 else
0448 targets += $(curdir-single)
0449 endif
0450 
0451 else
0452 
0453 __build: $(if $(KBUILD_BUILTIN), $(targets-for-builtin)) \
0454          $(if $(KBUILD_MODULES), $(targets-for-modules)) \
0455          $(subdir-ym) $(always-y)
0456         @:
0457 
0458 endif
0459 
0460 # Descending
0461 # ---------------------------------------------------------------------------
0462 
0463 PHONY += $(subdir-ym)
0464 $(subdir-ym):
0465         $(Q)$(MAKE) $(build)=$@ \
0466         $(if $(filter $@/, $(KBUILD_SINGLE_TARGETS)),single-build=) \
0467         need-builtin=$(if $(filter $@/built-in.a, $(subdir-builtin)),1) \
0468         need-modorder=$(if $(filter $@/modules.order, $(subdir-modorder)),1)
0469 
0470 # Add FORCE to the prequisites of a target to force it to be always rebuilt.
0471 # ---------------------------------------------------------------------------
0472 
0473 PHONY += FORCE
0474 
0475 FORCE:
0476 
0477 # Read all saved command lines and dependencies for the $(targets) we
0478 # may be building above, using $(if_changed{,_dep}). As an
0479 # optimization, we don't need to read them if the target does not
0480 # exist, we will rebuild anyway in that case.
0481 
0482 existing-targets := $(wildcard $(sort $(targets)))
0483 
0484 -include $(foreach f,$(existing-targets),$(dir $(f)).$(notdir $(f)).cmd) 
0485 
0486 # Create directories for object files if they do not exist
0487 obj-dirs := $(sort $(patsubst %/,%, $(dir $(targets))))
0488 # If targets exist, their directories apparently exist. Skip mkdir.
0489 existing-dirs := $(sort $(patsubst %/,%, $(dir $(existing-targets))))
0490 obj-dirs := $(strip $(filter-out $(existing-dirs), $(obj-dirs)))
0491 ifneq ($(obj-dirs),)
0492 $(shell mkdir -p $(obj-dirs))
0493 endif
0494 
0495 .PHONY: $(PHONY)