18ec4b4ffSSam Ravnborg#### 28ec4b4ffSSam Ravnborg# kbuild: Generic definitions 38ec4b4ffSSam Ravnborg 4*5de043f4SOleg Verych# Convenient constants 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### 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 59*5de043f4SOleg Verych# checker-shell 60*5de043f4SOleg Verych# Usage: option = $(call checker-shell, $(CC)...-o $$OUT, option-ok, otherwise) 61*5de043f4SOleg Verych# Exit code chooses option. $$OUT is safe location for needless output. 62*5de043f4SOleg Verychdefine checker-shell 63*5de043f4SOleg Verych $(shell set -e; \ 64*5de043f4SOleg Verych DIR=$(KBUILD_EXTMOD); \ 65*5de043f4SOleg Verych cd $${DIR:-$(objtree)}; \ 66*5de043f4SOleg Verych OUT=$$PWD/.$$$$.null; \ 67*5de043f4SOleg Verych \ 68*5de043f4SOleg Verych ln -s /dev/null $$OUT; \ 69*5de043f4SOleg Verych if $(1) >/dev/null 2>&1; \ 70*5de043f4SOleg Verych then echo "$(2)"; \ 71*5de043f4SOleg Verych else echo "$(3)"; \ 72*5de043f4SOleg Verych fi; \ 73*5de043f4SOleg Verych rm -f $$OUT) 74*5de043f4SOleg Verychendef 75347a00fbSRoman Zippel 7620a468b5SSam Ravnborg# as-option 7720a468b5SSam Ravnborg# Usage: cflags-y += $(call as-option, -Wa$(comma)-isa=foo,) 78*5de043f4SOleg Verychas-option = $(call checker-shell, \ 79*5de043f4SOleg Verych $(CC) $(CFLAGS) $(1) -c -xassembler /dev/null -o $$OUT, $(1), $(2)) 8020a468b5SSam Ravnborg 81e2414910SAndi Kleen# as-instr 82e2414910SAndi Kleen# Usage: cflags-y += $(call as-instr, instr, option1, option2) 83*5de043f4SOleg Verychas-instr = $(call checker-shell, \ 84*5de043f4SOleg Verych printf "$(1)" | $(CC) $(AFLAGS) -c -xassembler -o $$OUT -, $(2), $(3)) 85e2414910SAndi Kleen 8620a468b5SSam Ravnborg# cc-option 8720a468b5SSam Ravnborg# Usage: cflags-y += $(call cc-option, -march=winchip-c6, -march=i586) 88*5de043f4SOleg Verychcc-option = $(call checker-shell, \ 89*5de043f4SOleg Verych $(CC) $(CFLAGS) $(if $(3),$(3),$(1)) -S -xc /dev/null -o $$OUT, $(1), $(2)) 9020a468b5SSam Ravnborg 9120a468b5SSam Ravnborg# cc-option-yn 9220a468b5SSam Ravnborg# Usage: flag := $(call cc-option-yn, -march=winchip-c6) 93*5de043f4SOleg Verychcc-option-yn = $(call cc-option, "y", "n", $(1)) 9420a468b5SSam Ravnborg 9520a468b5SSam Ravnborg# cc-option-align 9620a468b5SSam Ravnborg# Prefix align with either -falign or -malign 9720a468b5SSam Ravnborgcc-option-align = $(subst -functions=0,,\ 9820a468b5SSam Ravnborg $(call cc-option,-falign-functions=0,-malign-functions=0)) 9920a468b5SSam Ravnborg 10020a468b5SSam Ravnborg# cc-version 10120a468b5SSam Ravnborg# Usage gcc-ver := $(call cc-version, $(CC)) 1028eb3afe0SSam Ravnborgcc-version = $(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-version.sh $(CC)) 10320a468b5SSam Ravnborg 10420a468b5SSam Ravnborg# cc-ifversion 10520a468b5SSam Ravnborg# Usage: EXTRA_CFLAGS += $(call cc-ifversion, -lt, 0402, -O1) 106*5de043f4SOleg Verychcc-ifversion = $(shell [ $(call cc-version, $(CC)) $(1) $(2) ] && echo $(3)) 10720a468b5SSam Ravnborg 1080b0bf7a3SRoland McGrath# ld-option 1090b0bf7a3SRoland McGrath# Usage: ldflags += $(call ld-option, -Wl$(comma)--hash-style=both) 110*5de043f4SOleg Verychld-option = $(call checker-shell, \ 111*5de043f4SOleg Verych $(CC) $(1) -nostdlib -xc /dev/null -o $$OUT, $(1), $(2)) 1120b0bf7a3SRoland McGrath 113*5de043f4SOleg Verych###### 114*5de043f4SOleg Verych 1158ec4b4ffSSam Ravnborg# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.build obj= 1168ec4b4ffSSam Ravnborg# Usage: 1178ec4b4ffSSam Ravnborg# $(Q)$(MAKE) $(build)=dir 1188ec4b4ffSSam Ravnborgbuild := -f $(if $(KBUILD_SRC),$(srctree)/)scripts/Makefile.build obj 1198ec4b4ffSSam Ravnborg 120*5de043f4SOleg Verych# Prefix -I with $(srctree) if it is not an absolute path, 121*5de043f4SOleg Verych# add original to the end 122*5de043f4SOleg Verychaddtree = $(if \ 123*5de043f4SOleg Verych $(filter-out -I/%, $(1)), $(patsubst -I%,-I$(srctree)/%,$(1))) $(1) 124d9df92e2SSam Ravnborg 125*5de043f4SOleg Verych# Find all -I options and call addtree 126*5de043f4SOleg Verychflags = $(foreach o,$($(1)), \ 127*5de043f4SOleg Verych $(if $(filter -I%,$(o)), $(call addtree, $(o)), $(o))) 128*5de043f4SOleg Verych 129*5de043f4SOleg Verych# echo command. 130*5de043f4SOleg Verych# Short version is used, if $(quiet) equals `quiet_', otherwise full one. 131*5de043f4SOleg Verychecho-cmd = $(if $($(quiet)cmd_$(1)), \ 132*5de043f4SOleg Verych echo ' $(call escsq,$($(quiet)cmd_$(1)))$(echo-why)';) 133*5de043f4SOleg Verych 134*5de043f4SOleg Verych# printing commands 1356176aa9aSJan Beulichcmd = @$(echo-cmd) $(cmd_$(1)) 1368ec4b4ffSSam Ravnborg 137*5de043f4SOleg Verych# Add $(obj)/ for paths that are not absolute 1380a504f25SSam Ravnborgobjectify = $(foreach o,$(1), $(if $(filter /%,$(o)), $(o), $(obj)/$(o))) 1390a504f25SSam Ravnborg 1408ec4b4ffSSam Ravnborg### 1418ec4b4ffSSam Ravnborg# if_changed - execute command if any prerequisite is newer than 1428ec4b4ffSSam Ravnborg# target, or command line has changed 1438ec4b4ffSSam Ravnborg# if_changed_dep - as if_changed, but uses fixdep to reveal dependencies 1448ec4b4ffSSam Ravnborg# including used config symbols 1458ec4b4ffSSam Ravnborg# if_changed_rule - as if_changed but execute rule instead 1468ec4b4ffSSam Ravnborg# See Documentation/kbuild/makefiles.txt for more info 1478ec4b4ffSSam Ravnborg 1488ec4b4ffSSam Ravnborgifneq ($(KBUILD_NOCMDDEP),1) 149*5de043f4SOleg Verych# Check if both arguments has same arguments. Result is empty string, if equal. 1508ec4b4ffSSam Ravnborg# User may override this check using make KBUILD_NOCMDDEP=1 15148f1f058SSam Ravnborgarg-check = $(strip $(filter-out $(cmd_$(1)), $(cmd_$@)) \ 15248f1f058SSam Ravnborg $(filter-out $(cmd_$@), $(cmd_$(1))) ) 1538ec4b4ffSSam Ravnborgendif 1548ec4b4ffSSam Ravnborg 15548f1f058SSam Ravnborg# >'< substitution is for echo to work, 15648f1f058SSam Ravnborg# >$< substitution to preserve $ when reloading .cmd file 15748f1f058SSam Ravnborg# note: when using inline perl scripts [perl -e '...$$t=1;...'] 15848f1f058SSam Ravnborg# in $(cmd_xxx) double $$ your perl vars 1596176aa9aSJan Beulichmake-cmd = $(subst \#,\\\#,$(subst $$,$$$$,$(call escsq,$(cmd_$(1))))) 1606176aa9aSJan Beulich 16148f1f058SSam Ravnborg# Find any prerequisites that is newer than target or that does not exist. 16248f1f058SSam Ravnborg# PHONY targets skipped in both cases. 16348f1f058SSam Ravnborgany-prereq = $(filter-out $(PHONY),$?) $(filter-out $(PHONY) $(wildcard $^),$^) 16448f1f058SSam Ravnborg 165*5de043f4SOleg Verych# Execute command if command has changed or prerequisite(s) are updated. 1668ec4b4ffSSam Ravnborg# 16748f1f058SSam Ravnborgif_changed = $(if $(strip $(any-prereq) $(arg-check)), \ 1688ec4b4ffSSam Ravnborg @set -e; \ 1696176aa9aSJan Beulich $(echo-cmd) $(cmd_$(1)); \ 17048f1f058SSam Ravnborg echo 'cmd_$@ := $(make-cmd)' > $(dot-target).cmd) 1718ec4b4ffSSam Ravnborg 172*5de043f4SOleg Verych# Execute the command and also postprocess generated .d dependencies file. 173*5de043f4SOleg Verych# 17448f1f058SSam Ravnborgif_changed_dep = $(if $(strip $(any-prereq) $(arg-check) ), \ 1758ec4b4ffSSam Ravnborg @set -e; \ 1766176aa9aSJan Beulich $(echo-cmd) $(cmd_$(1)); \ 17748f1f058SSam Ravnborg scripts/basic/fixdep $(depfile) $@ '$(make-cmd)' > $(dot-target).tmp;\ 1788ec4b4ffSSam Ravnborg rm -f $(depfile); \ 17948f1f058SSam Ravnborg mv -f $(dot-target).tmp $(dot-target).cmd) 1808ec4b4ffSSam Ravnborg 181*5de043f4SOleg Verych# Will check if $(cmd_foo) changed, or any of the prerequisites changed, 182*5de043f4SOleg Verych# and if so will execute $(rule_foo). 1838ec4b4ffSSam Ravnborg# Usage: $(call if_changed_rule,foo) 184*5de043f4SOleg Verych# 18548f1f058SSam Ravnborgif_changed_rule = $(if $(strip $(any-prereq) $(arg-check) ), \ 1868ec4b4ffSSam Ravnborg @set -e; \ 1878ec4b4ffSSam Ravnborg $(rule_$(1))) 18848f1f058SSam Ravnborg 18945d506bdSSam Ravnborg### 19045d506bdSSam Ravnborg# why - tell why a a target got build 19145d506bdSSam Ravnborg# enabled by make V=2 19245d506bdSSam Ravnborg# Output (listed in the order they are checked): 19345d506bdSSam Ravnborg# (1) - due to target is PHONY 19445d506bdSSam Ravnborg# (2) - due to target missing 19545d506bdSSam Ravnborg# (3) - due to: file1.h file2.h 19645d506bdSSam Ravnborg# (4) - due to command line change 19745d506bdSSam Ravnborg# (5) - due to missing .cmd file 19845d506bdSSam Ravnborg# (6) - due to target not in $(targets) 19945d506bdSSam Ravnborg# (1) PHONY targets are always build 20045d506bdSSam Ravnborg# (2) No target, so we better build it 20145d506bdSSam Ravnborg# (3) Prerequisite is newer than target 20245d506bdSSam Ravnborg# (4) The command line stored in the file named dir/.target.cmd 20345d506bdSSam Ravnborg# differed from actual command line. This happens when compiler 20445d506bdSSam Ravnborg# options changes 20545d506bdSSam Ravnborg# (5) No dir/.target.cmd file (used to store command line) 20645d506bdSSam Ravnborg# (6) No dir/.target.cmd file and target not listed in $(targets) 20745d506bdSSam Ravnborg# This is a good hint that there is a bug in the kbuild file 20845d506bdSSam Ravnborgifeq ($(KBUILD_VERBOSE),2) 20945d506bdSSam Ravnborgwhy = \ 21045d506bdSSam Ravnborg $(if $(filter $@, $(PHONY)),- due to target is PHONY, \ 21145d506bdSSam Ravnborg $(if $(wildcard $@), \ 21245d506bdSSam Ravnborg $(if $(strip $(any-prereq)),- due to: $(any-prereq), \ 21345d506bdSSam Ravnborg $(if $(arg-check), \ 21445d506bdSSam Ravnborg $(if $(cmd_$@),- due to command line change, \ 21545d506bdSSam Ravnborg $(if $(filter $@, $(targets)), \ 21645d506bdSSam Ravnborg - due to missing .cmd file, \ 21745d506bdSSam Ravnborg - due to $(notdir $@) not in $$(targets) \ 21845d506bdSSam Ravnborg ) \ 21945d506bdSSam Ravnborg ) \ 22045d506bdSSam Ravnborg ) \ 22145d506bdSSam Ravnborg ), \ 22245d506bdSSam Ravnborg - due to target missing \ 22345d506bdSSam Ravnborg ) \ 22445d506bdSSam Ravnborg ) 22545d506bdSSam Ravnborg 22645d506bdSSam Ravnborgecho-why = $(call escsq, $(strip $(why))) 22745d506bdSSam Ravnborgendif 228