xref: /linux/scripts/Kbuild.include (revision d8327c784b51b57dac2c26cfad87dce0d68dfd98)
1####
2# kbuild: Generic definitions
3
4# Convinient variables
5comma   := ,
6squote  := '
7empty   :=
8space   := $(empty) $(empty)
9
10###
11# The temporary file to save gcc -MD generated dependencies must not
12# contain a comma
13depfile = $(subst $(comma),_,$(@D)/.$(@F).d)
14
15###
16# Escape single quote for use in echo statements
17escsq = $(subst $(squote),'\$(squote)',$1)
18
19###
20# filechk is used to check if the content of a generated file is updated.
21# Sample usage:
22# define filechk_sample
23#	echo $KERNELRELEASE
24# endef
25# version.h : Makefile
26#	$(call filechk,sample)
27# The rule defined shall write to stdout the content of the new file.
28# The existing file will be compared with the new one.
29# - If no file exist it is created
30# - If the content differ the new file is used
31# - If they are equal no change, and no timestamp update
32# - stdin is piped in from the first prerequisite ($<) so one has
33#   to specify a valid file as first prerequisite (often the kbuild file)
34define filechk
35	$(Q)set -e;				\
36	echo '  CHK     $@';			\
37	mkdir -p $(dir $@);			\
38	$(filechk_$(1)) < $< > $@.tmp;		\
39	if [ -r $@ ] && cmp -s $@ $@.tmp; then	\
40		rm -f $@.tmp;			\
41	else					\
42		echo '  UPD     $@';		\
43		mv -f $@.tmp $@;		\
44	fi
45endef
46
47###
48# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.build obj=
49# Usage:
50# $(Q)$(MAKE) $(build)=dir
51build := -f $(if $(KBUILD_SRC),$(srctree)/)scripts/Makefile.build obj
52
53# If quiet is set, only print short version of command
54cmd = @$(if $($(quiet)cmd_$(1)),\
55      echo '  $(call escsq,$($(quiet)cmd_$(1)))' &&) $(cmd_$(1))
56
57# Add $(obj)/ for paths that is not absolute
58objectify = $(foreach o,$(1),$(if $(filter /%,$(o)),$(o),$(obj)/$(o)))
59
60###
61# if_changed      - execute command if any prerequisite is newer than
62#                   target, or command line has changed
63# if_changed_dep  - as if_changed, but uses fixdep to reveal dependencies
64#                   including used config symbols
65# if_changed_rule - as if_changed but execute rule instead
66# See Documentation/kbuild/makefiles.txt for more info
67
68ifneq ($(KBUILD_NOCMDDEP),1)
69# Check if both arguments has same arguments. Result in empty string if equal
70# User may override this check using make KBUILD_NOCMDDEP=1
71arg-check = $(strip $(filter-out $(1), $(2)) $(filter-out $(2), $(1)) )
72endif
73
74# echo command. Short version is $(quiet) equals quiet, otherwise full command
75echo-cmd = $(if $($(quiet)cmd_$(1)), \
76	echo '  $(call escsq,$($(quiet)cmd_$(1)))';)
77
78# function to only execute the passed command if necessary
79# >'< substitution is for echo to work, >$< substitution to preserve $ when reloading .cmd file
80# note: when using inline perl scripts [perl -e '...$$t=1;...'] in $(cmd_xxx) double $$ your perl vars
81#
82if_changed = $(if $(strip $? $(call arg-check, $(cmd_$(1)), $(cmd_$@)) ), \
83	@set -e; \
84	$(echo-cmd) \
85	$(cmd_$(1)); \
86	echo 'cmd_$@ := $(subst $$,$$$$,$(call escsq,$(cmd_$(1))))' > $(@D)/.$(@F).cmd)
87
88# execute the command and also postprocess generated .d dependencies
89# file
90if_changed_dep = $(if $(strip $? $(filter-out FORCE $(wildcard $^),$^)\
91	$(call arg-check, $(cmd_$(1)), $(cmd_$@)) ),                  \
92	@set -e; \
93	$(echo-cmd) \
94	$(cmd_$(1)); \
95	scripts/basic/fixdep $(depfile) $@ '$(subst $$,$$$$,$(call escsq,$(cmd_$(1))))' > $(@D)/.$(@F).tmp; \
96	rm -f $(depfile); \
97	mv -f $(@D)/.$(@F).tmp $(@D)/.$(@F).cmd)
98
99# Usage: $(call if_changed_rule,foo)
100# will check if $(cmd_foo) changed, or any of the prequisites changed,
101# and if so will execute $(rule_foo)
102if_changed_rule = $(if $(strip $? $(call arg-check, $(cmd_$(1)), $(cmd_$@)) ),\
103			@set -e; \
104			$(rule_$(1)))
105