blob: 998679f00a0db5b8dfe8d32707ff674aa6391e74 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001# SPDX-License-Identifier: GPL-2.0
Masahiro Yamada47b65f72014-02-04 17:24:27 +09002# ==========================================================================
3# Building
4# ==========================================================================
5
Masahiro Yamada0d68fb12014-02-04 17:24:28 +09006# Modified for U-Boot
Simon Glass7eec8842022-04-30 00:56:52 -06007prefix := vpl
8src := $(patsubst $(prefix)/%,%,$(obj))
9ifeq ($(obj),$(src))
Masahiro Yamada60bb7342014-07-30 14:08:17 +090010prefix := tpl
11src := $(patsubst $(prefix)/%,%,$(obj))
12ifeq ($(obj),$(src))
13prefix := spl
14src := $(patsubst $(prefix)/%,%,$(obj))
15ifeq ($(obj),$(src))
16prefix := .
17endif
Masahiro Yamada0d68fb12014-02-04 17:24:28 +090018endif
Simon Glass7eec8842022-04-30 00:56:52 -060019endif
Masahiro Yamada47b65f72014-02-04 17:24:27 +090020
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
Masahiro Yamada60bb7342014-07-30 14:08:17 +090048# Modified for U-Boot
Masahiro Yamada72fb32b2015-02-24 22:26:20 +090049-include include/config/auto.conf
Masahiro Yamada60bb7342014-07-30 14:08:17 +090050-include $(prefix)/include/autoconf.mk
Masahiro Yamada5ac303c2014-02-04 17:24:32 +090051
Masahiro Yamada47b65f72014-02-04 17:24:27 +090052include scripts/Kbuild.include
53
Masahiro Yamada47b65f72014-02-04 17:24:27 +090054# 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
Masahiro Yamada958fe8f2014-03-05 16:59:40 +090059# Added for U-Boot
60asflags-y += $(PLATFORM_CPPFLAGS)
61ccflags-y += $(PLATFORM_CPPFLAGS)
62cppflags-y += $(PLATFORM_CPPFLAGS)
63
Masahiro Yamada47b65f72014-02-04 17:24:27 +090064include scripts/Makefile.lib
65
Masahiro Yamada47b65f72014-02-04 17:24:27 +090066# Do not include host rules unless needed
Tom Rini5d4ecf22020-03-27 11:46:27 -040067ifneq ($(hostprogs-y)$(hostprogs-m)$(hostlibs-y)$(hostlibs-m)$(hostcxxlibs-y)$(hostcxxlibs-m),)
Masahiro Yamada47b65f72014-02-04 17:24:27 +090068include scripts/Makefile.host
69endif
70
Masahiro Yamada47b65f72014-02-04 17:24:27 +090071ifndef obj
72$(warning kbuild: Makefile.build is included improperly)
73endif
74
75# ===========================================================================
76
Masahiro Yamadaede865b2014-10-30 11:06:13 +090077ifneq ($(strip $(lib-y) $(lib-m) $(lib-)),)
Masahiro Yamada47b65f72014-02-04 17:24:27 +090078lib-target := $(obj)/lib.a
Ilias Apalodimas7860ad32025-06-27 21:57:16 +030079# Modified for U-Boot
80# real-obj-y += $(obj)/lib-ksyms.o
Masahiro Yamada47b65f72014-02-04 17:24:27 +090081endif
82
Ilias Apalodimas7860ad32025-06-27 21:57:16 +030083ifneq ($(strip $(real-obj-y) $(need-builtin)),)
84builtin-target := $(obj)/built-in.a
Masahiro Yamada47b65f72014-02-04 17:24:27 +090085endif
86
Ilias Apalodimasfb3b8c82025-05-20 08:21:30 +030087ifdef CONFIG_MODULES
Masahiro Yamada47b65f72014-02-04 17:24:27 +090088modorder-target := $(obj)/modules.order
Ilias Apalodimasfb3b8c82025-05-20 08:21:30 +030089endif
Masahiro Yamada47b65f72014-02-04 17:24:27 +090090
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
Tom Rini50800632021-06-17 18:07:25 -040099ifeq ($(KBUILD_CHECKSRC),1)
100 quiet_cmd_checksrc = CHECK $<
Ilias Apalodimas7e3e56c2025-05-20 08:21:26 +0300101 cmd_checksrc = $(CHECK) $(CHECKFLAGS) $(c_flags) $<
Tom Rini50800632021-06-17 18:07:25 -0400102else ifeq ($(KBUILD_CHECKSRC),2)
103 quiet_cmd_force_checksrc = CHECK $<
Ilias Apalodimas7e3e56c2025-05-20 08:21:26 +0300104 cmd_force_checksrc = $(CHECK) $(CHECKFLAGS) $(c_flags) $<
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900105endif
106
Ilias Apalodimas7860ad32025-06-27 21:57:16 +0300107ifneq ($(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
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900112ifdef 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
Ilias Apalodimas7860ad32025-06-27 21:57:16 +0300120$(foreach x, i ll lst o s symtypes, $(patsubst %.o,%.$(x),$(real-obj-m))): \
121 part-of-module := y
122
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900123modkern_cflags = \
124 $(if $(part-of-module), \
125 $(KBUILD_CFLAGS_MODULE) $(CFLAGS_MODULE), \
126 $(KBUILD_CFLAGS_KERNEL) $(CFLAGS_KERNEL))
Ilias Apalodimasfa378002025-05-20 08:21:29 +0300127quiet_modtag = $(if $(part-of-module),[M], )
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900128
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900129quiet_cmd_cc_s_c = CC $(quiet_modtag) $@
Ilias Apalodimas7860ad32025-06-27 21:57:16 +0300130 cmd_cc_s_c = $(CC) $(filter-out $(DEBUG_CFLAGS), $(c_flags)) $(DISABLE_LTO) -fverbose-asm -S -o $@ $<
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900131
132$(obj)/%.s: $(src)/%.c FORCE
133 $(call if_changed_dep,cc_s_c)
134
Ilias Apalodimas7860ad32025-06-27 21:57:16 +0300135quiet_cmd_cpp_i_c = CPP $(quiet_modtag) $@
136cmd_cpp_i_c = $(CPP) $(c_flags) -o $@ $<
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900137
138$(obj)/%.i: $(src)/%.c FORCE
Ilias Apalodimas7860ad32025-06-27 21:57:16 +0300139 $(call if_changed_dep,cpp_i_c)
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900140
Ilias Apalodimas7860ad32025-06-27 21:57:16 +0300141# These mirror gensymtypes_S and co below, keep them in synch.
142cmd_gensymtypes_c = \
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900143 $(CPP) -D__GENKSYMS__ $(c_flags) $< | \
Ilias Apalodimas7860ad32025-06-27 21:57:16 +0300144 scripts/genksyms/genksyms $(if $(1), -T $(2)) \
145 $(patsubst y,-R,$(CONFIG_MODULE_REL_CRCS)) \
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900146 $(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 = \
Ilias Apalodimas7860ad32025-06-27 21:57:16 +0300151 $(call cmd_gensymtypes_c,true,$@) >/dev/null; \
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900152 test -s $@ || rm -f $@
153
154$(obj)/%.symtypes : $(src)/%.c FORCE
155 $(call cmd,cc_symtypes_c)
156
Ilias Apalodimas7860ad32025-06-27 21:57:16 +0300157# 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
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900165# 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) $@
Ilias Apalodimas7860ad32025-06-27 21:57:16 +0300170 cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $<
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900171
Ilias Apalodimas0785df02025-05-20 08:21:24 +0300172ifdef CONFIG_MODVERSIONS
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900173# When module versioning is enabled the following steps are executed:
Ilias Apalodimas0785df02025-05-20 08:21:24 +0300174# o compile a <file>.o from <file>.c
175# o if <file>.o doesn't contain a __ksymtab version, i.e. does
Ilias Apalodimas7860ad32025-06-27 21:57:16 +0300176# not export symbols, it's done.
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900177# 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
Ilias Apalodimas0785df02025-05-20 08:21:24 +0300180# o generate .tmp_<file>.o from <file>.o using the linker to
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900181# replace the unresolved symbols __crc_exported_symbol with
182# the actual value of the checksum generated by genksyms
Ilias Apalodimas0785df02025-05-20 08:21:24 +0300183# o remove .tmp_<file>.o to <file>.o
Ilias Apalodimas7860ad32025-06-27 21:57:16 +0300184
185cmd_modversions_c = \
Ilias Apalodimas0785df02025-05-20 08:21:24 +0300186 if $(OBJDUMP) -h $@ | grep -q __ksymtab; then \
Ilias Apalodimas7860ad32025-06-27 21:57:16 +0300187 $(call cmd_gensymtypes_c,$(KBUILD_SYMTYPES),$(@:.o=.symtypes)) \
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900188 > $(@D)/.tmp_$(@F:.o=.ver); \
189 \
Ilias Apalodimas7860ad32025-06-27 21:57:16 +0300190 $(LD) $(KBUILD_LDFLAGS) -r -o $(@D)/.tmp_$(@F) $@ \
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900191 -T $(@D)/.tmp_$(@F:.o=.ver); \
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900192 mv -f $(@D)/.tmp_$(@F) $@; \
Ilias Apalodimas0785df02025-05-20 08:21:24 +0300193 rm -f $(@D)/.tmp_$(@F:.o=.ver); \
Ilias Apalodimas7860ad32025-06-27 21:57:16 +0300194 fi
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900195endif
196
197ifdef CONFIG_FTRACE_MCOUNT_RECORD
Ilias Apalodimas7860ad32025-06-27 21:57:16 +0300198ifndef CC_USING_RECORD_MCOUNT
199# compiler will not generate __mcount_loc use recordmcount or recordmcount.pl
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900200ifdef 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
Ilias Apalodimas7860ad32025-06-27 21:57:16 +0300206# the target endianness and word size. It is made before all other C
207# files, including recordmcount.
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900208sub_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
Ilias Apalodimas7860ad32025-06-27 21:57:16 +0300215sub_cmd_record_mcount = perl $(srctree)/scripts/recordmcount.pl "$(ARCH)" \
Jiaxun Yang0803a272024-07-17 16:07:03 +0800216 "$(if $(CONFIG_SYS_BIG_ENDIAN),big,little)" \
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900217 "$(if $(CONFIG_64BIT),64,32)" \
Tom Rini5d4ecf22020-03-27 11:46:27 -0400218 "$(OBJDUMP)" "$(OBJCOPY)" "$(CC) $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS)" \
219 "$(LD) $(KBUILD_LDFLAGS)" "$(NM)" "$(RM)" "$(MV)" \
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900220 "$(if $(part-of-module),1,0)" "$(@)";
221recordmcount_source := $(srctree)/scripts/recordmcount.pl
Ilias Apalodimas7860ad32025-06-27 21:57:16 +0300222endif # 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
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900242endif
Ilias Apalodimas7860ad32025-06-27 21:57:16 +0300243ifdef 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
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900268endif
269
270define rule_cc_o_c
Ilias Apalodimas25c46de2025-05-20 08:21:25 +0300271 $(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)
Ilias Apalodimas342cc872025-05-20 08:21:12 +0300278endef
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)
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900285endef
286
Ilias Apalodimas7860ad32025-06-27 21:57:16 +0300287# 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
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900294# Built-in and composite module parts
Ilias Apalodimas7860ad32025-06-27 21:57:16 +0300295$(obj)/%.o: $(src)/%.c $(recordmcount_source) $(objtool_dep) FORCE
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900296 $(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
Ilias Apalodimas7860ad32025-06-27 21:57:16 +0300301$(single-used-m): $(obj)/%.o: $(src)/%.c $(recordmcount_source) $(objtool_dep) FORCE
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900302 $(call cmd,force_checksrc)
303 $(call if_changed_rule,cc_o_c)
Ilias Apalodimas7860ad32025-06-27 21:57:16 +0300304 @{ echo $(@:.o=.ko); echo $@; \
305 $(cmd_undef_syms); } > $(MODVERDIR)/$(@F:.o=.mod)
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900306
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
Ilias Apalodimas7860ad32025-06-27 21:57:16 +0300320$(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
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900354
Ilias Apalodimas7860ad32025-06-27 21:57:16 +0300355quiet_cmd_cpp_s_S = CPP $(quiet_modtag) $@
356cmd_cpp_s_S = $(CPP) $(a_flags) -o $@ $<
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900357
358$(obj)/%.s: $(src)/%.S FORCE
Ilias Apalodimas7860ad32025-06-27 21:57:16 +0300359 $(call if_changed_dep,cpp_s_S)
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900360
361quiet_cmd_as_o_S = AS $(quiet_modtag) $@
Ilias Apalodimas7860ad32025-06-27 21:57:16 +0300362 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
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900385
Ilias Apalodimas7860ad32025-06-27 21:57:16 +0300386$(obj)/%.o: $(src)/%.S $(objtool_dep) FORCE
387 $(call if_changed_rule,as_o_S)
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900388
Ilias Apalodimas7860ad32025-06-27 21:57:16 +0300389targets += $(filter-out $(subdir-obj-y), $(real-obj-y)) $(real-obj-m) $(lib-y)
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900390targets += $(extra-y) $(MAKECMDGOALS) $(always)
Prasad Kummaricd91adc2024-09-06 12:38:07 +0530391targets += $(real-dtb-y) $(lib-y) $(always-y)
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900392
393# Linker scripts preprocessor (.lds.S -> .lds)
394# ---------------------------------------------------------------------------
395quiet_cmd_cpp_lds_S = LDS $@
Ilias Apalodimas7860ad32025-06-27 21:57:16 +0300396 cmd_cpp_lds_S = $(CPP) $(cpp_flags) -P -U$(ARCH) \
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900397 -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 $@
AKASHI Takahirof7025262019-11-13 09:44:53 +0900405 cmd_asn1_compiler = $(objtree)/tools/asn1_compiler $< \
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900406 $(subst .h,.c,$@) $(subst .c,.h,$@)
407
AKASHI Takahirof7025262019-11-13 09:44:53 +0900408$(obj)/%.asn1.c $(obj)/%.asn1.h: $(src)/%.asn1 $(objtree)/tools/asn1_compiler
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900409 $(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#
Ilias Apalodimas7860ad32025-06-27 21:57:16 +0300418# Rule to compile a set of .o files into one .a file (without symbol table)
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900419#
420ifdef builtin-target
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900421
Ilias Apalodimas7860ad32025-06-27 21:57:16 +0300422quiet_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)
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900429
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#
Ilias Apalodimas7860ad32025-06-27 21:57:16 +0300447# Rule to compile a set of .o files into one .a file (with symbol table)
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900448#
449ifdef lib-target
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900450
451$(lib-target): $(lib-y) FORCE
Ilias Apalodimas367cd3c2025-05-20 08:21:21 +0300452 $(call if_changed,ar)
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900453
454targets += $(lib-target)
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900455
Ilias Apalodimas7860ad32025-06-27 21:57:16 +0300456dummy-object = $(obj)/.lib_exports.o
457ksyms-lds = $(dot-target).lds
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900458
Ilias Apalodimas7860ad32025-06-27 21:57:16 +0300459quiet_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)
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900466
Ilias Apalodimas7860ad32025-06-27 21:57:16 +0300467$(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)
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900480
Masahiro Yamada8c4532f2014-10-30 11:06:10 +0900481$(multi-used-m): FORCE
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900482 $(call if_changed,link_multi-m)
Ilias Apalodimas7860ad32025-06-27 21:57:16 +0300483 @{ 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)
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900486
Ilias Apalodimas7860ad32025-06-27 21:57:16 +0300487targets += $(multi-used-m)
488targets := $(filter-out $(PHONY), $(targets))
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900489
Eugeniu Roscad57dd942018-05-19 14:13:50 +0200490# 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))))
Ilias Apalodimas7860ad32025-06-27 21:57:16 +0300496# %.asn1.o <- %.asn1.[ch] <- %.asn1
497# %.dtb.o <- %.dtb.S <- %.dtb <- %.dts
Eugeniu Roscad57dd942018-05-19 14:13:50 +0200498# %.lex.o <- %.lex.c <- %.l
499# %.tab.o <- %.tab.[ch] <- %.y
Ilias Apalodimas7860ad32025-06-27 21:57:16 +0300500targets += $(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) \
Eugeniu Roscad57dd942018-05-19 14:13:50 +0200503 $(call intermediate_targets, .tab.o, .tab.c .tab.h)
504
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900505# Descending
506# ---------------------------------------------------------------------------
507
508PHONY += $(subdir-ym)
509$(subdir-ym):
Ilias Apalodimas7860ad32025-06-27 21:57:16 +0300510 $(Q)$(MAKE) $(build)=$@ need-builtin=$(if $(findstring $@,$(subdir-obj-y)),1)
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900511
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
Ilias Apalodimas15d82012025-05-20 08:21:22 +0300524existing-targets := $(wildcard $(sort $(targets)))
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900525
Ilias Apalodimas15d82012025-05-20 08:21:22 +0300526-include $(foreach f,$(existing-targets),$(dir $(f)).$(notdir $(f)).cmd)
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900527
Masahiro Yamada77ddac82020-04-17 16:21:36 +0900528# Create directories for object files if they do not exist
529obj-dirs := $(sort $(obj) $(patsubst %/,%, $(dir $(targets))))
Ilias Apalodimas15d82012025-05-20 08:21:22 +0300530# 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),)
Masahiro Yamada77ddac82020-04-17 16:21:36 +0900534$(shell mkdir -p $(obj-dirs))
Ilias Apalodimas15d82012025-05-20 08:21:22 +0300535endif
Masahiro Yamada77ddac82020-04-17 16:21:36 +0900536
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900537.PHONY: $(PHONY)