xref: /linux/scripts/Makefile.lib (revision e27128db62834c5b906585c2d97f0ddd431fa28f)
1b2441318SGreg Kroah-Hartman# SPDX-License-Identifier: GPL-2.0
2f77bf014SSam Ravnborg# Backward compatibility
3f77bf014SSam Ravnborgasflags-y  += $(EXTRA_AFLAGS)
4f77bf014SSam Ravnborgccflags-y  += $(EXTRA_CFLAGS)
5f77bf014SSam Ravnborgcppflags-y += $(EXTRA_CPPFLAGS)
6f77bf014SSam Ravnborgldflags-y  += $(EXTRA_LDFLAGS)
7f77bf014SSam Ravnborg
88fdc3fbbSCao jin# flags that take effect in current and sub directories
94e13d47cSMasahiro YamadaKBUILD_AFLAGS += $(subdir-asflags-y)
104e13d47cSMasahiro YamadaKBUILD_CFLAGS += $(subdir-ccflags-y)
11720097d8SSam Ravnborg
121da177e4SLinus Torvalds# Figure out what we need to build from the various variables
131da177e4SLinus Torvalds# ===========================================================================
141da177e4SLinus Torvalds
151da177e4SLinus Torvalds# When an object is listed to be built compiled-in and modular,
161da177e4SLinus Torvalds# only build the compiled-in version
171da177e4SLinus Torvaldsobj-m := $(filter-out $(obj-y),$(obj-m))
181da177e4SLinus Torvalds
191da177e4SLinus Torvalds# Libraries are always collected in one lib file.
201da177e4SLinus Torvalds# Filter out objects already built-in
211da177e4SLinus Torvaldslib-y := $(filter-out $(obj-y), $(sort $(lib-y) $(lib-m)))
221da177e4SLinus Torvalds
238fdc3fbbSCao jin# Determine modorder.
248fdc3fbbSCao jin# Unfortunately, we don't have information about ordering between -y
258fdc3fbbSCao jin# and -m subdirs.  Just put -y's first.
268fdc3fbbSCao jinmodorder	:= $(patsubst %/,%/modules.order, $(filter %/, $(obj-y)) $(obj-m:.o=.ko))
271da177e4SLinus Torvalds
281da177e4SLinus Torvalds# Handle objects in subdirs
291da177e4SLinus Torvalds# ---------------------------------------------------------------------------
30f49821eeSNicholas Piggin# o if we encounter foo/ in $(obj-y), replace it by foo/built-in.a
311da177e4SLinus Torvalds#   and add the directory to the list of dirs to descend into: $(subdir-y)
321da177e4SLinus Torvalds# o if we encounter foo/ in $(obj-m), remove it from $(obj-m)
331da177e4SLinus Torvalds#   and add the directory to the list of dirs to descend into: $(subdir-m)
341da177e4SLinus Torvalds__subdir-y	:= $(patsubst %/,%,$(filter %/, $(obj-y)))
351da177e4SLinus Torvaldssubdir-y	+= $(__subdir-y)
361da177e4SLinus Torvalds__subdir-m	:= $(patsubst %/,%,$(filter %/, $(obj-m)))
371da177e4SLinus Torvaldssubdir-m	+= $(__subdir-m)
38f49821eeSNicholas Pigginobj-y		:= $(patsubst %/, %/built-in.a, $(obj-y))
391da177e4SLinus Torvaldsobj-m		:= $(filter-out %/, $(obj-m))
401da177e4SLinus Torvalds
411da177e4SLinus Torvalds# Subdirectories we need to descend into
421da177e4SLinus Torvaldssubdir-ym	:= $(sort $(subdir-y) $(subdir-m))
431da177e4SLinus Torvalds
4433e84f2eSMasahiro Yamada# If $(foo-objs), $(foo-y), $(foo-m), or $(foo-) exists, foo.o is a composite object
4533e84f2eSMasahiro Yamadamulti-used-y := $(sort $(foreach m,$(obj-y), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y)) $($(m:.o=-))), $(m))))
4633e84f2eSMasahiro Yamadamulti-used-m := $(sort $(foreach m,$(obj-m), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y)) $($(m:.o=-m)) $($(m:.o=-))), $(m))))
471da177e4SLinus Torvaldsmulti-used   := $(multi-used-y) $(multi-used-m)
481da177e4SLinus Torvalds
49f5fb9765SSam Ravnborg# $(subdir-obj-y) is the list of objects in $(obj-y) which uses dir/ to
50f5fb9765SSam Ravnborg# tell kbuild to descend
51f49821eeSNicholas Pigginsubdir-obj-y := $(filter %/built-in.a, $(obj-y))
521da177e4SLinus Torvalds
53f98fe47cSMasahiro Yamada# Replace multi-part objects by their individual parts,
54f98fe47cSMasahiro Yamada# including built-in.a from subdirectories
5533e84f2eSMasahiro Yamadareal-obj-y := $(foreach m, $(obj-y), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y)) $($(m:.o=-))),$($(m:.o=-objs)) $($(m:.o=-y)),$(m)))
5633e84f2eSMasahiro Yamadareal-obj-m := $(foreach m, $(obj-m), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y)) $($(m:.o=-m)) $($(m:.o=-))),$($(m:.o=-objs)) $($(m:.o=-y)) $($(m:.o=-m)),$(m)))
571da177e4SLinus Torvalds
587e7962ddSMasahiro Yamada# DTB
597e7962ddSMasahiro Yamada# If CONFIG_OF_ALL_DTBS is enabled, all DT blobs are built
607e7962ddSMasahiro Yamadaextra-y				+= $(dtb-y)
617e7962ddSMasahiro Yamadaextra-$(CONFIG_OF_ALL_DTBS)	+= $(dtb-)
627e7962ddSMasahiro Yamada
634f0e3a57SRob Herringifneq ($(CHECK_DTBS),)
644f0e3a57SRob Herringextra-y += $(patsubst %.dtb,%.dt.yaml, $(dtb-y))
654f0e3a57SRob Herringextra-$(CONFIG_OF_ALL_DTBS) += $(patsubst %.dtb,%.dt.yaml, $(dtb-))
664f0e3a57SRob Herringendif
674f0e3a57SRob Herring
68e846f0dcSJani Nikula# Test self-contained headers
691e21cbfaSMasahiro Yamada
701e21cbfaSMasahiro Yamada# Wildcard searches in $(srctree)/$(src)/, but not in $(objtree)/$(obj)/.
711e21cbfaSMasahiro Yamada# Stale generated headers are often left over, so pattern matching should
721e21cbfaSMasahiro Yamada# be avoided. Please notice $(srctree)/$(src)/ and $(objtree)/$(obj) point
731e21cbfaSMasahiro Yamada# to the same location for in-tree building. So, header-test-pattern-y should
741e21cbfaSMasahiro Yamada# be used with care.
751e21cbfaSMasahiro Yamadaheader-test-y	+= $(filter-out $(header-test-), \
761e21cbfaSMasahiro Yamada		$(patsubst $(srctree)/$(src)/%, %, \
771e21cbfaSMasahiro Yamada		$(wildcard $(addprefix $(srctree)/$(src)/, \
781e21cbfaSMasahiro Yamada		$(header-test-pattern-y)))))
791e21cbfaSMasahiro Yamada
804bd01de8SMasahiro Yamadaextra-$(CONFIG_HEADER_TEST) += $(addsuffix .s, $(header-test-y) $(header-test-m))
81e846f0dcSJani Nikula
821da177e4SLinus Torvalds# Add subdir path
831da177e4SLinus Torvalds
841da177e4SLinus Torvaldsextra-y		:= $(addprefix $(obj)/,$(extra-y))
851da177e4SLinus Torvaldsalways		:= $(addprefix $(obj)/,$(always))
861da177e4SLinus Torvaldstargets		:= $(addprefix $(obj)/,$(targets))
87551559e1STejun Heomodorder	:= $(addprefix $(obj)/,$(modorder))
881da177e4SLinus Torvaldsobj-m		:= $(addprefix $(obj)/,$(obj-m))
891da177e4SLinus Torvaldslib-y		:= $(addprefix $(obj)/,$(lib-y))
901da177e4SLinus Torvaldssubdir-obj-y	:= $(addprefix $(obj)/,$(subdir-obj-y))
91f5f33681SMasahiro Yamadareal-obj-y	:= $(addprefix $(obj)/,$(real-obj-y))
92f5f33681SMasahiro Yamadareal-obj-m	:= $(addprefix $(obj)/,$(real-obj-m))
931da177e4SLinus Torvaldsmulti-used-m	:= $(addprefix $(obj)/,$(multi-used-m))
941da177e4SLinus Torvaldssubdir-ym	:= $(addprefix $(obj)/,$(subdir-ym))
951da177e4SLinus Torvalds
96c0152e9aSMasahiro Yamada# Finds the multi-part object the current object will be linked into.
97c0152e9aSMasahiro Yamada# If the object belongs to two or more multi-part objects, all of them are
98c0152e9aSMasahiro Yamada# concatenated with a colon separator.
99c0152e9aSMasahiro Yamadamodname-multi = $(subst $(space),:,$(sort $(foreach m,$(multi-used),\
100c0152e9aSMasahiro Yamada		$(if $(filter $*.o, $($(m:.o=-objs)) $($(m:.o=-y)) $($(m:.o=-m))),$(m:.o=)))))
101c0152e9aSMasahiro Yamada
102c0152e9aSMasahiro Yamadamodname = $(if $(modname-multi),$(modname-multi),$(basetarget))
103c0152e9aSMasahiro Yamada
10454b8ae66SMasahiro Yamada# target with $(obj)/ and its suffix stripped
10554b8ae66SMasahiro Yamadatarget-stem = $(basename $(patsubst $(obj)/%,%,$@))
10654b8ae66SMasahiro Yamada
1071da177e4SLinus Torvalds# These flags are needed for modversions and compiling, so we define them here
1088fdc3fbbSCao jin# $(modname_flags) defines KBUILD_MODNAME as the name of the module it will
1091da177e4SLinus Torvalds# end up in (or would, if it gets compiled in)
110b42841b7SMichal Marekname-fix = $(squote)$(quote)$(subst $(comma),_,$(subst -,_,$1))$(quote)$(squote)
111b42841b7SMichal Marekbasename_flags = -DKBUILD_BASENAME=$(call name-fix,$(basetarget))
112aeacb019SMasahiro Yamadamodname_flags  = -DKBUILD_MODNAME=$(call name-fix,$(modname))
1131da177e4SLinus Torvalds
1144e13d47cSMasahiro Yamadaorig_c_flags   = $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) \
11554b8ae66SMasahiro Yamada                 $(ccflags-y) $(CFLAGS_$(target-stem).o)
11654b8ae66SMasahiro Yamada_c_flags       = $(filter-out $(CFLAGS_REMOVE_$(target-stem).o), $(orig_c_flags))
1174e13d47cSMasahiro Yamadaorig_a_flags   = $(KBUILD_CPPFLAGS) $(KBUILD_AFLAGS) \
11854b8ae66SMasahiro Yamada                 $(asflags-y) $(AFLAGS_$(target-stem).o)
11954b8ae66SMasahiro Yamada_a_flags       = $(filter-out $(AFLAGS_REMOVE_$(target-stem).o), $(orig_a_flags))
12054b8ae66SMasahiro Yamada_cpp_flags     = $(KBUILD_CPPFLAGS) $(cppflags-y) $(CPPFLAGS_$(target-stem).lds)
1211da177e4SLinus Torvalds
1222521f2c2SPeter Oberparleiter#
1232521f2c2SPeter Oberparleiter# Enable gcov profiling flags for a file, directory or for all files depending
1242521f2c2SPeter Oberparleiter# on variables GCOV_PROFILE_obj.o, GCOV_PROFILE and CONFIG_GCOV_PROFILE_ALL
1252521f2c2SPeter Oberparleiter# (in this order)
1262521f2c2SPeter Oberparleiter#
1272521f2c2SPeter Oberparleiterifeq ($(CONFIG_GCOV_KERNEL),y)
1282521f2c2SPeter Oberparleiter_c_flags += $(if $(patsubst n%,, \
1292521f2c2SPeter Oberparleiter		$(GCOV_PROFILE_$(basetarget).o)$(GCOV_PROFILE)$(CONFIG_GCOV_PROFILE_ALL)), \
1302521f2c2SPeter Oberparleiter		$(CFLAGS_GCOV))
1312521f2c2SPeter Oberparleiterendif
1322521f2c2SPeter Oberparleiter
1330b24beccSAndrey Ryabinin#
1340b24beccSAndrey Ryabinin# Enable address sanitizer flags for kernel except some files or directories
1350b24beccSAndrey Ryabinin# we don't want to check (depends on variables KASAN_SANITIZE_obj.o, KASAN_SANITIZE)
1360b24beccSAndrey Ryabinin#
1370b24beccSAndrey Ryabininifeq ($(CONFIG_KASAN),y)
1380b24beccSAndrey Ryabinin_c_flags += $(if $(patsubst n%,, \
1390b24beccSAndrey Ryabinin		$(KASAN_SANITIZE_$(basetarget).o)$(KASAN_SANITIZE)y), \
1400e410e15SAndrey Konovalov		$(CFLAGS_KASAN), $(CFLAGS_KASAN_NOSANITIZE))
1410b24beccSAndrey Ryabininendif
1420b24beccSAndrey Ryabinin
143c6d30853SAndrey Ryabininifeq ($(CONFIG_UBSAN),y)
144c6d30853SAndrey Ryabinin_c_flags += $(if $(patsubst n%,, \
145c6d30853SAndrey Ryabinin		$(UBSAN_SANITIZE_$(basetarget).o)$(UBSAN_SANITIZE)$(CONFIG_UBSAN_SANITIZE_ALL)), \
146c6d30853SAndrey Ryabinin		$(CFLAGS_UBSAN))
147c6d30853SAndrey Ryabininendif
148c6d30853SAndrey Ryabinin
1495c9a8750SDmitry Vyukovifeq ($(CONFIG_KCOV),y)
1505c9a8750SDmitry Vyukov_c_flags += $(if $(patsubst n%,, \
151a4691deaSVegard Nossum	$(KCOV_INSTRUMENT_$(basetarget).o)$(KCOV_INSTRUMENT)$(CONFIG_KCOV_INSTRUMENT_ALL)), \
1525c9a8750SDmitry Vyukov	$(CFLAGS_KCOV))
1535c9a8750SDmitry Vyukovendif
1545c9a8750SDmitry Vyukov
155cdd750bfSMasahiro Yamada# $(srctree)/$(src) for including checkin headers from generated source files
156cdd750bfSMasahiro Yamada# $(objtree)/$(obj) for including generated headers from checkin source files
15758156ba4SMasahiro Yamadaifeq ($(KBUILD_EXTMOD),)
158051f278eSMasahiro Yamadaifdef building_out_of_srctree
159cdd750bfSMasahiro Yamada_c_flags   += -I $(srctree)/$(src) -I $(objtree)/$(obj)
160cdd750bfSMasahiro Yamada_a_flags   += -I $(srctree)/$(src) -I $(objtree)/$(obj)
161cdd750bfSMasahiro Yamada_cpp_flags += -I $(srctree)/$(src) -I $(objtree)/$(obj)
1621da177e4SLinus Torvaldsendif
16358156ba4SMasahiro Yamadaendif
1641da177e4SLinus Torvalds
165eb27ea5cSMasahiro Yamadapart-of-module = $(if $(filter $(basename $@).o, $(real-obj-m)),y)
166eb27ea5cSMasahiro Yamadaquiet_modtag = $(if $(part-of-module),[M],   )
167eb27ea5cSMasahiro Yamada
168eb27ea5cSMasahiro Yamadamodkern_cflags =                                          \
169eb27ea5cSMasahiro Yamada	$(if $(part-of-module),                           \
170eb27ea5cSMasahiro Yamada		$(KBUILD_CFLAGS_MODULE) $(CFLAGS_MODULE), \
171eb27ea5cSMasahiro Yamada		$(KBUILD_CFLAGS_KERNEL) $(CFLAGS_KERNEL))
172eb27ea5cSMasahiro Yamada
173eb27ea5cSMasahiro Yamadamodkern_aflags = $(if $(part-of-module),				\
174eb27ea5cSMasahiro Yamada			$(KBUILD_AFLAGS_MODULE) $(AFLAGS_MODULE),	\
175eb27ea5cSMasahiro Yamada			$(KBUILD_AFLAGS_KERNEL) $(AFLAGS_KERNEL))
176eb27ea5cSMasahiro Yamada
177d8672b40SSam Ravnborgc_flags        = -Wp,-MD,$(depfile) $(NOSTDINC_FLAGS) $(LINUXINCLUDE)     \
178a95b37e2SMasahiro Yamada		 -include $(srctree)/include/linux/compiler_types.h       \
179cdd750bfSMasahiro Yamada		 $(_c_flags) $(modkern_cflags)                           \
180b42841b7SMichal Marek		 $(basename_flags) $(modname_flags)
1811da177e4SLinus Torvalds
182d8672b40SSam Ravnborga_flags        = -Wp,-MD,$(depfile) $(NOSTDINC_FLAGS) $(LINUXINCLUDE)     \
183cdd750bfSMasahiro Yamada		 $(_a_flags) $(modkern_aflags)
1841da177e4SLinus Torvalds
185d8672b40SSam Ravnborgcpp_flags      = -Wp,-MD,$(depfile) $(NOSTDINC_FLAGS) $(LINUXINCLUDE)     \
186cdd750bfSMasahiro Yamada		 $(_cpp_flags)
1871da177e4SLinus Torvalds
188d503ac53SMasahiro Yamadald_flags       = $(KBUILD_LDFLAGS) $(ldflags-y) $(LDFLAGS_$(@F))
1891da177e4SLinus Torvalds
19050f9ddafSMasahiro YamadaDTC_INCLUDE    := $(srctree)/scripts/dtc/include-prefixes
19150f9ddafSMasahiro Yamada
192b0a4d8b3SIan Campbelldtc_cpp_flags  = -Wp,-MD,$(depfile).pre.tmp -nostdinc                    \
19350f9ddafSMasahiro Yamada		 $(addprefix -I,$(DTC_INCLUDE))                          \
194e570d7c1SStephen Warren		 -undef -D__DTS__
195e570d7c1SStephen Warren
196c8589d1eSMasahiro Yamada# Useful for describing the dependency of composite objects
197c8589d1eSMasahiro Yamada# Usage:
198c8589d1eSMasahiro Yamada#   $(call multi_depend, multi_used_targets, suffix_to_remove, suffix_to_add)
199c8589d1eSMasahiro Yamadadefine multi_depend
200c8589d1eSMasahiro Yamada$(foreach m, $(notdir $1), \
201c8589d1eSMasahiro Yamada	$(eval $(obj)/$m: \
202c8589d1eSMasahiro Yamada	$(addprefix $(obj)/, $(foreach s, $3, $($(m:%$(strip $2)=%$(s)))))))
203c8589d1eSMasahiro Yamadaendef
204c8589d1eSMasahiro Yamada
2051da177e4SLinus Torvalds# Shipped files
2061da177e4SLinus Torvalds# ===========================================================================
2071da177e4SLinus Torvalds
2081da177e4SLinus Torvaldsquiet_cmd_shipped = SHIPPED $@
2091da177e4SLinus Torvaldscmd_shipped = cat $< > $@
2101da177e4SLinus Torvalds
211991d76c9SArnaud Lacombe$(obj)/%: $(src)/%_shipped
2121da177e4SLinus Torvalds	$(call cmd,shipped)
2131da177e4SLinus Torvalds
2141da177e4SLinus Torvalds# Commands useful for building a boot image
2151da177e4SLinus Torvalds# ===========================================================================
2161da177e4SLinus Torvalds#
2171da177e4SLinus Torvalds#	Use as following:
2181da177e4SLinus Torvalds#
2191da177e4SLinus Torvalds#	target: source(s) FORCE
2201da177e4SLinus Torvalds#		$(if_changed,ld/objcopy/gzip)
2211da177e4SLinus Torvalds#
222836caba7SSam Ravnborg#	and add target to extra-y so that we know we have to
2231da177e4SLinus Torvalds#	read in the saved command line
2241da177e4SLinus Torvalds
2251da177e4SLinus Torvalds# Linking
2261da177e4SLinus Torvalds# ---------------------------------------------------------------------------
2271da177e4SLinus Torvalds
2281da177e4SLinus Torvaldsquiet_cmd_ld = LD      $@
229afa974b7SMasahiro Yamada      cmd_ld = $(LD) $(ld_flags) $(real-prereqs) -o $@
2301da177e4SLinus Torvalds
231898f5a00SMasahiro Yamada# Archive
232898f5a00SMasahiro Yamada# ---------------------------------------------------------------------------
233898f5a00SMasahiro Yamada
234898f5a00SMasahiro Yamadaquiet_cmd_ar = AR      $@
235898f5a00SMasahiro Yamada      cmd_ar = rm -f $@; $(AR) rcsTP$(KBUILD_ARFLAGS) $@ $(real-prereqs)
236898f5a00SMasahiro Yamada
2371da177e4SLinus Torvalds# Objcopy
2381da177e4SLinus Torvalds# ---------------------------------------------------------------------------
2391da177e4SLinus Torvalds
2401da177e4SLinus Torvaldsquiet_cmd_objcopy = OBJCOPY $@
2411da177e4SLinus Torvaldscmd_objcopy = $(OBJCOPY) $(OBJCOPYFLAGS) $(OBJCOPYFLAGS_$(@F)) $< $@
2421da177e4SLinus Torvalds
2431da177e4SLinus Torvalds# Gzip
2441da177e4SLinus Torvalds# ---------------------------------------------------------------------------
2451da177e4SLinus Torvalds
2461da177e4SLinus Torvaldsquiet_cmd_gzip = GZIP    $@
247afa974b7SMasahiro Yamada      cmd_gzip = cat $(real-prereqs) | gzip -n -f -9 > $@
2481da177e4SLinus Torvalds
249aab94339SDirk Brandewie# DTC
250aab94339SDirk Brandewie# ---------------------------------------------------------------------------
2516b22b3d1SRob HerringDTC ?= $(objtree)/scripts/dtc/dtc
252aab94339SDirk Brandewie
253bc553986SRob Herring# Disable noisy checks by default
254*e27128dbSMasahiro Yamadaifeq ($(findstring 1,$(KBUILD_EXTRA_WARN)),)
2558654cb8dSRob HerringDTC_FLAGS += -Wno-unit_address_vs_reg \
2568654cb8dSRob Herring	-Wno-unit_address_format \
2574fd98e37SRob Herring	-Wno-avoid_unnecessary_addr_size \
2584fd98e37SRob Herring	-Wno-alias_paths \
25974656b68SRob Herring	-Wno-graph_child_address \
26070523a3cSRob Herring	-Wno-simple_bus_reg \
26174656b68SRob Herring	-Wno-unique_unit_address \
2628654cb8dSRob Herring	-Wno-pci_device_reg
263bc553986SRob Herringendif
264bc553986SRob Herring
265*e27128dbSMasahiro Yamadaifneq ($(findstring 2,$(KBUILD_EXTRA_WARN)),)
2668654cb8dSRob HerringDTC_FLAGS += -Wnode_name_chars_strict \
2678654cb8dSRob Herring	-Wproperty_name_chars_strict
268aab94339SDirk Brandewieendif
269aab94339SDirk Brandewie
270331f7416SFrank RowandDTC_FLAGS += $(DTC_FLAGS_$(basetarget))
271331f7416SFrank Rowand
272aab94339SDirk Brandewie# Generate an assembly file to wrap the output of the device tree compiler
273aab94339SDirk Brandewiequiet_cmd_dt_S_dtb= DTB     $@
274aab94339SDirk Brandewiecmd_dt_S_dtb=						\
275b79c6aa6SMasahiro Yamada{							\
276aab94339SDirk Brandewie	echo '\#include <asm-generic/vmlinux.lds.h>'; 	\
277aab94339SDirk Brandewie	echo '.section .dtb.init.rodata,"a"';		\
278aab94339SDirk Brandewie	echo '.balign STRUCT_ALIGNMENT';		\
27955fe6da9SJames Hogan	echo '.global __dtb_$(subst -,_,$(*F))_begin';	\
28055fe6da9SJames Hogan	echo '__dtb_$(subst -,_,$(*F))_begin:';		\
281aab94339SDirk Brandewie	echo '.incbin "$<" ';				\
28255fe6da9SJames Hogan	echo '__dtb_$(subst -,_,$(*F))_end:';		\
28355fe6da9SJames Hogan	echo '.global __dtb_$(subst -,_,$(*F))_end';	\
284aab94339SDirk Brandewie	echo '.balign STRUCT_ALIGNMENT'; 		\
285b79c6aa6SMasahiro Yamada} > $@
286aab94339SDirk Brandewie
287a7f92419SMasahiro Yamada$(obj)/%.dtb.S: $(obj)/%.dtb FORCE
288a7f92419SMasahiro Yamada	$(call if_changed,dt_S_dtb)
289aab94339SDirk Brandewie
290aab94339SDirk Brandewiequiet_cmd_dtc = DTC     $@
29177479b38SNathan Rossicmd_dtc = mkdir -p $(dir ${dtc-tmp}) ; \
29237c8a5faSRob Herring	$(HOSTCC) -E $(dtc_cpp_flags) -x assembler-with-cpp -o $(dtc-tmp) $< ; \
2934f0e3a57SRob Herring	$(DTC) -O $(2) -o $@ -b 0 \
29450f9ddafSMasahiro Yamada		$(addprefix -i,$(dir $<) $(DTC_INCLUDE)) $(DTC_FLAGS) \
295b0a4d8b3SIan Campbell		-d $(depfile).dtc.tmp $(dtc-tmp) ; \
296b0a4d8b3SIan Campbell	cat $(depfile).pre.tmp $(depfile).dtc.tmp > $(depfile)
2971da177e4SLinus Torvalds
298b8fc5b21SRob Herring$(obj)/%.dtb: $(src)/%.dts $(DTC) FORCE
2994f0e3a57SRob Herring	$(call if_changed_dep,dtc,dtb)
3004f0e3a57SRob Herring
3014f0e3a57SRob HerringDT_CHECKER ?= dt-validate
3024f0e3a57SRob HerringDT_BINDING_DIR := Documentation/devicetree/bindings
3034f0e3a57SRob HerringDT_TMP_SCHEMA := $(objtree)/$(DT_BINDING_DIR)/processed-schema.yaml
3044f0e3a57SRob Herring
3054f0e3a57SRob Herringquiet_cmd_dtb_check =	CHECK   $@
30605aeca7cSRob Herring      cmd_dtb_check =	$(DT_CHECKER) -u $(srctree)/$(DT_BINDING_DIR) -p $(DT_TMP_SCHEMA) $@ ;
3074f0e3a57SRob Herring
3084f0e3a57SRob Herringdefine rule_dtc_dt_yaml
309f5688663SMasahiro Yamada	$(call cmd_and_fixdep,dtc,yaml)
310f5688663SMasahiro Yamada	$(call cmd,dtb_check)
3114f0e3a57SRob Herringendef
3124f0e3a57SRob Herring
3134f0e3a57SRob Herring$(obj)/%.dt.yaml: $(src)/%.dts $(DTC) $(DT_TMP_SCHEMA) FORCE
3144f0e3a57SRob Herring	$(call if_changed_rule,dtc_dt_yaml)
31590b335fbSStephen Warren
316b0a4d8b3SIan Campbelldtc-tmp = $(subst $(comma),_,$(dot-target).dts.tmp)
31722435f38SStephen Warren
318bc22c17eSAlain Knaff# Bzip2
319bc22c17eSAlain Knaff# ---------------------------------------------------------------------------
320bc22c17eSAlain Knaff
321d3dd3b5aSH. Peter Anvin# Bzip2 and LZMA do not include size in file... so we have to fake that;
322d3dd3b5aSH. Peter Anvin# append the size as a 32-bit littleendian number as gzip does.
3234a2ff67cSMichael Tokarevsize_append = printf $(shell						\
324d3dd3b5aSH. Peter Anvindec_size=0;								\
325afa974b7SMasahiro Yamadafor F in $(real-prereqs); do					\
326a670b0b4SMichael Forney	fsize=$$($(CONFIG_SHELL) $(srctree)/scripts/file-size.sh $$F);	\
327d3dd3b5aSH. Peter Anvin	dec_size=$$(expr $$dec_size + $$fsize);				\
328d3dd3b5aSH. Peter Anvindone;									\
3291373411aSJonathan Niederprintf "%08x\n" $$dec_size |						\
3301373411aSJonathan Nieder	sed 's/\(..\)/\1 /g' | {					\
3311373411aSJonathan Nieder		read ch0 ch1 ch2 ch3;					\
3321373411aSJonathan Nieder		for ch in $$ch3 $$ch2 $$ch1 $$ch0; do			\
3331373411aSJonathan Nieder			printf '%s%03o' '\\' $$((0x$$ch)); 		\
3341373411aSJonathan Nieder		done;							\
3351373411aSJonathan Nieder	}								\
336d3dd3b5aSH. Peter Anvin)
337bc22c17eSAlain Knaff
338bc22c17eSAlain Knaffquiet_cmd_bzip2 = BZIP2   $@
339b956c7a6SHarald Seiler      cmd_bzip2 = { cat $(real-prereqs) | bzip2 -9; $(size_append); } > $@
340bc22c17eSAlain Knaff
341bc22c17eSAlain Knaff# Lzma
342bc22c17eSAlain Knaff# ---------------------------------------------------------------------------
343bc22c17eSAlain Knaff
344bc22c17eSAlain Knaffquiet_cmd_lzma = LZMA    $@
345b956c7a6SHarald Seiler      cmd_lzma = { cat $(real-prereqs) | lzma -9; $(size_append); } > $@
3467dd65febSAlbin Tonnerre
3477dd65febSAlbin Tonnerrequiet_cmd_lzo = LZO     $@
348b956c7a6SHarald Seiler      cmd_lzo = { cat $(real-prereqs) | lzop -9; $(size_append); } > $@
349d61931d8SBorislav Petkov
350e76e1fdfSKyungsik Leequiet_cmd_lz4 = LZ4     $@
351b956c7a6SHarald Seiler      cmd_lz4 = { cat $(real-prereqs) | lz4c -l -c1 stdin stdout; \
352b79c6aa6SMasahiro Yamada                  $(size_append); } > $@
353e76e1fdfSKyungsik Lee
354e3393645SStephen Warren# U-Boot mkimage
355e3393645SStephen Warren# ---------------------------------------------------------------------------
356e3393645SStephen Warren
357e3393645SStephen WarrenMKIMAGE := $(srctree)/scripts/mkuboot.sh
358e3393645SStephen Warren
359e3393645SStephen Warren# SRCARCH just happens to match slightly more than ARCH (on sparc), so reduces
360e3393645SStephen Warren# the number of overrides in arch makefiles
361e3393645SStephen WarrenUIMAGE_ARCH ?= $(SRCARCH)
362e3393645SStephen WarrenUIMAGE_COMPRESSION ?= $(if $(2),$(2),none)
363e3393645SStephen WarrenUIMAGE_OPTS-y ?=
364e3393645SStephen WarrenUIMAGE_TYPE ?= kernel
365e3393645SStephen WarrenUIMAGE_LOADADDR ?= arch_must_set_this
366e3393645SStephen WarrenUIMAGE_ENTRYADDR ?= $(UIMAGE_LOADADDR)
367e3393645SStephen WarrenUIMAGE_NAME ?= 'Linux-$(KERNELRELEASE)'
368e3393645SStephen Warren
369786ac51aSMasahiro Yamadaquiet_cmd_uimage = UIMAGE  $@
370858805b3SMasahiro Yamada      cmd_uimage = $(BASH) $(MKIMAGE) -A $(UIMAGE_ARCH) -O linux \
371e3393645SStephen Warren			-C $(UIMAGE_COMPRESSION) $(UIMAGE_OPTS-y) \
372e3393645SStephen Warren			-T $(UIMAGE_TYPE) \
373e3393645SStephen Warren			-a $(UIMAGE_LOADADDR) -e $(UIMAGE_ENTRYADDR) \
374786ac51aSMasahiro Yamada			-n $(UIMAGE_NAME) -d $< $@
375e3393645SStephen Warren
37624fa0402SLasse Collin# XZ
37724fa0402SLasse Collin# ---------------------------------------------------------------------------
37824fa0402SLasse Collin# Use xzkern to compress the kernel image and xzmisc to compress other things.
37924fa0402SLasse Collin#
38024fa0402SLasse Collin# xzkern uses a big LZMA2 dictionary since it doesn't increase memory usage
38124fa0402SLasse Collin# of the kernel decompressor. A BCJ filter is used if it is available for
38224fa0402SLasse Collin# the target architecture. xzkern also appends uncompressed size of the data
38324fa0402SLasse Collin# using size_append. The .xz format has the size information available at
38424fa0402SLasse Collin# the end of the file too, but it's in more complex format and it's good to
38524fa0402SLasse Collin# avoid changing the part of the boot code that reads the uncompressed size.
38624fa0402SLasse Collin# Note that the bytes added by size_append will make the xz tool think that
38724fa0402SLasse Collin# the file is corrupt. This is expected.
38824fa0402SLasse Collin#
38924fa0402SLasse Collin# xzmisc doesn't use size_append, so it can be used to create normal .xz
39024fa0402SLasse Collin# files. xzmisc uses smaller LZMA2 dictionary than xzkern, because a very
39124fa0402SLasse Collin# big dictionary would increase the memory usage too much in the multi-call
39224fa0402SLasse Collin# decompression mode. A BCJ filter isn't used either.
39324fa0402SLasse Collinquiet_cmd_xzkern = XZKERN  $@
394b956c7a6SHarald Seiler      cmd_xzkern = { cat $(real-prereqs) | sh $(srctree)/scripts/xz_wrap.sh; \
395b79c6aa6SMasahiro Yamada                     $(size_append); } > $@
39624fa0402SLasse Collin
39724fa0402SLasse Collinquiet_cmd_xzmisc = XZMISC  $@
398b79c6aa6SMasahiro Yamada      cmd_xzmisc = cat $(real-prereqs) | xz --check=crc32 --lzma2=dict=1MiB > $@
399ebf003f0SMatthias Kaehlcke
400ebf003f0SMatthias Kaehlcke# ASM offsets
401ebf003f0SMatthias Kaehlcke# ---------------------------------------------------------------------------
402ebf003f0SMatthias Kaehlcke
403ebf003f0SMatthias Kaehlcke# Default sed regexp - multiline due to syntax constraints
404cf0c3e68SJeroen Hofstee#
405cf0c3e68SJeroen Hofstee# Use [:space:] because LLVM's integrated assembler inserts <tab> around
406cf0c3e68SJeroen Hofstee# the .ascii directive whereas GCC keeps the <space> as-is.
407ebf003f0SMatthias Kaehlckedefine sed-offsets
408cf0c3e68SJeroen Hofstee	's:^[[:space:]]*\.ascii[[:space:]]*"\(.*\)".*:\1:; \
409cf0c3e68SJeroen Hofstee	/^->/{s:->#\(.*\):/* \1 */:; \
410ebf003f0SMatthias Kaehlcke	s:^->\([^ ]*\) [\$$#]*\([^ ]*\) \(.*\):#define \1 \2 /* \3 */:; \
411cf0c3e68SJeroen Hofstee	s:->::; p;}'
412ebf003f0SMatthias Kaehlckeendef
413ebf003f0SMatthias Kaehlcke
414ebf003f0SMatthias Kaehlcke# Use filechk to avoid rebuilds when a header changes, but the resulting file
415ebf003f0SMatthias Kaehlcke# does not
416ebf003f0SMatthias Kaehlckedefine filechk_offsets
417ebf003f0SMatthias Kaehlcke	 echo "#ifndef $2"; \
418ebf003f0SMatthias Kaehlcke	 echo "#define $2"; \
419ebf003f0SMatthias Kaehlcke	 echo "/*"; \
420ebf003f0SMatthias Kaehlcke	 echo " * DO NOT MODIFY."; \
421ebf003f0SMatthias Kaehlcke	 echo " *"; \
422ebf003f0SMatthias Kaehlcke	 echo " * This file was generated by Kbuild"; \
423ebf003f0SMatthias Kaehlcke	 echo " */"; \
424ebf003f0SMatthias Kaehlcke	 echo ""; \
42543fee2b2SMasahiro Yamada	 sed -ne $(sed-offsets) < $<; \
426ebf003f0SMatthias Kaehlcke	 echo ""; \
427ad774086SMasahiro Yamada	 echo "#endif"
428ebf003f0SMatthias Kaehlckeendef
429