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