Merge branch 'next'
[oweals/u-boot.git] / scripts / Makefile.build
1 # SPDX-License-Identifier: GPL-2.0
2 # ==========================================================================
3 # Building
4 # ==========================================================================
5
6 # Modified for U-Boot
7 prefix := tpl
8 src := $(patsubst $(prefix)/%,%,$(obj))
9 ifeq ($(obj),$(src))
10 prefix := spl
11 src := $(patsubst $(prefix)/%,%,$(obj))
12 ifeq ($(obj),$(src))
13 prefix := .
14 endif
15 endif
16
17 PHONY := __build
18 __build:
19
20 # Init all relevant variables used in kbuild files so
21 # 1) they have correct type
22 # 2) they do not inherit any value from the environment
23 obj-y :=
24 obj-m :=
25 lib-y :=
26 lib-m :=
27 always :=
28 targets :=
29 subdir-y :=
30 subdir-m :=
31 EXTRA_AFLAGS   :=
32 EXTRA_CFLAGS   :=
33 EXTRA_CPPFLAGS :=
34 EXTRA_LDFLAGS  :=
35 asflags-y  :=
36 ccflags-y  :=
37 cppflags-y :=
38 ldflags-y  :=
39
40 subdir-asflags-y :=
41 subdir-ccflags-y :=
42
43 # Read auto.conf if it exists, otherwise ignore
44 # Modified for U-Boot
45 -include include/config/auto.conf
46 -include $(prefix)/include/autoconf.mk
47 include scripts/Makefile.uncmd_spl
48
49 include scripts/Kbuild.include
50
51 # For backward compatibility check that these variables do not change
52 save-cflags := $(CFLAGS)
53
54 # The filename Kbuild has precedence over Makefile
55 kbuild-dir := $(if $(filter /%,$(src)),$(src),$(srctree)/$(src))
56 kbuild-file := $(if $(wildcard $(kbuild-dir)/Kbuild),$(kbuild-dir)/Kbuild,$(kbuild-dir)/Makefile)
57 include $(kbuild-file)
58
59 # Added for U-Boot
60 asflags-y  += $(PLATFORM_CPPFLAGS)
61 ccflags-y  += $(PLATFORM_CPPFLAGS)
62 cppflags-y += $(PLATFORM_CPPFLAGS)
63
64 # If the save-* variables changed error out
65 ifeq ($(KBUILD_NOPEDANTIC),)
66         ifneq ("$(save-cflags)","$(CFLAGS)")
67                 $(error CFLAGS was changed in "$(kbuild-file)". Fix it to use ccflags-y)
68         endif
69 endif
70
71 include scripts/Makefile.lib
72
73 # Do not include host rules unless needed
74 ifneq ($(hostprogs-y)$(hostprogs-m)$(hostlibs-y)$(hostlibs-m)$(hostcxxlibs-y)$(hostcxxlibs-m),)
75 include scripts/Makefile.host
76 endif
77
78 # Uncommented for U-Boot
79 #  We need to create output dicrectory for SPL and TPL even for in-tree build
80 #ifneq ($(KBUILD_SRC),)
81 # Create output directory if not already present
82 _dummy := $(shell [ -d $(obj) ] || mkdir -p $(obj))
83
84 # Create directories for object files if directory does not exist
85 # Needed when obj-y := dir/file.o syntax is used
86 _dummy := $(foreach d,$(obj-dirs), $(shell [ -d $(d) ] || mkdir -p $(d)))
87 #endif
88
89 ifndef obj
90 $(warning kbuild: Makefile.build is included improperly)
91 endif
92
93 # ===========================================================================
94
95 ifneq ($(strip $(lib-y) $(lib-m) $(lib-)),)
96 lib-target := $(obj)/lib.a
97 endif
98
99 ifneq ($(strip $(obj-y) $(obj-m) $(obj-) $(subdir-m) $(lib-target)),)
100 builtin-target := $(obj)/built-in.o
101 endif
102
103 modorder-target := $(obj)/modules.order
104
105 # We keep a list of all modules in $(MODVERDIR)
106
107 __build: $(if $(KBUILD_BUILTIN),$(builtin-target) $(lib-target) $(extra-y)) \
108          $(if $(KBUILD_MODULES),$(obj-m) $(modorder-target)) \
109          $(subdir-ym) $(always)
110         @:
111
112 # Linus' kernel sanity checking tool
113 ifneq ($(KBUILD_CHECKSRC),0)
114   ifeq ($(KBUILD_CHECKSRC),2)
115     quiet_cmd_force_checksrc = CHECK   $<
116           cmd_force_checksrc = $(CHECK) $(CHECKFLAGS) $(c_flags) $< ;
117   else
118       quiet_cmd_checksrc     = CHECK   $<
119             cmd_checksrc     = $(CHECK) $(CHECKFLAGS) $(c_flags) $< ;
120   endif
121 endif
122
123 # Do section mismatch analysis for each module/built-in.o
124 ifdef CONFIG_DEBUG_SECTION_MISMATCH
125   cmd_secanalysis = ; scripts/mod/modpost $@
126 endif
127
128 # Compile C sources (.c)
129 # ---------------------------------------------------------------------------
130
131 # Default is built-in, unless we know otherwise
132 modkern_cflags =                                          \
133         $(if $(part-of-module),                           \
134                 $(KBUILD_CFLAGS_MODULE) $(CFLAGS_MODULE), \
135                 $(KBUILD_CFLAGS_KERNEL) $(CFLAGS_KERNEL))
136 quiet_modtag := $(empty)   $(empty)
137
138 $(real-objs-m)        : part-of-module := y
139 $(real-objs-m:.o=.i)  : part-of-module := y
140 $(real-objs-m:.o=.s)  : part-of-module := y
141 $(real-objs-m:.o=.lst): part-of-module := y
142
143 $(real-objs-m)        : quiet_modtag := [M]
144 $(real-objs-m:.o=.i)  : quiet_modtag := [M]
145 $(real-objs-m:.o=.s)  : quiet_modtag := [M]
146 $(real-objs-m:.o=.lst): quiet_modtag := [M]
147
148 $(obj-m)              : quiet_modtag := [M]
149
150 # Default for not multi-part modules
151 modname = $(basetarget)
152
153 $(multi-objs-m)         : modname = $(modname-multi)
154 $(multi-objs-m:.o=.i)   : modname = $(modname-multi)
155 $(multi-objs-m:.o=.s)   : modname = $(modname-multi)
156 $(multi-objs-m:.o=.lst) : modname = $(modname-multi)
157 $(multi-objs-y)         : modname = $(modname-multi)
158 $(multi-objs-y:.o=.i)   : modname = $(modname-multi)
159 $(multi-objs-y:.o=.s)   : modname = $(modname-multi)
160 $(multi-objs-y:.o=.lst) : modname = $(modname-multi)
161
162 quiet_cmd_cc_s_c = CC $(quiet_modtag)  $@
163 cmd_cc_s_c       = $(CC) $(c_flags) $(DISABLE_LTO) -fverbose-asm -S -o $@ $<
164
165 $(obj)/%.s: $(src)/%.c FORCE
166         $(call if_changed_dep,cc_s_c)
167
168 quiet_cmd_cc_i_c = CPP $(quiet_modtag) $@
169 cmd_cc_i_c       = $(CPP) $(c_flags)   -o $@ $<
170
171 $(obj)/%.i: $(src)/%.c FORCE
172         $(call if_changed_dep,cc_i_c)
173
174 cmd_gensymtypes =                                                           \
175     $(CPP) -D__GENKSYMS__ $(c_flags) $< |                                   \
176     $(GENKSYMS) $(if $(1), -T $(2))                                         \
177      $(if $(KBUILD_PRESERVE),-p)                                            \
178      -r $(firstword $(wildcard $(2:.symtypes=.symref) /dev/null))
179
180 quiet_cmd_cc_symtypes_c = SYM $(quiet_modtag) $@
181 cmd_cc_symtypes_c =                                                         \
182     set -e;                                                                 \
183     $(call cmd_gensymtypes,true,$@) >/dev/null;                             \
184     test -s $@ || rm -f $@
185
186 $(obj)/%.symtypes : $(src)/%.c FORCE
187         $(call cmd,cc_symtypes_c)
188
189 # C (.c) files
190 # The C file is compiled and updated dependency information is generated.
191 # (See cmd_cc_o_c + relevant part of rule_cc_o_c)
192
193 quiet_cmd_cc_o_c = CC $(quiet_modtag)  $@
194
195 ifndef CONFIG_MODVERSIONS
196 cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $<
197
198 else
199 # When module versioning is enabled the following steps are executed:
200 # o compile a .tmp_<file>.o from <file>.c
201 # o if .tmp_<file>.o doesn't contain a __ksymtab version, i.e. does
202 #   not export symbols, we just rename .tmp_<file>.o to <file>.o and
203 #   are done.
204 # o otherwise, we calculate symbol versions using the good old
205 #   genksyms on the preprocessed source and postprocess them in a way
206 #   that they are usable as a linker script
207 # o generate <file>.o from .tmp_<file>.o using the linker to
208 #   replace the unresolved symbols __crc_exported_symbol with
209 #   the actual value of the checksum generated by genksyms
210
211 cmd_cc_o_c = $(CC) $(c_flags) -c -o $(@D)/.tmp_$(@F) $<
212 cmd_modversions =                                                               \
213         if $(OBJDUMP) -h $(@D)/.tmp_$(@F) | grep -q __ksymtab; then             \
214                 $(call cmd_gensymtypes,$(KBUILD_SYMTYPES),$(@:.o=.symtypes))    \
215                     > $(@D)/.tmp_$(@F:.o=.ver);                                 \
216                                                                                 \
217                 $(LD) $(KBUILD_LDFLAGS) -r -o $@ $(@D)/.tmp_$(@F)               \
218                         -T $(@D)/.tmp_$(@F:.o=.ver);                            \
219                 rm -f $(@D)/.tmp_$(@F) $(@D)/.tmp_$(@F:.o=.ver);                \
220         else                                                                    \
221                 mv -f $(@D)/.tmp_$(@F) $@;                                      \
222         fi;
223 endif
224
225 ifdef CONFIG_FTRACE_MCOUNT_RECORD
226 # gcc 5 supports generating the mcount tables directly
227 ifneq ($(call cc-option,-mrecord-mcount,y),y)
228 KBUILD_CFLAGS += -mrecord-mcount
229 else
230 # else do it all manually
231 ifdef BUILD_C_RECORDMCOUNT
232 ifeq ("$(origin RECORDMCOUNT_WARN)", "command line")
233   RECORDMCOUNT_FLAGS = -w
234 endif
235 # Due to recursion, we must skip empty.o.
236 # The empty.o file is created in the make process in order to determine
237 #  the target endianness and word size. It is made before all other C
238 #  files, including recordmcount.
239 sub_cmd_record_mcount =                                 \
240         if [ $(@) != "scripts/mod/empty.o" ]; then      \
241                 $(objtree)/scripts/recordmcount $(RECORDMCOUNT_FLAGS) "$(@)";   \
242         fi;
243 recordmcount_source := $(srctree)/scripts/recordmcount.c \
244                     $(srctree)/scripts/recordmcount.h
245 else
246 sub_cmd_record_mcount = set -e ; perl $(srctree)/scripts/recordmcount.pl "$(ARCH)" \
247         "$(if $(CONFIG_CPU_BIG_ENDIAN),big,little)" \
248         "$(if $(CONFIG_64BIT),64,32)" \
249         "$(OBJDUMP)" "$(OBJCOPY)" "$(CC) $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS)" \
250         "$(LD) $(KBUILD_LDFLAGS)" "$(NM)" "$(RM)" "$(MV)" \
251         "$(if $(part-of-module),1,0)" "$(@)";
252 recordmcount_source := $(srctree)/scripts/recordmcount.pl
253 endif
254 cmd_record_mcount =                                             \
255         if [ "$(findstring $(CC_FLAGS_FTRACE),$(_c_flags))" =   \
256              "$(CC_FLAGS_FTRACE)" ]; then                       \
257                 $(sub_cmd_record_mcount)                        \
258         fi;
259 endif # -record-mcount
260 endif
261
262 define rule_cc_o_c
263         $(call echo-cmd,checksrc) $(cmd_checksrc)                         \
264         $(call echo-cmd,cc_o_c) $(cmd_cc_o_c);                            \
265         $(cmd_modversions)                                                \
266         $(call echo-cmd,record_mcount)                                    \
267         $(cmd_record_mcount)                                              \
268         scripts/basic/fixdep $(depfile) $@ '$(call make-cmd,cc_o_c)' >    \
269                                                       $(dot-target).tmp;  \
270         rm -f $(depfile);                                                 \
271         mv -f $(dot-target).tmp $(dot-target).cmd
272 endef
273
274 # Built-in and composite module parts
275 $(obj)/%.o: $(src)/%.c $(recordmcount_source) FORCE
276         $(call cmd,force_checksrc)
277         $(call if_changed_rule,cc_o_c)
278
279 # Single-part modules are special since we need to mark them in $(MODVERDIR)
280
281 $(single-used-m): $(obj)/%.o: $(src)/%.c $(recordmcount_source) FORCE
282         $(call cmd,force_checksrc)
283         $(call if_changed_rule,cc_o_c)
284         @{ echo $(@:.o=.ko); echo $@; } > $(MODVERDIR)/$(@F:.o=.mod)
285
286 quiet_cmd_cc_lst_c = MKLST   $@
287       cmd_cc_lst_c = $(CC) $(c_flags) -g -c -o $*.o $< && \
288                      $(CONFIG_SHELL) $(srctree)/scripts/makelst $*.o \
289                                      System.map $(OBJDUMP) > $@
290
291 $(obj)/%.lst: $(src)/%.c FORCE
292         $(call if_changed_dep,cc_lst_c)
293
294 # Compile assembler sources (.S)
295 # ---------------------------------------------------------------------------
296
297 modkern_aflags := $(KBUILD_AFLAGS_KERNEL) $(AFLAGS_KERNEL)
298
299 $(real-objs-m)      : modkern_aflags := $(KBUILD_AFLAGS_MODULE) $(AFLAGS_MODULE)
300 $(real-objs-m:.o=.s): modkern_aflags := $(KBUILD_AFLAGS_MODULE) $(AFLAGS_MODULE)
301
302 quiet_cmd_as_s_S = CPP $(quiet_modtag) $@
303 cmd_as_s_S       = $(CPP) $(a_flags)   -o $@ $<
304
305 $(obj)/%.s: $(src)/%.S FORCE
306         $(call if_changed_dep,as_s_S)
307
308 quiet_cmd_as_o_S = AS $(quiet_modtag)  $@
309 cmd_as_o_S       = $(CC) $(a_flags) -c -o $@ $<
310
311 $(obj)/%.o: $(src)/%.S FORCE
312         $(call if_changed_dep,as_o_S)
313
314 targets += $(real-objs-y) $(real-objs-m) $(lib-y)
315 targets += $(extra-y) $(MAKECMDGOALS) $(always)
316
317 # Linker scripts preprocessor (.lds.S -> .lds)
318 # ---------------------------------------------------------------------------
319 quiet_cmd_cpp_lds_S = LDS     $@
320       cmd_cpp_lds_S = $(CPP) $(cpp_flags) -P -C -U$(ARCH) \
321                              -D__ASSEMBLY__ -DLINKER_SCRIPT -o $@ $<
322
323 $(obj)/%.lds: $(src)/%.lds.S FORCE
324         $(call if_changed_dep,cpp_lds_S)
325
326 # ASN.1 grammar
327 # ---------------------------------------------------------------------------
328 quiet_cmd_asn1_compiler = ASN.1   $@
329       cmd_asn1_compiler = $(objtree)/tools/asn1_compiler $< \
330                                 $(subst .h,.c,$@) $(subst .c,.h,$@)
331
332 $(obj)/%.asn1.c $(obj)/%.asn1.h: $(src)/%.asn1 $(objtree)/tools/asn1_compiler
333         $(call cmd,asn1_compiler)
334
335 # Build the compiled-in targets
336 # ---------------------------------------------------------------------------
337
338 # To build objects in subdirs, we need to descend into the directories
339 $(sort $(subdir-obj-y)): $(subdir-ym) ;
340
341 #
342 # Rule to compile a set of .o files into one .o file
343 #
344 ifdef builtin-target
345 quiet_cmd_link_o_target = LD      $@
346 # If the list of objects to link is empty, just create an empty built-in.o
347 cmd_link_o_target = $(if $(strip $(obj-y)),\
348                       $(LD) $(ld_flags) -r -o $@ $(filter $(obj-y), $^) \
349                       $(cmd_secanalysis),\
350                       rm -f $@; $(AR) rcs$(KBUILD_ARFLAGS) $@)
351
352 $(builtin-target): $(obj-y) FORCE
353         $(call if_changed,link_o_target)
354
355 targets += $(builtin-target)
356 endif # builtin-target
357
358 #
359 # Rule to create modules.order file
360 #
361 # Create commands to either record .ko file or cat modules.order from
362 # a subdirectory
363 modorder-cmds =                                         \
364         $(foreach m, $(modorder),                       \
365                 $(if $(filter %/modules.order, $m),     \
366                         cat $m;, echo kernel/$m;))
367
368 $(modorder-target): $(subdir-ym) FORCE
369         $(Q)(cat /dev/null; $(modorder-cmds)) > $@
370
371 #
372 # Rule to compile a set of .o files into one .a file
373 #
374 ifdef lib-target
375 quiet_cmd_link_l_target = AR      $@
376 cmd_link_l_target = rm -f $@; $(AR) rcs$(KBUILD_ARFLAGS) $@ $(lib-y)
377
378 $(lib-target): $(lib-y) FORCE
379         $(call if_changed,link_l_target)
380
381 targets += $(lib-target)
382 endif
383
384 #
385 # Rule to link composite objects
386 #
387 #  Composite objects are specified in kbuild makefile as follows:
388 #    <composite-object>-objs := <list of .o files>
389 #  or
390 #    <composite-object>-y    := <list of .o files>
391 link_multi_deps =                     \
392 $(filter $(addprefix $(obj)/,         \
393 $($(subst $(obj)/,,$(@:.o=-objs)))    \
394 $($(subst $(obj)/,,$(@:.o=-y)))), $^)
395
396 quiet_cmd_link_multi-y = LD      $@
397 cmd_link_multi-y = $(LD) $(ld_flags) -r -o $@ $(link_multi_deps) $(cmd_secanalysis)
398
399 quiet_cmd_link_multi-m = LD [M]  $@
400 cmd_link_multi-m = $(cmd_link_multi-y)
401
402 $(multi-used-y): FORCE
403         $(call if_changed,link_multi-y)
404 $(call multi_depend, $(multi-used-y), .o, -objs -y)
405
406 $(multi-used-m): FORCE
407         $(call if_changed,link_multi-m)
408         @{ echo $(@:.o=.ko); echo $(link_multi_deps); } > $(MODVERDIR)/$(@F:.o=.mod)
409 $(call multi_depend, $(multi-used-m), .o, -objs -y)
410
411 targets += $(multi-used-y) $(multi-used-m)
412
413
414 # Add intermediate targets:
415 # When building objects with specific suffix patterns, add intermediate
416 # targets that the final targets are derived from.
417 intermediate_targets = $(foreach sfx, $(2), \
418                                 $(patsubst %$(strip $(1)),%$(sfx), \
419                                         $(filter %$(strip $(1)), $(targets))))
420 # %.lex.o <- %.lex.c <- %.l
421 # %.tab.o <- %.tab.[ch] <- %.y
422 targets += $(call intermediate_targets, .lex.o, .lex.c) \
423            $(call intermediate_targets, .tab.o, .tab.c .tab.h)
424
425 # Descending
426 # ---------------------------------------------------------------------------
427
428 PHONY += $(subdir-ym)
429 $(subdir-ym):
430         $(Q)$(MAKE) $(build)=$@
431
432 # Add FORCE to the prequisites of a target to force it to be always rebuilt.
433 # ---------------------------------------------------------------------------
434
435 PHONY += FORCE
436
437 FORCE:
438
439 # Read all saved command lines and dependencies for the $(targets) we
440 # may be building above, using $(if_changed{,_dep}). As an
441 # optimization, we don't need to read them if the target does not
442 # exist, we will rebuild anyway in that case.
443
444 targets := $(wildcard $(sort $(targets)))
445 cmd_files := $(wildcard $(foreach f,$(targets),$(dir $(f)).$(notdir $(f)).cmd))
446
447 ifneq ($(cmd_files),)
448   include $(cmd_files)
449 endif
450
451 .PHONY: $(PHONY)