xref: /linux/scripts/Makefile.lib (revision e4a42c82e943b97ce124539fcd7a47445b43fa0d)
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)
759721d4eSMasahiro Yamadaifneq ($(always),)
859721d4eSMasahiro Yamada$(warning 'always' is deprecated. Please use 'always-y' instead)
95f2fb52fSMasahiro Yamadaalways-y   += $(always)
1059721d4eSMasahiro Yamadaendif
1159721d4eSMasahiro Yamadaifneq ($(hostprogs-y),)
1259721d4eSMasahiro Yamada$(warning 'hostprogs-y' is deprecated. Please use 'hostprogs' instead)
1359721d4eSMasahiro Yamadahostprogs  += $(hostprogs-y)
1459721d4eSMasahiro Yamadaendif
1559721d4eSMasahiro Yamadaifneq ($(hostprogs-m),)
1659721d4eSMasahiro Yamada$(warning 'hostprogs-m' is deprecated. Please use 'hostprogs' instead)
1759721d4eSMasahiro Yamadahostprogs  += $(hostprogs-m)
1859721d4eSMasahiro Yamadaendif
19f77bf014SSam Ravnborg
208fdc3fbbSCao jin# flags that take effect in current and sub directories
214e13d47cSMasahiro YamadaKBUILD_AFLAGS += $(subdir-asflags-y)
224e13d47cSMasahiro YamadaKBUILD_CFLAGS += $(subdir-ccflags-y)
23720097d8SSam Ravnborg
241da177e4SLinus Torvalds# Figure out what we need to build from the various variables
251da177e4SLinus Torvalds# ===========================================================================
261da177e4SLinus Torvalds
271da177e4SLinus Torvalds# When an object is listed to be built compiled-in and modular,
281da177e4SLinus Torvalds# only build the compiled-in version
291da177e4SLinus Torvaldsobj-m := $(filter-out $(obj-y),$(obj-m))
301da177e4SLinus Torvalds
311da177e4SLinus Torvalds# Libraries are always collected in one lib file.
321da177e4SLinus Torvalds# Filter out objects already built-in
331da177e4SLinus Torvaldslib-y := $(filter-out $(obj-y), $(sort $(lib-y) $(lib-m)))
341da177e4SLinus Torvalds
350a8820e7SMasahiro Yamada# Subdirectories we need to descend into
360a8820e7SMasahiro Yamadasubdir-ym := $(sort $(subdir-y) $(subdir-m) \
370a8820e7SMasahiro Yamada			$(patsubst %/,%, $(filter %/, $(obj-y) $(obj-m))))
380a8820e7SMasahiro Yamada
39b2c88554SMasahiro Yamada# Handle objects in subdirs:
40b2c88554SMasahiro Yamada# - If we encounter foo/ in $(obj-y), replace it by foo/built-in.a and
41b2c88554SMasahiro Yamada#   foo/modules.order
42b2c88554SMasahiro Yamada# - If we encounter foo/ in $(obj-m), replace it by foo/modules.order
43b2c88554SMasahiro Yamada#
44b2c88554SMasahiro Yamada# Generate modules.order to determine modorder. Unfortunately, we don't have
45b2c88554SMasahiro Yamada# information about ordering between -y and -m subdirs. Just put -y's first.
46b2c88554SMasahiro Yamada
47b2c88554SMasahiro Yamadaifdef need-modorder
48b2c88554SMasahiro Yamadaobj-m := $(patsubst %/,%/modules.order, $(filter %/, $(obj-y)) $(obj-m))
49b2c88554SMasahiro Yamadaelse
50b2c88554SMasahiro Yamadaobj-m := $(filter-out %/, $(obj-m))
51b2c88554SMasahiro Yamadaendif
52b2c88554SMasahiro Yamada
5356d58936SMasahiro Yamadaifdef need-builtin
54f49821eeSNicholas Pigginobj-y		:= $(patsubst %/, %/built-in.a, $(obj-y))
5556d58936SMasahiro Yamadaelse
5656d58936SMasahiro Yamadaobj-y		:= $(filter-out %/, $(obj-y))
5756d58936SMasahiro Yamadaendif
581da177e4SLinus Torvalds
5933e84f2eSMasahiro Yamada# If $(foo-objs), $(foo-y), $(foo-m), or $(foo-) exists, foo.o is a composite object
6033e84f2eSMasahiro Yamadamulti-used-y := $(sort $(foreach m,$(obj-y), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y)) $($(m:.o=-))), $(m))))
6133e84f2eSMasahiro Yamadamulti-used-m := $(sort $(foreach m,$(obj-m), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y)) $($(m:.o=-m)) $($(m:.o=-))), $(m))))
621da177e4SLinus Torvaldsmulti-used   := $(multi-used-y) $(multi-used-m)
631da177e4SLinus Torvalds
64f98fe47cSMasahiro Yamada# Replace multi-part objects by their individual parts,
65f98fe47cSMasahiro Yamada# including built-in.a from subdirectories
6633e84f2eSMasahiro Yamadareal-obj-y := $(foreach m, $(obj-y), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y)) $($(m:.o=-))),$($(m:.o=-objs)) $($(m:.o=-y)),$(m)))
6733e84f2eSMasahiro 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)))
681da177e4SLinus Torvalds
695f2fb52fSMasahiro Yamadaalways-y += $(always-m)
705f2fb52fSMasahiro Yamada
717e7962ddSMasahiro Yamada# DTB
727e7962ddSMasahiro Yamada# If CONFIG_OF_ALL_DTBS is enabled, all DT blobs are built
737e7962ddSMasahiro Yamadaextra-y				+= $(dtb-y)
747e7962ddSMasahiro Yamadaextra-$(CONFIG_OF_ALL_DTBS)	+= $(dtb-)
757e7962ddSMasahiro Yamada
764f0e3a57SRob Herringifneq ($(CHECK_DTBS),)
774f0e3a57SRob Herringextra-y += $(patsubst %.dtb,%.dt.yaml, $(dtb-y))
784f0e3a57SRob Herringextra-$(CONFIG_OF_ALL_DTBS) += $(patsubst %.dtb,%.dt.yaml, $(dtb-))
794f0e3a57SRob Herringendif
804f0e3a57SRob Herring
811da177e4SLinus Torvalds# Add subdir path
821da177e4SLinus Torvalds
831da177e4SLinus Torvaldsextra-y		:= $(addprefix $(obj)/,$(extra-y))
845f2fb52fSMasahiro Yamadaalways-y	:= $(addprefix $(obj)/,$(always-y))
851da177e4SLinus Torvaldstargets		:= $(addprefix $(obj)/,$(targets))
861da177e4SLinus Torvaldsobj-m		:= $(addprefix $(obj)/,$(obj-m))
871da177e4SLinus Torvaldslib-y		:= $(addprefix $(obj)/,$(lib-y))
88f5f33681SMasahiro Yamadareal-obj-y	:= $(addprefix $(obj)/,$(real-obj-y))
89f5f33681SMasahiro Yamadareal-obj-m	:= $(addprefix $(obj)/,$(real-obj-m))
901da177e4SLinus Torvaldsmulti-used-m	:= $(addprefix $(obj)/,$(multi-used-m))
911da177e4SLinus Torvaldssubdir-ym	:= $(addprefix $(obj)/,$(subdir-ym))
921da177e4SLinus Torvalds
93c0152e9aSMasahiro Yamada# Finds the multi-part object the current object will be linked into.
941664a377SMasahiro Yamada# If the object belongs to two or more multi-part objects, list them all.
951664a377SMasahiro Yamadamodname-multi = $(sort $(foreach m,$(multi-used),\
961664a377SMasahiro Yamada		$(if $(filter $*.o, $($(m:.o=-objs)) $($(m:.o=-y)) $($(m:.o=-m))),$(m:.o=))))
97c0152e9aSMasahiro Yamada
981664a377SMasahiro Yamada__modname = $(if $(modname-multi),$(modname-multi),$(basetarget))
991664a377SMasahiro Yamada
1001664a377SMasahiro Yamadamodname = $(subst $(space),:,$(__modname))
1011664a377SMasahiro Yamadamodfile = $(addprefix $(obj)/,$(__modname))
102c0152e9aSMasahiro Yamada
10354b8ae66SMasahiro Yamada# target with $(obj)/ and its suffix stripped
10454b8ae66SMasahiro Yamadatarget-stem = $(basename $(patsubst $(obj)/%,%,$@))
10554b8ae66SMasahiro Yamada
1061da177e4SLinus Torvalds# These flags are needed for modversions and compiling, so we define them here
1078fdc3fbbSCao jin# $(modname_flags) defines KBUILD_MODNAME as the name of the module it will
1081da177e4SLinus Torvalds# end up in (or would, if it gets compiled in)
1097e826c44SMasahiro Yamadaname-fix = $(call stringify,$(subst $(comma),_,$(subst -,_,$1)))
110b42841b7SMichal Marekbasename_flags = -DKBUILD_BASENAME=$(call name-fix,$(basetarget))
111aeacb019SMasahiro Yamadamodname_flags  = -DKBUILD_MODNAME=$(call name-fix,$(modname))
1121664a377SMasahiro Yamadamodfile_flags  = -DKBUILD_MODFILE=$(call stringify,$(modfile))
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), \
1711664a377SMasahiro Yamada		$(KBUILD_CFLAGS_KERNEL) $(CFLAGS_KERNEL) $(modfile_flags))
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
17730a77297SMasahiro Yamadac_flags        = -Wp,-MMD,$(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
18230a77297SMasahiro Yamadaa_flags        = -Wp,-MMD,$(depfile) $(NOSTDINC_FLAGS) $(LINUXINCLUDE)     \
183cdd750bfSMasahiro Yamada		 $(_a_flags) $(modkern_aflags)
1841da177e4SLinus Torvalds
18530a77297SMasahiro Yamadacpp_flags      = -Wp,-MMD,$(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
19230a77297SMasahiro Yamadadtc_cpp_flags  = -Wp,-MMD,$(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      $@
23513dc8c02SMasahiro Yamada      cmd_ar = rm -f $@; $(AR) cDPrsT $@ $(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    $@
247*e4a42c82SDenis Efremov      cmd_gzip = cat $(real-prereqs) | $(KGZIP) -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
254e27128dbSMasahiro 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
265e27128dbSMasahiro 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) $< ; \
29378046fabSMasahiro Yamada	$(DTC) -O $(patsubst .%,%,$(suffix $@)) -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
29978046fabSMasahiro Yamada	$(call if_changed_dep,dtc)
3004f0e3a57SRob Herring
3014f0e3a57SRob HerringDT_CHECKER ?= dt-validate
3024f0e3a57SRob HerringDT_BINDING_DIR := Documentation/devicetree/bindings
3032ba06cd8SRob Herring# DT_TMP_SCHEMA may be overridden from Documentation/devicetree/bindings/Makefile
3042ba06cd8SRob HerringDT_TMP_SCHEMA ?= $(objtree)/$(DT_BINDING_DIR)/processed-schema.yaml
3054f0e3a57SRob Herring
3064f0e3a57SRob Herringquiet_cmd_dtb_check =	CHECK   $@
307fd63fab4SMasahiro Yamada      cmd_dtb_check =	$(DT_CHECKER) -u $(srctree)/$(DT_BINDING_DIR) -p $(DT_TMP_SCHEMA) $@
3084f0e3a57SRob Herring
3097a049605SMasahiro Yamadadefine rule_dtc
31078046fabSMasahiro Yamada	$(call cmd_and_fixdep,dtc)
311f5688663SMasahiro Yamada	$(call cmd,dtb_check)
3124f0e3a57SRob Herringendef
3134f0e3a57SRob Herring
3144f0e3a57SRob Herring$(obj)/%.dt.yaml: $(src)/%.dts $(DTC) $(DT_TMP_SCHEMA) FORCE
3153d4b2238SMasahiro Yamada	$(call if_changed_rule,dtc,yaml)
31690b335fbSStephen Warren
317b0a4d8b3SIan Campbelldtc-tmp = $(subst $(comma),_,$(dot-target).dts.tmp)
31822435f38SStephen Warren
319bc22c17eSAlain Knaff# Bzip2
320bc22c17eSAlain Knaff# ---------------------------------------------------------------------------
321bc22c17eSAlain Knaff
322d3dd3b5aSH. Peter Anvin# Bzip2 and LZMA do not include size in file... so we have to fake that;
323d3dd3b5aSH. Peter Anvin# append the size as a 32-bit littleendian number as gzip does.
3244a2ff67cSMichael Tokarevsize_append = printf $(shell						\
325d3dd3b5aSH. Peter Anvindec_size=0;								\
326afa974b7SMasahiro Yamadafor F in $(real-prereqs); do					\
327a670b0b4SMichael Forney	fsize=$$($(CONFIG_SHELL) $(srctree)/scripts/file-size.sh $$F);	\
328d3dd3b5aSH. Peter Anvin	dec_size=$$(expr $$dec_size + $$fsize);				\
329d3dd3b5aSH. Peter Anvindone;									\
3301373411aSJonathan Niederprintf "%08x\n" $$dec_size |						\
3311373411aSJonathan Nieder	sed 's/\(..\)/\1 /g' | {					\
3321373411aSJonathan Nieder		read ch0 ch1 ch2 ch3;					\
3331373411aSJonathan Nieder		for ch in $$ch3 $$ch2 $$ch1 $$ch0; do			\
3341373411aSJonathan Nieder			printf '%s%03o' '\\' $$((0x$$ch)); 		\
3351373411aSJonathan Nieder		done;							\
3361373411aSJonathan Nieder	}								\
337d3dd3b5aSH. Peter Anvin)
338bc22c17eSAlain Knaff
339bc22c17eSAlain Knaffquiet_cmd_bzip2 = BZIP2   $@
340*e4a42c82SDenis Efremov      cmd_bzip2 = { cat $(real-prereqs) | $(KBZIP2) -9; $(size_append); } > $@
341bc22c17eSAlain Knaff
342bc22c17eSAlain Knaff# Lzma
343bc22c17eSAlain Knaff# ---------------------------------------------------------------------------
344bc22c17eSAlain Knaff
345bc22c17eSAlain Knaffquiet_cmd_lzma = LZMA    $@
3468dfb61dcSDenis Efremov      cmd_lzma = { cat $(real-prereqs) | $(LZMA) -9; $(size_append); } > $@
3477dd65febSAlbin Tonnerre
3487dd65febSAlbin Tonnerrequiet_cmd_lzo = LZO     $@
349*e4a42c82SDenis Efremov      cmd_lzo = { cat $(real-prereqs) | $(KLZOP) -9; $(size_append); } > $@
350d61931d8SBorislav Petkov
351e76e1fdfSKyungsik Leequiet_cmd_lz4 = LZ4     $@
3528dfb61dcSDenis Efremov      cmd_lz4 = { cat $(real-prereqs) | $(LZ4) -l -c1 stdin stdout; \
353b79c6aa6SMasahiro Yamada                  $(size_append); } > $@
354e76e1fdfSKyungsik Lee
355e3393645SStephen Warren# U-Boot mkimage
356e3393645SStephen Warren# ---------------------------------------------------------------------------
357e3393645SStephen Warren
358e3393645SStephen WarrenMKIMAGE := $(srctree)/scripts/mkuboot.sh
359e3393645SStephen Warren
360e3393645SStephen Warren# SRCARCH just happens to match slightly more than ARCH (on sparc), so reduces
361e3393645SStephen Warren# the number of overrides in arch makefiles
362e3393645SStephen WarrenUIMAGE_ARCH ?= $(SRCARCH)
363e3393645SStephen WarrenUIMAGE_COMPRESSION ?= $(if $(2),$(2),none)
364e3393645SStephen WarrenUIMAGE_OPTS-y ?=
365e3393645SStephen WarrenUIMAGE_TYPE ?= kernel
366e3393645SStephen WarrenUIMAGE_LOADADDR ?= arch_must_set_this
367e3393645SStephen WarrenUIMAGE_ENTRYADDR ?= $(UIMAGE_LOADADDR)
368e3393645SStephen WarrenUIMAGE_NAME ?= 'Linux-$(KERNELRELEASE)'
369e3393645SStephen Warren
370786ac51aSMasahiro Yamadaquiet_cmd_uimage = UIMAGE  $@
371858805b3SMasahiro Yamada      cmd_uimage = $(BASH) $(MKIMAGE) -A $(UIMAGE_ARCH) -O linux \
372e3393645SStephen Warren			-C $(UIMAGE_COMPRESSION) $(UIMAGE_OPTS-y) \
373e3393645SStephen Warren			-T $(UIMAGE_TYPE) \
374e3393645SStephen Warren			-a $(UIMAGE_LOADADDR) -e $(UIMAGE_ENTRYADDR) \
375786ac51aSMasahiro Yamada			-n $(UIMAGE_NAME) -d $< $@
376e3393645SStephen Warren
37724fa0402SLasse Collin# XZ
37824fa0402SLasse Collin# ---------------------------------------------------------------------------
37924fa0402SLasse Collin# Use xzkern to compress the kernel image and xzmisc to compress other things.
38024fa0402SLasse Collin#
38124fa0402SLasse Collin# xzkern uses a big LZMA2 dictionary since it doesn't increase memory usage
38224fa0402SLasse Collin# of the kernel decompressor. A BCJ filter is used if it is available for
38324fa0402SLasse Collin# the target architecture. xzkern also appends uncompressed size of the data
38424fa0402SLasse Collin# using size_append. The .xz format has the size information available at
38524fa0402SLasse Collin# the end of the file too, but it's in more complex format and it's good to
38624fa0402SLasse Collin# avoid changing the part of the boot code that reads the uncompressed size.
38724fa0402SLasse Collin# Note that the bytes added by size_append will make the xz tool think that
38824fa0402SLasse Collin# the file is corrupt. This is expected.
38924fa0402SLasse Collin#
39024fa0402SLasse Collin# xzmisc doesn't use size_append, so it can be used to create normal .xz
39124fa0402SLasse Collin# files. xzmisc uses smaller LZMA2 dictionary than xzkern, because a very
39224fa0402SLasse Collin# big dictionary would increase the memory usage too much in the multi-call
39324fa0402SLasse Collin# decompression mode. A BCJ filter isn't used either.
39424fa0402SLasse Collinquiet_cmd_xzkern = XZKERN  $@
395b956c7a6SHarald Seiler      cmd_xzkern = { cat $(real-prereqs) | sh $(srctree)/scripts/xz_wrap.sh; \
396b79c6aa6SMasahiro Yamada                     $(size_append); } > $@
39724fa0402SLasse Collin
39824fa0402SLasse Collinquiet_cmd_xzmisc = XZMISC  $@
3998dfb61dcSDenis Efremov      cmd_xzmisc = cat $(real-prereqs) | $(XZ) --check=crc32 --lzma2=dict=1MiB > $@
400ebf003f0SMatthias Kaehlcke
401ebf003f0SMatthias Kaehlcke# ASM offsets
402ebf003f0SMatthias Kaehlcke# ---------------------------------------------------------------------------
403ebf003f0SMatthias Kaehlcke
404ebf003f0SMatthias Kaehlcke# Default sed regexp - multiline due to syntax constraints
405cf0c3e68SJeroen Hofstee#
406cf0c3e68SJeroen Hofstee# Use [:space:] because LLVM's integrated assembler inserts <tab> around
407cf0c3e68SJeroen Hofstee# the .ascii directive whereas GCC keeps the <space> as-is.
408ebf003f0SMatthias Kaehlckedefine sed-offsets
409cf0c3e68SJeroen Hofstee	's:^[[:space:]]*\.ascii[[:space:]]*"\(.*\)".*:\1:; \
410cf0c3e68SJeroen Hofstee	/^->/{s:->#\(.*\):/* \1 */:; \
411ebf003f0SMatthias Kaehlcke	s:^->\([^ ]*\) [\$$#]*\([^ ]*\) \(.*\):#define \1 \2 /* \3 */:; \
412cf0c3e68SJeroen Hofstee	s:->::; p;}'
413ebf003f0SMatthias Kaehlckeendef
414ebf003f0SMatthias Kaehlcke
415ebf003f0SMatthias Kaehlcke# Use filechk to avoid rebuilds when a header changes, but the resulting file
416ebf003f0SMatthias Kaehlcke# does not
417ebf003f0SMatthias Kaehlckedefine filechk_offsets
418ebf003f0SMatthias Kaehlcke	 echo "#ifndef $2"; \
419ebf003f0SMatthias Kaehlcke	 echo "#define $2"; \
420ebf003f0SMatthias Kaehlcke	 echo "/*"; \
421ebf003f0SMatthias Kaehlcke	 echo " * DO NOT MODIFY."; \
422ebf003f0SMatthias Kaehlcke	 echo " *"; \
423ebf003f0SMatthias Kaehlcke	 echo " * This file was generated by Kbuild"; \
424ebf003f0SMatthias Kaehlcke	 echo " */"; \
425ebf003f0SMatthias Kaehlcke	 echo ""; \
42643fee2b2SMasahiro Yamada	 sed -ne $(sed-offsets) < $<; \
427ebf003f0SMatthias Kaehlcke	 echo ""; \
428ad774086SMasahiro Yamada	 echo "#endif"
429ebf003f0SMatthias Kaehlckeendef
430