xref: /linux/scripts/Kbuild.include (revision fbfa9be9904e2c0d0c97d860fd071089d21dd9be)
18ec4b4ffSSam Ravnborg####
28ec4b4ffSSam Ravnborg# kbuild: Generic definitions
38ec4b4ffSSam Ravnborg
4beda9f3aSRoman Zippel# Convenient variables
58ec4b4ffSSam Ravnborgcomma   := ,
613338935SMasahiro Yamadaquote   := "
7d51bfb78SSam Ravnborgsquote  := '
88ec4b4ffSSam Ravnborgempty   :=
98ec4b4ffSSam Ravnborgspace   := $(empty) $(empty)
109c8fa9bcSMasahiro Yamadaspace_escape := _-_SPACE_-_
113298b690SDouglas Andersonright_paren := )
123298b690SDouglas Andersonleft_paren := (
138ec4b4ffSSam Ravnborg
148ec4b4ffSSam Ravnborg###
1548f1f058SSam Ravnborg# Name of target with a '.' as filename prefix. foo/bar.o => foo/.bar.o
1648f1f058SSam Ravnborgdot-target = $(dir $@).$(notdir $@)
1748f1f058SSam Ravnborg
1848f1f058SSam Ravnborg###
198ec4b4ffSSam Ravnborg# The temporary file to save gcc -MD generated dependencies must not
208ec4b4ffSSam Ravnborg# contain a comma
2148f1f058SSam Ravnborgdepfile = $(subst $(comma),_,$(dot-target).d)
228ec4b4ffSSam Ravnborg
238ec4b4ffSSam Ravnborg###
245e8d780dSSam Ravnborg# filename of target with directory and extension stripped
255e8d780dSSam Ravnborgbasetarget = $(basename $(notdir $@))
265e8d780dSSam Ravnborg
275e8d780dSSam Ravnborg###
28e0318d85SArnaud Lacombe# filename of first prerequisite with directory and extension stripped
29e0318d85SArnaud Lacombebaseprereq = $(basename $(notdir $<))
30e0318d85SArnaud Lacombe
31e0318d85SArnaud Lacombe###
32d51bfb78SSam Ravnborg# Escape single quote for use in echo statements
33d51bfb78SSam Ravnborgescsq = $(subst $(squote),'\$(squote)',$1)
34d51bfb78SSam Ravnborg
35d51bfb78SSam Ravnborg###
365410ecc0SMike Frysinger# Easy method for doing a status message
375410ecc0SMike Frysinger       kecho := :
385410ecc0SMike Frysinger quiet_kecho := echo
395410ecc0SMike Frysingersilent_kecho := :
405410ecc0SMike Frysingerkecho := $($(quiet)kecho)
415410ecc0SMike Frysinger
425410ecc0SMike Frysinger###
438ec4b4ffSSam Ravnborg# filechk is used to check if the content of a generated file is updated.
448ec4b4ffSSam Ravnborg# Sample usage:
458ec4b4ffSSam Ravnborg# define filechk_sample
468ec4b4ffSSam Ravnborg#	echo $KERNELRELEASE
478ec4b4ffSSam Ravnborg# endef
488ec4b4ffSSam Ravnborg# version.h : Makefile
498ec4b4ffSSam Ravnborg#	$(call filechk,sample)
508ec4b4ffSSam Ravnborg# The rule defined shall write to stdout the content of the new file.
518ec4b4ffSSam Ravnborg# The existing file will be compared with the new one.
528ec4b4ffSSam Ravnborg# - If no file exist it is created
538ec4b4ffSSam Ravnborg# - If the content differ the new file is used
548ec4b4ffSSam Ravnborg# - If they are equal no change, and no timestamp update
558ec4b4ffSSam Ravnborg# - stdin is piped in from the first prerequisite ($<) so one has
568ec4b4ffSSam Ravnborg#   to specify a valid file as first prerequisite (often the kbuild file)
578ec4b4ffSSam Ravnborgdefine filechk
588ec4b4ffSSam Ravnborg	$(Q)set -e;				\
59fd54f502SMike Frysinger	$(kecho) '  CHK     $@';		\
608ec4b4ffSSam Ravnborg	mkdir -p $(dir $@);			\
618ec4b4ffSSam Ravnborg	$(filechk_$(1)) < $< > $@.tmp;		\
628ec4b4ffSSam Ravnborg	if [ -r $@ ] && cmp -s $@ $@.tmp; then	\
638ec4b4ffSSam Ravnborg		rm -f $@.tmp;			\
648ec4b4ffSSam Ravnborg	else					\
65fd54f502SMike Frysinger		$(kecho) '  UPD     $@';	\
668ec4b4ffSSam Ravnborg		mv -f $@.tmp $@;		\
678ec4b4ffSSam Ravnborg	fi
688ec4b4ffSSam Ravnborgendef
698ec4b4ffSSam Ravnborg
7020a468b5SSam Ravnborg######
719d6e7a70SSam Ravnborg# gcc support functions
7220a468b5SSam Ravnborg# See documentation in Documentation/kbuild/makefiles.txt
7320a468b5SSam Ravnborg
74910b4046SSam Ravnborg# cc-cross-prefix
75910b4046SSam Ravnborg# Usage: CROSS_COMPILE := $(call cc-cross-prefix, m68k-linux-gnu- m68k-linux-)
76910b4046SSam Ravnborg# Return first prefix where a prefix$(CC) is found in PATH.
77910b4046SSam Ravnborg# If no $(CC) found in PATH with listed prefixes return nothing
78910b4046SSam Ravnborgcc-cross-prefix =  \
79910b4046SSam Ravnborg	$(word 1, $(foreach c,$(1),                                   \
80910b4046SSam Ravnborg		$(shell set -e;                                       \
81910b4046SSam Ravnborg		if (which $(strip $(c))$(CC)) > /dev/null 2>&1 ; then \
82910b4046SSam Ravnborg			echo $(c);                                    \
83910b4046SSam Ravnborg		fi)))
84910b4046SSam Ravnborg
853298b690SDouglas Anderson# Tools for caching Makefile variables that are "expensive" to compute.
863298b690SDouglas Anderson#
873298b690SDouglas Anderson# Here we want to help deal with variables that take a long time to compute
883298b690SDouglas Anderson# by making it easy to store these variables in a cache.
893298b690SDouglas Anderson#
903298b690SDouglas Anderson# The canonical example here is testing for compiler flags.  On a simple system
913298b690SDouglas Anderson# each call to the compiler takes 10 ms, but on a system with a compiler that's
923298b690SDouglas Anderson# called through various wrappers it can take upwards of 100 ms.  If we have
933298b690SDouglas Anderson# 100 calls to the compiler this can take 1 second (on a simple system) or 10
943298b690SDouglas Anderson# seconds (on a complicated system).
953298b690SDouglas Anderson#
963298b690SDouglas Anderson# The "cache" will be in Makefile syntax and can be directly included.
973298b690SDouglas Anderson# Any time we try to reference a variable that's not in the cache we'll
983298b690SDouglas Anderson# calculate it and store it in the cache for next time.
993298b690SDouglas Anderson
1003298b690SDouglas Anderson# Include values from last time
1013298b690SDouglas Andersonmake-cache := $(if $(KBUILD_EXTMOD),$(KBUILD_EXTMOD)/,$(if $(obj),$(obj)/)).cache.mk
1023298b690SDouglas Anderson$(make-cache): ;
1033298b690SDouglas Anderson-include $(make-cache)
1043298b690SDouglas Anderson
1059a234a2eSMasahiro Yamadacached-data := $(filter __cached_%, $(.VARIABLES))
1069a234a2eSMasahiro Yamada
107e17c400aSMasahiro Yamada# If cache exceeds 1000 lines, shrink it down to 500.
1089a234a2eSMasahiro Yamadaifneq ($(word 1000,$(cached-data)),)
109e17c400aSMasahiro Yamada$(shell tail -n 500 $(make-cache) > $(make-cache).tmp; \
110e17c400aSMasahiro Yamada	mv $(make-cache).tmp $(make-cache))
111e17c400aSMasahiro Yamadaendif
112e17c400aSMasahiro Yamada
1139a234a2eSMasahiro Yamadacreate-cache-dir := $(if $(KBUILD_SRC),$(if $(cache-data),,1))
1149a234a2eSMasahiro Yamada
1153298b690SDouglas Anderson# Usage: $(call __sanitize-opt,Hello=Hola$(comma)Goodbye Adios)
1163298b690SDouglas Anderson#
1173298b690SDouglas Anderson# Convert all '$', ')', '(', '\', '=', ' ', ',', ':' to '_'
1183298b690SDouglas Anderson__sanitize-opt = $(subst $$,_,$(subst $(right_paren),_,$(subst $(left_paren),_,$(subst \,_,$(subst =,_,$(subst $(space),_,$(subst $(comma),_,$(subst :,_,$(1)))))))))
1193298b690SDouglas Anderson
1203298b690SDouglas Anderson# Usage:   $(call shell-cached,shell_command)
1213298b690SDouglas Anderson# Example: $(call shell-cached,md5sum /usr/bin/gcc)
1223298b690SDouglas Anderson#
1233298b690SDouglas Anderson# If we've already seen a call to this exact shell command (even in a
1243298b690SDouglas Anderson# previous invocation of make!) we'll return the value.  If not, we'll
1253298b690SDouglas Anderson# compute it and store the result for future runs.
1263298b690SDouglas Anderson#
1273298b690SDouglas Anderson# This is a bit of voodoo, but basic explanation is that if the variable
1283298b690SDouglas Anderson# was undefined then we'll evaluate the shell command and store the result
1293298b690SDouglas Anderson# into the variable.  We'll then store that value in the cache and finally
1303298b690SDouglas Anderson# output the value.
1313298b690SDouglas Anderson#
1323298b690SDouglas Anderson# NOTE: The $$(2) here isn't actually a parameter to __run-and-store.  We
1333298b690SDouglas Anderson# happen to know that the caller will have their shell command in $(2) so the
1343298b690SDouglas Anderson# result of "call"ing this will produce a reference to that $(2).  The reason
1353298b690SDouglas Anderson# for this strangeness is to avoid an extra level of eval (and escaping) of
1363298b690SDouglas Anderson# $(2).
1373298b690SDouglas Andersondefine __run-and-store
1383298b690SDouglas Andersonifeq ($(origin $(1)),undefined)
1393298b690SDouglas Anderson  $$(eval $(1) := $$(shell $$(2)))
1409a234a2eSMasahiro Yamadaifeq ($(create-cache-dir),1)
1419a234a2eSMasahiro Yamada  $$(shell mkdir -p $(dir $(make-cache)))
1429a234a2eSMasahiro Yamada  $$(eval create-cache-dir :=)
1439a234a2eSMasahiro Yamadaendif
1443298b690SDouglas Anderson  $$(shell echo '$(1) := $$($(1))' >> $(make-cache))
1453298b690SDouglas Andersonendif
1463298b690SDouglas Andersonendef
1473298b690SDouglas Anderson__shell-cached = $(eval $(call __run-and-store,$(1)))$($(1))
1483298b690SDouglas Andersonshell-cached = $(call __shell-cached,__cached_$(call __sanitize-opt,$(1)),$(1))
1493298b690SDouglas Anderson
150beda9f3aSRoman Zippel# output directory for tests below
151beda9f3aSRoman ZippelTMPOUT := $(if $(KBUILD_EXTMOD),$(firstword $(KBUILD_EXTMOD))/)
152beda9f3aSRoman Zippel
153beda9f3aSRoman Zippel# try-run
154beda9f3aSRoman Zippel# Usage: option = $(call try-run, $(CC)...-o "$$TMP",option-ok,otherwise)
155312a3d09SCao jin# Exit code chooses option. "$$TMP" serves as a temporary file and is
156312a3d09SCao jin# automatically cleaned up.
1573298b690SDouglas Anderson__try-run = set -e;			\
158beda9f3aSRoman Zippel	TMP="$(TMPOUT).$$$$.tmp";	\
159691ef3e7SSam Ravnborg	TMPO="$(TMPOUT).$$$$.o";	\
160beda9f3aSRoman Zippel	if ($(1)) >/dev/null 2>&1;	\
1615de043f4SOleg Verych	then echo "$(2)";		\
1625de043f4SOleg Verych	else echo "$(3)";		\
1635de043f4SOleg Verych	fi;				\
1643298b690SDouglas Anderson	rm -f "$$TMP" "$$TMPO"
1653298b690SDouglas Anderson
1663298b690SDouglas Andersontry-run = $(shell $(__try-run))
1673298b690SDouglas Anderson
1683298b690SDouglas Anderson# try-run-cached
1693298b690SDouglas Anderson# This works like try-run, but the result is cached.
1703298b690SDouglas Andersontry-run-cached = $(call shell-cached,$(__try-run))
171347a00fbSRoman Zippel
17220a468b5SSam Ravnborg# as-option
17320a468b5SSam Ravnborg# Usage: cflags-y += $(call as-option,-Wa$(comma)-isa=foo,)
174beda9f3aSRoman Zippel
1753298b690SDouglas Andersonas-option = $(call try-run-cached,\
176a0f97e06SSam Ravnborg	$(CC) $(KBUILD_CFLAGS) $(1) -c -x assembler /dev/null -o "$$TMP",$(1),$(2))
17720a468b5SSam Ravnborg
178e2414910SAndi Kleen# as-instr
179e2414910SAndi Kleen# Usage: cflags-y += $(call as-instr,instr,option1,option2)
180beda9f3aSRoman Zippel
1813298b690SDouglas Andersonas-instr = $(call try-run-cached,\
182875de986SBernhard Walle	printf "%b\n" "$(1)" | $(CC) $(KBUILD_AFLAGS) -c -x assembler -o "$$TMP" -,$(2),$(3))
183e2414910SAndi Kleen
1849f3f1fd2SMatthias Kaehlcke# __cc-option
1859f3f1fd2SMatthias Kaehlcke# Usage: MY_CFLAGS += $(call __cc-option,$(CC),$(MY_CFLAGS),-march=winchip-c6,-march=i586)
1863298b690SDouglas Anderson__cc-option = $(call try-run-cached,\
1879f3f1fd2SMatthias Kaehlcke	$(1) -Werror $(2) $(3) -c -x c /dev/null -o "$$TMP",$(3),$(4))
1889f3f1fd2SMatthias Kaehlcke
189d26e9414SEmese Revfy# Do not attempt to build with gcc plugins during cc-option tests.
190d26e9414SEmese Revfy# (And this uses delayed resolution so the flags will be up to date.)
191d26e9414SEmese RevfyCC_OPTION_CFLAGS = $(filter-out $(GCC_PLUGINS_CFLAGS),$(KBUILD_CFLAGS))
192d26e9414SEmese Revfy
19320a468b5SSam Ravnborg# cc-option
19420a468b5SSam Ravnborg# Usage: cflags-y += $(call cc-option,-march=winchip-c6,-march=i586)
195beda9f3aSRoman Zippel
1969f3f1fd2SMatthias Kaehlckecc-option = $(call __cc-option, $(CC),\
1979f3f1fd2SMatthias Kaehlcke	$(KBUILD_CPPFLAGS) $(CC_OPTION_CFLAGS),$(1),$(2))
1989f3f1fd2SMatthias Kaehlcke
1999f3f1fd2SMatthias Kaehlcke# hostcc-option
2009f3f1fd2SMatthias Kaehlcke# Usage: cflags-y += $(call hostcc-option,-march=winchip-c6,-march=i586)
2019f3f1fd2SMatthias Kaehlckehostcc-option = $(call __cc-option, $(HOSTCC),\
2029f3f1fd2SMatthias Kaehlcke	$(HOSTCFLAGS) $(HOST_EXTRACFLAGS),$(1),$(2))
20320a468b5SSam Ravnborg
20420a468b5SSam Ravnborg# cc-option-yn
20520a468b5SSam Ravnborg# Usage: flag := $(call cc-option-yn,-march=winchip-c6)
2063298b690SDouglas Andersoncc-option-yn = $(call try-run-cached,\
207c3f0d0bcSMark Charlebois	$(CC) -Werror $(KBUILD_CPPFLAGS) $(CC_OPTION_CFLAGS) $(1) -c -x c /dev/null -o "$$TMP",y,n)
20820a468b5SSam Ravnborg
2098417da6fSMichal Marek# cc-disable-warning
2108417da6fSMichal Marek# Usage: cflags-y += $(call cc-disable-warning,unused-but-set-variable)
2113298b690SDouglas Andersoncc-disable-warning = $(call try-run-cached,\
212c3f0d0bcSMark Charlebois	$(CC) -Werror $(KBUILD_CPPFLAGS) $(CC_OPTION_CFLAGS) -W$(strip $(1)) -c -x c /dev/null -o "$$TMP",-Wno-$(strip $(1)))
2138417da6fSMichal Marek
2145631d9c4SMichal Marek# cc-name
2155631d9c4SMichal Marek# Expands to either gcc or clang
2163298b690SDouglas Andersoncc-name = $(call shell-cached,$(CC) -v 2>&1 | grep -q "clang version" && echo clang || echo gcc)
2175631d9c4SMichal Marek
21820a468b5SSam Ravnborg# cc-version
2193298b690SDouglas Andersoncc-version = $(call shell-cached,$(CONFIG_SHELL) $(srctree)/scripts/gcc-version.sh $(CC))
22020a468b5SSam Ravnborg
2210ab2a272SSegher Boessenkool# cc-fullversion
2223298b690SDouglas Andersoncc-fullversion = $(call shell-cached,$(CONFIG_SHELL) \
2230ab2a272SSegher Boessenkool	$(srctree)/scripts/gcc-version.sh -p $(CC))
2240ab2a272SSegher Boessenkool
22520a468b5SSam Ravnborg# cc-ifversion
22620a468b5SSam Ravnborg# Usage:  EXTRA_CFLAGS += $(call cc-ifversion, -lt, 0402, -O1)
2276dcb4e5eSMasahiro Yamadacc-ifversion = $(shell [ $(cc-version) $(1) $(2) ] && echo $(3) || echo $(4))
22820a468b5SSam Ravnborg
2293f135e57SJosh Poimboeuf# cc-if-fullversion
2303f135e57SJosh Poimboeuf# Usage:  EXTRA_CFLAGS += $(call cc-if-fullversion, -lt, 040502, -O1)
2313f135e57SJosh Poimboeufcc-if-fullversion = $(shell [ $(cc-fullversion) $(1) $(2) ] && echo $(3) || echo $(4))
2323f135e57SJosh Poimboeuf
233f86fd306SSam Ravnborg# cc-ldoption
234f86fd306SSam Ravnborg# Usage: ldflags += $(call cc-ldoption, -Wl$(comma)--hash-style=both)
2353298b690SDouglas Andersoncc-ldoption = $(call try-run-cached,\
23686a9df59SNick Desaulniers	$(CC) $(1) $(KBUILD_CPPFLAGS) $(CC_OPTION_CFLAGS) -nostdlib -x c /dev/null -o "$$TMP",$(1),$(2))
2370b0bf7a3SRoland McGrath
238691ef3e7SSam Ravnborg# ld-option
239691ef3e7SSam Ravnborg# Usage: LDFLAGS += $(call ld-option, -X)
2400294e6f4SMasahiro Yamadald-option = $(call try-run-cached, $(LD) $(LDFLAGS) $(1) -v,$(1),$(2))
241691ef3e7SSam Ravnborg
24240df759eSMichal Marek# ar-option
24340df759eSMichal Marek# Usage: KBUILD_ARFLAGS := $(call ar-option,D)
24440df759eSMichal Marek# Important: no spaces around options
2453298b690SDouglas Andersonar-option = $(call try-run-cached, $(AR) rc$(1) "$$TMP",$(1),$(2))
24640df759eSMichal Marek
247ccbef167SAndi Kleen# ld-version
248ccbef167SAndi Kleen# Note this is mainly for HJ Lu's 3 number binutil versions
2493298b690SDouglas Andersonld-version = $(call shell-cached,$(LD) --version | $(srctree)/scripts/ld-version.sh)
250ccbef167SAndi Kleen
251ccbef167SAndi Kleen# ld-ifversion
252ccbef167SAndi Kleen# Usage:  $(call ld-ifversion, -ge, 22252, y)
2536dcb4e5eSMasahiro Yamadald-ifversion = $(shell [ $(ld-version) $(1) $(2) ] && echo $(3) || echo $(4))
254ccbef167SAndi Kleen
2555de043f4SOleg Verych######
2565de043f4SOleg Verych
257beda9f3aSRoman Zippel###
2588ec4b4ffSSam Ravnborg# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.build obj=
2598ec4b4ffSSam Ravnborg# Usage:
2608ec4b4ffSSam Ravnborg# $(Q)$(MAKE) $(build)=dir
2615b2389b4SMasahiro Yamadabuild := -f $(srctree)/scripts/Makefile.build obj
2628ec4b4ffSSam Ravnborg
263bc081dd6SMichal Marek###
264bc081dd6SMichal Marek# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.modbuiltin obj=
265bc081dd6SMichal Marek# Usage:
266bc081dd6SMichal Marek# $(Q)$(MAKE) $(modbuiltin)=dir
2675b2389b4SMasahiro Yamadamodbuiltin := -f $(srctree)/scripts/Makefile.modbuiltin obj
268bc081dd6SMichal Marek
2699fb5e537SRobert Richter###
2709fb5e537SRobert Richter# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.dtbinst obj=
2719fb5e537SRobert Richter# Usage:
2729fb5e537SRobert Richter# $(Q)$(MAKE) $(dtbinst)=dir
2739fb5e537SRobert Richterdtbinst := -f $(if $(KBUILD_SRC),$(srctree)/)scripts/Makefile.dtbinst obj
2749fb5e537SRobert Richter
275d08372caSLinus Torvalds###
276371fdc77SMasahiro Yamada# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.clean obj=
277371fdc77SMasahiro Yamada# Usage:
278371fdc77SMasahiro Yamada# $(Q)$(MAKE) $(clean)=dir
279371fdc77SMasahiro Yamadaclean := -f $(srctree)/scripts/Makefile.clean obj
280371fdc77SMasahiro Yamada
281371fdc77SMasahiro Yamada###
2821846dfbdSMasahiro Yamada# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.headersinst obj=
283371fdc77SMasahiro Yamada# Usage:
284371fdc77SMasahiro Yamada# $(Q)$(MAKE) $(hdr-inst)=dir
2851846dfbdSMasahiro Yamadahdr-inst := -f $(srctree)/scripts/Makefile.headersinst obj
286371fdc77SMasahiro Yamada
287beda9f3aSRoman Zippel# Prefix -I with $(srctree) if it is not an absolute path.
2885b91c33cSSam Ravnborg# skip if -I has no parameter
2895b91c33cSSam Ravnborgaddtree = $(if $(patsubst -I%,%,$(1)), \
290db547ef1SArnd Bergmann$(if $(filter-out -I/% -I./% -I../%,$(1)),$(patsubst -I%,-I$(srctree)/%,$(1)),$(1)))
291d9df92e2SSam Ravnborg
2925de043f4SOleg Verych# Find all -I options and call addtree
293beda9f3aSRoman Zippelflags = $(foreach o,$($(1)),$(if $(filter -I%,$(o)),$(call addtree,$(o)),$(o)))
2945de043f4SOleg Verych
2955de043f4SOleg Verych# echo command.
2965de043f4SOleg Verych# Short version is used, if $(quiet) equals `quiet_', otherwise full one.
2975de043f4SOleg Verychecho-cmd = $(if $($(quiet)cmd_$(1)),\
2985de043f4SOleg Verych	echo '  $(call escsq,$($(quiet)cmd_$(1)))$(echo-why)';)
2995de043f4SOleg Verych
3005de043f4SOleg Verych# printing commands
3016176aa9aSJan Beulichcmd = @$(echo-cmd) $(cmd_$(1))
3028ec4b4ffSSam Ravnborg
3035de043f4SOleg Verych# Add $(obj)/ for paths that are not absolute
3040a504f25SSam Ravnborgobjectify = $(foreach o,$(1),$(if $(filter /%,$(o)),$(o),$(obj)/$(o)))
3050a504f25SSam Ravnborg
3068ec4b4ffSSam Ravnborg###
3078ec4b4ffSSam Ravnborg# if_changed      - execute command if any prerequisite is newer than
3088ec4b4ffSSam Ravnborg#                   target, or command line has changed
3098ec4b4ffSSam Ravnborg# if_changed_dep  - as if_changed, but uses fixdep to reveal dependencies
3108ec4b4ffSSam Ravnborg#                   including used config symbols
3118ec4b4ffSSam Ravnborg# if_changed_rule - as if_changed but execute rule instead
3128ec4b4ffSSam Ravnborg# See Documentation/kbuild/makefiles.txt for more info
3138ec4b4ffSSam Ravnborg
3148ec4b4ffSSam Ravnborgifneq ($(KBUILD_NOCMDDEP),1)
3159c8fa9bcSMasahiro Yamada# Check if both arguments are the same including their order. Result is empty
3169c8fa9bcSMasahiro Yamada# string if equal. User may override this check using make KBUILD_NOCMDDEP=1
3179c8fa9bcSMasahiro Yamadaarg-check = $(filter-out $(subst $(space),$(space_escape),$(strip $(cmd_$@))), \
3189c8fa9bcSMasahiro Yamada                         $(subst $(space),$(space_escape),$(strip $(cmd_$1))))
319c4d5ee13SMichal Marekelse
320c4d5ee13SMichal Marekarg-check = $(if $(strip $(cmd_$@)),,1)
3218ec4b4ffSSam Ravnborgendif
3228ec4b4ffSSam Ravnborg
323164f0d2eSMichal Marek# Replace >$< with >$$< to preserve $ when reloading the .cmd file
324164f0d2eSMichal Marek# (needed for make)
325164f0d2eSMichal Marek# Replace >#< with >\#< to avoid starting a comment in the .cmd file
326164f0d2eSMichal Marek# (needed for make)
327164f0d2eSMichal Marek# Replace >'< with >'\''< to be able to enclose the whole string in '...'
328164f0d2eSMichal Marek# (needed for the shell)
329164f0d2eSMichal Marekmake-cmd = $(call escsq,$(subst \#,\\\#,$(subst $$,$$$$,$(cmd_$(1)))))
3306176aa9aSJan Beulich
33148f1f058SSam Ravnborg# Find any prerequisites that is newer than target or that does not exist.
33248f1f058SSam Ravnborg# PHONY targets skipped in both cases.
33348f1f058SSam Ravnborgany-prereq = $(filter-out $(PHONY),$?) $(filter-out $(PHONY) $(wildcard $^),$^)
33448f1f058SSam Ravnborg
3355de043f4SOleg Verych# Execute command if command has changed or prerequisite(s) are updated.
33648f1f058SSam Ravnborgif_changed = $(if $(strip $(any-prereq) $(arg-check)),                       \
3378ec4b4ffSSam Ravnborg	@set -e;                                                             \
3386176aa9aSJan Beulich	$(echo-cmd) $(cmd_$(1));                                             \
3392aedcd09SMasahiro Yamada	printf '%s\n' 'cmd_$@ := $(make-cmd)' > $(dot-target).cmd, @:)
3408ec4b4ffSSam Ravnborg
3415de043f4SOleg Verych# Execute the command and also postprocess generated .d dependencies file.
34248f1f058SSam Ravnborgif_changed_dep = $(if $(strip $(any-prereq) $(arg-check) ),                  \
3438ec4b4ffSSam Ravnborg	@set -e;                                                             \
344e4aca459SNicolas Pitre	$(cmd_and_fixdep), @:)
345e4aca459SNicolas Pitre
346c1a95fdaSNicolas Pitreifndef CONFIG_TRIM_UNUSED_KSYMS
347c1a95fdaSNicolas Pitre
348e4aca459SNicolas Pitrecmd_and_fixdep =                                                             \
3496176aa9aSJan Beulich	$(echo-cmd) $(cmd_$(1));                                             \
35048f1f058SSam Ravnborg	scripts/basic/fixdep $(depfile) $@ '$(make-cmd)' > $(dot-target).tmp;\
3518ec4b4ffSSam Ravnborg	rm -f $(depfile);                                                    \
352e4aca459SNicolas Pitre	mv -f $(dot-target).tmp $(dot-target).cmd;
3538ec4b4ffSSam Ravnborg
354c1a95fdaSNicolas Pitreelse
355c1a95fdaSNicolas Pitre
356c1a95fdaSNicolas Pitre# Filter out exported kernel symbol names from the preprocessor output.
357c1a95fdaSNicolas Pitre# See also __KSYM_DEPS__ in include/linux/export.h.
358c1a95fdaSNicolas Pitre# We disable the depfile generation here, so as not to overwrite the existing
359c1a95fdaSNicolas Pitre# depfile while fixdep is parsing it.
360c1a95fdaSNicolas Pitreflags_nodeps = $(filter-out -Wp$(comma)-M%, $($(1)))
361c1a95fdaSNicolas Pitreksym_dep_filter =                                                            \
362c1a95fdaSNicolas Pitre	case "$(1)" in                                                       \
363366f4856SNicolas Pitre	  cc_*_c|cpp_i_c)                                                    \
364366f4856SNicolas Pitre	    $(CPP) $(call flags_nodeps,c_flags) -D__KSYM_DEPS__ $< ;;        \
365366f4856SNicolas Pitre	  as_*_S|cpp_s_S)                                                    \
366366f4856SNicolas Pitre	    $(CPP) $(call flags_nodeps,a_flags) -D__KSYM_DEPS__ $< ;;        \
3670d070d2bSMarcin Nowakowski	  boot*|build*|cpp_its_S|*cpp_lds_S|dtc|host*|vdso*) : ;;            \
368c1a95fdaSNicolas Pitre	  *) echo "Don't know how to preprocess $(1)" >&2; false ;;          \
369*fbfa9be9SMasahiro Yamada	esac | tr ";" "\n" | sed -n 's/^.*=== __KSYM_\(.*\) ===.*$$/_\1/p'
370c1a95fdaSNicolas Pitre
371c1a95fdaSNicolas Pitrecmd_and_fixdep =                                                             \
372c1a95fdaSNicolas Pitre	$(echo-cmd) $(cmd_$(1));                                             \
373c1a95fdaSNicolas Pitre	$(ksym_dep_filter) |                                                 \
374c1a95fdaSNicolas Pitre		scripts/basic/fixdep -e $(depfile) $@ '$(make-cmd)'          \
375c1a95fdaSNicolas Pitre			> $(dot-target).tmp;	                             \
376c1a95fdaSNicolas Pitre	rm -f $(depfile);                                                    \
377c1a95fdaSNicolas Pitre	mv -f $(dot-target).tmp $(dot-target).cmd;
378c1a95fdaSNicolas Pitre
379c1a95fdaSNicolas Pitreendif
380c1a95fdaSNicolas Pitre
3818ec4b4ffSSam Ravnborg# Usage: $(call if_changed_rule,foo)
382beda9f3aSRoman Zippel# Will check if $(cmd_foo) or any of the prerequisites changed,
383beda9f3aSRoman Zippel# and if so will execute $(rule_foo).
38448f1f058SSam Ravnborgif_changed_rule = $(if $(strip $(any-prereq) $(arg-check) ),                 \
3858ec4b4ffSSam Ravnborg	@set -e;                                                             \
3862aedcd09SMasahiro Yamada	$(rule_$(1)), @:)
38748f1f058SSam Ravnborg
38845d506bdSSam Ravnborg###
389312a3d09SCao jin# why - tell why a target got built
39045d506bdSSam Ravnborg#       enabled by make V=2
39145d506bdSSam Ravnborg#       Output (listed in the order they are checked):
39245d506bdSSam Ravnborg#          (1) - due to target is PHONY
39345d506bdSSam Ravnborg#          (2) - due to target missing
39445d506bdSSam Ravnborg#          (3) - due to: file1.h file2.h
39545d506bdSSam Ravnborg#          (4) - due to command line change
39645d506bdSSam Ravnborg#          (5) - due to missing .cmd file
39745d506bdSSam Ravnborg#          (6) - due to target not in $(targets)
39845d506bdSSam Ravnborg# (1) PHONY targets are always build
39945d506bdSSam Ravnborg# (2) No target, so we better build it
40045d506bdSSam Ravnborg# (3) Prerequisite is newer than target
40145d506bdSSam Ravnborg# (4) The command line stored in the file named dir/.target.cmd
40245d506bdSSam Ravnborg#     differed from actual command line. This happens when compiler
40345d506bdSSam Ravnborg#     options changes
40445d506bdSSam Ravnborg# (5) No dir/.target.cmd file (used to store command line)
40545d506bdSSam Ravnborg# (6) No dir/.target.cmd file and target not listed in $(targets)
40645d506bdSSam Ravnborg#     This is a good hint that there is a bug in the kbuild file
40745d506bdSSam Ravnborgifeq ($(KBUILD_VERBOSE),2)
40845d506bdSSam Ravnborgwhy =                                                                        \
40945d506bdSSam Ravnborg    $(if $(filter $@, $(PHONY)),- due to target is PHONY,                    \
41045d506bdSSam Ravnborg        $(if $(wildcard $@),                                                 \
41145d506bdSSam Ravnborg            $(if $(strip $(any-prereq)),- due to: $(any-prereq),             \
41245d506bdSSam Ravnborg                $(if $(arg-check),                                           \
41345d506bdSSam Ravnborg                    $(if $(cmd_$@),- due to command line change,             \
41445d506bdSSam Ravnborg                        $(if $(filter $@, $(targets)),                       \
41545d506bdSSam Ravnborg                            - due to missing .cmd file,                      \
41645d506bdSSam Ravnborg                            - due to $(notdir $@) not in $$(targets)         \
41745d506bdSSam Ravnborg                         )                                                   \
41845d506bdSSam Ravnborg                     )                                                       \
41945d506bdSSam Ravnborg                 )                                                           \
42045d506bdSSam Ravnborg             ),                                                              \
42145d506bdSSam Ravnborg             - due to target missing                                         \
42245d506bdSSam Ravnborg         )                                                                   \
42345d506bdSSam Ravnborg     )
42445d506bdSSam Ravnborg
42545d506bdSSam Ravnborgecho-why = $(call escsq, $(strip $(why)))
42645d506bdSSam Ravnborgendif
4273ee550f1SDavid Woodhouse
4283ee550f1SDavid Woodhouse###############################################################################
4293ee550f1SDavid Woodhouse#
4303ee550f1SDavid Woodhouse# When a Kconfig string contains a filename, it is suitable for
4313ee550f1SDavid Woodhouse# passing to shell commands. It is surrounded by double-quotes, and
4323ee550f1SDavid Woodhouse# any double-quotes or backslashes within it are escaped by
4333ee550f1SDavid Woodhouse# backslashes.
4343ee550f1SDavid Woodhouse#
4353ee550f1SDavid Woodhouse# This is no use for dependencies or $(wildcard). We need to strip the
4363ee550f1SDavid Woodhouse# surrounding quotes and the escaping from quotes and backslashes, and
4373ee550f1SDavid Woodhouse# we *do* need to escape any spaces in the string. So, for example:
4383ee550f1SDavid Woodhouse#
4393ee550f1SDavid Woodhouse# Usage: $(eval $(call config_filename,FOO))
4403ee550f1SDavid Woodhouse#
4413ee550f1SDavid Woodhouse# Defines FOO_FILENAME based on the contents of the CONFIG_FOO option,
4423ee550f1SDavid Woodhouse# transformed as described above to be suitable for use within the
4433ee550f1SDavid Woodhouse# makefile.
4443ee550f1SDavid Woodhouse#
4453ee550f1SDavid Woodhouse# Also, if the filename is a relative filename and exists in the source
4463ee550f1SDavid Woodhouse# tree but not the build tree, define FOO_SRCPREFIX as $(srctree)/ to
4473ee550f1SDavid Woodhouse# be prefixed to *both* command invocation and dependencies.
4483ee550f1SDavid Woodhouse#
4493ee550f1SDavid Woodhouse# Note: We also print the filenames in the quiet_cmd_foo text, and
4503ee550f1SDavid Woodhouse# perhaps ought to have a version specially escaped for that purpose.
4513ee550f1SDavid Woodhouse# But it's only cosmetic, and $(patsubst "%",%,$(CONFIG_FOO)) is good
4523ee550f1SDavid Woodhouse# enough.  It'll strip the quotes in the common case where there's no
4533ee550f1SDavid Woodhouse# space and it's a simple filename, and it'll retain the quotes when
4543ee550f1SDavid Woodhouse# there's a space. There are some esoteric cases in which it'll print
4553ee550f1SDavid Woodhouse# the wrong thing, but we don't really care. The actual dependencies
4563ee550f1SDavid Woodhouse# and commands *do* get it right, with various combinations of single
4573ee550f1SDavid Woodhouse# and double quotes, backslashes and spaces in the filenames.
4583ee550f1SDavid Woodhouse#
4593ee550f1SDavid Woodhouse###############################################################################
4603ee550f1SDavid Woodhouse#
4613ee550f1SDavid Woodhousedefine config_filename
4623ee550f1SDavid Woodhouseifneq ($$(CONFIG_$(1)),"")
4633ee550f1SDavid Woodhouse$(1)_FILENAME := $$(subst \\,\,$$(subst \$$(quote),$$(quote),$$(subst $$(space_escape),\$$(space),$$(patsubst "%",%,$$(subst $$(space),$$(space_escape),$$(CONFIG_$(1)))))))
4643ee550f1SDavid Woodhouseifneq ($$(patsubst /%,%,$$(firstword $$($(1)_FILENAME))),$$(firstword $$($(1)_FILENAME)))
4653ee550f1SDavid Woodhouseelse
4663ee550f1SDavid Woodhouseifeq ($$(wildcard $$($(1)_FILENAME)),)
4673ee550f1SDavid Woodhouseifneq ($$(wildcard $$(srctree)/$$($(1)_FILENAME)),)
4683ee550f1SDavid Woodhouse$(1)_SRCPREFIX := $(srctree)/
4693ee550f1SDavid Woodhouseendif
4703ee550f1SDavid Woodhouseendif
4713ee550f1SDavid Woodhouseendif
4723ee550f1SDavid Woodhouseendif
4733ee550f1SDavid Woodhouseendef
4743ee550f1SDavid Woodhouse#
4753ee550f1SDavid Woodhouse###############################################################################
476