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