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