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