]> git.ipfire.org Git - thirdparty/linux.git/blame - scripts/Makefile.build
modpost: remove annoying namespace_from_kstrtabns()
[thirdparty/linux.git] / scripts / Makefile.build
CommitLineData
b2441318 1# SPDX-License-Identifier: GPL-2.0
1da177e4
LT
2# ==========================================================================
3# Building
4# ==========================================================================
5
6src := $(obj)
7
4f193362 8PHONY := __build
1da177e4
LT
9__build:
10
d72e5edb
SR
11# Init all relevant variables used in kbuild files so
12# 1) they have correct type
13# 2) they do not inherit any value from the environment
14obj-y :=
15obj-m :=
16lib-y :=
17lib-m :=
5f2fb52f
MY
18always-y :=
19always-m :=
d72e5edb
SR
20targets :=
21subdir-y :=
22subdir-m :=
23EXTRA_AFLAGS :=
24EXTRA_CFLAGS :=
25EXTRA_CPPFLAGS :=
26EXTRA_LDFLAGS :=
f77bf014
SR
27asflags-y :=
28ccflags-y :=
29cppflags-y :=
30ldflags-y :=
d72e5edb 31
720097d8
SR
32subdir-asflags-y :=
33subdir-ccflags-y :=
34
3156fd05 35# Read auto.conf if it exists, otherwise ignore
c955ccaf 36-include include/config/auto.conf
1da177e4 37
3204a7fb 38include $(srctree)/scripts/Kbuild.include
57fd251c 39include $(srctree)/scripts/Makefile.compiler
20a468b5 40
2a691470 41# The filename Kbuild has precedence over Makefile
db8c1a7b 42kbuild-dir := $(if $(filter /%,$(src)),$(src),$(srctree)/$(src))
5c816641 43include $(or $(wildcard $(kbuild-dir)/Kbuild),$(kbuild-dir)/Makefile)
0c53c8e6 44
3204a7fb 45include $(srctree)/scripts/Makefile.lib
1da177e4 46
85569d19
MY
47# Do not include hostprogs rules unless needed.
48# $(sort ...) is used here to remove duplicated words and excessive spaces.
49hostprogs := $(sort $(hostprogs))
42640b13 50ifneq ($(hostprogs),)
3204a7fb 51include $(srctree)/scripts/Makefile.host
1da177e4
LT
52endif
53
7f3a59db 54# Do not include userprogs rules unless needed.
85569d19 55# $(sort ...) is used here to remove duplicated words and excessive spaces.
7f3a59db
MY
56userprogs := $(sort $(userprogs))
57ifneq ($(userprogs),)
3204a7fb 58include $(srctree)/scripts/Makefile.userprogs
7f3a59db
MY
59endif
60
1da177e4
LT
61ifndef obj
62$(warning kbuild: Makefile.build is included improperly)
63endif
64
394053f4 65ifeq ($(need-modorder),)
c07d8d47
MY
66ifneq ($(obj-m),)
67$(warning $(patsubst %.o,'%.ko',$(obj-m)) will not be built even though obj-m is specified.)
68$(warning You cannot use subdir-y/m to visit a module Makefile. Use obj-y/m instead.)
69endif
70endif
71
1da177e4
LT
72# ===========================================================================
73
454753d9 74# subdir-builtin and subdir-modorder may contain duplications. Use $(sort ...)
aaa385ba 75subdir-builtin := $(sort $(filter %/built-in.a, $(real-obj-y)))
b2c88554 76subdir-modorder := $(sort $(filter %/modules.order, $(obj-m)))
6ba3bcb0 77
f3908ab3
MY
78targets-for-builtin := $(extra-y)
79
a4954fd7 80ifneq ($(strip $(lib-y) $(lib-m) $(lib-)),)
f3908ab3 81targets-for-builtin += $(obj)/lib.a
1da177e4
LT
82endif
83
56d58936 84ifdef need-builtin
f3908ab3 85targets-for-builtin += $(obj)/built-in.a
1da177e4
LT
86endif
87
d31ed5d7 88targets-for-modules := $(patsubst %.o, %.mod, $(filter %.o, $(obj-m)))
f3908ab3 89
d31ed5d7
PZ
90ifneq ($(CONFIG_LTO_CLANG)$(CONFIG_X86_KERNEL_IBT),)
91targets-for-modules += $(patsubst %.o, %.prelink.o, $(filter %.o, $(obj-m)))
ed53a0d9
PZ
92endif
93
b480fec9 94ifdef need-modorder
f3908ab3 95targets-for-modules += $(obj)/modules.order
1d8001ef 96endif
551559e1 97
f3908ab3 98targets += $(targets-for-builtin) $(targets-for-modules)
9f69a496 99
1da177e4 100# Linus' kernel sanity checking tool
7d0ea252
MY
101ifeq ($(KBUILD_CHECKSRC),1)
102 quiet_cmd_checksrc = CHECK $<
e5d28910 103 cmd_checksrc = $(CHECK) $(CHECKFLAGS) $(c_flags) $<
7d0ea252
MY
104else ifeq ($(KBUILD_CHECKSRC),2)
105 quiet_cmd_force_checksrc = CHECK $<
e5d28910 106 cmd_force_checksrc = $(CHECK) $(CHECKFLAGS) $(c_flags) $<
1da177e4
LT
107endif
108
e27128db 109ifneq ($(KBUILD_EXTRA_WARN),)
e5d28910 110 cmd_checkdoc = $(srctree)/scripts/kernel-doc -none $<
3a025e1d
MW
111endif
112
1da177e4
LT
113# Compile C sources (.c)
114# ---------------------------------------------------------------------------
115
1da177e4 116quiet_cmd_cc_s_c = CC $(quiet_modtag) $@
dc5723b0 117 cmd_cc_s_c = $(CC) $(filter-out $(DEBUG_CFLAGS) $(CC_FLAGS_LTO), $(c_flags)) -fverbose-asm -S -o $@ $<
1da177e4 118
767e581d 119$(obj)/%.s: $(src)/%.c FORCE
1da177e4
LT
120 $(call if_changed_dep,cc_s_c)
121
23d43848
MY
122quiet_cmd_cpp_i_c = CPP $(quiet_modtag) $@
123cmd_cpp_i_c = $(CPP) $(c_flags) -o $@ $<
1da177e4 124
767e581d 125$(obj)/%.i: $(src)/%.c FORCE
23d43848 126 $(call if_changed_dep,cpp_i_c)
1da177e4 127
f43e31d5
MY
128genksyms = scripts/genksyms/genksyms \
129 $(if $(1), -T $(2)) \
130 $(if $(CONFIG_MODULE_REL_CRCS), -R) \
131 $(if $(KBUILD_PRESERVE), -p) \
132 -r $(or $(wildcard $(2:.symtypes=.symref)), /dev/null)
133
4efca4ed 134# These mirror gensymtypes_S and co below, keep them in synch.
f43e31d5 135cmd_gensymtypes_c = $(CPP) -D__GENKSYMS__ $(c_flags) $< | $(genksyms)
37a8d9f6 136
15fde675 137quiet_cmd_cc_symtypes_c = SYM $(quiet_modtag) $@
dc6dc3e7 138 cmd_cc_symtypes_c = $(call cmd_gensymtypes_c,true,$@) >/dev/null
15fde675 139
767e581d 140$(obj)/%.symtypes : $(src)/%.c FORCE
64e6c1e1 141 $(call cmd,cc_symtypes_c)
15fde675 142
433db3e2
VT
143# LLVM assembly
144# Generate .ll files from .c
145quiet_cmd_cc_ll_c = CC $(quiet_modtag) $@
146 cmd_cc_ll_c = $(CC) $(c_flags) -emit-llvm -S -o $@ $<
147
148$(obj)/%.ll: $(src)/%.c FORCE
149 $(call if_changed_dep,cc_ll_c)
150
1da177e4
LT
151# C (.c) files
152# The C file is compiled and updated dependency information is generated.
153# (See cmd_cc_o_c + relevant part of rule_cc_o_c)
154
155quiet_cmd_cc_o_c = CC $(quiet_modtag) $@
92594d56 156 cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $< $(cmd_objtool)
1da177e4 157
ee3e46b7 158ifdef CONFIG_MODVERSIONS
1da177e4 159# When module versioning is enabled the following steps are executed:
ee3e46b7
MY
160# o compile a <file>.o from <file>.c
161# o if <file>.o doesn't contain a __ksymtab version, i.e. does
162# not export symbols, it's done.
1da177e4
LT
163# o otherwise, we calculate symbol versions using the good old
164# genksyms on the preprocessed source and postprocess them in a way
2ea03891 165# that they are usable as a linker script
ee3e46b7 166# o generate .tmp_<file>.o from <file>.o using the linker to
2ea03891
SR
167# replace the unresolved symbols __crc_exported_symbol with
168# the actual value of the checksum generated by genksyms
ee3e46b7 169# o remove .tmp_<file>.o to <file>.o
4efca4ed 170
38e89184
ST
171ifdef CONFIG_LTO_CLANG
172# Generate .o.symversions files for each .o with exported symbols, and link these
173# to the kernel and/or modules at the end.
174cmd_modversions_c = \
175 if $(NM) $@ 2>/dev/null | grep -q __ksymtab; then \
176 $(call cmd_gensymtypes_c,$(KBUILD_SYMTYPES),$(@:.o=.symtypes)) \
177 > $@.symversions; \
a8390ba9
MY
178 else \
179 rm -f $@.symversions; \
38e89184
ST
180 fi;
181else
4efca4ed 182cmd_modversions_c = \
ee3e46b7 183 if $(OBJDUMP) -h $@ | grep -q __ksymtab; then \
4efca4ed 184 $(call cmd_gensymtypes_c,$(KBUILD_SYMTYPES),$(@:.o=.symtypes)) \
e26d6b83
DZ
185 > $(@D)/.tmp_$(@F:.o=.ver); \
186 \
ee3e46b7 187 $(LD) $(KBUILD_LDFLAGS) -r -o $(@D)/.tmp_$(@F) $@ \
e26d6b83 188 -T $(@D)/.tmp_$(@F:.o=.ver); \
e26d6b83 189 mv -f $(@D)/.tmp_$(@F) $@; \
ee3e46b7 190 rm -f $(@D)/.tmp_$(@F:.o=.ver); \
e5d28910 191 fi
1da177e4 192endif
38e89184 193endif
1da177e4 194
3b15cdc1 195ifdef CONFIG_FTRACE_MCOUNT_USE_RECORDMCOUNT
07d04081 196# compiler will not generate __mcount_loc use recordmcount or recordmcount.pl
72441cb1 197ifdef BUILD_C_RECORDMCOUNT
85356f80
SR
198ifeq ("$(origin RECORDMCOUNT_WARN)", "command line")
199 RECORDMCOUNT_FLAGS = -w
200endif
d7b4d6de
SR
201# Due to recursion, we must skip empty.o.
202# The empty.o file is created in the make process in order to determine
312a3d09
C
203# the target endianness and word size. It is made before all other C
204# files, including recordmcount.
45677454
WZ
205sub_cmd_record_mcount = \
206 if [ $(@) != "scripts/mod/empty.o" ]; then \
85356f80 207 $(objtree)/scripts/recordmcount $(RECORDMCOUNT_FLAGS) "$(@)"; \
45677454 208 fi;
d6971822
MM
209recordmcount_source := $(srctree)/scripts/recordmcount.c \
210 $(srctree)/scripts/recordmcount.h
72441cb1 211else
4317ee3b 212sub_cmd_record_mcount = perl $(srctree)/scripts/recordmcount.pl "$(ARCH)" \
e6299d26 213 "$(if $(CONFIG_CPU_BIG_ENDIAN),big,little)" \
b3acf29a 214 "$(if $(CONFIG_64BIT),64,32)" \
5a4630aa 215 "$(OBJDUMP)" "$(OBJCOPY)" "$(CC) $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS)" \
d503ac53 216 "$(LD) $(KBUILD_LDFLAGS)" "$(NM)" "$(RM)" "$(MV)" \
18c167fd 217 "$(if $(part-of-module),1,0)" "$(@)";
d6971822 218recordmcount_source := $(srctree)/scripts/recordmcount.pl
312a3d09 219endif # BUILD_C_RECORDMCOUNT
1a49b2fd
JL
220cmd_record_mcount = $(if $(findstring $(strip $(CC_FLAGS_FTRACE)),$(_c_flags)), \
221 $(sub_cmd_record_mcount))
3b15cdc1 222endif # CONFIG_FTRACE_MCOUNT_USE_RECORDMCOUNT
8da3821b 223
b9ab5ebb 224ifdef CONFIG_STACK_VALIDATION
8f0c32c7 225
5c4859e7
MY
226objtool := $(objtree)/tools/objtool/objtool
227
8f0c32c7
MY
228objtool_args = \
229 $(if $(CONFIG_UNWINDER_ORC),orc generate,check) \
230 $(if $(part-of-module), --module) \
ed53a0d9 231 $(if $(CONFIG_X86_KERNEL_IBT), --lto --ibt) \
8f0c32c7
MY
232 $(if $(CONFIG_FRAME_POINTER),, --no-fp) \
233 $(if $(CONFIG_GCOV_KERNEL)$(CONFIG_LTO_CLANG), --no-unreachable)\
234 $(if $(CONFIG_RETPOLINE), --retpoline) \
235 $(if $(CONFIG_X86_SMAP), --uaccess) \
e463a09a
PZ
236 $(if $(CONFIG_FTRACE_MCOUNT_USE_OBJTOOL), --mcount) \
237 $(if $(CONFIG_SLS), --sls)
8f0c32c7 238
d31ed5d7
PZ
239cmd_objtool = $(if $(objtool-enabled), ; $(objtool) $(objtool_args) $@)
240cmd_gen_objtooldep = $(if $(objtool-enabled), { echo ; echo '$@: $$(wildcard $(objtool))' ; } >> $(dot-target).cmd)
90a35349
MY
241
242endif # CONFIG_STACK_VALIDATION
243
d31ed5d7 244ifneq ($(CONFIG_LTO_CLANG)$(CONFIG_X86_KERNEL_IBT),)
90a35349
MY
245
246# Skip objtool for LLVM bitcode
247$(obj)/%.o: objtool-enabled :=
248
249else
b9ab5ebb 250
b9ab5ebb
JP
251# 'OBJECT_FILES_NON_STANDARD := y': skip objtool checking for a directory
252# 'OBJECT_FILES_NON_STANDARD_foo.o := 'y': skip objtool checking for a file
253# 'OBJECT_FILES_NON_STANDARD_foo.o := 'n': override directory skip for a file
918a6b7f 254
90a35349 255$(obj)/%.o: objtool-enabled = $(if $(filter-out y%, \
918a6b7f
MY
256 $(OBJECT_FILES_NON_STANDARD_$(basetarget).o)$(OBJECT_FILES_NON_STANDARD)n),y)
257
90a35349 258endif
b9ab5ebb 259
bbda5ec6
MY
260ifdef CONFIG_TRIM_UNUSED_KSYMS
261cmd_gen_ksymdeps = \
e5d28910 262 $(CONFIG_SHELL) $(srctree)/scripts/gen_ksymdeps.sh $@ >> $(dot-target).cmd
7d32358b
MY
263
264# List module undefined symbols
265undefined_syms = $(NM) $< | $(AWK) '$$1 == "U" { printf("%s%s", x++ ? " " : "", $$2) }';
bbda5ec6
MY
266endif
267
1da177e4 268define rule_cc_o_c
3a2429e1
MY
269 $(call cmd_and_fixdep,cc_o_c)
270 $(call cmd,gen_ksymdeps)
0c33f125 271 $(call cmd,checksrc)
3a2429e1 272 $(call cmd,checkdoc)
ef62588c 273 $(call cmd,gen_objtooldep)
3a2429e1
MY
274 $(call cmd,modversions_c)
275 $(call cmd,record_mcount)
1da177e4
LT
276endef
277
b9ab5ebb 278define rule_as_o_S
3a2429e1
MY
279 $(call cmd_and_fixdep,as_o_S)
280 $(call cmd,gen_ksymdeps)
ef62588c 281 $(call cmd,gen_objtooldep)
3a2429e1 282 $(call cmd,modversions_S)
b9ab5ebb
JP
283endef
284
1da177e4 285# Built-in and composite module parts
ef62588c 286$(obj)/%.o: $(src)/%.c $(recordmcount_source) FORCE
1da177e4 287 $(call if_changed_rule,cc_o_c)
0c33f125 288 $(call cmd,force_checksrc)
1da177e4 289
d31ed5d7 290ifneq ($(CONFIG_LTO_CLANG)$(CONFIG_X86_KERNEL_IBT),)
850ded46
ST
291# Module .o files may contain LLVM bitcode, compile them into native code
292# before ELF processing
d31ed5d7
PZ
293quiet_cmd_cc_prelink_modules = LD [M] $@
294 cmd_cc_prelink_modules = \
850ded46 295 $(LD) $(ld_flags) -r -o $@ \
d31ed5d7
PZ
296 $(shell [ -s $(@:.prelink.o=.o.symversions) ] && \
297 echo -T $(@:.prelink.o=.o.symversions)) \
90a35349
MY
298 --whole-archive $(filter-out FORCE,$^) \
299 $(cmd_objtool)
850ded46 300
d31ed5d7
PZ
301# objtool was skipped for LLVM bitcode, run it now that we have compiled
302# modules into native code
303$(obj)/%.prelink.o: objtool-enabled = y
304$(obj)/%.prelink.o: part-of-module := y
305
306$(obj)/%.prelink.o: $(obj)/%.o FORCE
307 $(call if_changed,cc_prelink_modules)
850ded46
ST
308endif
309
9f69a496
MY
310cmd_mod = { \
311 echo $(if $($*-objs)$($*-y)$($*-m), $(addprefix $(obj)/, $($*-objs) $($*-y) $($*-m)), $(@:.mod=.o)); \
7d32358b 312 $(undefined_syms) echo; \
9f69a496
MY
313 } > $@
314
850ded46 315$(obj)/%.mod: $(obj)/%$(mod-prelink-ext).o FORCE
9f69a496
MY
316 $(call if_changed,mod)
317
1da177e4
LT
318quiet_cmd_cc_lst_c = MKLST $@
319 cmd_cc_lst_c = $(CC) $(c_flags) -g -c -o $*.o $< && \
320 $(CONFIG_SHELL) $(srctree)/scripts/makelst $*.o \
321 System.map $(OBJDUMP) > $@
322
767e581d 323$(obj)/%.lst: $(src)/%.c FORCE
1da177e4
LT
324 $(call if_changed_dep,cc_lst_c)
325
326# Compile assembler sources (.S)
327# ---------------------------------------------------------------------------
328
4efca4ed
NP
329# .S file exports must have their C prototypes defined in asm/asm-prototypes.h
330# or a file that it includes, in order to get versioned symbols. We build a
331# dummy C file that includes asm-prototypes and the EXPORT_SYMBOL lines from
332# the .S file (with trailing ';'), and run genksyms on that, to extract vers.
333#
334# This is convoluted. The .S file must first be preprocessed to run guards and
335# expand names, then the resulting exports must be constructed into plain
336# EXPORT_SYMBOL(symbol); to build our dummy C file, and that gets preprocessed
337# to make the genksyms input.
338#
339# These mirror gensymtypes_c and co above, keep them in synch.
340cmd_gensymtypes_S = \
b79c6aa6 341 { echo "\#include <linux/kernel.h>" ; \
4efca4ed
NP
342 echo "\#include <asm/asm-prototypes.h>" ; \
343 $(CPP) $(a_flags) $< | \
cc6acc11 344 grep "\<___EXPORT_SYMBOL\>" | \
b79c6aa6 345 sed 's/.*___EXPORT_SYMBOL[[:space:]]*\([a-zA-Z0-9_]*\)[[:space:]]*,.*/EXPORT_SYMBOL(\1);/' ; } | \
f43e31d5 346 $(CPP) -D__GENKSYMS__ $(c_flags) -xc - | $(genksyms)
4efca4ed
NP
347
348quiet_cmd_cc_symtypes_S = SYM $(quiet_modtag) $@
dc6dc3e7 349 cmd_cc_symtypes_S = $(call cmd_gensymtypes_S,true,$@) >/dev/null
4efca4ed
NP
350
351$(obj)/%.symtypes : $(src)/%.S FORCE
352 $(call cmd,cc_symtypes_S)
353
354
e0f41e52
MY
355quiet_cmd_cpp_s_S = CPP $(quiet_modtag) $@
356cmd_cpp_s_S = $(CPP) $(a_flags) -o $@ $<
1da177e4 357
767e581d 358$(obj)/%.s: $(src)/%.S FORCE
e0f41e52 359 $(call if_changed_dep,cpp_s_S)
1da177e4
LT
360
361quiet_cmd_as_o_S = AS $(quiet_modtag) $@
92594d56 362 cmd_as_o_S = $(CC) $(a_flags) -c -o $@ $< $(cmd_objtool)
4efca4ed 363
2ff2b7ec 364ifdef CONFIG_ASM_MODVERSIONS
4efca4ed
NP
365
366# versioning matches the C process described above, with difference that
367# we parse asm-prototypes.h C header to get function definitions.
368
4efca4ed 369cmd_modversions_S = \
ee3e46b7 370 if $(OBJDUMP) -h $@ | grep -q __ksymtab; then \
4efca4ed
NP
371 $(call cmd_gensymtypes_S,$(KBUILD_SYMTYPES),$(@:.o=.symtypes)) \
372 > $(@D)/.tmp_$(@F:.o=.ver); \
373 \
ee3e46b7 374 $(LD) $(KBUILD_LDFLAGS) -r -o $(@D)/.tmp_$(@F) $@ \
4efca4ed 375 -T $(@D)/.tmp_$(@F:.o=.ver); \
4efca4ed 376 mv -f $(@D)/.tmp_$(@F) $@; \
ee3e46b7 377 rm -f $(@D)/.tmp_$(@F:.o=.ver); \
e5d28910 378 fi
4efca4ed 379endif
1da177e4 380
ef62588c 381$(obj)/%.o: $(src)/%.S FORCE
b9ab5ebb 382 $(call if_changed_rule,as_o_S)
1da177e4 383
b2c88554
MY
384targets += $(filter-out $(subdir-builtin), $(real-obj-y))
385targets += $(filter-out $(subdir-modorder), $(real-obj-m))
bcf0c664 386targets += $(real-dtb-y) $(lib-y) $(always-y) $(MAKECMDGOALS)
1da177e4
LT
387
388# Linker scripts preprocessor (.lds.S -> .lds)
389# ---------------------------------------------------------------------------
390quiet_cmd_cpp_lds_S = LDS $@
5cb0512c 391 cmd_cpp_lds_S = $(CPP) $(cpp_flags) -P -U$(ARCH) \
42f29a25 392 -D__ASSEMBLY__ -DLINKER_SCRIPT -o $@ $<
1da177e4 393
767e581d 394$(obj)/%.lds: $(src)/%.lds.S FORCE
1da177e4
LT
395 $(call if_changed_dep,cpp_lds_S)
396
4520c6a4
DH
397# ASN.1 grammar
398# ---------------------------------------------------------------------------
49d5089d 399quiet_cmd_asn1_compiler = ASN.1 $(basename $@).[ch]
4520c6a4 400 cmd_asn1_compiler = $(objtree)/scripts/asn1_compiler $< \
49d5089d 401 $(basename $@).c $(basename $@).h
4520c6a4 402
4fa8bc94 403$(obj)/%.asn1.c $(obj)/%.asn1.h: $(src)/%.asn1 $(objtree)/scripts/asn1_compiler
4520c6a4
DH
404 $(call cmd,asn1_compiler)
405
1da177e4
LT
406# Build the compiled-in targets
407# ---------------------------------------------------------------------------
408
409# To build objects in subdirs, we need to descend into the directories
aaa385ba 410$(subdir-builtin): $(obj)/%/built-in.a: $(obj)/% ;
454753d9 411$(subdir-modorder): $(obj)/%/modules.order: $(obj)/% ;
1da177e4 412
38e89184 413# combine symversions for later processing
38e89184
ST
414ifeq ($(CONFIG_LTO_CLANG) $(CONFIG_MODVERSIONS),y y)
415 cmd_update_lto_symversions = \
416 rm -f $@.symversions \
417 $(foreach n, $(filter-out FORCE,$^), \
1d11053d 418 $(if $(shell test -s $(n).symversions && echo y), \
38e89184
ST
419 ; cat $(n).symversions >> $@.symversions))
420else
421 cmd_update_lto_symversions = echo >/dev/null
422endif
423
1da177e4 424#
dee94953 425# Rule to compile a set of .o files into one .a file (without symbol table)
1da177e4 426#
a5967db9 427
5e18f029 428quiet_cmd_ar_builtin = AR $@
13dc8c02 429 cmd_ar_builtin = rm -f $@; $(AR) cDPrST $@ $(real-prereqs)
1da177e4 430
38e89184
ST
431quiet_cmd_ar_and_symver = AR $@
432 cmd_ar_and_symver = $(cmd_update_lto_symversions); $(cmd_ar_builtin)
433
e578edc7 434$(obj)/built-in.a: $(real-obj-y) FORCE
38e89184 435 $(call if_changed,ar_and_symver)
1da177e4 436
551559e1
TH
437#
438# Rule to create modules.order file
439#
440# Create commands to either record .ko file or cat modules.order from
441# a subdirectory
b2c88554
MY
442# Add $(obj-m) as the prerequisite to avoid updating the timestamp of
443# modules.order unless contained modules are updated.
444
445cmd_modules_order = { $(foreach m, $(real-prereqs), \
446 $(if $(filter %/modules.order, $m), cat $m, echo $(patsubst %.o,%.ko,$m));) :; } \
e0e1b1ec 447 | $(AWK) '!x[$$0]++' - > $@
551559e1 448
b2c88554
MY
449$(obj)/modules.order: $(obj-m) FORCE
450 $(call if_changed,modules_order)
451
1da177e4 452#
dee94953 453# Rule to compile a set of .o files into one .a file (with symbol table)
1da177e4 454#
38e89184
ST
455quiet_cmd_ar_lib = AR $@
456 cmd_ar_lib = $(cmd_update_lto_symversions); $(cmd_ar)
457
e578edc7 458$(obj)/lib.a: $(lib-y) FORCE
38e89184 459 $(call if_changed,ar_lib)
1da177e4 460
afa974b7
MY
461# NOTE:
462# Do not replace $(filter %.o,^) with $(real-prereqs). When a single object
463# module is turned into a multi object module, $^ will contain header file
464# dependencies recorded in the .*.cmd file.
d31ed5d7 465ifneq ($(CONFIG_LTO_CLANG)$(CONFIG_X86_KERNEL_IBT),)
dc5723b0
ST
466quiet_cmd_link_multi-m = AR [M] $@
467cmd_link_multi-m = \
38e89184 468 $(cmd_update_lto_symversions); \
dc5723b0
ST
469 rm -f $@; \
470 $(AR) cDPrsT $@ $(filter %.o,$^)
471else
1da177e4 472quiet_cmd_link_multi-m = LD [M] $@
b7dca6dd 473 cmd_link_multi-m = $(LD) $(ld_flags) -r -o $@ $(filter %.o,$^)
dc5723b0 474endif
1da177e4 475
a6601e01 476$(multi-obj-m): FORCE
1da177e4 477 $(call if_changed,link_multi-m)
a6601e01 478$(call multi_depend, $(multi-obj-m), .o, -objs -y -m)
1da177e4 479
a6601e01 480targets += $(multi-obj-m)
591f6689 481targets := $(filter-out $(PHONY), $(targets))
1da177e4 482
b23d1a24
MY
483# Add intermediate targets:
484# When building objects with specific suffix patterns, add intermediate
485# targets that the final targets are derived from.
486intermediate_targets = $(foreach sfx, $(2), \
487 $(patsubst %$(strip $(1)),%$(sfx), \
488 $(filter %$(strip $(1)), $(targets))))
54a702f7 489# %.asn1.o <- %.asn1.[ch] <- %.asn1
a7f92419 490# %.dtb.o <- %.dtb.S <- %.dtb <- %.dts
b23d1a24
MY
491# %.lex.o <- %.lex.c <- %.l
492# %.tab.o <- %.tab.[ch] <- %.y
54a702f7
MY
493targets += $(call intermediate_targets, .asn1.o, .asn1.c .asn1.h) \
494 $(call intermediate_targets, .dtb.o, .dtb.S .dtb) \
a7f92419 495 $(call intermediate_targets, .lex.o, .lex.c) \
b23d1a24
MY
496 $(call intermediate_targets, .tab.o, .tab.c .tab.h)
497
394053f4
MY
498# Build
499# ---------------------------------------------------------------------------
500
501ifdef single-build
502
20312629
MY
503KBUILD_SINGLE_TARGETS := $(filter $(obj)/%, $(KBUILD_SINGLE_TARGETS))
504
394053f4
MY
505curdir-single := $(sort $(foreach x, $(KBUILD_SINGLE_TARGETS), \
506 $(if $(filter $(x) $(basename $(x)).o, $(targets)), $(x))))
507
508# Handle single targets without any rule: show "Nothing to be done for ..." or
509# "No rule to make target ..." depending on whether the target exists.
510unknown-single := $(filter-out $(addsuffix /%, $(subdir-ym)), \
20312629 511 $(filter-out $(curdir-single), $(KBUILD_SINGLE_TARGETS)))
394053f4 512
2dffd23f
MY
513single-subdirs := $(foreach d, $(subdir-ym), \
514 $(if $(filter $(d)/%, $(KBUILD_SINGLE_TARGETS)), $(d)))
515
516__build: $(curdir-single) $(single-subdirs)
394053f4
MY
517ifneq ($(unknown-single),)
518 $(Q)$(MAKE) -f /dev/null $(unknown-single)
519endif
520 @:
521
522ifeq ($(curdir-single),)
523# Nothing to do in this directory. Do not include any .*.cmd file for speed-up
524targets :=
525else
526targets += $(curdir-single)
527endif
528
529else
530
f3908ab3
MY
531__build: $(if $(KBUILD_BUILTIN), $(targets-for-builtin)) \
532 $(if $(KBUILD_MODULES), $(targets-for-modules)) \
5f2fb52f 533 $(subdir-ym) $(always-y)
394053f4
MY
534 @:
535
536endif
537
1da177e4
LT
538# Descending
539# ---------------------------------------------------------------------------
540
4f193362 541PHONY += $(subdir-ym)
1da177e4 542$(subdir-ym):
d9f78edf 543 $(Q)$(MAKE) $(build)=$@ \
394053f4 544 $(if $(filter $@/, $(KBUILD_SINGLE_TARGETS)),single-build=) \
aaa385ba 545 need-builtin=$(if $(filter $@/built-in.a, $(subdir-builtin)),1) \
454753d9 546 need-modorder=$(if $(filter $@/modules.order, $(subdir-modorder)),1)
1da177e4
LT
547
548# Add FORCE to the prequisites of a target to force it to be always rebuilt.
549# ---------------------------------------------------------------------------
550
4f193362 551PHONY += FORCE
1da177e4
LT
552
553FORCE:
554
555# Read all saved command lines and dependencies for the $(targets) we
556# may be building above, using $(if_changed{,_dep}). As an
557# optimization, we don't need to read them if the target does not
558# exist, we will rebuild anyway in that case.
559
392885ee 560existing-targets := $(wildcard $(sort $(targets)))
1da177e4 561
392885ee 562-include $(foreach f,$(existing-targets),$(dir $(f)).$(notdir $(f)).cmd)
4f193362 563
8a78756e 564# Create directories for object files if they do not exist
cc8a51ca 565obj-dirs := $(sort $(patsubst %/,%, $(dir $(targets))))
392885ee
MY
566# If targets exist, their directories apparently exist. Skip mkdir.
567existing-dirs := $(sort $(patsubst %/,%, $(dir $(existing-targets))))
568obj-dirs := $(strip $(filter-out $(existing-dirs), $(obj-dirs)))
c4da7ed0 569ifneq ($(obj-dirs),)
8a78756e
MY
570$(shell mkdir -p $(obj-dirs))
571endif
572
4f193362 573.PHONY: $(PHONY)