10d4a4b13SWarner Losh#### 20d4a4b13SWarner Losh# kbuild: Generic definitions 30d4a4b13SWarner Losh 40d4a4b13SWarner Losh# Convenient variables 50d4a4b13SWarner Loshcomma := , 60d4a4b13SWarner Loshsquote := ' 70d4a4b13SWarner Loshempty := 80d4a4b13SWarner Loshspace := $(empty) $(empty) 90d4a4b13SWarner Losh 100d4a4b13SWarner Losh### 110d4a4b13SWarner Losh# Name of target with a '.' as filename prefix. foo/bar.o => foo/.bar.o 120d4a4b13SWarner Loshdot-target = $(dir $@).$(notdir $@) 130d4a4b13SWarner Losh 140d4a4b13SWarner Losh### 150d4a4b13SWarner Losh# The temporary file to save gcc -MD generated dependencies must not 160d4a4b13SWarner Losh# contain a comma 170d4a4b13SWarner Loshdepfile = $(subst $(comma),_,$(dot-target).d) 180d4a4b13SWarner Losh 190d4a4b13SWarner Losh### 200d4a4b13SWarner Losh# filename of target with directory and extension stripped 210d4a4b13SWarner Loshbasetarget = $(basename $(notdir $@)) 220d4a4b13SWarner Losh 230d4a4b13SWarner Losh### 240d4a4b13SWarner Losh# filename of first prerequisite with directory and extension stripped 250d4a4b13SWarner Loshbaseprereq = $(basename $(notdir $<)) 260d4a4b13SWarner Losh 270d4a4b13SWarner Losh### 280d4a4b13SWarner Losh# Escape single quote for use in echo statements 290d4a4b13SWarner Loshescsq = $(subst $(squote),'\$(squote)',$1) 300d4a4b13SWarner Losh 310d4a4b13SWarner Losh### 320d4a4b13SWarner Losh# Easy method for doing a status message 330d4a4b13SWarner Losh kecho := : 340d4a4b13SWarner Losh quiet_kecho := echo 350d4a4b13SWarner Loshsilent_kecho := : 360d4a4b13SWarner Loshkecho := $($(quiet)kecho) 370d4a4b13SWarner Losh 380d4a4b13SWarner Losh### 390d4a4b13SWarner Losh# filechk is used to check if the content of a generated file is updated. 400d4a4b13SWarner Losh# Sample usage: 410d4a4b13SWarner Losh# define filechk_sample 420d4a4b13SWarner Losh# echo $KERNELRELEASE 430d4a4b13SWarner Losh# endef 440d4a4b13SWarner Losh# version.h : Makefile 450d4a4b13SWarner Losh# $(call filechk,sample) 460d4a4b13SWarner Losh# The rule defined shall write to stdout the content of the new file. 470d4a4b13SWarner Losh# The existing file will be compared with the new one. 480d4a4b13SWarner Losh# - If no file exist it is created 490d4a4b13SWarner Losh# - If the content differ the new file is used 500d4a4b13SWarner Losh# - If they are equal no change, and no timestamp update 510d4a4b13SWarner Losh# - stdin is piped in from the first prerequisite ($<) so one has 520d4a4b13SWarner Losh# to specify a valid file as first prerequisite (often the kbuild file) 530d4a4b13SWarner Loshdefine filechk 540d4a4b13SWarner Losh $(Q)set -e; \ 550d4a4b13SWarner Losh $(kecho) ' CHK $@'; \ 560d4a4b13SWarner Losh mkdir -p $(dir $@); \ 570d4a4b13SWarner Losh $(filechk_$(1)) < $< > $@.tmp; \ 580d4a4b13SWarner Losh if [ -r $@ ] && cmp -s $@ $@.tmp; then \ 590d4a4b13SWarner Losh rm -f $@.tmp; \ 600d4a4b13SWarner Losh else \ 610d4a4b13SWarner Losh $(kecho) ' UPD $@'; \ 620d4a4b13SWarner Losh mv -f $@.tmp $@; \ 630d4a4b13SWarner Losh fi 640d4a4b13SWarner Loshendef 650d4a4b13SWarner Losh 660d4a4b13SWarner Losh###### 670d4a4b13SWarner Losh# gcc support functions 680d4a4b13SWarner Losh# See documentation in Documentation/kbuild/makefiles.txt 690d4a4b13SWarner Losh 700d4a4b13SWarner Losh# cc-cross-prefix 710d4a4b13SWarner Losh# Usage: CROSS_COMPILE := $(call cc-cross-prefix, m68k-linux-gnu- m68k-linux-) 720d4a4b13SWarner Losh# Return first prefix where a prefix$(CC) is found in PATH. 730d4a4b13SWarner Losh# If no $(CC) found in PATH with listed prefixes return nothing 740d4a4b13SWarner Loshcc-cross-prefix = \ 750d4a4b13SWarner Losh $(word 1, $(foreach c,$(1), \ 760d4a4b13SWarner Losh $(shell set -e; \ 770d4a4b13SWarner Losh if (which $(strip $(c))$(CC)) > /dev/null 2>&1 ; then \ 780d4a4b13SWarner Losh echo $(c); \ 790d4a4b13SWarner Losh fi))) 800d4a4b13SWarner Losh 810d4a4b13SWarner Losh# output directory for tests below 820d4a4b13SWarner LoshTMPOUT := $(if $(KBUILD_EXTMOD),$(firstword $(KBUILD_EXTMOD))/) 830d4a4b13SWarner Losh 840d4a4b13SWarner Losh# try-run 850d4a4b13SWarner Losh# Usage: option = $(call try-run, $(CC)...-o "$$TMP",option-ok,otherwise) 860d4a4b13SWarner Losh# Exit code chooses option. "$$TMP" is can be used as temporary file and 870d4a4b13SWarner Losh# is automatically cleaned up. 880d4a4b13SWarner Loshtry-run = $(shell set -e; \ 890d4a4b13SWarner Losh TMP="$(TMPOUT).$$$$.tmp"; \ 900d4a4b13SWarner Losh TMPO="$(TMPOUT).$$$$.o"; \ 910d4a4b13SWarner Losh if ($(1)) >/dev/null 2>&1; \ 920d4a4b13SWarner Losh then echo "$(2)"; \ 930d4a4b13SWarner Losh else echo "$(3)"; \ 940d4a4b13SWarner Losh fi; \ 950d4a4b13SWarner Losh rm -f "$$TMP" "$$TMPO") 960d4a4b13SWarner Losh 970d4a4b13SWarner Losh# as-option 980d4a4b13SWarner Losh# Usage: cflags-y += $(call as-option,-Wa$(comma)-isa=foo,) 990d4a4b13SWarner Losh 1000d4a4b13SWarner Loshas-option = $(call try-run,\ 1010d4a4b13SWarner Losh $(CC) $(KBUILD_CFLAGS) $(1) -c -x assembler /dev/null -o "$$TMP",$(1),$(2)) 1020d4a4b13SWarner Losh 1030d4a4b13SWarner Losh# as-instr 1040d4a4b13SWarner Losh# Usage: cflags-y += $(call as-instr,instr,option1,option2) 1050d4a4b13SWarner Losh 1060d4a4b13SWarner Loshas-instr = $(call try-run,\ 1070d4a4b13SWarner Losh printf "%b\n" "$(1)" | $(CC) $(KBUILD_AFLAGS) -c -x assembler -o "$$TMP" -,$(2),$(3)) 1080d4a4b13SWarner Losh 1090d4a4b13SWarner Losh# cc-option 1100d4a4b13SWarner Losh# Usage: cflags-y += $(call cc-option,-march=winchip-c6,-march=i586) 1110d4a4b13SWarner Losh 1120d4a4b13SWarner Loshcc-option = $(call try-run,\ 1130d4a4b13SWarner Losh $(CC) $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) $(1) -c -x c /dev/null -o "$$TMP",$(1),$(2)) 1140d4a4b13SWarner Losh 1150d4a4b13SWarner Losh# cc-option-yn 1160d4a4b13SWarner Losh# Usage: flag := $(call cc-option-yn,-march=winchip-c6) 1170d4a4b13SWarner Loshcc-option-yn = $(call try-run,\ 1180d4a4b13SWarner Losh $(CC) $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) $(1) -c -x c /dev/null -o "$$TMP",y,n) 1190d4a4b13SWarner Losh 1200d4a4b13SWarner Losh# cc-option-align 1210d4a4b13SWarner Losh# Prefix align with either -falign or -malign 1220d4a4b13SWarner Loshcc-option-align = $(subst -functions=0,,\ 1230d4a4b13SWarner Losh $(call cc-option,-falign-functions=0,-malign-functions=0)) 1240d4a4b13SWarner Losh 1250d4a4b13SWarner Losh# cc-disable-warning 1260d4a4b13SWarner Losh# Usage: cflags-y += $(call cc-disable-warning,unused-but-set-variable) 1270d4a4b13SWarner Loshcc-disable-warning = $(call try-run,\ 1280d4a4b13SWarner Losh $(CC) $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) -W$(strip $(1)) -c -x c /dev/null -o "$$TMP",-Wno-$(strip $(1))) 1290d4a4b13SWarner Losh 1300d4a4b13SWarner Losh# cc-version 1310d4a4b13SWarner Losh# Usage gcc-ver := $(call cc-version) 1320d4a4b13SWarner Loshcc-version = $(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-version.sh $(CC)) 1330d4a4b13SWarner Losh 1340d4a4b13SWarner Losh# cc-fullversion 1350d4a4b13SWarner Losh# Usage gcc-ver := $(call cc-fullversion) 1360d4a4b13SWarner Loshcc-fullversion = $(shell $(CONFIG_SHELL) \ 1370d4a4b13SWarner Losh $(srctree)/scripts/gcc-version.sh -p $(CC)) 1380d4a4b13SWarner Losh 1390d4a4b13SWarner Losh# cc-ifversion 1400d4a4b13SWarner Losh# Usage: EXTRA_CFLAGS += $(call cc-ifversion, -lt, 0402, -O1) 1410d4a4b13SWarner Loshcc-ifversion = $(shell [ $(call cc-version, $(CC)) $(1) $(2) ] && echo $(3)) 1420d4a4b13SWarner Losh 1430d4a4b13SWarner Losh# cc-ldoption 1440d4a4b13SWarner Losh# Usage: ldflags += $(call cc-ldoption, -Wl$(comma)--hash-style=both) 1450d4a4b13SWarner Loshcc-ldoption = $(call try-run,\ 1460d4a4b13SWarner Losh $(CC) $(1) -nostdlib -x c /dev/null -o "$$TMP",$(1),$(2)) 1470d4a4b13SWarner Losh 1480d4a4b13SWarner Losh# ld-option 1490d4a4b13SWarner Losh# Usage: LDFLAGS += $(call ld-option, -X) 1500d4a4b13SWarner Loshld-option = $(call try-run,\ 1510d4a4b13SWarner Losh $(CC) /dev/null -c -o "$$TMPO" ; $(LD) $(1) "$$TMPO" -o "$$TMP",$(1),$(2)) 1520d4a4b13SWarner Losh 1530d4a4b13SWarner Losh# ar-option 1540d4a4b13SWarner Losh# Usage: KBUILD_ARFLAGS := $(call ar-option,D) 1550d4a4b13SWarner Losh# Important: no spaces around options 1560d4a4b13SWarner Loshar-option = $(call try-run, $(AR) rc$(1) "$$TMP",$(1),$(2)) 1570d4a4b13SWarner Losh 1580d4a4b13SWarner Losh###### 1590d4a4b13SWarner Losh 1600d4a4b13SWarner Losh### 1610d4a4b13SWarner Losh# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.build obj= 1620d4a4b13SWarner Losh# Usage: 1630d4a4b13SWarner Losh# $(Q)$(MAKE) $(build)=dir 1640d4a4b13SWarner Loshbuild := -f $(if $(KBUILD_SRC),$(srctree)/)scripts/Makefile.build obj 1650d4a4b13SWarner Losh 1660d4a4b13SWarner Losh### 1670d4a4b13SWarner Losh# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.modbuiltin obj= 1680d4a4b13SWarner Losh# Usage: 1690d4a4b13SWarner Losh# $(Q)$(MAKE) $(modbuiltin)=dir 1700d4a4b13SWarner Loshmodbuiltin := -f $(if $(KBUILD_SRC),$(srctree)/)scripts/Makefile.modbuiltin obj 1710d4a4b13SWarner Losh 1720d4a4b13SWarner Losh# Prefix -I with $(srctree) if it is not an absolute path. 1730d4a4b13SWarner Losh# skip if -I has no parameter 1740d4a4b13SWarner Loshaddtree = $(if $(patsubst -I%,%,$(1)), \ 1750d4a4b13SWarner Losh$(if $(filter-out -I/%,$(1)),$(patsubst -I%,-I$(srctree)/%,$(1))) $(1)) 1760d4a4b13SWarner Losh 1770d4a4b13SWarner Losh# Find all -I options and call addtree 1780d4a4b13SWarner Loshflags = $(foreach o,$($(1)),$(if $(filter -I%,$(o)),$(call addtree,$(o)),$(o))) 1790d4a4b13SWarner Losh 1800d4a4b13SWarner Losh# echo command. 1810d4a4b13SWarner Losh# Short version is used, if $(quiet) equals `quiet_', otherwise full one. 1820d4a4b13SWarner Loshecho-cmd = $(if $($(quiet)cmd_$(1)),\ 1830d4a4b13SWarner Losh echo ' $(call escsq,$($(quiet)cmd_$(1)))$(echo-why)';) 1840d4a4b13SWarner Losh 1850d4a4b13SWarner Losh# printing commands 1860d4a4b13SWarner Loshcmd = @$(echo-cmd) $(cmd_$(1)) 1870d4a4b13SWarner Losh 1880d4a4b13SWarner Losh# Add $(obj)/ for paths that are not absolute 1890d4a4b13SWarner Loshobjectify = $(foreach o,$(1),$(if $(filter /%,$(o)),$(o),$(obj)/$(o))) 1900d4a4b13SWarner Losh 1910d4a4b13SWarner Losh### 1920d4a4b13SWarner Losh# if_changed - execute command if any prerequisite is newer than 1930d4a4b13SWarner Losh# target, or command line has changed 1940d4a4b13SWarner Losh# if_changed_dep - as if_changed, but uses fixdep to reveal dependencies 1950d4a4b13SWarner Losh# including used config symbols 1960d4a4b13SWarner Losh# if_changed_rule - as if_changed but execute rule instead 1970d4a4b13SWarner Losh# See Documentation/kbuild/makefiles.txt for more info 1980d4a4b13SWarner Losh 1990d4a4b13SWarner Loshifneq ($(KBUILD_NOCMDDEP),1) 2000d4a4b13SWarner Losh# Check if both arguments has same arguments. Result is empty string if equal. 2010d4a4b13SWarner Losh# User may override this check using make KBUILD_NOCMDDEP=1 2020d4a4b13SWarner Losharg-check = $(strip $(filter-out $(cmd_$(1)), $(cmd_$@)) \ 2030d4a4b13SWarner Losh $(filter-out $(cmd_$@), $(cmd_$(1))) ) 2040d4a4b13SWarner Loshelse 2050d4a4b13SWarner Losharg-check = $(if $(strip $(cmd_$@)),,1) 2060d4a4b13SWarner Loshendif 2070d4a4b13SWarner Losh 2080d4a4b13SWarner Losh# >'< substitution is for echo to work, 2090d4a4b13SWarner Losh# >$< substitution to preserve $ when reloading .cmd file 2100d4a4b13SWarner Losh# note: when using inline perl scripts [perl -e '...$$t=1;...'] 2110d4a4b13SWarner Losh# in $(cmd_xxx) double $$ your perl vars 2120d4a4b13SWarner Loshmake-cmd = $(subst \\,\\\\,$(subst \#,\\\#,$(subst $$,$$$$,$(call escsq,$(cmd_$(1)))))) 2130d4a4b13SWarner Losh 2140d4a4b13SWarner Losh# Find any prerequisites that is newer than target or that does not exist. 2150d4a4b13SWarner Losh# PHONY targets skipped in both cases. 2160d4a4b13SWarner Loshany-prereq = $(filter-out $(PHONY),$?) $(filter-out $(PHONY) $(wildcard $^),$^) 2170d4a4b13SWarner Losh 2180d4a4b13SWarner Losh# Execute command if command has changed or prerequisite(s) are updated. 2190d4a4b13SWarner Losh# 2200d4a4b13SWarner Loshif_changed = $(if $(strip $(any-prereq) $(arg-check)), \ 2210d4a4b13SWarner Losh @set -e; \ 2220d4a4b13SWarner Losh $(echo-cmd) $(cmd_$(1)); \ 2230d4a4b13SWarner Losh echo 'cmd_$@ := $(make-cmd)' > $(dot-target).cmd) 2240d4a4b13SWarner Losh 2250d4a4b13SWarner Losh# Execute the command and also postprocess generated .d dependencies file. 2260d4a4b13SWarner Loshif_changed_dep = $(if $(strip $(any-prereq) $(arg-check) ), \ 2270d4a4b13SWarner Losh @set -e; \ 2280d4a4b13SWarner Losh $(echo-cmd) $(cmd_$(1)); \ 2290d4a4b13SWarner Losh scripts/basic/fixdep $(depfile) $@ '$(make-cmd)' > $(dot-target).tmp;\ 2300d4a4b13SWarner Losh rm -f $(depfile); \ 2310d4a4b13SWarner Losh mv -f $(dot-target).tmp $(dot-target).cmd) 2320d4a4b13SWarner Losh 2330d4a4b13SWarner Losh# Usage: $(call if_changed_rule,foo) 2340d4a4b13SWarner Losh# Will check if $(cmd_foo) or any of the prerequisites changed, 2350d4a4b13SWarner Losh# and if so will execute $(rule_foo). 2360d4a4b13SWarner Loshif_changed_rule = $(if $(strip $(any-prereq) $(arg-check) ), \ 2370d4a4b13SWarner Losh @set -e; \ 2380d4a4b13SWarner Losh $(rule_$(1))) 2390d4a4b13SWarner Losh 2400d4a4b13SWarner Losh### 2410d4a4b13SWarner Losh# why - tell why a a target got build 2420d4a4b13SWarner Losh# enabled by make V=2 2430d4a4b13SWarner Losh# Output (listed in the order they are checked): 2440d4a4b13SWarner Losh# (1) - due to target is PHONY 2450d4a4b13SWarner Losh# (2) - due to target missing 2460d4a4b13SWarner Losh# (3) - due to: file1.h file2.h 2470d4a4b13SWarner Losh# (4) - due to command line change 2480d4a4b13SWarner Losh# (5) - due to missing .cmd file 2490d4a4b13SWarner Losh# (6) - due to target not in $(targets) 2500d4a4b13SWarner Losh# (1) PHONY targets are always build 2510d4a4b13SWarner Losh# (2) No target, so we better build it 2520d4a4b13SWarner Losh# (3) Prerequisite is newer than target 2530d4a4b13SWarner Losh# (4) The command line stored in the file named dir/.target.cmd 2540d4a4b13SWarner Losh# differed from actual command line. This happens when compiler 2550d4a4b13SWarner Losh# options changes 2560d4a4b13SWarner Losh# (5) No dir/.target.cmd file (used to store command line) 2570d4a4b13SWarner Losh# (6) No dir/.target.cmd file and target not listed in $(targets) 2580d4a4b13SWarner Losh# This is a good hint that there is a bug in the kbuild file 2590d4a4b13SWarner Loshifeq ($(KBUILD_VERBOSE),2) 2600d4a4b13SWarner Loshwhy = \ 2610d4a4b13SWarner Losh $(if $(filter $@, $(PHONY)),- due to target is PHONY, \ 2620d4a4b13SWarner Losh $(if $(wildcard $@), \ 2630d4a4b13SWarner Losh $(if $(strip $(any-prereq)),- due to: $(any-prereq), \ 2640d4a4b13SWarner Losh $(if $(arg-check), \ 2650d4a4b13SWarner Losh $(if $(cmd_$@),- due to command line change, \ 2660d4a4b13SWarner Losh $(if $(filter $@, $(targets)), \ 2670d4a4b13SWarner Losh - due to missing .cmd file, \ 2680d4a4b13SWarner Losh - due to $(notdir $@) not in $$(targets) \ 2690d4a4b13SWarner Losh ) \ 2700d4a4b13SWarner Losh ) \ 2710d4a4b13SWarner Losh ) \ 2720d4a4b13SWarner Losh ), \ 2730d4a4b13SWarner Losh - due to target missing \ 2740d4a4b13SWarner Losh ) \ 2750d4a4b13SWarner Losh ) 2760d4a4b13SWarner Losh 2770d4a4b13SWarner Loshecho-why = $(call escsq, $(strip $(why))) 2780d4a4b13SWarner Loshendif 279