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