xref: /OK3568_Linux_fs/device/rockchip/common/linux-kbuild/armhf/linux-kbuild-5.10/scripts/Makefile.build (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun# SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun# ==========================================================================
3*4882a593Smuzhiyun# Building
4*4882a593Smuzhiyun# ==========================================================================
5*4882a593Smuzhiyun
6*4882a593Smuzhiyunsrc := $(obj)
7*4882a593Smuzhiyun
8*4882a593SmuzhiyunPHONY := __build
9*4882a593Smuzhiyun__build:
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun# Init all relevant variables used in kbuild files so
12*4882a593Smuzhiyun# 1) they have correct type
13*4882a593Smuzhiyun# 2) they do not inherit any value from the environment
14*4882a593Smuzhiyunobj-y :=
15*4882a593Smuzhiyunobj-m :=
16*4882a593Smuzhiyunlib-y :=
17*4882a593Smuzhiyunlib-m :=
18*4882a593Smuzhiyunalways :=
19*4882a593Smuzhiyunalways-y :=
20*4882a593Smuzhiyunalways-m :=
21*4882a593Smuzhiyuntargets :=
22*4882a593Smuzhiyunsubdir-y :=
23*4882a593Smuzhiyunsubdir-m :=
24*4882a593SmuzhiyunEXTRA_AFLAGS   :=
25*4882a593SmuzhiyunEXTRA_CFLAGS   :=
26*4882a593SmuzhiyunEXTRA_CPPFLAGS :=
27*4882a593SmuzhiyunEXTRA_LDFLAGS  :=
28*4882a593Smuzhiyunasflags-y  :=
29*4882a593Smuzhiyunccflags-y  :=
30*4882a593Smuzhiyuncppflags-y :=
31*4882a593Smuzhiyunldflags-y  :=
32*4882a593Smuzhiyun
33*4882a593Smuzhiyunsubdir-asflags-y :=
34*4882a593Smuzhiyunsubdir-ccflags-y :=
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun# Read auto.conf if it exists, otherwise ignore
37*4882a593Smuzhiyun-include include/config/auto.conf
38*4882a593Smuzhiyun
39*4882a593Smuzhiyuninclude scripts/Kbuild.include
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun# The filename Kbuild has precedence over Makefile
42*4882a593Smuzhiyunkbuild-dir := $(if $(filter /%,$(src)),$(src),$(srctree)/$(src))
43*4882a593Smuzhiyunkbuild-file := $(if $(wildcard $(kbuild-dir)/Kbuild),$(kbuild-dir)/Kbuild,$(kbuild-dir)/Makefile)
44*4882a593Smuzhiyuninclude $(kbuild-file)
45*4882a593Smuzhiyun
46*4882a593Smuzhiyuninclude scripts/Makefile.lib
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun# Do not include hostprogs rules unless needed.
49*4882a593Smuzhiyun# $(sort ...) is used here to remove duplicated words and excessive spaces.
50*4882a593Smuzhiyunhostprogs := $(sort $(hostprogs))
51*4882a593Smuzhiyunifneq ($(hostprogs),)
52*4882a593Smuzhiyuninclude scripts/Makefile.host
53*4882a593Smuzhiyunendif
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun# Do not include userprogs rules unless needed.
56*4882a593Smuzhiyun# $(sort ...) is used here to remove duplicated words and excessive spaces.
57*4882a593Smuzhiyunuserprogs := $(sort $(userprogs))
58*4882a593Smuzhiyunifneq ($(userprogs),)
59*4882a593Smuzhiyuninclude scripts/Makefile.userprogs
60*4882a593Smuzhiyunendif
61*4882a593Smuzhiyun
62*4882a593Smuzhiyunifndef obj
63*4882a593Smuzhiyun$(warning kbuild: Makefile.build is included improperly)
64*4882a593Smuzhiyunendif
65*4882a593Smuzhiyun
66*4882a593Smuzhiyunifeq ($(need-modorder),)
67*4882a593Smuzhiyunifneq ($(obj-m),)
68*4882a593Smuzhiyun$(warning $(patsubst %.o,'%.ko',$(obj-m)) will not be built even though obj-m is specified.)
69*4882a593Smuzhiyun$(warning You cannot use subdir-y/m to visit a module Makefile. Use obj-y/m instead.)
70*4882a593Smuzhiyunendif
71*4882a593Smuzhiyunendif
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun# ===========================================================================
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun# subdir-builtin and subdir-modorder may contain duplications. Use $(sort ...)
76*4882a593Smuzhiyunsubdir-builtin := $(sort $(filter %/built-in.a, $(real-obj-y)))
77*4882a593Smuzhiyunsubdir-modorder := $(sort $(filter %/modules.order, $(obj-m)))
78*4882a593Smuzhiyun
79*4882a593Smuzhiyuntargets-for-builtin := $(extra-y)
80*4882a593Smuzhiyun
81*4882a593Smuzhiyunifneq ($(strip $(lib-y) $(lib-m) $(lib-)),)
82*4882a593Smuzhiyuntargets-for-builtin += $(obj)/lib.a
83*4882a593Smuzhiyunendif
84*4882a593Smuzhiyun
85*4882a593Smuzhiyunifdef need-builtin
86*4882a593Smuzhiyuntargets-for-builtin += $(obj)/built-in.a
87*4882a593Smuzhiyunendif
88*4882a593Smuzhiyun
89*4882a593Smuzhiyuntargets-for-modules := $(patsubst %.o, %.mod, $(filter %.o, $(obj-m)))
90*4882a593Smuzhiyun
91*4882a593Smuzhiyunifdef need-modorder
92*4882a593Smuzhiyuntargets-for-modules += $(obj)/modules.order
93*4882a593Smuzhiyunendif
94*4882a593Smuzhiyun
95*4882a593Smuzhiyuntargets += $(targets-for-builtin) $(targets-for-modules)
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun# Linus' kernel sanity checking tool
98*4882a593Smuzhiyunifeq ($(KBUILD_CHECKSRC),1)
99*4882a593Smuzhiyun  quiet_cmd_checksrc       = CHECK   $<
100*4882a593Smuzhiyun        cmd_checksrc       = $(CHECK) $(CHECKFLAGS) $(c_flags) $<
101*4882a593Smuzhiyunelse ifeq ($(KBUILD_CHECKSRC),2)
102*4882a593Smuzhiyun  quiet_cmd_force_checksrc = CHECK   $<
103*4882a593Smuzhiyun        cmd_force_checksrc = $(CHECK) $(CHECKFLAGS) $(c_flags) $<
104*4882a593Smuzhiyunendif
105*4882a593Smuzhiyun
106*4882a593Smuzhiyunifneq ($(KBUILD_EXTRA_WARN),)
107*4882a593Smuzhiyun  cmd_checkdoc = $(srctree)/scripts/kernel-doc -none $<
108*4882a593Smuzhiyunendif
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun# Compile C sources (.c)
111*4882a593Smuzhiyun# ---------------------------------------------------------------------------
112*4882a593Smuzhiyun
113*4882a593Smuzhiyunquiet_cmd_cc_s_c = CC $(quiet_modtag)  $@
114*4882a593Smuzhiyun      cmd_cc_s_c = $(CC) -fverbose-asm $(filter-out $(DEBUG_CFLAGS) $(CC_FLAGS_LTO), $(c_flags)) -S -o $@ $<
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun$(obj)/%.s: $(src)/%.c FORCE
117*4882a593Smuzhiyun	$(call if_changed_dep,cc_s_c)
118*4882a593Smuzhiyun
119*4882a593Smuzhiyunquiet_cmd_cpp_i_c = CPP $(quiet_modtag) $@
120*4882a593Smuzhiyuncmd_cpp_i_c       = $(CPP) $(c_flags) -o $@ $<
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun$(obj)/%.i: $(src)/%.c FORCE
123*4882a593Smuzhiyun	$(call if_changed_dep,cpp_i_c)
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun# These mirror gensymtypes_S and co below, keep them in synch.
126*4882a593Smuzhiyuncmd_gensymtypes_c =                                                         \
127*4882a593Smuzhiyun    $(CPP) -D__GENKSYMS__ $(c_flags) $< |                                   \
128*4882a593Smuzhiyun    scripts/genksyms/genksyms $(if $(1), -T $(2))                           \
129*4882a593Smuzhiyun     $(patsubst y,-R,$(CONFIG_MODULE_REL_CRCS))                             \
130*4882a593Smuzhiyun     $(if $(KBUILD_PRESERVE),-p)                                            \
131*4882a593Smuzhiyun     -r $(firstword $(wildcard $(2:.symtypes=.symref) /dev/null))
132*4882a593Smuzhiyun
133*4882a593Smuzhiyunquiet_cmd_cc_symtypes_c = SYM $(quiet_modtag) $@
134*4882a593Smuzhiyuncmd_cc_symtypes_c =                                                         \
135*4882a593Smuzhiyun    $(call cmd_gensymtypes_c,true,$@) >/dev/null;                           \
136*4882a593Smuzhiyun    test -s $@ || rm -f $@
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun$(obj)/%.symtypes : $(src)/%.c FORCE
139*4882a593Smuzhiyun	$(call cmd,cc_symtypes_c)
140*4882a593Smuzhiyun
141*4882a593Smuzhiyun# LLVM assembly
142*4882a593Smuzhiyun# Generate .ll files from .c
143*4882a593Smuzhiyunquiet_cmd_cc_ll_c = CC $(quiet_modtag)  $@
144*4882a593Smuzhiyun      cmd_cc_ll_c = $(CC) $(c_flags) -emit-llvm -S -o $@ $<
145*4882a593Smuzhiyun
146*4882a593Smuzhiyun$(obj)/%.ll: $(src)/%.c FORCE
147*4882a593Smuzhiyun	$(call if_changed_dep,cc_ll_c)
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun# C (.c) files
150*4882a593Smuzhiyun# The C file is compiled and updated dependency information is generated.
151*4882a593Smuzhiyun# (See cmd_cc_o_c + relevant part of rule_cc_o_c)
152*4882a593Smuzhiyun
153*4882a593Smuzhiyunquiet_cmd_cc_o_c = CC $(quiet_modtag)  $@
154*4882a593Smuzhiyun      cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $<
155*4882a593Smuzhiyun
156*4882a593Smuzhiyunifdef CONFIG_MODVERSIONS
157*4882a593Smuzhiyun# When module versioning is enabled the following steps are executed:
158*4882a593Smuzhiyun# o compile a <file>.o from <file>.c
159*4882a593Smuzhiyun# o if <file>.o doesn't contain a __ksymtab version, i.e. does
160*4882a593Smuzhiyun#   not export symbols, it's done.
161*4882a593Smuzhiyun# o otherwise, we calculate symbol versions using the good old
162*4882a593Smuzhiyun#   genksyms on the preprocessed source and postprocess them in a way
163*4882a593Smuzhiyun#   that they are usable as a linker script
164*4882a593Smuzhiyun# o generate .tmp_<file>.o from <file>.o using the linker to
165*4882a593Smuzhiyun#   replace the unresolved symbols __crc_exported_symbol with
166*4882a593Smuzhiyun#   the actual value of the checksum generated by genksyms
167*4882a593Smuzhiyun# o remove .tmp_<file>.o to <file>.o
168*4882a593Smuzhiyun
169*4882a593Smuzhiyunifdef CONFIG_LTO_CLANG
170*4882a593Smuzhiyun# Generate .o.symversions files for each .o with exported symbols, and link these
171*4882a593Smuzhiyun# to the kernel and/or modules at the end.
172*4882a593Smuzhiyuncmd_modversions_c =								\
173*4882a593Smuzhiyun	if $(NM) $@ 2>/dev/null | grep -q __ksymtab; then			\
174*4882a593Smuzhiyun		$(call cmd_gensymtypes_c,$(KBUILD_SYMTYPES),$(@:.o=.symtypes))	\
175*4882a593Smuzhiyun		    > $@.symversions;						\
176*4882a593Smuzhiyun	fi;
177*4882a593Smuzhiyunelse
178*4882a593Smuzhiyuncmd_modversions_c =								\
179*4882a593Smuzhiyun	if $(OBJDUMP) -h $@ | grep -q __ksymtab; then				\
180*4882a593Smuzhiyun		$(call cmd_gensymtypes_c,$(KBUILD_SYMTYPES),$(@:.o=.symtypes))	\
181*4882a593Smuzhiyun		    > $(@D)/.tmp_$(@F:.o=.ver);					\
182*4882a593Smuzhiyun										\
183*4882a593Smuzhiyun		$(LD) $(KBUILD_LDFLAGS) -r -o $(@D)/.tmp_$(@F) $@ 		\
184*4882a593Smuzhiyun			-T $(@D)/.tmp_$(@F:.o=.ver);				\
185*4882a593Smuzhiyun		mv -f $(@D)/.tmp_$(@F) $@;					\
186*4882a593Smuzhiyun		rm -f $(@D)/.tmp_$(@F:.o=.ver);					\
187*4882a593Smuzhiyun	fi
188*4882a593Smuzhiyunendif
189*4882a593Smuzhiyunendif
190*4882a593Smuzhiyun
191*4882a593Smuzhiyunifdef CONFIG_FTRACE_MCOUNT_USE_RECORDMCOUNT
192*4882a593Smuzhiyun# compiler will not generate __mcount_loc use recordmcount or recordmcount.pl
193*4882a593Smuzhiyunifdef BUILD_C_RECORDMCOUNT
194*4882a593Smuzhiyunifeq ("$(origin RECORDMCOUNT_WARN)", "command line")
195*4882a593Smuzhiyun  RECORDMCOUNT_FLAGS = -w
196*4882a593Smuzhiyunendif
197*4882a593Smuzhiyun# Due to recursion, we must skip empty.o.
198*4882a593Smuzhiyun# The empty.o file is created in the make process in order to determine
199*4882a593Smuzhiyun# the target endianness and word size. It is made before all other C
200*4882a593Smuzhiyun# files, including recordmcount.
201*4882a593Smuzhiyunsub_cmd_record_mcount =					\
202*4882a593Smuzhiyun	if [ $(@) != "scripts/mod/empty.o" ]; then	\
203*4882a593Smuzhiyun		$(objtree)/scripts/recordmcount $(RECORDMCOUNT_FLAGS) "$(@)";	\
204*4882a593Smuzhiyun	fi;
205*4882a593Smuzhiyunrecordmcount_source := $(srctree)/scripts/recordmcount.c \
206*4882a593Smuzhiyun		    $(srctree)/scripts/recordmcount.h
207*4882a593Smuzhiyunelse
208*4882a593Smuzhiyunsub_cmd_record_mcount = perl $(srctree)/scripts/recordmcount.pl "$(ARCH)" \
209*4882a593Smuzhiyun	"$(if $(CONFIG_CPU_BIG_ENDIAN),big,little)" \
210*4882a593Smuzhiyun	"$(if $(CONFIG_64BIT),64,32)" \
211*4882a593Smuzhiyun	"$(OBJDUMP)" "$(OBJCOPY)" "$(CC) $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS)" \
212*4882a593Smuzhiyun	"$(LD) $(KBUILD_LDFLAGS)" "$(NM)" "$(RM)" "$(MV)" \
213*4882a593Smuzhiyun	"$(if $(part-of-module),1,0)" "$(@)";
214*4882a593Smuzhiyunrecordmcount_source := $(srctree)/scripts/recordmcount.pl
215*4882a593Smuzhiyunendif # BUILD_C_RECORDMCOUNT
216*4882a593Smuzhiyuncmd_record_mcount = $(if $(findstring $(strip $(CC_FLAGS_FTRACE)),$(_c_flags)),	\
217*4882a593Smuzhiyun	$(sub_cmd_record_mcount))
218*4882a593Smuzhiyunendif # CONFIG_FTRACE_MCOUNT_USE_RECORDMCOUNT
219*4882a593Smuzhiyun
220*4882a593Smuzhiyunifdef CONFIG_STACK_VALIDATION
221*4882a593Smuzhiyunifndef CONFIG_LTO_CLANG
222*4882a593Smuzhiyunifneq ($(SKIP_STACK_VALIDATION),1)
223*4882a593Smuzhiyun
224*4882a593Smuzhiyun__objtool_obj := $(objtree)/tools/objtool/objtool
225*4882a593Smuzhiyun
226*4882a593Smuzhiyun# 'OBJECT_FILES_NON_STANDARD := y': skip objtool checking for a directory
227*4882a593Smuzhiyun# 'OBJECT_FILES_NON_STANDARD_foo.o := 'y': skip objtool checking for a file
228*4882a593Smuzhiyun# 'OBJECT_FILES_NON_STANDARD_foo.o := 'n': override directory skip for a file
229*4882a593Smuzhiyuncmd_objtool = $(if $(patsubst y%,, \
230*4882a593Smuzhiyun	$(OBJECT_FILES_NON_STANDARD_$(basetarget).o)$(OBJECT_FILES_NON_STANDARD)n), \
231*4882a593Smuzhiyun	$(__objtool_obj) $(objtool_args) $@)
232*4882a593Smuzhiyunobjtool_obj = $(if $(patsubst y%,, \
233*4882a593Smuzhiyun	$(OBJECT_FILES_NON_STANDARD_$(basetarget).o)$(OBJECT_FILES_NON_STANDARD)n), \
234*4882a593Smuzhiyun	$(__objtool_obj))
235*4882a593Smuzhiyun
236*4882a593Smuzhiyunendif # SKIP_STACK_VALIDATION
237*4882a593Smuzhiyunendif # CONFIG_LTO_CLANG
238*4882a593Smuzhiyunendif # CONFIG_STACK_VALIDATION
239*4882a593Smuzhiyun
240*4882a593Smuzhiyun# Rebuild all objects when objtool changes, or is enabled/disabled.
241*4882a593Smuzhiyunobjtool_dep = $(objtool_obj)					\
242*4882a593Smuzhiyun	      $(wildcard include/config/orc/unwinder.h		\
243*4882a593Smuzhiyun			 include/config/stack/validation.h)
244*4882a593Smuzhiyun
245*4882a593Smuzhiyunifdef CONFIG_TRIM_UNUSED_KSYMS
246*4882a593Smuzhiyuncmd_gen_ksymdeps = \
247*4882a593Smuzhiyun	$(CONFIG_SHELL) $(srctree)/scripts/gen_ksymdeps.sh $@ >> $(dot-target).cmd
248*4882a593Smuzhiyun
249*4882a593Smuzhiyun# List module undefined symbols
250*4882a593Smuzhiyunundefined_syms = $(NM) $< | $(AWK) '$$1 == "U" { printf("%s%s", x++ ? " " : "", $$2) }';
251*4882a593Smuzhiyunendif
252*4882a593Smuzhiyun
253*4882a593Smuzhiyundefine rule_cc_o_c
254*4882a593Smuzhiyun	$(call cmd_and_fixdep,cc_o_c)
255*4882a593Smuzhiyun	$(call cmd,gen_ksymdeps)
256*4882a593Smuzhiyun	$(call cmd,checksrc)
257*4882a593Smuzhiyun	$(call cmd,checkdoc)
258*4882a593Smuzhiyun	$(call cmd,objtool)
259*4882a593Smuzhiyun	$(call cmd,modversions_c)
260*4882a593Smuzhiyun	$(call cmd,record_mcount)
261*4882a593Smuzhiyunendef
262*4882a593Smuzhiyun
263*4882a593Smuzhiyundefine rule_as_o_S
264*4882a593Smuzhiyun	$(call cmd_and_fixdep,as_o_S)
265*4882a593Smuzhiyun	$(call cmd,gen_ksymdeps)
266*4882a593Smuzhiyun	$(call cmd,objtool)
267*4882a593Smuzhiyun	$(call cmd,modversions_S)
268*4882a593Smuzhiyunendef
269*4882a593Smuzhiyun
270*4882a593Smuzhiyun# Built-in and composite module parts
271*4882a593Smuzhiyun.SECONDEXPANSION:
272*4882a593Smuzhiyun$(obj)/%.o: $(src)/%.c $(recordmcount_source) $$(objtool_dep) FORCE
273*4882a593Smuzhiyun	$(call if_changed_rule,cc_o_c)
274*4882a593Smuzhiyun	$(call cmd,force_checksrc)
275*4882a593Smuzhiyun
276*4882a593Smuzhiyuncmd_mod = { \
277*4882a593Smuzhiyun	echo $(if $($*-objs)$($*-y)$($*-m), $(addprefix $(obj)/, $($*-objs) $($*-y) $($*-m)), $(@:.mod=.o)); \
278*4882a593Smuzhiyun	$(undefined_syms) echo; \
279*4882a593Smuzhiyun	} > $@
280*4882a593Smuzhiyun
281*4882a593Smuzhiyun$(obj)/%.mod: $(obj)/%.o FORCE
282*4882a593Smuzhiyun	$(call if_changed,mod)
283*4882a593Smuzhiyun
284*4882a593Smuzhiyunquiet_cmd_cc_lst_c = MKLST   $@
285*4882a593Smuzhiyun      cmd_cc_lst_c = $(CC) $(c_flags) -g -c -o $*.o $< && \
286*4882a593Smuzhiyun		     $(CONFIG_SHELL) $(srctree)/scripts/makelst $*.o \
287*4882a593Smuzhiyun				     System.map $(OBJDUMP) > $@
288*4882a593Smuzhiyun
289*4882a593Smuzhiyun$(obj)/%.lst: $(src)/%.c FORCE
290*4882a593Smuzhiyun	$(call if_changed_dep,cc_lst_c)
291*4882a593Smuzhiyun
292*4882a593Smuzhiyun# Compile assembler sources (.S)
293*4882a593Smuzhiyun# ---------------------------------------------------------------------------
294*4882a593Smuzhiyun
295*4882a593Smuzhiyun# .S file exports must have their C prototypes defined in asm/asm-prototypes.h
296*4882a593Smuzhiyun# or a file that it includes, in order to get versioned symbols. We build a
297*4882a593Smuzhiyun# dummy C file that includes asm-prototypes and the EXPORT_SYMBOL lines from
298*4882a593Smuzhiyun# the .S file (with trailing ';'), and run genksyms on that, to extract vers.
299*4882a593Smuzhiyun#
300*4882a593Smuzhiyun# This is convoluted. The .S file must first be preprocessed to run guards and
301*4882a593Smuzhiyun# expand names, then the resulting exports must be constructed into plain
302*4882a593Smuzhiyun# EXPORT_SYMBOL(symbol); to build our dummy C file, and that gets preprocessed
303*4882a593Smuzhiyun# to make the genksyms input.
304*4882a593Smuzhiyun#
305*4882a593Smuzhiyun# These mirror gensymtypes_c and co above, keep them in synch.
306*4882a593Smuzhiyuncmd_gensymtypes_S =                                                         \
307*4882a593Smuzhiyun   { echo "\#include <linux/kernel.h>" ;                                    \
308*4882a593Smuzhiyun     echo "\#include <asm/asm-prototypes.h>" ;                              \
309*4882a593Smuzhiyun    $(CPP) $(a_flags) $< |                                                  \
310*4882a593Smuzhiyun     grep "\<___EXPORT_SYMBOL\>" |                                          \
311*4882a593Smuzhiyun     sed 's/.*___EXPORT_SYMBOL[[:space:]]*\([a-zA-Z0-9_]*\)[[:space:]]*,.*/EXPORT_SYMBOL(\1);/' ; } | \
312*4882a593Smuzhiyun    $(CPP) -D__GENKSYMS__ $(c_flags) -xc - |                                \
313*4882a593Smuzhiyun    scripts/genksyms/genksyms $(if $(1), -T $(2))                           \
314*4882a593Smuzhiyun     $(patsubst y,-R,$(CONFIG_MODULE_REL_CRCS))                             \
315*4882a593Smuzhiyun     $(if $(KBUILD_PRESERVE),-p)                                            \
316*4882a593Smuzhiyun     -r $(firstword $(wildcard $(2:.symtypes=.symref) /dev/null))
317*4882a593Smuzhiyun
318*4882a593Smuzhiyunquiet_cmd_cc_symtypes_S = SYM $(quiet_modtag) $@
319*4882a593Smuzhiyuncmd_cc_symtypes_S =                                                         \
320*4882a593Smuzhiyun    $(call cmd_gensymtypes_S,true,$@) >/dev/null;                           \
321*4882a593Smuzhiyun    test -s $@ || rm -f $@
322*4882a593Smuzhiyun
323*4882a593Smuzhiyun$(obj)/%.symtypes : $(src)/%.S FORCE
324*4882a593Smuzhiyun	$(call cmd,cc_symtypes_S)
325*4882a593Smuzhiyun
326*4882a593Smuzhiyun
327*4882a593Smuzhiyunquiet_cmd_cpp_s_S = CPP $(quiet_modtag) $@
328*4882a593Smuzhiyuncmd_cpp_s_S       = $(CPP) $(a_flags) -o $@ $<
329*4882a593Smuzhiyun
330*4882a593Smuzhiyun$(obj)/%.s: $(src)/%.S FORCE
331*4882a593Smuzhiyun	$(call if_changed_dep,cpp_s_S)
332*4882a593Smuzhiyun
333*4882a593Smuzhiyunquiet_cmd_as_o_S = AS $(quiet_modtag)  $@
334*4882a593Smuzhiyun      cmd_as_o_S = $(CC) $(a_flags) -c -o $@ $<
335*4882a593Smuzhiyun
336*4882a593Smuzhiyunifdef CONFIG_ASM_MODVERSIONS
337*4882a593Smuzhiyun
338*4882a593Smuzhiyun# versioning matches the C process described above, with difference that
339*4882a593Smuzhiyun# we parse asm-prototypes.h C header to get function definitions.
340*4882a593Smuzhiyun
341*4882a593Smuzhiyuncmd_modversions_S =								\
342*4882a593Smuzhiyun	if $(OBJDUMP) -h $@ | grep -q __ksymtab; then				\
343*4882a593Smuzhiyun		$(call cmd_gensymtypes_S,$(KBUILD_SYMTYPES),$(@:.o=.symtypes))	\
344*4882a593Smuzhiyun		    > $(@D)/.tmp_$(@F:.o=.ver);					\
345*4882a593Smuzhiyun										\
346*4882a593Smuzhiyun		$(LD) $(KBUILD_LDFLAGS) -r -o $(@D)/.tmp_$(@F) $@ 		\
347*4882a593Smuzhiyun			-T $(@D)/.tmp_$(@F:.o=.ver);				\
348*4882a593Smuzhiyun		mv -f $(@D)/.tmp_$(@F) $@;					\
349*4882a593Smuzhiyun		rm -f $(@D)/.tmp_$(@F:.o=.ver);					\
350*4882a593Smuzhiyun	fi
351*4882a593Smuzhiyunendif
352*4882a593Smuzhiyun
353*4882a593Smuzhiyun$(obj)/%.o: $(src)/%.S $$(objtool_dep) FORCE
354*4882a593Smuzhiyun	$(call if_changed_rule,as_o_S)
355*4882a593Smuzhiyun
356*4882a593Smuzhiyuntargets += $(filter-out $(subdir-builtin), $(real-obj-y))
357*4882a593Smuzhiyuntargets += $(filter-out $(subdir-modorder), $(real-obj-m))
358*4882a593Smuzhiyuntargets += $(lib-y) $(always-y) $(MAKECMDGOALS)
359*4882a593Smuzhiyun
360*4882a593Smuzhiyun# Linker scripts preprocessor (.lds.S -> .lds)
361*4882a593Smuzhiyun# ---------------------------------------------------------------------------
362*4882a593Smuzhiyunquiet_cmd_cpp_lds_S = LDS     $@
363*4882a593Smuzhiyun      cmd_cpp_lds_S = $(CPP) $(cpp_flags) -P -U$(ARCH) \
364*4882a593Smuzhiyun	                     -D__ASSEMBLY__ -DLINKER_SCRIPT -o $@ $<
365*4882a593Smuzhiyun
366*4882a593Smuzhiyun$(obj)/%.lds: $(src)/%.lds.S FORCE
367*4882a593Smuzhiyun	$(call if_changed_dep,cpp_lds_S)
368*4882a593Smuzhiyun
369*4882a593Smuzhiyun# ASN.1 grammar
370*4882a593Smuzhiyun# ---------------------------------------------------------------------------
371*4882a593Smuzhiyunquiet_cmd_asn1_compiler = ASN.1   $(basename $@).[ch]
372*4882a593Smuzhiyun      cmd_asn1_compiler = $(objtree)/scripts/asn1_compiler $< \
373*4882a593Smuzhiyun				$(basename $@).c $(basename $@).h
374*4882a593Smuzhiyun
375*4882a593Smuzhiyun$(obj)/%.asn1.c $(obj)/%.asn1.h: $(src)/%.asn1 $(objtree)/scripts/asn1_compiler
376*4882a593Smuzhiyun	$(call cmd,asn1_compiler)
377*4882a593Smuzhiyun
378*4882a593Smuzhiyun# Build the compiled-in targets
379*4882a593Smuzhiyun# ---------------------------------------------------------------------------
380*4882a593Smuzhiyun
381*4882a593Smuzhiyun# To build objects in subdirs, we need to descend into the directories
382*4882a593Smuzhiyun$(subdir-builtin): $(obj)/%/built-in.a: $(obj)/% ;
383*4882a593Smuzhiyun$(subdir-modorder): $(obj)/%/modules.order: $(obj)/% ;
384*4882a593Smuzhiyun
385*4882a593Smuzhiyun# combine symversions for later processing
386*4882a593Smuzhiyunquiet_cmd_update_lto_symversions = SYMVER  $@
387*4882a593Smuzhiyunifeq ($(CONFIG_LTO_CLANG) $(CONFIG_MODVERSIONS),y y)
388*4882a593Smuzhiyun      cmd_update_lto_symversions =					\
389*4882a593Smuzhiyun	rm -f $@.symversions						\
390*4882a593Smuzhiyun	$(foreach n, $(filter-out FORCE,$^),				\
391*4882a593Smuzhiyun		$(if $(shell test -s $(n).symversions && echo y),	\
392*4882a593Smuzhiyun			; cat $(n).symversions >> $@.symversions))
393*4882a593Smuzhiyunelse
394*4882a593Smuzhiyun      cmd_update_lto_symversions = echo >/dev/null
395*4882a593Smuzhiyunendif
396*4882a593Smuzhiyun
397*4882a593Smuzhiyun#
398*4882a593Smuzhiyun# Rule to compile a set of .o files into one .a file (without symbol table)
399*4882a593Smuzhiyun#
400*4882a593Smuzhiyun
401*4882a593Smuzhiyunquiet_cmd_ar_builtin = AR      $@
402*4882a593Smuzhiyun      cmd_ar_builtin = rm -f $@; $(AR) cDPrST $@ $(real-prereqs)
403*4882a593Smuzhiyun
404*4882a593Smuzhiyunquiet_cmd_ar_and_symver = AR      $@
405*4882a593Smuzhiyun      cmd_ar_and_symver = $(cmd_update_lto_symversions); $(cmd_ar_builtin)
406*4882a593Smuzhiyun
407*4882a593Smuzhiyun$(obj)/built-in.a: $(real-obj-y) FORCE
408*4882a593Smuzhiyun	$(call if_changed,ar_and_symver)
409*4882a593Smuzhiyun
410*4882a593Smuzhiyun#
411*4882a593Smuzhiyun# Rule to create modules.order file
412*4882a593Smuzhiyun#
413*4882a593Smuzhiyun# Create commands to either record .ko file or cat modules.order from
414*4882a593Smuzhiyun# a subdirectory
415*4882a593Smuzhiyun# Add $(obj-m) as the prerequisite to avoid updating the timestamp of
416*4882a593Smuzhiyun# modules.order unless contained modules are updated.
417*4882a593Smuzhiyun
418*4882a593Smuzhiyuncmd_modules_order = { $(foreach m, $(real-prereqs), \
419*4882a593Smuzhiyun	$(if $(filter %/modules.order, $m), cat $m, echo $(patsubst %.o,%.ko,$m));) :; } \
420*4882a593Smuzhiyun	| $(AWK) '!x[$$0]++' - > $@
421*4882a593Smuzhiyun
422*4882a593Smuzhiyun$(obj)/modules.order: $(obj-m) FORCE
423*4882a593Smuzhiyun	$(call if_changed,modules_order)
424*4882a593Smuzhiyun
425*4882a593Smuzhiyun#
426*4882a593Smuzhiyun# Rule to compile a set of .o files into one .a file (with symbol table)
427*4882a593Smuzhiyun#
428*4882a593Smuzhiyunquiet_cmd_ar_lib = AR      $@
429*4882a593Smuzhiyun      cmd_ar_lib = $(cmd_update_lto_symversions); $(cmd_ar)
430*4882a593Smuzhiyun
431*4882a593Smuzhiyun$(obj)/lib.a: $(lib-y) FORCE
432*4882a593Smuzhiyun	$(call if_changed,ar_lib)
433*4882a593Smuzhiyun
434*4882a593Smuzhiyun# NOTE:
435*4882a593Smuzhiyun# Do not replace $(filter %.o,^) with $(real-prereqs). When a single object
436*4882a593Smuzhiyun# module is turned into a multi object module, $^ will contain header file
437*4882a593Smuzhiyun# dependencies recorded in the .*.cmd file.
438*4882a593Smuzhiyunifdef CONFIG_LTO_CLANG
439*4882a593Smuzhiyunquiet_cmd_link_multi-m = AR [M]  $@
440*4882a593Smuzhiyuncmd_link_multi-m =						\
441*4882a593Smuzhiyun	$(cmd_update_lto_symversions);				\
442*4882a593Smuzhiyun	rm -f $@; 						\
443*4882a593Smuzhiyun	$(AR) cDPrsT $@ $(filter %.o %.a,$^)
444*4882a593Smuzhiyunelse
445*4882a593Smuzhiyunquiet_cmd_link_multi-m = LD [M]  $@
446*4882a593Smuzhiyun      cmd_link_multi-m = $(LD) $(ld_flags) -r -o $@ $(filter %.o,$^)
447*4882a593Smuzhiyunendif
448*4882a593Smuzhiyun
449*4882a593Smuzhiyun$(multi-used-m): FORCE
450*4882a593Smuzhiyun	$(call if_changed,link_multi-m)
451*4882a593Smuzhiyun$(call multi_depend, $(multi-used-m), .o, -objs -y -m)
452*4882a593Smuzhiyun
453*4882a593Smuzhiyuntargets += $(multi-used-m)
454*4882a593Smuzhiyuntargets := $(filter-out $(PHONY), $(targets))
455*4882a593Smuzhiyun
456*4882a593Smuzhiyun# Add intermediate targets:
457*4882a593Smuzhiyun# When building objects with specific suffix patterns, add intermediate
458*4882a593Smuzhiyun# targets that the final targets are derived from.
459*4882a593Smuzhiyunintermediate_targets = $(foreach sfx, $(2), \
460*4882a593Smuzhiyun				$(patsubst %$(strip $(1)),%$(sfx), \
461*4882a593Smuzhiyun					$(filter %$(strip $(1)), $(targets))))
462*4882a593Smuzhiyun# %.asn1.o <- %.asn1.[ch] <- %.asn1
463*4882a593Smuzhiyun# %.dtb.o <- %.dtb.S <- %.dtb <- %.dts
464*4882a593Smuzhiyun# %.lex.o <- %.lex.c <- %.l
465*4882a593Smuzhiyun# %.tab.o <- %.tab.[ch] <- %.y
466*4882a593Smuzhiyuntargets += $(call intermediate_targets, .asn1.o, .asn1.c .asn1.h) \
467*4882a593Smuzhiyun	   $(call intermediate_targets, .dtb.o, .dtb.S .dtb) \
468*4882a593Smuzhiyun	   $(call intermediate_targets, .lex.o, .lex.c) \
469*4882a593Smuzhiyun	   $(call intermediate_targets, .tab.o, .tab.c .tab.h)
470*4882a593Smuzhiyun
471*4882a593Smuzhiyun# Build
472*4882a593Smuzhiyun# ---------------------------------------------------------------------------
473*4882a593Smuzhiyun
474*4882a593Smuzhiyunifdef single-build
475*4882a593Smuzhiyun
476*4882a593SmuzhiyunKBUILD_SINGLE_TARGETS := $(filter $(obj)/%, $(KBUILD_SINGLE_TARGETS))
477*4882a593Smuzhiyun
478*4882a593Smuzhiyuncurdir-single := $(sort $(foreach x, $(KBUILD_SINGLE_TARGETS), \
479*4882a593Smuzhiyun			$(if $(filter $(x) $(basename $(x)).o, $(targets)), $(x))))
480*4882a593Smuzhiyun
481*4882a593Smuzhiyun# Handle single targets without any rule: show "Nothing to be done for ..." or
482*4882a593Smuzhiyun# "No rule to make target ..." depending on whether the target exists.
483*4882a593Smuzhiyununknown-single := $(filter-out $(addsuffix /%, $(subdir-ym)), \
484*4882a593Smuzhiyun			$(filter-out $(curdir-single), $(KBUILD_SINGLE_TARGETS)))
485*4882a593Smuzhiyun
486*4882a593Smuzhiyunsingle-subdirs := $(foreach d, $(subdir-ym), \
487*4882a593Smuzhiyun			$(if $(filter $(d)/%, $(KBUILD_SINGLE_TARGETS)), $(d)))
488*4882a593Smuzhiyun
489*4882a593Smuzhiyun__build: $(curdir-single) $(single-subdirs)
490*4882a593Smuzhiyunifneq ($(unknown-single),)
491*4882a593Smuzhiyun	$(Q)$(MAKE) -f /dev/null $(unknown-single)
492*4882a593Smuzhiyunendif
493*4882a593Smuzhiyun	@:
494*4882a593Smuzhiyun
495*4882a593Smuzhiyunifeq ($(curdir-single),)
496*4882a593Smuzhiyun# Nothing to do in this directory. Do not include any .*.cmd file for speed-up
497*4882a593Smuzhiyuntargets :=
498*4882a593Smuzhiyunelse
499*4882a593Smuzhiyuntargets += $(curdir-single)
500*4882a593Smuzhiyunendif
501*4882a593Smuzhiyun
502*4882a593Smuzhiyunelse
503*4882a593Smuzhiyun
504*4882a593Smuzhiyun__build: $(if $(KBUILD_BUILTIN), $(targets-for-builtin)) \
505*4882a593Smuzhiyun	 $(if $(KBUILD_MODULES), $(targets-for-modules)) \
506*4882a593Smuzhiyun	 $(subdir-ym) $(always-y)
507*4882a593Smuzhiyun	@:
508*4882a593Smuzhiyun
509*4882a593Smuzhiyunendif
510*4882a593Smuzhiyun
511*4882a593Smuzhiyun# Descending
512*4882a593Smuzhiyun# ---------------------------------------------------------------------------
513*4882a593Smuzhiyun
514*4882a593SmuzhiyunPHONY += $(subdir-ym)
515*4882a593Smuzhiyun$(subdir-ym):
516*4882a593Smuzhiyun	$(Q)$(MAKE) $(build)=$@ \
517*4882a593Smuzhiyun	$(if $(filter $@/, $(KBUILD_SINGLE_TARGETS)),single-build=) \
518*4882a593Smuzhiyun	need-builtin=$(if $(filter $@/built-in.a, $(subdir-builtin)),1) \
519*4882a593Smuzhiyun	need-modorder=$(if $(filter $@/modules.order, $(subdir-modorder)),1)
520*4882a593Smuzhiyun
521*4882a593Smuzhiyun# Add FORCE to the prequisites of a target to force it to be always rebuilt.
522*4882a593Smuzhiyun# ---------------------------------------------------------------------------
523*4882a593Smuzhiyun
524*4882a593SmuzhiyunPHONY += FORCE
525*4882a593Smuzhiyun
526*4882a593SmuzhiyunFORCE:
527*4882a593Smuzhiyun
528*4882a593Smuzhiyun# Read all saved command lines and dependencies for the $(targets) we
529*4882a593Smuzhiyun# may be building above, using $(if_changed{,_dep}). As an
530*4882a593Smuzhiyun# optimization, we don't need to read them if the target does not
531*4882a593Smuzhiyun# exist, we will rebuild anyway in that case.
532*4882a593Smuzhiyun
533*4882a593Smuzhiyunexisting-targets := $(wildcard $(sort $(targets)))
534*4882a593Smuzhiyun
535*4882a593Smuzhiyun-include $(foreach f,$(existing-targets),$(dir $(f)).$(notdir $(f)).cmd)
536*4882a593Smuzhiyun
537*4882a593Smuzhiyun# Create directories for object files if they do not exist
538*4882a593Smuzhiyunobj-dirs := $(sort $(patsubst %/,%, $(dir $(targets))))
539*4882a593Smuzhiyun# If targets exist, their directories apparently exist. Skip mkdir.
540*4882a593Smuzhiyunexisting-dirs := $(sort $(patsubst %/,%, $(dir $(existing-targets))))
541*4882a593Smuzhiyunobj-dirs := $(strip $(filter-out $(existing-dirs), $(obj-dirs)))
542*4882a593Smuzhiyunifneq ($(obj-dirs),)
543*4882a593Smuzhiyun$(shell mkdir -p $(obj-dirs))
544*4882a593Smuzhiyunendif
545*4882a593Smuzhiyun
546*4882a593Smuzhiyun.PHONY: $(PHONY)
547