blob: 59620b1554e022a316daa7185a0cf17a02fe6e3d [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
Sam Ravnborg20a468b2006-01-22 13:34:15 +010047######
Sam Ravnborg9d6e7a72006-02-18 10:03:40 +010048# gcc support functions
Sam Ravnborg20a468b2006-01-22 13:34:15 +010049# See documentation in Documentation/kbuild/makefiles.txt
50
51# as-option
52# Usage: cflags-y += $(call as-option, -Wa$(comma)-isa=foo,)
53
54as-option = $(shell if $(CC) $(CFLAGS) $(1) -Wa,-Z -c -o /dev/null \
55 -xassembler /dev/null > /dev/null 2>&1; then echo "$(1)"; \
56 else echo "$(2)"; fi ;)
57
58# cc-option
59# Usage: cflags-y += $(call cc-option, -march=winchip-c6, -march=i586)
60
61cc-option = $(shell if $(CC) $(CFLAGS) $(1) -S -o /dev/null -xc /dev/null \
62 > /dev/null 2>&1; then echo "$(1)"; else echo "$(2)"; fi ;)
63
64# cc-option-yn
65# Usage: flag := $(call cc-option-yn, -march=winchip-c6)
66cc-option-yn = $(shell if $(CC) $(CFLAGS) $(1) -S -o /dev/null -xc /dev/null \
67 > /dev/null 2>&1; then echo "y"; else echo "n"; fi;)
68
69# cc-option-align
70# Prefix align with either -falign or -malign
71cc-option-align = $(subst -functions=0,,\
72 $(call cc-option,-falign-functions=0,-malign-functions=0))
73
74# cc-version
75# Usage gcc-ver := $(call cc-version, $(CC))
76cc-version = $(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-version.sh \
77 $(if $(1), $(1), $(CC)))
78
79# cc-ifversion
80# Usage: EXTRA_CFLAGS += $(call cc-ifversion, -lt, 0402, -O1)
81cc-ifversion = $(shell if [ $(call cc-version, $(CC)) $(1) $(2) ]; then \
82 echo $(3); fi;)
83
Sam Ravnborg8ec4b4f2005-07-25 20:10:36 +000084###
85# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.build obj=
86# Usage:
87# $(Q)$(MAKE) $(build)=dir
88build := -f $(if $(KBUILD_SRC),$(srctree)/)scripts/Makefile.build obj
89
90# If quiet is set, only print short version of command
Jan Beulich6176aa92006-01-30 10:04:27 +010091cmd = @$(echo-cmd) $(cmd_$(1))
Sam Ravnborg8ec4b4f2005-07-25 20:10:36 +000092
Sam Ravnborg0a504f22005-09-10 21:02:11 +020093# Add $(obj)/ for paths that is not absolute
94objectify = $(foreach o,$(1),$(if $(filter /%,$(o)),$(o),$(obj)/$(o)))
95
Sam Ravnborg8ec4b4f2005-07-25 20:10:36 +000096###
97# if_changed - execute command if any prerequisite is newer than
98# target, or command line has changed
99# if_changed_dep - as if_changed, but uses fixdep to reveal dependencies
100# including used config symbols
101# if_changed_rule - as if_changed but execute rule instead
102# See Documentation/kbuild/makefiles.txt for more info
103
104ifneq ($(KBUILD_NOCMDDEP),1)
105# Check if both arguments has same arguments. Result in empty string if equal
106# User may override this check using make KBUILD_NOCMDDEP=1
107arg-check = $(strip $(filter-out $(1), $(2)) $(filter-out $(2), $(1)) )
108endif
109
110# echo command. Short version is $(quiet) equals quiet, otherwise full command
111echo-cmd = $(if $($(quiet)cmd_$(1)), \
Sam Ravnborgd51bfb72006-01-06 22:35:59 +0100112 echo ' $(call escsq,$($(quiet)cmd_$(1)))';)
Sam Ravnborg8ec4b4f2005-07-25 20:10:36 +0000113
Jan Beulich6176aa92006-01-30 10:04:27 +0100114make-cmd = $(subst \#,\\\#,$(subst $$,$$$$,$(call escsq,$(cmd_$(1)))))
115
Sam Ravnborg8ec4b4f2005-07-25 20:10:36 +0000116# function to only execute the passed command if necessary
117# >'< substitution is for echo to work, >$< substitution to preserve $ when reloading .cmd file
118# note: when using inline perl scripts [perl -e '...$$t=1;...'] in $(cmd_xxx) double $$ your perl vars
Paul Smith4f193362006-03-05 17:14:10 -0500119#
120if_changed = $(if $(strip $(filter-out $(PHONY),$?) \
121 $(call arg-check, $(cmd_$(1)), $(cmd_$@)) ), \
Sam Ravnborg8ec4b4f2005-07-25 20:10:36 +0000122 @set -e; \
Jan Beulich6176aa92006-01-30 10:04:27 +0100123 $(echo-cmd) $(cmd_$(1)); \
124 echo 'cmd_$@ := $(make-cmd)' > $(@D)/.$(@F).cmd)
Sam Ravnborg8ec4b4f2005-07-25 20:10:36 +0000125
126# execute the command and also postprocess generated .d dependencies
127# file
Paul Smith4f193362006-03-05 17:14:10 -0500128if_changed_dep = $(if $(strip $(filter-out $(PHONY),$?) \
129 $(filter-out FORCE $(wildcard $^),$^) \
130 $(call arg-check, $(cmd_$(1)), $(cmd_$@)) ), \
Sam Ravnborg8ec4b4f2005-07-25 20:10:36 +0000131 @set -e; \
Jan Beulich6176aa92006-01-30 10:04:27 +0100132 $(echo-cmd) $(cmd_$(1)); \
133 scripts/basic/fixdep $(depfile) $@ '$(make-cmd)' > $(@D)/.$(@F).tmp; \
Sam Ravnborg8ec4b4f2005-07-25 20:10:36 +0000134 rm -f $(depfile); \
135 mv -f $(@D)/.$(@F).tmp $(@D)/.$(@F).cmd)
136
137# Usage: $(call if_changed_rule,foo)
138# will check if $(cmd_foo) changed, or any of the prequisites changed,
139# and if so will execute $(rule_foo)
Paul Smith4f193362006-03-05 17:14:10 -0500140if_changed_rule = $(if $(strip $(filter-out $(PHONY),$?) \
141 $(call arg-check, $(cmd_$(1)), $(cmd_$@)) ),\
Sam Ravnborg8ec4b4f2005-07-25 20:10:36 +0000142 @set -e; \
143 $(rule_$(1)))