1 # ==========================================================================
3 # ==========================================================================
6 ifeq ($(CONFIG_TPL_BUILD),y)
7 src := $(patsubst tpl/%,%,$(obj))
9 ifeq ($(CONFIG_SPL_BUILD),y)
10 src := $(patsubst spl/%,%,$(obj))
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
42 # Read auto.conf if it exists, otherwise ignore
43 -include include/config/auto.conf
45 # Added for U-Boot: Load U-Boot configuration
46 ifeq ($(CONFIG_TPL_BUILD),y)
47 -include include/tpl-autoconf.mk
49 ifeq ($(CONFIG_SPL_BUILD),y)
50 -include include/spl-autoconf.mk
52 -include include/autoconf.mk
56 include scripts/Kbuild.include
58 # For backward compatibility check that these variables do not change
59 save-cflags := $(CFLAGS)
61 # The filename Kbuild has precedence over Makefile
62 kbuild-dir := $(if $(filter /%,$(src)),$(src),$(srctree)/$(src))
63 kbuild-file := $(if $(wildcard $(kbuild-dir)/Kbuild),$(kbuild-dir)/Kbuild,$(kbuild-dir)/Makefile)
64 include $(kbuild-file)
67 asflags-y += $(PLATFORM_CPPFLAGS)
68 ccflags-y += $(PLATFORM_CPPFLAGS)
69 cppflags-y += $(PLATFORM_CPPFLAGS)
71 # If the save-* variables changed error out
72 ifeq ($(KBUILD_NOPEDANTIC),)
73 ifneq ("$(save-cflags)","$(CFLAGS)")
74 $(error CFLAGS was changed in "$(kbuild-file)". Fix it to use ccflags-y)
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
85 # $(call cc-option, -W...) handles gcc -W.. options which
86 # are not supported by all versions of the compiler
87 ifdef KBUILD_ENABLE_EXTRA_GCC_CHECKS
90 warning-1 := -Wextra -Wunused -Wno-unused-parameter
91 warning-1 += -Wmissing-declarations
92 warning-1 += -Wmissing-format-attribute
93 warning-1 += $(call cc-option, -Wmissing-prototypes)
94 warning-1 += -Wold-style-definition
95 warning-1 += $(call cc-option, -Wmissing-include-dirs)
96 warning-1 += $(call cc-option, -Wunused-but-set-variable)
97 warning-1 += $(call cc-disable-warning, missing-field-initializers)
100 warning-1 += $(call cc-disable-warning, initializer-overrides)
101 warning-1 += $(call cc-disable-warning, unused-value)
102 warning-1 += $(call cc-disable-warning, format)
103 warning-1 += $(call cc-disable-warning, unknown-warning-option)
104 warning-1 += $(call cc-disable-warning, sign-compare)
105 warning-1 += $(call cc-disable-warning, format-zero-length)
106 warning-1 += $(call cc-disable-warning, uninitialized)
107 warning-1 += $(call cc-option, -fcatch-undefined-behavior)
109 warning-2 := -Waggregate-return
110 warning-2 += -Wcast-align
111 warning-2 += -Wdisabled-optimization
112 warning-2 += -Wnested-externs
113 warning-2 += -Wshadow
114 warning-2 += $(call cc-option, -Wlogical-op)
115 warning-2 += $(call cc-option, -Wmissing-field-initializers)
117 warning-3 := -Wbad-function-cast
118 warning-3 += -Wcast-qual
119 warning-3 += -Wconversion
120 warning-3 += -Wpacked
121 warning-3 += -Wpadded
122 warning-3 += -Wpointer-arith
123 warning-3 += -Wredundant-decls
124 warning-3 += -Wswitch-default
125 warning-3 += $(call cc-option, -Wpacked-bitfield-compat)
126 warning-3 += $(call cc-option, -Wvla)
128 warning := $(warning-$(findstring 1, $(KBUILD_ENABLE_EXTRA_GCC_CHECKS)))
129 warning += $(warning-$(findstring 2, $(KBUILD_ENABLE_EXTRA_GCC_CHECKS)))
130 warning += $(warning-$(findstring 3, $(KBUILD_ENABLE_EXTRA_GCC_CHECKS)))
132 ifeq ("$(strip $(warning))","")
133 $(error W=$(KBUILD_ENABLE_EXTRA_GCC_CHECKS) is unknown)
136 KBUILD_CFLAGS += $(warning)
139 include scripts/Makefile.lib
142 ifneq ($(hostprogs-y),$(host-progs))
143 $(warning kbuild: $(obj)/Makefile - Usage of host-progs is deprecated. Please replace with hostprogs-y!)
144 hostprogs-y += $(host-progs)
148 # Do not include host rules unless needed
149 ifneq ($(hostprogs-y)$(hostprogs-m),)
150 include scripts/Makefile.host
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))
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)))
165 $(warning kbuild: Makefile.build is included improperly)
168 # ===========================================================================
170 ifneq ($(strip $(lib-y) $(lib-m) $(lib-n) $(lib-)),)
171 lib-target := $(obj)/lib.a
174 ifneq ($(strip $(obj-y) $(obj-m) $(obj-n) $(obj-) $(subdir-m) $(lib-target)),)
175 builtin-target := $(obj)/built-in.o
178 modorder-target := $(obj)/modules.order
180 # We keep a list of all modules in $(MODVERDIR)
182 __build: $(if $(KBUILD_BUILTIN),$(builtin-target) $(lib-target) $(extra-y)) \
183 $(if $(KBUILD_MODULES),$(obj-m) $(modorder-target)) \
184 $(subdir-ym) $(always)
187 # Linus' kernel sanity checking tool
188 ifneq ($(KBUILD_CHECKSRC),0)
189 ifeq ($(KBUILD_CHECKSRC),2)
190 quiet_cmd_force_checksrc = CHECK $<
191 cmd_force_checksrc = $(CHECK) $(CHECKFLAGS) $(c_flags) $< ;
193 quiet_cmd_checksrc = CHECK $<
194 cmd_checksrc = $(CHECK) $(CHECKFLAGS) $(c_flags) $< ;
198 # Do section mismatch analysis for each module/built-in.o
199 ifdef CONFIG_DEBUG_SECTION_MISMATCH
200 cmd_secanalysis = ; scripts/mod/modpost $@
203 # Compile C sources (.c)
204 # ---------------------------------------------------------------------------
206 # Default is built-in, unless we know otherwise
208 $(if $(part-of-module), \
209 $(KBUILD_CFLAGS_MODULE) $(CFLAGS_MODULE), \
210 $(KBUILD_CFLAGS_KERNEL) $(CFLAGS_KERNEL))
211 quiet_modtag := $(empty) $(empty)
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
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]
223 $(obj-m) : quiet_modtag := [M]
225 # Default for not multi-part modules
226 modname = $(basetarget)
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)
237 quiet_cmd_cc_s_c = CC $(quiet_modtag) $@
238 cmd_cc_s_c = $(CC) $(c_flags) $(DISABLE_LTO) -fverbose-asm -S -o $@ $<
240 $(obj)/%.s: $(src)/%.c FORCE
241 $(call if_changed_dep,cc_s_c)
243 quiet_cmd_cc_i_c = CPP $(quiet_modtag) $@
244 cmd_cc_i_c = $(CPP) $(c_flags) -o $@ $<
246 $(obj)/%.i: $(src)/%.c FORCE
247 $(call if_changed_dep,cc_i_c)
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))
256 quiet_cmd_cc_symtypes_c = SYM $(quiet_modtag) $@
257 cmd_cc_symtypes_c = \
259 $(call cmd_gensymtypes,true,$@) >/dev/null; \
260 test -s $@ || rm -f $@
262 $(obj)/%.symtypes : $(src)/%.c FORCE
263 $(call cmd,cc_symtypes_c)
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)
269 quiet_cmd_cc_o_c = CC $(quiet_modtag) $@
271 ifndef CONFIG_MODVERSIONS
272 cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $<
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
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
287 cmd_cc_o_c = $(CC) $(c_flags) -c -o $(@D)/.tmp_$(@F) $<
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); \
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); \
297 mv -f $(@D)/.tmp_$(@F) $@; \
301 ifdef CONFIG_FTRACE_MCOUNT_RECORD
302 ifdef BUILD_C_RECORDMCOUNT
303 ifeq ("$(origin RECORDMCOUNT_WARN)", "command line")
304 RECORDMCOUNT_FLAGS = -w
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.
310 sub_cmd_record_mcount = \
311 if [ $(@) != "scripts/mod/empty.o" ]; then \
312 $(objtree)/scripts/recordmcount $(RECORDMCOUNT_FLAGS) "$(@)"; \
314 recordmcount_source := $(srctree)/scripts/recordmcount.c \
315 $(srctree)/scripts/recordmcount.h
317 sub_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)" "$(@)";
323 recordmcount_source := $(srctree)/scripts/recordmcount.pl
325 cmd_record_mcount = \
326 if [ "$(findstring -pg,$(_c_flags))" = "-pg" ]; then \
327 $(sub_cmd_record_mcount) \
332 $(call echo-cmd,checksrc) $(cmd_checksrc) \
333 $(call echo-cmd,cc_o_c) $(cmd_cc_o_c); \
335 $(call echo-cmd,record_mcount) \
336 $(cmd_record_mcount) \
337 scripts/basic/fixdep $(depfile) $@ '$(call make-cmd,cc_o_c)' > \
340 mv -f $(dot-target).tmp $(dot-target).cmd
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)
348 # Single-part modules are special since we need to mark them in $(MODVERDIR)
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)
355 quiet_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) > $@
360 $(obj)/%.lst: $(src)/%.c FORCE
361 $(call if_changed_dep,cc_lst_c)
363 # Compile assembler sources (.S)
364 # ---------------------------------------------------------------------------
366 modkern_aflags := $(KBUILD_AFLAGS_KERNEL) $(AFLAGS_KERNEL)
368 $(real-objs-m) : modkern_aflags := $(KBUILD_AFLAGS_MODULE) $(AFLAGS_MODULE)
369 $(real-objs-m:.o=.s): modkern_aflags := $(KBUILD_AFLAGS_MODULE) $(AFLAGS_MODULE)
371 quiet_cmd_as_s_S = CPP $(quiet_modtag) $@
372 cmd_as_s_S = $(CPP) $(a_flags) -o $@ $<
374 $(obj)/%.s: $(src)/%.S FORCE
375 $(call if_changed_dep,as_s_S)
377 quiet_cmd_as_o_S = AS $(quiet_modtag) $@
378 cmd_as_o_S = $(CC) $(a_flags) -c -o $@ $<
380 $(obj)/%.o: $(src)/%.S FORCE
381 $(call if_changed_dep,as_o_S)
383 targets += $(real-objs-y) $(real-objs-m) $(lib-y)
384 targets += $(extra-y) $(MAKECMDGOALS) $(always)
386 # Linker scripts preprocessor (.lds.S -> .lds)
387 # ---------------------------------------------------------------------------
388 quiet_cmd_cpp_lds_S = LDS $@
389 cmd_cpp_lds_S = $(CPP) $(cpp_flags) -P -C -U$(ARCH) \
390 -D__ASSEMBLY__ -DLINKER_SCRIPT -o $@ $<
392 $(obj)/%.lds: $(src)/%.lds.S FORCE
393 $(call if_changed_dep,cpp_lds_S)
396 # ---------------------------------------------------------------------------
397 quiet_cmd_asn1_compiler = ASN.1 $@
398 cmd_asn1_compiler = $(objtree)/scripts/asn1_compiler $< \
399 $(subst .h,.c,$@) $(subst .c,.h,$@)
401 .PRECIOUS: $(objtree)/$(obj)/%-asn1.c $(objtree)/$(obj)/%-asn1.h
403 $(obj)/%-asn1.c $(obj)/%-asn1.h: $(src)/%.asn1 $(objtree)/scripts/asn1_compiler
404 $(call cmd,asn1_compiler)
406 # Build the compiled-in targets
407 # ---------------------------------------------------------------------------
409 # To build objects in subdirs, we need to descend into the directories
410 $(sort $(subdir-obj-y)): $(subdir-ym) ;
413 # Rule to compile a set of .o files into one .o file
416 quiet_cmd_link_o_target = LD $@
417 # If the list of objects to link is empty, just create an empty built-in.o
418 cmd_link_o_target = $(if $(strip $(obj-y)),\
419 $(LD) $(ld_flags) -r -o $@ $(filter $(obj-y), $^) \
421 rm -f $@; $(AR) rcs$(KBUILD_ARFLAGS) $@)
423 $(builtin-target): $(obj-y) FORCE
424 $(call if_changed,link_o_target)
426 targets += $(builtin-target)
427 endif # builtin-target
430 # Rule to create modules.order file
432 # Create commands to either record .ko file or cat modules.order from
435 $(foreach m, $(modorder), \
436 $(if $(filter %/modules.order, $m), \
437 cat $m;, echo kernel/$m;))
439 $(modorder-target): $(subdir-ym) FORCE
440 $(Q)(cat /dev/null; $(modorder-cmds)) > $@
443 # Rule to compile a set of .o files into one .a file
446 quiet_cmd_link_l_target = AR $@
447 cmd_link_l_target = rm -f $@; $(AR) rcs$(KBUILD_ARFLAGS) $@ $(lib-y)
449 $(lib-target): $(lib-y) FORCE
450 $(call if_changed,link_l_target)
452 targets += $(lib-target)
456 # Rule to link composite objects
458 # Composite objects are specified in kbuild makefile as follows:
459 # <composite-object>-objs := <list of .o files>
461 # <composite-object>-y := <list of .o files>
463 $(filter $(addprefix $(obj)/, \
464 $($(subst $(obj)/,,$(@:.o=-objs))) \
465 $($(subst $(obj)/,,$(@:.o=-y)))), $^)
467 quiet_cmd_link_multi-y = LD $@
468 cmd_link_multi-y = $(LD) $(ld_flags) -r -o $@ $(link_multi_deps) $(cmd_secanalysis)
470 quiet_cmd_link_multi-m = LD [M] $@
471 cmd_link_multi-m = $(cmd_link_multi-y)
473 # We would rather have a list of rules like
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)
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)
484 targets += $(multi-used-y) $(multi-used-m)
488 # ---------------------------------------------------------------------------
490 PHONY += $(subdir-ym)
492 $(Q)$(MAKE) $(build)=$@
494 # Add FORCE to the prequisites of a target to force it to be always rebuilt.
495 # ---------------------------------------------------------------------------
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.
506 targets := $(wildcard $(sort $(targets)))
507 cmd_files := $(wildcard $(foreach f,$(targets),$(dir $(f)).$(notdir $(f)).cmd))
509 ifneq ($(cmd_files),)
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.