blob: f08d2bda80ef3e2418635625eacd248cc158eb72 [file] [log] [blame]
Anas Nashif24da31f2015-09-25 16:52:30 -04001# vim: filetype=make
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07002# Backward compatibility
3asflags-y += $(EXTRA_AFLAGS)
4ccflags-y += $(EXTRA_CFLAGS)
5cppflags-y += $(EXTRA_CPPFLAGS)
Peter Mitsis8e35cc82016-01-13 13:02:56 -05006cxxflags-y += $(EXTRA_CXXFLAGS)
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07007ldflags-y += $(EXTRA_LDFLAGS)
8
9#
10# flags that take effect in sub directories
11export KBUILD_SUBDIR_ASFLAGS := $(KBUILD_SUBDIR_ASFLAGS) $(subdir-asflags-y)
12export KBUILD_SUBDIR_CCFLAGS := $(KBUILD_SUBDIR_CCFLAGS) $(subdir-ccflags-y)
Sakari Poussa1bfdfbf2016-11-08 13:30:28 +020013export KBUILD_SUBDIR_CXXFLAGS := $(KBUILD_SUBDIR_CXXFLAGS) $(subdir-cxxflags-y)
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070014
15# Figure out what we need to build from the various variables
16# ===========================================================================
17
18# When an object is listed to be built compiled-in and modular,
19# only build the compiled-in version
20
21obj-m := $(filter-out $(obj-y),$(obj-m))
22
23# Libraries are always collected in one lib file.
24# Filter out objects already built-in
25
26lib-y := $(filter-out $(obj-y), $(sort $(lib-y) $(lib-m)))
27
28
29# Handle objects in subdirs
30# ---------------------------------------------------------------------------
31# o if we encounter foo/ in $(obj-y), replace it by foo/built-in.o
32# and add the directory to the list of dirs to descend into: $(subdir-y)
33# o if we encounter foo/ in $(obj-m), remove it from $(obj-m)
34# and add the directory to the list of dirs to descend into: $(subdir-m)
35
36# Determine modorder.
37# Unfortunately, we don't have information about ordering between -y
38# and -m subdirs. Just put -y's first.
Anas Nashifcdf88a72015-05-22 22:34:53 -040039#modorder := $(patsubst %/,%/modules.order, $(filter %/, $(obj-y)) $(obj-m:.o=.ko))
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070040
Anas Nashif24da31f2015-09-25 16:52:30 -040041_subd-y := $(if $(findstring *,$(subdir-y)),$(wildcard $(addprefix $(srctree)/$(obj)/,$(subdir-y))),)
42subdir-y := $(sort $(foreach d,$(_subd-y), $(patsubst $(srctree)/$(obj)/%,%,$(d))))
43_obj-y := $(sort $(foreach d,$(_subd-y), $(patsubst $(srctree)/$(obj)/%,%/built-in.o,$(d))))
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070044__subdir-y := $(patsubst %/,%,$(filter %/, $(obj-y)))
45subdir-y += $(__subdir-y)
46__subdir-m := $(patsubst %/,%,$(filter %/, $(obj-m)))
47subdir-m += $(__subdir-m)
48obj-y := $(patsubst %/, %/built-in.o, $(obj-y))
Anas Nashif24da31f2015-09-25 16:52:30 -040049obj-y += $(_obj-y)
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070050obj-m := $(filter-out %/, $(obj-m))
51
52# Subdirectories we need to descend into
53
54subdir-ym := $(sort $(subdir-y) $(subdir-m))
55
56# if $(foo-objs) exists, foo.o is a composite object
57multi-used-y := $(sort $(foreach m,$(obj-y), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y))), $(m))))
58multi-used-m := $(sort $(foreach m,$(obj-m), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y))), $(m))))
59multi-used := $(multi-used-y) $(multi-used-m)
60single-used-m := $(sort $(filter-out $(multi-used-m),$(obj-m)))
61
62# Build list of the parts of our composite objects, our composite
63# objects depend on those (obviously)
64multi-objs-y := $(foreach m, $(multi-used-y), $($(m:.o=-objs)) $($(m:.o=-y)))
65multi-objs-m := $(foreach m, $(multi-used-m), $($(m:.o=-objs)) $($(m:.o=-y)))
66multi-objs := $(multi-objs-y) $(multi-objs-m)
67
68# $(subdir-obj-y) is the list of objects in $(obj-y) which uses dir/ to
69# tell kbuild to descend
70subdir-obj-y := $(filter %/built-in.o, $(obj-y))
71
72# $(obj-dirs) is a list of directories that contain object files
73obj-dirs := $(dir $(multi-objs) $(obj-y))
Juan Manuel Cruz62cf77c2015-03-04 17:34:47 -060074lib-dirs := $(dir $(lib-y))
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070075
76# Replace multi-part objects by their individual parts, look at local dir only
77real-objs-y := $(foreach m, $(filter-out $(subdir-obj-y), $(obj-y)), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y))),$($(m:.o=-objs)) $($(m:.o=-y)),$(m))) $(extra-y)
78real-objs-m := $(foreach m, $(obj-m), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y))),$($(m:.o=-objs)) $($(m:.o=-y)),$(m)))
79
80# Add subdir path
81
82extra-y := $(addprefix $(obj)/,$(extra-y))
83always := $(addprefix $(obj)/,$(always))
84targets := $(addprefix $(obj)/,$(targets))
Anas Nashifcdf88a72015-05-22 22:34:53 -040085#modorder := $(addprefix $(obj)/,$(modorder))
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070086obj-y := $(addprefix $(obj)/,$(obj-y))
87obj-m := $(addprefix $(obj)/,$(obj-m))
88lib-y := $(addprefix $(obj)/,$(lib-y))
89subdir-obj-y := $(addprefix $(obj)/,$(subdir-obj-y))
90real-objs-y := $(addprefix $(obj)/,$(real-objs-y))
91real-objs-m := $(addprefix $(obj)/,$(real-objs-m))
92single-used-m := $(addprefix $(obj)/,$(single-used-m))
93multi-used-y := $(addprefix $(obj)/,$(multi-used-y))
94multi-used-m := $(addprefix $(obj)/,$(multi-used-m))
95multi-objs-y := $(addprefix $(obj)/,$(multi-objs-y))
96multi-objs-m := $(addprefix $(obj)/,$(multi-objs-m))
97subdir-ym := $(addprefix $(obj)/,$(subdir-ym))
98obj-dirs := $(addprefix $(obj)/,$(obj-dirs))
Juan Manuel Cruz62cf77c2015-03-04 17:34:47 -060099lib-dirs := $(addprefix $(obj)/,$(lib-dirs))
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700100
101# These flags are needed for modversions and compiling, so we define them here
102# already
103# $(modname_flags) #defines KBUILD_MODNAME as the name of the module it will
104# end up in (or would, if it gets compiled in)
105# Note: Files that end up in two or more modules are compiled without the
106# KBUILD_MODNAME definition. The reason is that any made-up name would
107# differ in different configs.
108name-fix = $(subst $(comma),_,$(subst -,_,$1))
109basename_flags = -D"KBUILD_BASENAME=KBUILD_STR($(call name-fix,$(basetarget)))"
110modname_flags = $(if $(filter 1,$(words $(modname))),\
111 -D"KBUILD_MODNAME=KBUILD_STR($(call name-fix,$(modname)))")
112
113orig_c_flags = $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) $(KBUILD_SUBDIR_CCFLAGS) \
114 $(ccflags-y) $(CFLAGS_$(basetarget).o)
115_c_flags = $(filter-out $(CFLAGS_REMOVE_$(basetarget).o), $(orig_c_flags))
116_a_flags = $(KBUILD_CPPFLAGS) $(KBUILD_AFLAGS) $(KBUILD_SUBDIR_ASFLAGS) \
117 $(asflags-y) $(AFLAGS_$(basetarget).o)
118_cpp_flags = $(KBUILD_CPPFLAGS) $(cppflags-y) $(CPPFLAGS_$(@F))
Peter Mitsis8e35cc82016-01-13 13:02:56 -0500119orig_cxx_flags = $(KBUILD_CPPFLAGS) $(KBUILD_CXXFLAGS) $(KBUILD_SUBDIR_CXXFLAGS) \
120 $(cxxflags-y) $(CXXFLAGS_$(basetarget).o)
121_cxx_flags = $(filter-out $(CXXFLAGS_REMOVE_$(basetarget).o), $(orig_cxx_flags))
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700122
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700123# If building the kernel in a separate objtree expand all occurrences
124# of -Idir to -I$(srctree)/dir except for absolute paths (starting with '/').
125
126ifeq ($(KBUILD_SRC),)
127__c_flags = $(_c_flags)
128__a_flags = $(_a_flags)
129__cpp_flags = $(_cpp_flags)
Peter Mitsis8e35cc82016-01-13 13:02:56 -0500130__cxx_flags = $(_cxx_flags)
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700131else
132
133# -I$(obj) locates generated .h files
134# $(call addtree,-I$(obj)) locates .h files in srctree, from generated .c files
135# and locates generated .h files
136# FIXME: Replace both with specific CFLAGS* statements in the makefiles
137__c_flags = $(call addtree,-I$(obj)) $(call flags,_c_flags)
138__a_flags = $(call flags,_a_flags)
139__cpp_flags = $(call flags,_cpp_flags)
Peter Mitsis8e35cc82016-01-13 13:02:56 -0500140__cxx_flags = $(call flags,_cxx_flags)
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700141endif
142
Anas Nashifb8823772015-06-05 22:46:00 -0400143c_flags = -Wp,-MD,$(depfile) $(NOSTDINC_FLAGS) $(ZEPHYRINCLUDE) \
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700144 $(__c_flags) $(modkern_cflags) \
145 -D"KBUILD_STR(s)=\#s" $(basename_flags) $(modname_flags)
146
Anas Nashifb8823772015-06-05 22:46:00 -0400147a_flags = -Wp,-MD,$(depfile) $(NOSTDINC_FLAGS) $(ZEPHYRINCLUDE) \
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700148 $(__a_flags) $(modkern_aflags)
149
Anas Nashifb8823772015-06-05 22:46:00 -0400150cpp_flags = -Wp,-MD,$(depfile) $(NOSTDINC_FLAGS) $(ZEPHYRINCLUDE) \
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700151 $(__cpp_flags)
152
153ld_flags = $(LDFLAGS) $(ldflags-y)
154
155dtc_cpp_flags = -Wp,-MD,$(depfile).pre.tmp -nostdinc \
Andy Grossbb063162017-01-29 23:53:17 -0600156 $(ZEPHYRINCLUDE) \
Vincenzo Frascinoae5dbe42017-02-06 00:12:40 -0600157 -I$(srctree)/arch/$(ARCH)/soc \
Andy Grossbb063162017-01-29 23:53:17 -0600158 -I$(srctree)/dts/common \
Kumar Galaa0fbc882017-04-03 16:35:53 -0500159 -I$(srctree)/dts \
160 -I$(srctree)/dts/$(ARCH) \
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700161 -I$(srctree)/drivers/of/testcase-data \
Andy Gross2fb6dce2017-03-01 17:14:20 -0600162 -I$(PROJECT_BASE) \
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700163 -undef -D__DTS__
164
Peter Mitsis8e35cc82016-01-13 13:02:56 -0500165cxx_flags = -Wp,-MD,$(depfile) $(NOSTDINC_FLAGS) $(ZEPHYRINCLUDE) \
166 $(__cxx_flags) $(modkern_cflags) \
167 -D"KBUILD_STR(s)=\#s" $(basename_flags) $(modname_flags)
168
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700169# Finds the multi-part object the current object will be linked into
170modname-multi = $(sort $(foreach m,$(multi-used),\
171 $(if $(filter $(subst $(obj)/,,$*.o), $($(m:.o=-objs)) $($(m:.o=-y))),$(m:.o=))))
172
173# Useful for describing the dependency of composite objects
174# Usage:
175# $(call multi_depend, multi_used_targets, suffix_to_remove, suffix_to_add)
176define multi_depend
177$(foreach m, $(notdir $1), \
178 $(eval $(obj)/$m: \
179 $(addprefix $(obj)/, $(foreach s, $3, $($(m:%$(strip $2)=%$(s)))))))
180endef
181
182ifdef REGENERATE_PARSERS
183
184# GPERF
185# ---------------------------------------------------------------------------
186quiet_cmd_gperf = GPERF $@
187 cmd_gperf = gperf -t --output-file $@ -a -C -E -g -k 1,3,$$ -p -t $<
188
189.PRECIOUS: $(src)/%.hash.c_shipped
190$(src)/%.hash.c_shipped: $(src)/%.gperf
191 $(call cmd,gperf)
192
193# LEX
194# ---------------------------------------------------------------------------
195LEX_PREFIX = $(if $(LEX_PREFIX_${baseprereq}),$(LEX_PREFIX_${baseprereq}),yy)
196
197quiet_cmd_flex = LEX $@
198 cmd_flex = flex -o$@ -L -P $(LEX_PREFIX) $<
199
200.PRECIOUS: $(src)/%.lex.c_shipped
201$(src)/%.lex.c_shipped: $(src)/%.l
202 $(call cmd,flex)
203
204# YACC
205# ---------------------------------------------------------------------------
206YACC_PREFIX = $(if $(YACC_PREFIX_${baseprereq}),$(YACC_PREFIX_${baseprereq}),yy)
207
208quiet_cmd_bison = YACC $@
209 cmd_bison = bison -o$@ -t -l -p $(YACC_PREFIX) $<
210
211.PRECIOUS: $(src)/%.tab.c_shipped
212$(src)/%.tab.c_shipped: $(src)/%.y
213 $(call cmd,bison)
214
215quiet_cmd_bison_h = YACC $@
216 cmd_bison_h = bison -o/dev/null --defines=$@ -t -l -p $(YACC_PREFIX) $<
217
218.PRECIOUS: $(src)/%.tab.h_shipped
219$(src)/%.tab.h_shipped: $(src)/%.y
220 $(call cmd,bison_h)
221
222endif
223
224# Shipped files
225# ===========================================================================
226
227quiet_cmd_shipped = SHIPPED $@
228cmd_shipped = cat $< > $@
229
230$(obj)/%: $(src)/%_shipped
231 $(call cmd,shipped)
232
233# Commands useful for building a boot image
234# ===========================================================================
235#
236# Use as following:
237#
238# target: source(s) FORCE
239# $(if_changed,ld/objcopy/gzip)
240#
241# and add target to extra-y so that we know we have to
242# read in the saved command line
243
244# Linking
245# ---------------------------------------------------------------------------
246
247quiet_cmd_ld = LD $@
248cmd_ld = $(LD) $(LDFLAGS) $(ldflags-y) $(LDFLAGS_$(@F)) \
249 $(filter-out FORCE,$^) -o $@
250
251# Objcopy
252# ---------------------------------------------------------------------------
253
254quiet_cmd_objcopy = OBJCOPY $@
255cmd_objcopy = $(OBJCOPY) $(OBJCOPYFLAGS) $(OBJCOPYFLAGS_$(@F)) $< $@
256
257# Gzip
258# ---------------------------------------------------------------------------
259
260quiet_cmd_gzip = GZIP $@
261cmd_gzip = (cat $(filter-out FORCE,$^) | gzip -n -f -9 > $@) || \
262 (rm -f $@ ; false)
263
264# DTC
265# ---------------------------------------------------------------------------
266
Marti Bolivar94bf9242017-07-28 12:05:32 -0400267ifeq ($(DTC_OVERLAY_FILE),)
David Brown7a8380a2017-05-25 10:17:33 -0600268DTC_OVERLAY_DIR ?= $(PROJECT_BASE)
Carles Cufi5dec78b2017-05-29 17:19:08 +0200269# Since this goes into a file, use the native path
270DTC_ABS_OVERLAY_DIR = $(shell cd $(DTC_OVERLAY_DIR) && pwd $(NATIVE_PWD_OPT))
271DTC_OVERLAY_FILE = $(DTC_ABS_OVERLAY_DIR)/$(BOARD_NAME).overlay
Marti Bolivar94bf9242017-07-28 12:05:32 -0400272endif
David Brown7a8380a2017-05-25 10:17:33 -0600273
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700274# Generate an assembly file to wrap the output of the device tree compiler
275quiet_cmd_dt_S_dtb= DTB $@
276cmd_dt_S_dtb= \
277( \
278 echo '\#include <asm-generic/vmlinux.lds.h>'; \
279 echo '.section .dtb.init.rodata,"a"'; \
280 echo '.balign STRUCT_ALIGNMENT'; \
281 echo '.global __dtb_$(*F)_begin'; \
282 echo '__dtb_$(*F)_begin:'; \
283 echo '.incbin "$<" '; \
284 echo '__dtb_$(*F)_end:'; \
285 echo '.global __dtb_$(*F)_end'; \
286 echo '.balign STRUCT_ALIGNMENT'; \
287) > $@
288
289$(obj)/%.dtb.S: $(obj)/%.dtb
290 $(call cmd,dt_S_dtb)
291
292quiet_cmd_dtc = DTC $@
Andy Gross2fb6dce2017-03-01 17:14:20 -0600293cmd_dtc = echo '\#include "$(notdir $<)"' > dts/$(ARCH)/$(notdir $<)_pre_compiled ; \
David Brown7a8380a2017-05-25 10:17:33 -0600294 if test -e $(DTC_OVERLAY_FILE); then \
295 echo '\#include "$(DTC_OVERLAY_FILE)"' >> dts/$(ARCH)/$(notdir $<)_pre_compiled ; \
Andy Gross2fb6dce2017-03-01 17:14:20 -0600296 fi ; \
297 $(CPP) $(dtc_cpp_flags) -x assembler-with-cpp -o $(dtc-tmp) dts/$(ARCH)/$(notdir $<)_pre_compiled ; \
Andy Grossbb063162017-01-29 23:53:17 -0600298 $(DTC) -O dts -o $@ -b 0 \
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700299 -i $(dir $<) $(DTC_FLAGS) \
300 -d $(depfile).dtc.tmp $(dtc-tmp) ; \
301 cat $(depfile).pre.tmp $(depfile).dtc.tmp > $(depfile)
302
303$(obj)/%.dtb: $(src)/%.dts FORCE
304 $(call if_changed_dep,dtc)
305
David Brown7a8380a2017-05-25 10:17:33 -0600306$(obj)/%.dts_compiled: $(src)/%.dts $(wildcard $(DTC_OVERLAY_FILE)) FORCE
Andy Grossbb063162017-01-29 23:53:17 -0600307 $(call if_changed_dep,dtc)
308
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700309dtc-tmp = $(subst $(comma),_,$(dot-target).dts.tmp)
310
311# Bzip2
312# ---------------------------------------------------------------------------
313
314# Bzip2 and LZMA do not include size in file... so we have to fake that;
315# append the size as a 32-bit littleendian number as gzip does.
316size_append = printf $(shell \
317dec_size=0; \
318for F in $1; do \
319 fsize=$$(stat -c "%s" $$F); \
320 dec_size=$$(expr $$dec_size + $$fsize); \
321done; \
322printf "%08x\n" $$dec_size | \
323 sed 's/\(..\)/\1 /g' | { \
324 read ch0 ch1 ch2 ch3; \
325 for ch in $$ch3 $$ch2 $$ch1 $$ch0; do \
326 printf '%s%03o' '\\' $$((0x$$ch)); \
327 done; \
328 } \
329)
330
331quiet_cmd_bzip2 = BZIP2 $@
332cmd_bzip2 = (cat $(filter-out FORCE,$^) | \
333 bzip2 -9 && $(call size_append, $(filter-out FORCE,$^))) > $@ || \
334 (rm -f $@ ; false)
335
336# Lzma
337# ---------------------------------------------------------------------------
338
339quiet_cmd_lzma = LZMA $@
340cmd_lzma = (cat $(filter-out FORCE,$^) | \
341 lzma -9 && $(call size_append, $(filter-out FORCE,$^))) > $@ || \
342 (rm -f $@ ; false)
343
344quiet_cmd_lzo = LZO $@
345cmd_lzo = (cat $(filter-out FORCE,$^) | \
346 lzop -9 && $(call size_append, $(filter-out FORCE,$^))) > $@ || \
347 (rm -f $@ ; false)
348
349quiet_cmd_lz4 = LZ4 $@
350cmd_lz4 = (cat $(filter-out FORCE,$^) | \
351 lz4c -l -c1 stdin stdout && $(call size_append, $(filter-out FORCE,$^))) > $@ || \
352 (rm -f $@ ; false)
353
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700354
355# XZ
356# ---------------------------------------------------------------------------
357# Use xzkern to compress the kernel image and xzmisc to compress other things.
358#
359# xzkern uses a big LZMA2 dictionary since it doesn't increase memory usage
360# of the kernel decompressor. A BCJ filter is used if it is available for
361# the target architecture. xzkern also appends uncompressed size of the data
362# using size_append. The .xz format has the size information available at
363# the end of the file too, but it's in more complex format and it's good to
364# avoid changing the part of the boot code that reads the uncompressed size.
365# Note that the bytes added by size_append will make the xz tool think that
366# the file is corrupt. This is expected.
367#
368# xzmisc doesn't use size_append, so it can be used to create normal .xz
369# files. xzmisc uses smaller LZMA2 dictionary than xzkern, because a very
370# big dictionary would increase the memory usage too much in the multi-call
371# decompression mode. A BCJ filter isn't used either.
372quiet_cmd_xzkern = XZKERN $@
373cmd_xzkern = (cat $(filter-out FORCE,$^) | \
374 sh $(srctree)/scripts/xz_wrap.sh && \
375 $(call size_append, $(filter-out FORCE,$^))) > $@ || \
376 (rm -f $@ ; false)
377
378quiet_cmd_xzmisc = XZMISC $@
379cmd_xzmisc = (cat $(filter-out FORCE,$^) | \
380 xz --check=crc32 --lzma2=dict=1MiB) > $@ || \
381 (rm -f $@ ; false)