blob: ac5f275b0283f1f9807fcc281c3a443e30955eda [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 Ravnborge5c44fd2006-06-24 22:50:18 +020016# basetarget equals the filename of the target with no extension.
17# So 'foo/bar.o' becomes 'bar'
18basetarget = $(basename $(notdir $@))
19
20###
Sam Ravnborgd51bfb72006-01-06 22:35:59 +010021# Escape single quote for use in echo statements
22escsq = $(subst $(squote),'\$(squote)',$1)
23
24###
Sam Ravnborg8ec4b4f2005-07-25 20:10:36 +000025# filechk is used to check if the content of a generated file is updated.
26# Sample usage:
27# define filechk_sample
28# echo $KERNELRELEASE
29# endef
30# version.h : Makefile
31# $(call filechk,sample)
32# The rule defined shall write to stdout the content of the new file.
33# The existing file will be compared with the new one.
34# - If no file exist it is created
35# - If the content differ the new file is used
36# - If they are equal no change, and no timestamp update
37# - stdin is piped in from the first prerequisite ($<) so one has
38# to specify a valid file as first prerequisite (often the kbuild file)
39define filechk
40 $(Q)set -e; \
41 echo ' CHK $@'; \
42 mkdir -p $(dir $@); \
43 $(filechk_$(1)) < $< > $@.tmp; \
44 if [ -r $@ ] && cmp -s $@ $@.tmp; then \
45 rm -f $@.tmp; \
46 else \
47 echo ' UPD $@'; \
48 mv -f $@.tmp $@; \
49 fi
50endef
51
Sam Ravnborg20a468b2006-01-22 13:34:15 +010052######
Sam Ravnborg9d6e7a72006-02-18 10:03:40 +010053# gcc support functions
Sam Ravnborg20a468b2006-01-22 13:34:15 +010054# See documentation in Documentation/kbuild/makefiles.txt
55
56# as-option
57# Usage: cflags-y += $(call as-option, -Wa$(comma)-isa=foo,)
58
59as-option = $(shell if $(CC) $(CFLAGS) $(1) -Wa,-Z -c -o /dev/null \
60 -xassembler /dev/null > /dev/null 2>&1; then echo "$(1)"; \
61 else echo "$(2)"; fi ;)
62
63# cc-option
64# Usage: cflags-y += $(call cc-option, -march=winchip-c6, -march=i586)
65
66cc-option = $(shell if $(CC) $(CFLAGS) $(1) -S -o /dev/null -xc /dev/null \
67 > /dev/null 2>&1; then echo "$(1)"; else echo "$(2)"; fi ;)
68
69# cc-option-yn
70# Usage: flag := $(call cc-option-yn, -march=winchip-c6)
71cc-option-yn = $(shell if $(CC) $(CFLAGS) $(1) -S -o /dev/null -xc /dev/null \
72 > /dev/null 2>&1; then echo "y"; else echo "n"; fi;)
73
74# cc-option-align
75# Prefix align with either -falign or -malign
76cc-option-align = $(subst -functions=0,,\
77 $(call cc-option,-falign-functions=0,-malign-functions=0))
78
79# cc-version
80# Usage gcc-ver := $(call cc-version, $(CC))
81cc-version = $(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-version.sh \
82 $(if $(1), $(1), $(CC)))
83
84# cc-ifversion
85# Usage: EXTRA_CFLAGS += $(call cc-ifversion, -lt, 0402, -O1)
86cc-ifversion = $(shell if [ $(call cc-version, $(CC)) $(1) $(2) ]; then \
87 echo $(3); fi;)
88
Sam Ravnborg8ec4b4f2005-07-25 20:10:36 +000089###
90# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.build obj=
91# Usage:
92# $(Q)$(MAKE) $(build)=dir
93build := -f $(if $(KBUILD_SRC),$(srctree)/)scripts/Makefile.build obj
94
Sam Ravnborgd9df92e2006-04-07 08:36:49 +020095# Prefix -I with $(srctree) if it is not an absolute path
96addtree = $(if $(filter-out -I/%,$(1)),$(patsubst -I%,-I$(srctree)/%,$(1))) $(1)
97# Find all -I options and call addtree
98flags = $(foreach o,$($(1)),$(if $(filter -I%,$(o)),$(call addtree,$(o)),$(o)))
99
Sam Ravnborg8ec4b4f2005-07-25 20:10:36 +0000100# If quiet is set, only print short version of command
Jan Beulich6176aa92006-01-30 10:04:27 +0100101cmd = @$(echo-cmd) $(cmd_$(1))
Sam Ravnborg8ec4b4f2005-07-25 20:10:36 +0000102
Sam Ravnborg0a504f22005-09-10 21:02:11 +0200103# Add $(obj)/ for paths that is not absolute
104objectify = $(foreach o,$(1),$(if $(filter /%,$(o)),$(o),$(obj)/$(o)))
105
Sam Ravnborg8ec4b4f2005-07-25 20:10:36 +0000106###
107# if_changed - execute command if any prerequisite is newer than
108# target, or command line has changed
109# if_changed_dep - as if_changed, but uses fixdep to reveal dependencies
110# including used config symbols
111# if_changed_rule - as if_changed but execute rule instead
112# See Documentation/kbuild/makefiles.txt for more info
113
114ifneq ($(KBUILD_NOCMDDEP),1)
115# Check if both arguments has same arguments. Result in empty string if equal
116# User may override this check using make KBUILD_NOCMDDEP=1
117arg-check = $(strip $(filter-out $(1), $(2)) $(filter-out $(2), $(1)) )
118endif
119
120# echo command. Short version is $(quiet) equals quiet, otherwise full command
121echo-cmd = $(if $($(quiet)cmd_$(1)), \
Sam Ravnborgd51bfb72006-01-06 22:35:59 +0100122 echo ' $(call escsq,$($(quiet)cmd_$(1)))';)
Sam Ravnborg8ec4b4f2005-07-25 20:10:36 +0000123
Jan Beulich6176aa92006-01-30 10:04:27 +0100124make-cmd = $(subst \#,\\\#,$(subst $$,$$$$,$(call escsq,$(cmd_$(1)))))
125
Sam Ravnborg8ec4b4f2005-07-25 20:10:36 +0000126# function to only execute the passed command if necessary
127# >'< substitution is for echo to work, >$< substitution to preserve $ when reloading .cmd file
128# note: when using inline perl scripts [perl -e '...$$t=1;...'] in $(cmd_xxx) double $$ your perl vars
Paul Smith4f193362006-03-05 17:14:10 -0500129#
130if_changed = $(if $(strip $(filter-out $(PHONY),$?) \
131 $(call arg-check, $(cmd_$(1)), $(cmd_$@)) ), \
Sam Ravnborg8ec4b4f2005-07-25 20:10:36 +0000132 @set -e; \
Jan Beulich6176aa92006-01-30 10:04:27 +0100133 $(echo-cmd) $(cmd_$(1)); \
134 echo 'cmd_$@ := $(make-cmd)' > $(@D)/.$(@F).cmd)
Sam Ravnborg8ec4b4f2005-07-25 20:10:36 +0000135
136# execute the command and also postprocess generated .d dependencies
137# file
Paul Smith4f193362006-03-05 17:14:10 -0500138if_changed_dep = $(if $(strip $(filter-out $(PHONY),$?) \
139 $(filter-out FORCE $(wildcard $^),$^) \
140 $(call arg-check, $(cmd_$(1)), $(cmd_$@)) ), \
Sam Ravnborg8ec4b4f2005-07-25 20:10:36 +0000141 @set -e; \
Jan Beulich6176aa92006-01-30 10:04:27 +0100142 $(echo-cmd) $(cmd_$(1)); \
143 scripts/basic/fixdep $(depfile) $@ '$(make-cmd)' > $(@D)/.$(@F).tmp; \
Sam Ravnborg8ec4b4f2005-07-25 20:10:36 +0000144 rm -f $(depfile); \
145 mv -f $(@D)/.$(@F).tmp $(@D)/.$(@F).cmd)
146
147# Usage: $(call if_changed_rule,foo)
148# will check if $(cmd_foo) changed, or any of the prequisites changed,
149# and if so will execute $(rule_foo)
Paul Smith4f193362006-03-05 17:14:10 -0500150if_changed_rule = $(if $(strip $(filter-out $(PHONY),$?) \
151 $(call arg-check, $(cmd_$(1)), $(cmd_$@)) ),\
Sam Ravnborg8ec4b4f2005-07-25 20:10:36 +0000152 @set -e; \
153 $(rule_$(1)))