xref: /linux/scripts/Kbuild.include (revision 48f1f0589dd09df6ea07d41c737db3218ad2cb79)
18ec4b4ffSSam Ravnborg####
28ec4b4ffSSam Ravnborg# kbuild: Generic definitions
38ec4b4ffSSam Ravnborg
48ec4b4ffSSam Ravnborg# Convinient variables
58ec4b4ffSSam Ravnborgcomma   := ,
6d51bfb78SSam Ravnborgsquote  := '
78ec4b4ffSSam Ravnborgempty   :=
88ec4b4ffSSam Ravnborgspace   := $(empty) $(empty)
98ec4b4ffSSam Ravnborg
108ec4b4ffSSam Ravnborg###
11*48f1f058SSam Ravnborg# Name of target with a '.' as filename prefix. foo/bar.o => foo/.bar.o
12*48f1f058SSam Ravnborgdot-target = $(dir $@).$(notdir $@)
13*48f1f058SSam Ravnborg
14*48f1f058SSam Ravnborg###
158ec4b4ffSSam Ravnborg# The temporary file to save gcc -MD generated dependencies must not
168ec4b4ffSSam Ravnborg# contain a comma
17*48f1f058SSam Ravnborgdepfile = $(subst $(comma),_,$(dot-target).d)
188ec4b4ffSSam Ravnborg
198ec4b4ffSSam Ravnborg###
205e8d780dSSam Ravnborg# filename of target with directory and extension stripped
215e8d780dSSam Ravnborgbasetarget = $(basename $(notdir $@))
225e8d780dSSam Ravnborg
235e8d780dSSam Ravnborg###
24d51bfb78SSam Ravnborg# Escape single quote for use in echo statements
25d51bfb78SSam Ravnborgescsq = $(subst $(squote),'\$(squote)',$1)
26d51bfb78SSam Ravnborg
27d51bfb78SSam Ravnborg###
288ec4b4ffSSam Ravnborg# filechk is used to check if the content of a generated file is updated.
298ec4b4ffSSam Ravnborg# Sample usage:
308ec4b4ffSSam Ravnborg# define filechk_sample
318ec4b4ffSSam Ravnborg#	echo $KERNELRELEASE
328ec4b4ffSSam Ravnborg# endef
338ec4b4ffSSam Ravnborg# version.h : Makefile
348ec4b4ffSSam Ravnborg#	$(call filechk,sample)
358ec4b4ffSSam Ravnborg# The rule defined shall write to stdout the content of the new file.
368ec4b4ffSSam Ravnborg# The existing file will be compared with the new one.
378ec4b4ffSSam Ravnborg# - If no file exist it is created
388ec4b4ffSSam Ravnborg# - If the content differ the new file is used
398ec4b4ffSSam Ravnborg# - If they are equal no change, and no timestamp update
408ec4b4ffSSam Ravnborg# - stdin is piped in from the first prerequisite ($<) so one has
418ec4b4ffSSam Ravnborg#   to specify a valid file as first prerequisite (often the kbuild file)
428ec4b4ffSSam Ravnborgdefine filechk
438ec4b4ffSSam Ravnborg	$(Q)set -e;				\
448ec4b4ffSSam Ravnborg	echo '  CHK     $@';			\
458ec4b4ffSSam Ravnborg	mkdir -p $(dir $@);			\
468ec4b4ffSSam Ravnborg	$(filechk_$(1)) < $< > $@.tmp;		\
478ec4b4ffSSam Ravnborg	if [ -r $@ ] && cmp -s $@ $@.tmp; then	\
488ec4b4ffSSam Ravnborg		rm -f $@.tmp;			\
498ec4b4ffSSam Ravnborg	else					\
508ec4b4ffSSam Ravnborg		echo '  UPD     $@';		\
518ec4b4ffSSam Ravnborg		mv -f $@.tmp $@;		\
528ec4b4ffSSam Ravnborg	fi
538ec4b4ffSSam Ravnborgendef
548ec4b4ffSSam Ravnborg
5520a468b5SSam Ravnborg######
569d6e7a70SSam Ravnborg# gcc support functions
5720a468b5SSam Ravnborg# See documentation in Documentation/kbuild/makefiles.txt
5820a468b5SSam Ravnborg
5920a468b5SSam Ravnborg# as-option
6020a468b5SSam Ravnborg# Usage: cflags-y += $(call as-option, -Wa$(comma)-isa=foo,)
6120a468b5SSam Ravnborg
6220a468b5SSam Ravnborgas-option = $(shell if $(CC) $(CFLAGS) $(1) -Wa,-Z -c -o /dev/null \
6320a468b5SSam Ravnborg	     -xassembler /dev/null > /dev/null 2>&1; then echo "$(1)"; \
6420a468b5SSam Ravnborg	     else echo "$(2)"; fi ;)
6520a468b5SSam Ravnborg
6620a468b5SSam Ravnborg# cc-option
6720a468b5SSam Ravnborg# Usage: cflags-y += $(call cc-option, -march=winchip-c6, -march=i586)
6820a468b5SSam Ravnborg
6920a468b5SSam Ravnborgcc-option = $(shell if $(CC) $(CFLAGS) $(1) -S -o /dev/null -xc /dev/null \
7020a468b5SSam Ravnborg             > /dev/null 2>&1; then echo "$(1)"; else echo "$(2)"; fi ;)
7120a468b5SSam Ravnborg
7220a468b5SSam Ravnborg# cc-option-yn
7320a468b5SSam Ravnborg# Usage: flag := $(call cc-option-yn, -march=winchip-c6)
7420a468b5SSam Ravnborgcc-option-yn = $(shell if $(CC) $(CFLAGS) $(1) -S -o /dev/null -xc /dev/null \
7520a468b5SSam Ravnborg                > /dev/null 2>&1; then echo "y"; else echo "n"; fi;)
7620a468b5SSam Ravnborg
7720a468b5SSam Ravnborg# cc-option-align
7820a468b5SSam Ravnborg# Prefix align with either -falign or -malign
7920a468b5SSam Ravnborgcc-option-align = $(subst -functions=0,,\
8020a468b5SSam Ravnborg	$(call cc-option,-falign-functions=0,-malign-functions=0))
8120a468b5SSam Ravnborg
8220a468b5SSam Ravnborg# cc-version
8320a468b5SSam Ravnborg# Usage gcc-ver := $(call cc-version, $(CC))
848eb3afe0SSam Ravnborgcc-version = $(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-version.sh $(CC))
8520a468b5SSam Ravnborg
8620a468b5SSam Ravnborg# cc-ifversion
8720a468b5SSam Ravnborg# Usage:  EXTRA_CFLAGS += $(call cc-ifversion, -lt, 0402, -O1)
8820a468b5SSam Ravnborgcc-ifversion = $(shell if [ $(call cc-version, $(CC)) $(1) $(2) ]; then \
8920a468b5SSam Ravnborg                       echo $(3); fi;)
9020a468b5SSam Ravnborg
910b0bf7a3SRoland McGrath# ld-option
920b0bf7a3SRoland McGrath# Usage: ldflags += $(call ld-option, -Wl$(comma)--hash-style=both)
930b0bf7a3SRoland McGrathld-option = $(shell if $(CC) $(1) \
940b0bf7a3SRoland McGrath			     -nostdlib -o ldtest$$$$.out -xc /dev/null \
950b0bf7a3SRoland McGrath             > /dev/null 2>&1; then echo "$(1)"; else echo "$(2)"; fi; \
960b0bf7a3SRoland McGrath	     rm -f ldtest$$$$.out)
970b0bf7a3SRoland McGrath
988ec4b4ffSSam Ravnborg###
998ec4b4ffSSam Ravnborg# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.build obj=
1008ec4b4ffSSam Ravnborg# Usage:
1018ec4b4ffSSam Ravnborg# $(Q)$(MAKE) $(build)=dir
1028ec4b4ffSSam Ravnborgbuild := -f $(if $(KBUILD_SRC),$(srctree)/)scripts/Makefile.build obj
1038ec4b4ffSSam Ravnborg
104d9df92e2SSam Ravnborg# Prefix -I with $(srctree) if it is not an absolute path
105d9df92e2SSam Ravnborgaddtree = $(if $(filter-out -I/%,$(1)),$(patsubst -I%,-I$(srctree)/%,$(1))) $(1)
106d9df92e2SSam Ravnborg# Find all -I options and call addtree
107d9df92e2SSam Ravnborgflags = $(foreach o,$($(1)),$(if $(filter -I%,$(o)),$(call addtree,$(o)),$(o)))
108d9df92e2SSam Ravnborg
1098ec4b4ffSSam Ravnborg# If quiet is set, only print short version of command
1106176aa9aSJan Beulichcmd = @$(echo-cmd) $(cmd_$(1))
1118ec4b4ffSSam Ravnborg
1120a504f25SSam Ravnborg# Add $(obj)/ for paths that is not absolute
1130a504f25SSam Ravnborgobjectify = $(foreach o,$(1),$(if $(filter /%,$(o)),$(o),$(obj)/$(o)))
1140a504f25SSam Ravnborg
1158ec4b4ffSSam Ravnborg###
1168ec4b4ffSSam Ravnborg# if_changed      - execute command if any prerequisite is newer than
1178ec4b4ffSSam Ravnborg#                   target, or command line has changed
1188ec4b4ffSSam Ravnborg# if_changed_dep  - as if_changed, but uses fixdep to reveal dependencies
1198ec4b4ffSSam Ravnborg#                   including used config symbols
1208ec4b4ffSSam Ravnborg# if_changed_rule - as if_changed but execute rule instead
1218ec4b4ffSSam Ravnborg# See Documentation/kbuild/makefiles.txt for more info
1228ec4b4ffSSam Ravnborg
1238ec4b4ffSSam Ravnborgifneq ($(KBUILD_NOCMDDEP),1)
1248ec4b4ffSSam Ravnborg# Check if both arguments has same arguments. Result in empty string if equal
1258ec4b4ffSSam Ravnborg# User may override this check using make KBUILD_NOCMDDEP=1
126*48f1f058SSam Ravnborgarg-check = $(strip $(filter-out $(cmd_$(1)), $(cmd_$@)) \
127*48f1f058SSam Ravnborg                    $(filter-out $(cmd_$@),   $(cmd_$(1))) )
1288ec4b4ffSSam Ravnborgendif
1298ec4b4ffSSam Ravnborg
1308ec4b4ffSSam Ravnborg# echo command. Short version is $(quiet) equals quiet, otherwise full command
1318ec4b4ffSSam Ravnborgecho-cmd = $(if $($(quiet)cmd_$(1)), \
132d51bfb78SSam Ravnborg	echo '  $(call escsq,$($(quiet)cmd_$(1)))';)
1338ec4b4ffSSam Ravnborg
134*48f1f058SSam Ravnborg# >'< substitution is for echo to work,
135*48f1f058SSam Ravnborg# >$< substitution to preserve $ when reloading .cmd file
136*48f1f058SSam Ravnborg# note: when using inline perl scripts [perl -e '...$$t=1;...']
137*48f1f058SSam Ravnborg# in $(cmd_xxx) double $$ your perl vars
1386176aa9aSJan Beulichmake-cmd = $(subst \#,\\\#,$(subst $$,$$$$,$(call escsq,$(cmd_$(1)))))
1396176aa9aSJan Beulich
140*48f1f058SSam Ravnborg# Find any prerequisites that is newer than target or that does not exist.
141*48f1f058SSam Ravnborg# PHONY targets skipped in both cases.
142*48f1f058SSam Ravnborgany-prereq = $(filter-out $(PHONY),$?) $(filter-out $(PHONY) $(wildcard $^),$^)
143*48f1f058SSam Ravnborg
144*48f1f058SSam Ravnborg# Execute command if command has changed or prerequisitei(s) are updated
1458ec4b4ffSSam Ravnborg#
146*48f1f058SSam Ravnborgif_changed = $(if $(strip $(any-prereq) $(arg-check)),                       \
1478ec4b4ffSSam Ravnborg	@set -e;                                                             \
1486176aa9aSJan Beulich	$(echo-cmd) $(cmd_$(1));                                             \
149*48f1f058SSam Ravnborg	echo 'cmd_$@ := $(make-cmd)' > $(dot-target).cmd)
1508ec4b4ffSSam Ravnborg
1518ec4b4ffSSam Ravnborg# execute the command and also postprocess generated .d dependencies
1528ec4b4ffSSam Ravnborg# file
153*48f1f058SSam Ravnborgif_changed_dep = $(if $(strip $(any-prereq) $(arg-check) ),                  \
1548ec4b4ffSSam Ravnborg	@set -e;                                                             \
1556176aa9aSJan Beulich	$(echo-cmd) $(cmd_$(1));                                             \
156*48f1f058SSam Ravnborg	scripts/basic/fixdep $(depfile) $@ '$(make-cmd)' > $(dot-target).tmp;\
1578ec4b4ffSSam Ravnborg	rm -f $(depfile);                                                    \
158*48f1f058SSam Ravnborg	mv -f $(dot-target).tmp $(dot-target).cmd)
1598ec4b4ffSSam Ravnborg
1608ec4b4ffSSam Ravnborg# Usage: $(call if_changed_rule,foo)
1618ec4b4ffSSam Ravnborg# will check if $(cmd_foo) changed, or any of the prequisites changed,
1628ec4b4ffSSam Ravnborg# and if so will execute $(rule_foo)
163*48f1f058SSam Ravnborgif_changed_rule = $(if $(strip $(any-prereq) $(arg-check) ),                 \
1648ec4b4ffSSam Ravnborg	@set -e;                                                             \
1658ec4b4ffSSam Ravnborg	$(rule_$(1)))
166*48f1f058SSam Ravnborg
167