blob: 0168d6c37075dd46b4b5bd090ac7c85fb153ce60 [file] [log] [blame]
Sam Ravnborg8ec4b4f2005-07-25 20:10:36 +00001####
2# kbuild: Generic definitions
3
4# Convinient variables
5comma := ,
Sam Ravnborgd51bfb72006-01-06 22:35:59 +01006squote := '
Sam Ravnborg8ec4b4f2005-07-25 20:10:36 +00007empty :=
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###
Sam Ravnborgd51bfb72006-01-06 22:35:59 +010016# Escape single quote for use in echo statements
17escsq = $(subst $(squote),'\$(squote)',$1)
18
19###
Sam Ravnborg8ec4b4f2005-07-25 20:10:36 +000020# 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)),\
Sam Ravnborgd51bfb72006-01-06 22:35:59 +010055 echo ' $(call escsq,$($(quiet)cmd_$(1)))' &&) $(cmd_$(1))
Sam Ravnborg8ec4b4f2005-07-25 20:10:36 +000056
Sam Ravnborg0a504f22005-09-10 21:02:11 +020057# Add $(obj)/ for paths that is not absolute
58objectify = $(foreach o,$(1),$(if $(filter /%,$(o)),$(o),$(obj)/$(o)))
59
Sam Ravnborg8ec4b4f2005-07-25 20:10:36 +000060###
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)), \
Sam Ravnborgd51bfb72006-01-06 22:35:59 +010076 echo ' $(call escsq,$($(quiet)cmd_$(1)))';)
Sam Ravnborg8ec4b4f2005-07-25 20:10:36 +000077
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)); \
Sam Ravnborgd51bfb72006-01-06 22:35:59 +010086 echo 'cmd_$@ := $(subst $$,$$$$,$(call escsq,$(cmd_$(1))))' > $(@D)/.$(@F).cmd)
Sam Ravnborg8ec4b4f2005-07-25 20:10:36 +000087
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)); \
Sam Ravnborgd51bfb72006-01-06 22:35:59 +010095 scripts/basic/fixdep $(depfile) $@ '$(subst $$,$$$$,$(call escsq,$(cmd_$(1))))' > $(@D)/.$(@F).tmp; \
Sam Ravnborg8ec4b4f2005-07-25 20:10:36 +000096 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)))