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