18ec4b4ffSSam Ravnborg#### 28ec4b4ffSSam Ravnborg# kbuild: Generic definitions 38ec4b4ffSSam Ravnborg 4beda9f3aSRoman Zippel# Convenient variables 58ec4b4ffSSam Ravnborgcomma := , 613338935SMasahiro Yamadaquote := " 7d51bfb78SSam Ravnborgsquote := ' 88ec4b4ffSSam Ravnborgempty := 98ec4b4ffSSam Ravnborgspace := $(empty) $(empty) 108ec4b4ffSSam Ravnborg 118ec4b4ffSSam Ravnborg### 1248f1f058SSam Ravnborg# Name of target with a '.' as filename prefix. foo/bar.o => foo/.bar.o 1348f1f058SSam Ravnborgdot-target = $(dir $@).$(notdir $@) 1448f1f058SSam Ravnborg 1548f1f058SSam Ravnborg### 168ec4b4ffSSam Ravnborg# The temporary file to save gcc -MD generated dependencies must not 178ec4b4ffSSam Ravnborg# contain a comma 1848f1f058SSam Ravnborgdepfile = $(subst $(comma),_,$(dot-target).d) 198ec4b4ffSSam Ravnborg 208ec4b4ffSSam Ravnborg### 215e8d780dSSam Ravnborg# filename of target with directory and extension stripped 225e8d780dSSam Ravnborgbasetarget = $(basename $(notdir $@)) 235e8d780dSSam Ravnborg 245e8d780dSSam Ravnborg### 25e0318d85SArnaud Lacombe# filename of first prerequisite with directory and extension stripped 26e0318d85SArnaud Lacombebaseprereq = $(basename $(notdir $<)) 27e0318d85SArnaud Lacombe 28e0318d85SArnaud Lacombe### 29d51bfb78SSam Ravnborg# Escape single quote for use in echo statements 30d51bfb78SSam Ravnborgescsq = $(subst $(squote),'\$(squote)',$1) 31d51bfb78SSam Ravnborg 32d51bfb78SSam Ravnborg### 335410ecc0SMike Frysinger# Easy method for doing a status message 345410ecc0SMike Frysinger kecho := : 355410ecc0SMike Frysinger quiet_kecho := echo 365410ecc0SMike Frysingersilent_kecho := : 375410ecc0SMike Frysingerkecho := $($(quiet)kecho) 385410ecc0SMike Frysinger 395410ecc0SMike Frysinger### 408ec4b4ffSSam Ravnborg# filechk is used to check if the content of a generated file is updated. 418ec4b4ffSSam Ravnborg# Sample usage: 428ec4b4ffSSam Ravnborg# define filechk_sample 438ec4b4ffSSam Ravnborg# echo $KERNELRELEASE 448ec4b4ffSSam Ravnborg# endef 458ec4b4ffSSam Ravnborg# version.h : Makefile 468ec4b4ffSSam Ravnborg# $(call filechk,sample) 478ec4b4ffSSam Ravnborg# The rule defined shall write to stdout the content of the new file. 488ec4b4ffSSam Ravnborg# The existing file will be compared with the new one. 498ec4b4ffSSam Ravnborg# - If no file exist it is created 508ec4b4ffSSam Ravnborg# - If the content differ the new file is used 518ec4b4ffSSam Ravnborg# - If they are equal no change, and no timestamp update 528ec4b4ffSSam Ravnborg# - stdin is piped in from the first prerequisite ($<) so one has 538ec4b4ffSSam Ravnborg# to specify a valid file as first prerequisite (often the kbuild file) 548ec4b4ffSSam Ravnborgdefine filechk 558ec4b4ffSSam Ravnborg $(Q)set -e; \ 56fd54f502SMike Frysinger $(kecho) ' CHK $@'; \ 578ec4b4ffSSam Ravnborg mkdir -p $(dir $@); \ 588ec4b4ffSSam Ravnborg $(filechk_$(1)) < $< > $@.tmp; \ 598ec4b4ffSSam Ravnborg if [ -r $@ ] && cmp -s $@ $@.tmp; then \ 608ec4b4ffSSam Ravnborg rm -f $@.tmp; \ 618ec4b4ffSSam Ravnborg else \ 62fd54f502SMike Frysinger $(kecho) ' UPD $@'; \ 638ec4b4ffSSam Ravnborg mv -f $@.tmp $@; \ 648ec4b4ffSSam Ravnborg fi 658ec4b4ffSSam Ravnborgendef 668ec4b4ffSSam Ravnborg 6720a468b5SSam Ravnborg###### 689d6e7a70SSam Ravnborg# gcc support functions 6920a468b5SSam Ravnborg# See documentation in Documentation/kbuild/makefiles.txt 7020a468b5SSam Ravnborg 71910b4046SSam Ravnborg# cc-cross-prefix 72910b4046SSam Ravnborg# Usage: CROSS_COMPILE := $(call cc-cross-prefix, m68k-linux-gnu- m68k-linux-) 73910b4046SSam Ravnborg# Return first prefix where a prefix$(CC) is found in PATH. 74910b4046SSam Ravnborg# If no $(CC) found in PATH with listed prefixes return nothing 75910b4046SSam Ravnborgcc-cross-prefix = \ 76910b4046SSam Ravnborg $(word 1, $(foreach c,$(1), \ 77910b4046SSam Ravnborg $(shell set -e; \ 78910b4046SSam Ravnborg if (which $(strip $(c))$(CC)) > /dev/null 2>&1 ; then \ 79910b4046SSam Ravnborg echo $(c); \ 80910b4046SSam Ravnborg fi))) 81910b4046SSam Ravnborg 82beda9f3aSRoman Zippel# output directory for tests below 83beda9f3aSRoman ZippelTMPOUT := $(if $(KBUILD_EXTMOD),$(firstword $(KBUILD_EXTMOD))/) 84beda9f3aSRoman Zippel 85beda9f3aSRoman Zippel# try-run 86beda9f3aSRoman Zippel# Usage: option = $(call try-run, $(CC)...-o "$$TMP",option-ok,otherwise) 87beda9f3aSRoman Zippel# Exit code chooses option. "$$TMP" is can be used as temporary file and 88beda9f3aSRoman Zippel# is automatically cleaned up. 89beda9f3aSRoman Zippeltry-run = $(shell set -e; \ 90beda9f3aSRoman Zippel TMP="$(TMPOUT).$$$$.tmp"; \ 91691ef3e7SSam Ravnborg TMPO="$(TMPOUT).$$$$.o"; \ 92beda9f3aSRoman Zippel if ($(1)) >/dev/null 2>&1; \ 935de043f4SOleg Verych then echo "$(2)"; \ 945de043f4SOleg Verych else echo "$(3)"; \ 955de043f4SOleg Verych fi; \ 96691ef3e7SSam Ravnborg rm -f "$$TMP" "$$TMPO") 97347a00fbSRoman Zippel 9820a468b5SSam Ravnborg# as-option 9920a468b5SSam Ravnborg# Usage: cflags-y += $(call as-option,-Wa$(comma)-isa=foo,) 100beda9f3aSRoman Zippel 101beda9f3aSRoman Zippelas-option = $(call try-run,\ 102a0f97e06SSam Ravnborg $(CC) $(KBUILD_CFLAGS) $(1) -c -x assembler /dev/null -o "$$TMP",$(1),$(2)) 10320a468b5SSam Ravnborg 104e2414910SAndi Kleen# as-instr 105e2414910SAndi Kleen# Usage: cflags-y += $(call as-instr,instr,option1,option2) 106beda9f3aSRoman Zippel 107beda9f3aSRoman Zippelas-instr = $(call try-run,\ 108875de986SBernhard Walle printf "%b\n" "$(1)" | $(CC) $(KBUILD_AFLAGS) -c -x assembler -o "$$TMP" -,$(2),$(3)) 109e2414910SAndi Kleen 11020a468b5SSam Ravnborg# cc-option 11120a468b5SSam Ravnborg# Usage: cflags-y += $(call cc-option,-march=winchip-c6,-march=i586) 112beda9f3aSRoman Zippel 113beda9f3aSRoman Zippelcc-option = $(call try-run,\ 114c47efe55SJory A. Pratt $(CC) $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) $(1) -c -x c /dev/null -o "$$TMP",$(1),$(2)) 11520a468b5SSam Ravnborg 11620a468b5SSam Ravnborg# cc-option-yn 11720a468b5SSam Ravnborg# Usage: flag := $(call cc-option-yn,-march=winchip-c6) 118beda9f3aSRoman Zippelcc-option-yn = $(call try-run,\ 119c47efe55SJory A. Pratt $(CC) $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) $(1) -c -x c /dev/null -o "$$TMP",y,n) 12020a468b5SSam Ravnborg 12120a468b5SSam Ravnborg# cc-option-align 12220a468b5SSam Ravnborg# Prefix align with either -falign or -malign 12320a468b5SSam Ravnborgcc-option-align = $(subst -functions=0,,\ 12420a468b5SSam Ravnborg $(call cc-option,-falign-functions=0,-malign-functions=0)) 12520a468b5SSam Ravnborg 1268417da6fSMichal Marek# cc-disable-warning 1278417da6fSMichal Marek# Usage: cflags-y += $(call cc-disable-warning,unused-but-set-variable) 1288417da6fSMichal Marekcc-disable-warning = $(call try-run,\ 1298417da6fSMichal Marek $(CC) $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) -W$(strip $(1)) -c -x c /dev/null -o "$$TMP",-Wno-$(strip $(1))) 1308417da6fSMichal Marek 13120a468b5SSam Ravnborg# cc-version 1320ab2a272SSegher Boessenkool# Usage gcc-ver := $(call cc-version) 1338eb3afe0SSam Ravnborgcc-version = $(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-version.sh $(CC)) 13420a468b5SSam Ravnborg 1350ab2a272SSegher Boessenkool# cc-fullversion 1360ab2a272SSegher Boessenkool# Usage gcc-ver := $(call cc-fullversion) 1370ab2a272SSegher Boessenkoolcc-fullversion = $(shell $(CONFIG_SHELL) \ 1380ab2a272SSegher Boessenkool $(srctree)/scripts/gcc-version.sh -p $(CC)) 1390ab2a272SSegher Boessenkool 14020a468b5SSam Ravnborg# cc-ifversion 14120a468b5SSam Ravnborg# Usage: EXTRA_CFLAGS += $(call cc-ifversion, -lt, 0402, -O1) 1425de043f4SOleg Verychcc-ifversion = $(shell [ $(call cc-version, $(CC)) $(1) $(2) ] && echo $(3)) 14320a468b5SSam Ravnborg 144f86fd306SSam Ravnborg# cc-ldoption 145f86fd306SSam Ravnborg# Usage: ldflags += $(call cc-ldoption, -Wl$(comma)--hash-style=both) 146f86fd306SSam Ravnborgcc-ldoption = $(call try-run,\ 147beda9f3aSRoman Zippel $(CC) $(1) -nostdlib -x c /dev/null -o "$$TMP",$(1),$(2)) 1480b0bf7a3SRoland McGrath 149691ef3e7SSam Ravnborg# ld-option 150691ef3e7SSam Ravnborg# Usage: LDFLAGS += $(call ld-option, -X) 151691ef3e7SSam Ravnborgld-option = $(call try-run,\ 1525b83df2bSAntony Pavlov $(CC) -x c /dev/null -c -o "$$TMPO" ; $(LD) $(1) "$$TMPO" -o "$$TMP",$(1),$(2)) 153691ef3e7SSam Ravnborg 15440df759eSMichal Marek# ar-option 15540df759eSMichal Marek# Usage: KBUILD_ARFLAGS := $(call ar-option,D) 15640df759eSMichal Marek# Important: no spaces around options 15740df759eSMichal Marekar-option = $(call try-run, $(AR) rc$(1) "$$TMP",$(1),$(2)) 15840df759eSMichal Marek 159ccbef167SAndi Kleen# ld-version 160ccbef167SAndi Kleen# Usage: $(call ld-version) 161ccbef167SAndi Kleen# Note this is mainly for HJ Lu's 3 number binutil versions 162ccbef167SAndi Kleenld-version = $(shell $(LD) --version | $(srctree)/scripts/ld-version.sh) 163ccbef167SAndi Kleen 164ccbef167SAndi Kleen# ld-ifversion 165ccbef167SAndi Kleen# Usage: $(call ld-ifversion, -ge, 22252, y) 166ccbef167SAndi Kleenld-ifversion = $(shell [ $(call ld-version) $(1) $(2) ] && echo $(3)) 167ccbef167SAndi Kleen 1685de043f4SOleg Verych###### 1695de043f4SOleg Verych 170beda9f3aSRoman Zippel### 1718ec4b4ffSSam Ravnborg# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.build obj= 1728ec4b4ffSSam Ravnborg# Usage: 1738ec4b4ffSSam Ravnborg# $(Q)$(MAKE) $(build)=dir 1748ec4b4ffSSam Ravnborgbuild := -f $(if $(KBUILD_SRC),$(srctree)/)scripts/Makefile.build obj 1758ec4b4ffSSam Ravnborg 176bc081dd6SMichal Marek### 177bc081dd6SMichal Marek# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.modbuiltin obj= 178bc081dd6SMichal Marek# Usage: 179bc081dd6SMichal Marek# $(Q)$(MAKE) $(modbuiltin)=dir 180bc081dd6SMichal Marekmodbuiltin := -f $(if $(KBUILD_SRC),$(srctree)/)scripts/Makefile.modbuiltin obj 181bc081dd6SMichal Marek 182beda9f3aSRoman Zippel# Prefix -I with $(srctree) if it is not an absolute path. 1835b91c33cSSam Ravnborg# skip if -I has no parameter 1845b91c33cSSam Ravnborgaddtree = $(if $(patsubst -I%,%,$(1)), \ 1855b91c33cSSam Ravnborg$(if $(filter-out -I/%,$(1)),$(patsubst -I%,-I$(srctree)/%,$(1))) $(1)) 186d9df92e2SSam Ravnborg 1875de043f4SOleg Verych# Find all -I options and call addtree 188beda9f3aSRoman Zippelflags = $(foreach o,$($(1)),$(if $(filter -I%,$(o)),$(call addtree,$(o)),$(o))) 1895de043f4SOleg Verych 1905de043f4SOleg Verych# echo command. 1915de043f4SOleg Verych# Short version is used, if $(quiet) equals `quiet_', otherwise full one. 1925de043f4SOleg Verychecho-cmd = $(if $($(quiet)cmd_$(1)),\ 1935de043f4SOleg Verych echo ' $(call escsq,$($(quiet)cmd_$(1)))$(echo-why)';) 1945de043f4SOleg Verych 1955de043f4SOleg Verych# printing commands 1966176aa9aSJan Beulichcmd = @$(echo-cmd) $(cmd_$(1)) 1978ec4b4ffSSam Ravnborg 1985de043f4SOleg Verych# Add $(obj)/ for paths that are not absolute 1990a504f25SSam Ravnborgobjectify = $(foreach o,$(1),$(if $(filter /%,$(o)),$(o),$(obj)/$(o))) 2000a504f25SSam Ravnborg 2018ec4b4ffSSam Ravnborg### 2028ec4b4ffSSam Ravnborg# if_changed - execute command if any prerequisite is newer than 2038ec4b4ffSSam Ravnborg# target, or command line has changed 2048ec4b4ffSSam Ravnborg# if_changed_dep - as if_changed, but uses fixdep to reveal dependencies 2058ec4b4ffSSam Ravnborg# including used config symbols 2068ec4b4ffSSam Ravnborg# if_changed_rule - as if_changed but execute rule instead 2078ec4b4ffSSam Ravnborg# See Documentation/kbuild/makefiles.txt for more info 2088ec4b4ffSSam Ravnborg 2098ec4b4ffSSam Ravnborgifneq ($(KBUILD_NOCMDDEP),1) 210beda9f3aSRoman Zippel# Check if both arguments has same arguments. Result is empty string if equal. 2118ec4b4ffSSam Ravnborg# User may override this check using make KBUILD_NOCMDDEP=1 21248f1f058SSam Ravnborgarg-check = $(strip $(filter-out $(cmd_$(1)), $(cmd_$@)) \ 21348f1f058SSam Ravnborg $(filter-out $(cmd_$@), $(cmd_$(1))) ) 214c4d5ee13SMichal Marekelse 215c4d5ee13SMichal Marekarg-check = $(if $(strip $(cmd_$@)),,1) 2168ec4b4ffSSam Ravnborgendif 2178ec4b4ffSSam Ravnborg 218*164f0d2eSMichal Marek# Replace >$< with >$$< to preserve $ when reloading the .cmd file 219*164f0d2eSMichal Marek# (needed for make) 220*164f0d2eSMichal Marek# Replace >#< with >\#< to avoid starting a comment in the .cmd file 221*164f0d2eSMichal Marek# (needed for make) 222*164f0d2eSMichal Marek# Replace >'< with >'\''< to be able to enclose the whole string in '...' 223*164f0d2eSMichal Marek# (needed for the shell) 224*164f0d2eSMichal Marekmake-cmd = $(call escsq,$(subst \#,\\\#,$(subst $$,$$$$,$(cmd_$(1))))) 2256176aa9aSJan Beulich 22648f1f058SSam Ravnborg# Find any prerequisites that is newer than target or that does not exist. 22748f1f058SSam Ravnborg# PHONY targets skipped in both cases. 22848f1f058SSam Ravnborgany-prereq = $(filter-out $(PHONY),$?) $(filter-out $(PHONY) $(wildcard $^),$^) 22948f1f058SSam Ravnborg 2305de043f4SOleg Verych# Execute command if command has changed or prerequisite(s) are updated. 2318ec4b4ffSSam Ravnborg# 23248f1f058SSam Ravnborgif_changed = $(if $(strip $(any-prereq) $(arg-check)), \ 2338ec4b4ffSSam Ravnborg @set -e; \ 2346176aa9aSJan Beulich $(echo-cmd) $(cmd_$(1)); \ 235*164f0d2eSMichal Marek printf '%s\n' 'cmd_$@ := $(make-cmd)' > $(dot-target).cmd) 2368ec4b4ffSSam Ravnborg 2375de043f4SOleg Verych# Execute the command and also postprocess generated .d dependencies file. 23848f1f058SSam Ravnborgif_changed_dep = $(if $(strip $(any-prereq) $(arg-check) ), \ 2398ec4b4ffSSam Ravnborg @set -e; \ 2406176aa9aSJan Beulich $(echo-cmd) $(cmd_$(1)); \ 24148f1f058SSam Ravnborg scripts/basic/fixdep $(depfile) $@ '$(make-cmd)' > $(dot-target).tmp;\ 2428ec4b4ffSSam Ravnborg rm -f $(depfile); \ 24348f1f058SSam Ravnborg mv -f $(dot-target).tmp $(dot-target).cmd) 2448ec4b4ffSSam Ravnborg 2458ec4b4ffSSam Ravnborg# Usage: $(call if_changed_rule,foo) 246beda9f3aSRoman Zippel# Will check if $(cmd_foo) or any of the prerequisites changed, 247beda9f3aSRoman Zippel# and if so will execute $(rule_foo). 24848f1f058SSam Ravnborgif_changed_rule = $(if $(strip $(any-prereq) $(arg-check) ), \ 2498ec4b4ffSSam Ravnborg @set -e; \ 2508ec4b4ffSSam Ravnborg $(rule_$(1))) 25148f1f058SSam Ravnborg 25245d506bdSSam Ravnborg### 25345d506bdSSam Ravnborg# why - tell why a a target got build 25445d506bdSSam Ravnborg# enabled by make V=2 25545d506bdSSam Ravnborg# Output (listed in the order they are checked): 25645d506bdSSam Ravnborg# (1) - due to target is PHONY 25745d506bdSSam Ravnborg# (2) - due to target missing 25845d506bdSSam Ravnborg# (3) - due to: file1.h file2.h 25945d506bdSSam Ravnborg# (4) - due to command line change 26045d506bdSSam Ravnborg# (5) - due to missing .cmd file 26145d506bdSSam Ravnborg# (6) - due to target not in $(targets) 26245d506bdSSam Ravnborg# (1) PHONY targets are always build 26345d506bdSSam Ravnborg# (2) No target, so we better build it 26445d506bdSSam Ravnborg# (3) Prerequisite is newer than target 26545d506bdSSam Ravnborg# (4) The command line stored in the file named dir/.target.cmd 26645d506bdSSam Ravnborg# differed from actual command line. This happens when compiler 26745d506bdSSam Ravnborg# options changes 26845d506bdSSam Ravnborg# (5) No dir/.target.cmd file (used to store command line) 26945d506bdSSam Ravnborg# (6) No dir/.target.cmd file and target not listed in $(targets) 27045d506bdSSam Ravnborg# This is a good hint that there is a bug in the kbuild file 27145d506bdSSam Ravnborgifeq ($(KBUILD_VERBOSE),2) 27245d506bdSSam Ravnborgwhy = \ 27345d506bdSSam Ravnborg $(if $(filter $@, $(PHONY)),- due to target is PHONY, \ 27445d506bdSSam Ravnborg $(if $(wildcard $@), \ 27545d506bdSSam Ravnborg $(if $(strip $(any-prereq)),- due to: $(any-prereq), \ 27645d506bdSSam Ravnborg $(if $(arg-check), \ 27745d506bdSSam Ravnborg $(if $(cmd_$@),- due to command line change, \ 27845d506bdSSam Ravnborg $(if $(filter $@, $(targets)), \ 27945d506bdSSam Ravnborg - due to missing .cmd file, \ 28045d506bdSSam Ravnborg - due to $(notdir $@) not in $$(targets) \ 28145d506bdSSam Ravnborg ) \ 28245d506bdSSam Ravnborg ) \ 28345d506bdSSam Ravnborg ) \ 28445d506bdSSam Ravnborg ), \ 28545d506bdSSam Ravnborg - due to target missing \ 28645d506bdSSam Ravnborg ) \ 28745d506bdSSam Ravnborg ) 28845d506bdSSam Ravnborg 28945d506bdSSam Ravnborgecho-why = $(call escsq, $(strip $(why))) 29045d506bdSSam Ravnborgendif 291