xref: /linux/scripts/Kbuild.include (revision 2a2ed2db353d949c06b6ef8b6913f65b39111eab)
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# basetarget equals the filename of the target with no extension.
17# So 'foo/bar.o' becomes 'bar'
18basetarget = $(basename $(notdir $@))
19
20###
21# Escape single quote for use in echo statements
22escsq = $(subst $(squote),'\$(squote)',$1)
23
24###
25# 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
52######
53# gcc support functions
54# 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
89###
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
95# 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
100# If quiet is set, only print short version of command
101cmd = @$(echo-cmd) $(cmd_$(1))
102
103# Add $(obj)/ for paths that is not absolute
104objectify = $(foreach o,$(1),$(if $(filter /%,$(o)),$(o),$(obj)/$(o)))
105
106###
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)), \
122	echo '  $(call escsq,$($(quiet)cmd_$(1)))';)
123
124make-cmd = $(subst \#,\\\#,$(subst $$,$$$$,$(call escsq,$(cmd_$(1)))))
125
126# 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
129#
130if_changed = $(if $(strip $(filter-out $(PHONY),$?)          \
131		$(call arg-check, $(cmd_$(1)), $(cmd_$@)) ), \
132	@set -e; \
133	$(echo-cmd) $(cmd_$(1)); \
134	echo 'cmd_$@ := $(make-cmd)' > $(@D)/.$(@F).cmd)
135
136# execute the command and also postprocess generated .d dependencies
137# file
138if_changed_dep = $(if $(strip $(filter-out $(PHONY),$?)  \
139		$(filter-out FORCE $(wildcard $^),$^)    \
140	$(call arg-check, $(cmd_$(1)), $(cmd_$@)) ),     \
141	@set -e; \
142	$(echo-cmd) $(cmd_$(1)); \
143	scripts/basic/fixdep $(depfile) $@ '$(make-cmd)' > $(@D)/.$(@F).tmp; \
144	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)
150if_changed_rule = $(if $(strip $(filter-out $(PHONY),$?)            \
151			$(call arg-check, $(cmd_$(1)), $(cmd_$@)) ),\
152			@set -e; \
153			$(rule_$(1)))
154