18ec4b4ffSSam Ravnborg#### 28ec4b4ffSSam Ravnborg# kbuild: Generic definitions 38ec4b4ffSSam Ravnborg 4beda9f3aSRoman Zippel# Convenient variables 58ec4b4ffSSam Ravnborgcomma := , 6d51bfb78SSam Ravnborgsquote := ' 78ec4b4ffSSam Ravnborgempty := 88ec4b4ffSSam Ravnborgspace := $(empty) $(empty) 98ec4b4ffSSam Ravnborg 108ec4b4ffSSam Ravnborg### 1148f1f058SSam Ravnborg# Name of target with a '.' as filename prefix. foo/bar.o => foo/.bar.o 1248f1f058SSam Ravnborgdot-target = $(dir $@).$(notdir $@) 1348f1f058SSam Ravnborg 1448f1f058SSam Ravnborg### 158ec4b4ffSSam Ravnborg# The temporary file to save gcc -MD generated dependencies must not 168ec4b4ffSSam Ravnborg# contain a comma 1748f1f058SSam 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### 24e0318d85SArnaud Lacombe# filename of first prerequisite with directory and extension stripped 25e0318d85SArnaud Lacombebaseprereq = $(basename $(notdir $<)) 26e0318d85SArnaud Lacombe 27e0318d85SArnaud Lacombe### 28d51bfb78SSam Ravnborg# Escape single quote for use in echo statements 29d51bfb78SSam Ravnborgescsq = $(subst $(squote),'\$(squote)',$1) 30d51bfb78SSam Ravnborg 31d51bfb78SSam Ravnborg### 325410ecc0SMike Frysinger# Easy method for doing a status message 335410ecc0SMike Frysinger kecho := : 345410ecc0SMike Frysinger quiet_kecho := echo 355410ecc0SMike Frysingersilent_kecho := : 365410ecc0SMike Frysingerkecho := $($(quiet)kecho) 375410ecc0SMike Frysinger 385410ecc0SMike Frysinger### 398ec4b4ffSSam Ravnborg# filechk is used to check if the content of a generated file is updated. 408ec4b4ffSSam Ravnborg# Sample usage: 418ec4b4ffSSam Ravnborg# define filechk_sample 428ec4b4ffSSam Ravnborg# echo $KERNELRELEASE 438ec4b4ffSSam Ravnborg# endef 448ec4b4ffSSam Ravnborg# version.h : Makefile 458ec4b4ffSSam Ravnborg# $(call filechk,sample) 468ec4b4ffSSam Ravnborg# The rule defined shall write to stdout the content of the new file. 478ec4b4ffSSam Ravnborg# The existing file will be compared with the new one. 488ec4b4ffSSam Ravnborg# - If no file exist it is created 498ec4b4ffSSam Ravnborg# - If the content differ the new file is used 508ec4b4ffSSam Ravnborg# - If they are equal no change, and no timestamp update 518ec4b4ffSSam Ravnborg# - stdin is piped in from the first prerequisite ($<) so one has 528ec4b4ffSSam Ravnborg# to specify a valid file as first prerequisite (often the kbuild file) 538ec4b4ffSSam Ravnborgdefine filechk 548ec4b4ffSSam Ravnborg $(Q)set -e; \ 55fd54f502SMike Frysinger $(kecho) ' CHK $@'; \ 568ec4b4ffSSam Ravnborg mkdir -p $(dir $@); \ 578ec4b4ffSSam Ravnborg $(filechk_$(1)) < $< > $@.tmp; \ 588ec4b4ffSSam Ravnborg if [ -r $@ ] && cmp -s $@ $@.tmp; then \ 598ec4b4ffSSam Ravnborg rm -f $@.tmp; \ 608ec4b4ffSSam Ravnborg else \ 61fd54f502SMike Frysinger $(kecho) ' UPD $@'; \ 628ec4b4ffSSam Ravnborg mv -f $@.tmp $@; \ 638ec4b4ffSSam Ravnborg fi 648ec4b4ffSSam Ravnborgendef 658ec4b4ffSSam Ravnborg 6620a468b5SSam Ravnborg###### 679d6e7a70SSam Ravnborg# gcc support functions 6820a468b5SSam Ravnborg# See documentation in Documentation/kbuild/makefiles.txt 6920a468b5SSam Ravnborg 70910b4046SSam Ravnborg# cc-cross-prefix 71910b4046SSam Ravnborg# Usage: CROSS_COMPILE := $(call cc-cross-prefix, m68k-linux-gnu- m68k-linux-) 72910b4046SSam Ravnborg# Return first prefix where a prefix$(CC) is found in PATH. 73910b4046SSam Ravnborg# If no $(CC) found in PATH with listed prefixes return nothing 74910b4046SSam Ravnborgcc-cross-prefix = \ 75910b4046SSam Ravnborg $(word 1, $(foreach c,$(1), \ 76910b4046SSam Ravnborg $(shell set -e; \ 77910b4046SSam Ravnborg if (which $(strip $(c))$(CC)) > /dev/null 2>&1 ; then \ 78910b4046SSam Ravnborg echo $(c); \ 79910b4046SSam Ravnborg fi))) 80910b4046SSam Ravnborg 81beda9f3aSRoman Zippel# output directory for tests below 82beda9f3aSRoman ZippelTMPOUT := $(if $(KBUILD_EXTMOD),$(firstword $(KBUILD_EXTMOD))/) 83beda9f3aSRoman Zippel 84beda9f3aSRoman Zippel# try-run 85beda9f3aSRoman Zippel# Usage: option = $(call try-run, $(CC)...-o "$$TMP",option-ok,otherwise) 86beda9f3aSRoman Zippel# Exit code chooses option. "$$TMP" is can be used as temporary file and 87beda9f3aSRoman Zippel# is automatically cleaned up. 88beda9f3aSRoman Zippeltry-run = $(shell set -e; \ 89beda9f3aSRoman Zippel TMP="$(TMPOUT).$$$$.tmp"; \ 90691ef3e7SSam Ravnborg TMPO="$(TMPOUT).$$$$.o"; \ 91beda9f3aSRoman Zippel if ($(1)) >/dev/null 2>&1; \ 925de043f4SOleg Verych then echo "$(2)"; \ 935de043f4SOleg Verych else echo "$(3)"; \ 945de043f4SOleg Verych fi; \ 95691ef3e7SSam Ravnborg rm -f "$$TMP" "$$TMPO") 96347a00fbSRoman Zippel 9720a468b5SSam Ravnborg# as-option 9820a468b5SSam Ravnborg# Usage: cflags-y += $(call as-option,-Wa$(comma)-isa=foo,) 99beda9f3aSRoman Zippel 100beda9f3aSRoman Zippelas-option = $(call try-run,\ 101a0f97e06SSam Ravnborg $(CC) $(KBUILD_CFLAGS) $(1) -c -xassembler /dev/null -o "$$TMP",$(1),$(2)) 10220a468b5SSam Ravnborg 103e2414910SAndi Kleen# as-instr 104e2414910SAndi Kleen# Usage: cflags-y += $(call as-instr,instr,option1,option2) 105beda9f3aSRoman Zippel 106beda9f3aSRoman Zippelas-instr = $(call try-run,\ 107*875de986SBernhard Walle printf "%b\n" "$(1)" | $(CC) $(KBUILD_AFLAGS) -c -xassembler -o "$$TMP" -,$(2),$(3)) 108e2414910SAndi Kleen 10920a468b5SSam Ravnborg# cc-option 11020a468b5SSam Ravnborg# Usage: cflags-y += $(call cc-option,-march=winchip-c6,-march=i586) 111beda9f3aSRoman Zippel 112beda9f3aSRoman Zippelcc-option = $(call try-run,\ 113c47efe55SJory A. Pratt $(CC) $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) $(1) -c -xc /dev/null -o "$$TMP",$(1),$(2)) 11420a468b5SSam Ravnborg 11520a468b5SSam Ravnborg# cc-option-yn 11620a468b5SSam Ravnborg# Usage: flag := $(call cc-option-yn,-march=winchip-c6) 117beda9f3aSRoman Zippelcc-option-yn = $(call try-run,\ 118c47efe55SJory A. Pratt $(CC) $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) $(1) -c -xc /dev/null -o "$$TMP",y,n) 11920a468b5SSam Ravnborg 12020a468b5SSam Ravnborg# cc-option-align 12120a468b5SSam Ravnborg# Prefix align with either -falign or -malign 12220a468b5SSam Ravnborgcc-option-align = $(subst -functions=0,,\ 12320a468b5SSam Ravnborg $(call cc-option,-falign-functions=0,-malign-functions=0)) 12420a468b5SSam Ravnborg 1258417da6fSMichal Marek# cc-disable-warning 1268417da6fSMichal Marek# Usage: cflags-y += $(call cc-disable-warning,unused-but-set-variable) 1278417da6fSMichal Marekcc-disable-warning = $(call try-run,\ 1288417da6fSMichal Marek $(CC) $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) -W$(strip $(1)) -c -xc /dev/null -o "$$TMP",-Wno-$(strip $(1))) 1298417da6fSMichal Marek 13020a468b5SSam Ravnborg# cc-version 1310ab2a272SSegher Boessenkool# Usage gcc-ver := $(call cc-version) 1328eb3afe0SSam Ravnborgcc-version = $(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-version.sh $(CC)) 13320a468b5SSam Ravnborg 1340ab2a272SSegher Boessenkool# cc-fullversion 1350ab2a272SSegher Boessenkool# Usage gcc-ver := $(call cc-fullversion) 1360ab2a272SSegher Boessenkoolcc-fullversion = $(shell $(CONFIG_SHELL) \ 1370ab2a272SSegher Boessenkool $(srctree)/scripts/gcc-version.sh -p $(CC)) 1380ab2a272SSegher Boessenkool 13920a468b5SSam Ravnborg# cc-ifversion 14020a468b5SSam Ravnborg# Usage: EXTRA_CFLAGS += $(call cc-ifversion, -lt, 0402, -O1) 1415de043f4SOleg Verychcc-ifversion = $(shell [ $(call cc-version, $(CC)) $(1) $(2) ] && echo $(3)) 14220a468b5SSam Ravnborg 143f86fd306SSam Ravnborg# cc-ldoption 144f86fd306SSam Ravnborg# Usage: ldflags += $(call cc-ldoption, -Wl$(comma)--hash-style=both) 145f86fd306SSam Ravnborgcc-ldoption = $(call try-run,\ 146beda9f3aSRoman Zippel $(CC) $(1) -nostdlib -xc /dev/null -o "$$TMP",$(1),$(2)) 1470b0bf7a3SRoland McGrath 148691ef3e7SSam Ravnborg# ld-option 149691ef3e7SSam Ravnborg# Usage: LDFLAGS += $(call ld-option, -X) 150691ef3e7SSam Ravnborgld-option = $(call try-run,\ 151691ef3e7SSam Ravnborg $(CC) /dev/null -c -o "$$TMPO" ; $(LD) $(1) "$$TMPO" -o "$$TMP",$(1),$(2)) 152691ef3e7SSam Ravnborg 15340df759eSMichal Marek# ar-option 15440df759eSMichal Marek# Usage: KBUILD_ARFLAGS := $(call ar-option,D) 15540df759eSMichal Marek# Important: no spaces around options 15640df759eSMichal Marekar-option = $(call try-run, $(AR) rc$(1) "$$TMP",$(1),$(2)) 15740df759eSMichal Marek 1585de043f4SOleg Verych###### 1595de043f4SOleg Verych 160beda9f3aSRoman Zippel### 1618ec4b4ffSSam Ravnborg# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.build obj= 1628ec4b4ffSSam Ravnborg# Usage: 1638ec4b4ffSSam Ravnborg# $(Q)$(MAKE) $(build)=dir 1648ec4b4ffSSam Ravnborgbuild := -f $(if $(KBUILD_SRC),$(srctree)/)scripts/Makefile.build obj 1658ec4b4ffSSam Ravnborg 166bc081dd6SMichal Marek### 167bc081dd6SMichal Marek# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.modbuiltin obj= 168bc081dd6SMichal Marek# Usage: 169bc081dd6SMichal Marek# $(Q)$(MAKE) $(modbuiltin)=dir 170bc081dd6SMichal Marekmodbuiltin := -f $(if $(KBUILD_SRC),$(srctree)/)scripts/Makefile.modbuiltin obj 171bc081dd6SMichal Marek 172beda9f3aSRoman Zippel# Prefix -I with $(srctree) if it is not an absolute path. 1735b91c33cSSam Ravnborg# skip if -I has no parameter 1745b91c33cSSam Ravnborgaddtree = $(if $(patsubst -I%,%,$(1)), \ 1755b91c33cSSam Ravnborg$(if $(filter-out -I/%,$(1)),$(patsubst -I%,-I$(srctree)/%,$(1))) $(1)) 176d9df92e2SSam Ravnborg 1775de043f4SOleg Verych# Find all -I options and call addtree 178beda9f3aSRoman Zippelflags = $(foreach o,$($(1)),$(if $(filter -I%,$(o)),$(call addtree,$(o)),$(o))) 1795de043f4SOleg Verych 1805de043f4SOleg Verych# echo command. 1815de043f4SOleg Verych# Short version is used, if $(quiet) equals `quiet_', otherwise full one. 1825de043f4SOleg Verychecho-cmd = $(if $($(quiet)cmd_$(1)),\ 1835de043f4SOleg Verych echo ' $(call escsq,$($(quiet)cmd_$(1)))$(echo-why)';) 1845de043f4SOleg Verych 1855de043f4SOleg Verych# printing commands 1866176aa9aSJan Beulichcmd = @$(echo-cmd) $(cmd_$(1)) 1878ec4b4ffSSam Ravnborg 1885de043f4SOleg Verych# Add $(obj)/ for paths that are not absolute 1890a504f25SSam Ravnborgobjectify = $(foreach o,$(1),$(if $(filter /%,$(o)),$(o),$(obj)/$(o))) 1900a504f25SSam Ravnborg 1918ec4b4ffSSam Ravnborg### 1928ec4b4ffSSam Ravnborg# if_changed - execute command if any prerequisite is newer than 1938ec4b4ffSSam Ravnborg# target, or command line has changed 1948ec4b4ffSSam Ravnborg# if_changed_dep - as if_changed, but uses fixdep to reveal dependencies 1958ec4b4ffSSam Ravnborg# including used config symbols 1968ec4b4ffSSam Ravnborg# if_changed_rule - as if_changed but execute rule instead 1978ec4b4ffSSam Ravnborg# See Documentation/kbuild/makefiles.txt for more info 1988ec4b4ffSSam Ravnborg 1998ec4b4ffSSam Ravnborgifneq ($(KBUILD_NOCMDDEP),1) 200beda9f3aSRoman Zippel# Check if both arguments has same arguments. Result is empty string if equal. 2018ec4b4ffSSam Ravnborg# User may override this check using make KBUILD_NOCMDDEP=1 20248f1f058SSam Ravnborgarg-check = $(strip $(filter-out $(cmd_$(1)), $(cmd_$@)) \ 20348f1f058SSam Ravnborg $(filter-out $(cmd_$@), $(cmd_$(1))) ) 204c4d5ee13SMichal Marekelse 205c4d5ee13SMichal Marekarg-check = $(if $(strip $(cmd_$@)),,1) 2068ec4b4ffSSam Ravnborgendif 2078ec4b4ffSSam Ravnborg 20848f1f058SSam Ravnborg# >'< substitution is for echo to work, 20948f1f058SSam Ravnborg# >$< substitution to preserve $ when reloading .cmd file 21048f1f058SSam Ravnborg# note: when using inline perl scripts [perl -e '...$$t=1;...'] 21148f1f058SSam Ravnborg# in $(cmd_xxx) double $$ your perl vars 2126176aa9aSJan Beulichmake-cmd = $(subst \#,\\\#,$(subst $$,$$$$,$(call escsq,$(cmd_$(1))))) 2136176aa9aSJan Beulich 21448f1f058SSam Ravnborg# Find any prerequisites that is newer than target or that does not exist. 21548f1f058SSam Ravnborg# PHONY targets skipped in both cases. 21648f1f058SSam Ravnborgany-prereq = $(filter-out $(PHONY),$?) $(filter-out $(PHONY) $(wildcard $^),$^) 21748f1f058SSam Ravnborg 2185de043f4SOleg Verych# Execute command if command has changed or prerequisite(s) are updated. 2198ec4b4ffSSam Ravnborg# 22048f1f058SSam Ravnborgif_changed = $(if $(strip $(any-prereq) $(arg-check)), \ 2218ec4b4ffSSam Ravnborg @set -e; \ 2226176aa9aSJan Beulich $(echo-cmd) $(cmd_$(1)); \ 22348f1f058SSam Ravnborg echo 'cmd_$@ := $(make-cmd)' > $(dot-target).cmd) 2248ec4b4ffSSam Ravnborg 2255de043f4SOleg Verych# Execute the command and also postprocess generated .d dependencies file. 22648f1f058SSam Ravnborgif_changed_dep = $(if $(strip $(any-prereq) $(arg-check) ), \ 2278ec4b4ffSSam Ravnborg @set -e; \ 2286176aa9aSJan Beulich $(echo-cmd) $(cmd_$(1)); \ 22948f1f058SSam Ravnborg scripts/basic/fixdep $(depfile) $@ '$(make-cmd)' > $(dot-target).tmp;\ 2308ec4b4ffSSam Ravnborg rm -f $(depfile); \ 23148f1f058SSam Ravnborg mv -f $(dot-target).tmp $(dot-target).cmd) 2328ec4b4ffSSam Ravnborg 2338ec4b4ffSSam Ravnborg# Usage: $(call if_changed_rule,foo) 234beda9f3aSRoman Zippel# Will check if $(cmd_foo) or any of the prerequisites changed, 235beda9f3aSRoman Zippel# and if so will execute $(rule_foo). 23648f1f058SSam Ravnborgif_changed_rule = $(if $(strip $(any-prereq) $(arg-check) ), \ 2378ec4b4ffSSam Ravnborg @set -e; \ 2388ec4b4ffSSam Ravnborg $(rule_$(1))) 23948f1f058SSam Ravnborg 24045d506bdSSam Ravnborg### 24145d506bdSSam Ravnborg# why - tell why a a target got build 24245d506bdSSam Ravnborg# enabled by make V=2 24345d506bdSSam Ravnborg# Output (listed in the order they are checked): 24445d506bdSSam Ravnborg# (1) - due to target is PHONY 24545d506bdSSam Ravnborg# (2) - due to target missing 24645d506bdSSam Ravnborg# (3) - due to: file1.h file2.h 24745d506bdSSam Ravnborg# (4) - due to command line change 24845d506bdSSam Ravnborg# (5) - due to missing .cmd file 24945d506bdSSam Ravnborg# (6) - due to target not in $(targets) 25045d506bdSSam Ravnborg# (1) PHONY targets are always build 25145d506bdSSam Ravnborg# (2) No target, so we better build it 25245d506bdSSam Ravnborg# (3) Prerequisite is newer than target 25345d506bdSSam Ravnborg# (4) The command line stored in the file named dir/.target.cmd 25445d506bdSSam Ravnborg# differed from actual command line. This happens when compiler 25545d506bdSSam Ravnborg# options changes 25645d506bdSSam Ravnborg# (5) No dir/.target.cmd file (used to store command line) 25745d506bdSSam Ravnborg# (6) No dir/.target.cmd file and target not listed in $(targets) 25845d506bdSSam Ravnborg# This is a good hint that there is a bug in the kbuild file 25945d506bdSSam Ravnborgifeq ($(KBUILD_VERBOSE),2) 26045d506bdSSam Ravnborgwhy = \ 26145d506bdSSam Ravnborg $(if $(filter $@, $(PHONY)),- due to target is PHONY, \ 26245d506bdSSam Ravnborg $(if $(wildcard $@), \ 26345d506bdSSam Ravnborg $(if $(strip $(any-prereq)),- due to: $(any-prereq), \ 26445d506bdSSam Ravnborg $(if $(arg-check), \ 26545d506bdSSam Ravnborg $(if $(cmd_$@),- due to command line change, \ 26645d506bdSSam Ravnborg $(if $(filter $@, $(targets)), \ 26745d506bdSSam Ravnborg - due to missing .cmd file, \ 26845d506bdSSam Ravnborg - due to $(notdir $@) not in $$(targets) \ 26945d506bdSSam Ravnborg ) \ 27045d506bdSSam Ravnborg ) \ 27145d506bdSSam Ravnborg ) \ 27245d506bdSSam Ravnborg ), \ 27345d506bdSSam Ravnborg - due to target missing \ 27445d506bdSSam Ravnborg ) \ 27545d506bdSSam Ravnborg ) 27645d506bdSSam Ravnborg 27745d506bdSSam Ravnborgecho-why = $(call escsq, $(strip $(why))) 27845d506bdSSam Ravnborgendif 279