1*22ce4affSfengbojiang#### 2*22ce4affSfengbojiang# kbuild: Generic definitions 3*22ce4affSfengbojiang 4*22ce4affSfengbojiang# Convenient variables 5*22ce4affSfengbojiangcomma := , 6*22ce4affSfengbojiangsquote := ' 7*22ce4affSfengbojiangempty := 8*22ce4affSfengbojiangspace := $(empty) $(empty) 9*22ce4affSfengbojiang 10*22ce4affSfengbojiang### 11*22ce4affSfengbojiang# Name of target with a '.' as filename prefix. foo/bar.o => foo/.bar.o 12*22ce4affSfengbojiangdot-target = $(dir $@).$(notdir $@) 13*22ce4affSfengbojiang 14*22ce4affSfengbojiang### 15*22ce4affSfengbojiang# The temporary file to save gcc -MD generated dependencies must not 16*22ce4affSfengbojiang# contain a comma 17*22ce4affSfengbojiangdepfile = $(subst $(comma),_,$(dot-target).d) 18*22ce4affSfengbojiang 19*22ce4affSfengbojiang### 20*22ce4affSfengbojiang# filename of target with directory and extension stripped 21*22ce4affSfengbojiangbasetarget = $(basename $(notdir $@)) 22*22ce4affSfengbojiang 23*22ce4affSfengbojiang### 24*22ce4affSfengbojiang# filename of first prerequisite with directory and extension stripped 25*22ce4affSfengbojiangbaseprereq = $(basename $(notdir $<)) 26*22ce4affSfengbojiang 27*22ce4affSfengbojiang### 28*22ce4affSfengbojiang# Escape single quote for use in echo statements 29*22ce4affSfengbojiangescsq = $(subst $(squote),'\$(squote)',$1) 30*22ce4affSfengbojiang 31*22ce4affSfengbojiang### 32*22ce4affSfengbojiang# Easy method for doing a status message 33*22ce4affSfengbojiang kecho := : 34*22ce4affSfengbojiang quiet_kecho := echo 35*22ce4affSfengbojiangsilent_kecho := : 36*22ce4affSfengbojiangkecho := $($(quiet)kecho) 37*22ce4affSfengbojiang 38*22ce4affSfengbojiang### 39*22ce4affSfengbojiang# filechk is used to check if the content of a generated file is updated. 40*22ce4affSfengbojiang# Sample usage: 41*22ce4affSfengbojiang# define filechk_sample 42*22ce4affSfengbojiang# echo $KERNELRELEASE 43*22ce4affSfengbojiang# endef 44*22ce4affSfengbojiang# version.h : Makefile 45*22ce4affSfengbojiang# $(call filechk,sample) 46*22ce4affSfengbojiang# The rule defined shall write to stdout the content of the new file. 47*22ce4affSfengbojiang# The existing file will be compared with the new one. 48*22ce4affSfengbojiang# - If no file exist it is created 49*22ce4affSfengbojiang# - If the content differ the new file is used 50*22ce4affSfengbojiang# - If they are equal no change, and no timestamp update 51*22ce4affSfengbojiang# - stdin is piped in from the first prerequisite ($<) so one has 52*22ce4affSfengbojiang# to specify a valid file as first prerequisite (often the kbuild file) 53*22ce4affSfengbojiangdefine filechk 54*22ce4affSfengbojiang $(Q)set -e; \ 55*22ce4affSfengbojiang $(kecho) ' CHK $@'; \ 56*22ce4affSfengbojiang mkdir -p $(dir $@); \ 57*22ce4affSfengbojiang $(filechk_$(1)) < $< > [email protected]; \ 58*22ce4affSfengbojiang if [ -r $@ ] && cmp -s $@ [email protected]; then \ 59*22ce4affSfengbojiang rm -f [email protected]; \ 60*22ce4affSfengbojiang else \ 61*22ce4affSfengbojiang $(kecho) ' UPD $@'; \ 62*22ce4affSfengbojiang mv -f [email protected] $@; \ 63*22ce4affSfengbojiang fi 64*22ce4affSfengbojiangendef 65*22ce4affSfengbojiang 66*22ce4affSfengbojiang###### 67*22ce4affSfengbojiang# gcc support functions 68*22ce4affSfengbojiang# See documentation in Documentation/kbuild/makefiles.txt 69*22ce4affSfengbojiang 70*22ce4affSfengbojiang# cc-cross-prefix 71*22ce4affSfengbojiang# Usage: CROSS_COMPILE := $(call cc-cross-prefix, m68k-linux-gnu- m68k-linux-) 72*22ce4affSfengbojiang# Return first prefix where a prefix$(CC) is found in PATH. 73*22ce4affSfengbojiang# If no $(CC) found in PATH with listed prefixes return nothing 74*22ce4affSfengbojiangcc-cross-prefix = \ 75*22ce4affSfengbojiang $(word 1, $(foreach c,$(1), \ 76*22ce4affSfengbojiang $(shell set -e; \ 77*22ce4affSfengbojiang if (which $(strip $(c))$(CC)) > /dev/null 2>&1 ; then \ 78*22ce4affSfengbojiang echo $(c); \ 79*22ce4affSfengbojiang fi))) 80*22ce4affSfengbojiang 81*22ce4affSfengbojiang# output directory for tests below 82*22ce4affSfengbojiangTMPOUT := $(if $(KBUILD_EXTMOD),$(firstword $(KBUILD_EXTMOD))/) 83*22ce4affSfengbojiang 84*22ce4affSfengbojiang# try-run 85*22ce4affSfengbojiang# Usage: option = $(call try-run, $(CC)...-o "$$TMP",option-ok,otherwise) 86*22ce4affSfengbojiang# Exit code chooses option. "$$TMP" is can be used as temporary file and 87*22ce4affSfengbojiang# is automatically cleaned up. 88*22ce4affSfengbojiangtry-run = $(shell set -e; \ 89*22ce4affSfengbojiang TMP="$(TMPOUT).$$$$.tmp"; \ 90*22ce4affSfengbojiang TMPO="$(TMPOUT).$$$$.o"; \ 91*22ce4affSfengbojiang if ($(1)) >/dev/null 2>&1; \ 92*22ce4affSfengbojiang then echo "$(2)"; \ 93*22ce4affSfengbojiang else echo "$(3)"; \ 94*22ce4affSfengbojiang fi; \ 95*22ce4affSfengbojiang rm -f "$$TMP" "$$TMPO") 96*22ce4affSfengbojiang 97*22ce4affSfengbojiang# as-option 98*22ce4affSfengbojiang# Usage: cflags-y += $(call as-option,-Wa$(comma)-isa=foo,) 99*22ce4affSfengbojiang 100*22ce4affSfengbojiangas-option = $(call try-run,\ 101*22ce4affSfengbojiang $(CC) $(KBUILD_CFLAGS) $(1) -c -x assembler /dev/null -o "$$TMP",$(1),$(2)) 102*22ce4affSfengbojiang 103*22ce4affSfengbojiang# as-instr 104*22ce4affSfengbojiang# Usage: cflags-y += $(call as-instr,instr,option1,option2) 105*22ce4affSfengbojiang 106*22ce4affSfengbojiangas-instr = $(call try-run,\ 107*22ce4affSfengbojiang printf "%b\n" "$(1)" | $(CC) $(KBUILD_AFLAGS) -c -x assembler -o "$$TMP" -,$(2),$(3)) 108*22ce4affSfengbojiang 109*22ce4affSfengbojiang# cc-option 110*22ce4affSfengbojiang# Usage: cflags-y += $(call cc-option,-march=winchip-c6,-march=i586) 111*22ce4affSfengbojiang 112*22ce4affSfengbojiangcc-option = $(call try-run,\ 113*22ce4affSfengbojiang $(CC) $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) $(1) -c -x c /dev/null -o "$$TMP",$(1),$(2)) 114*22ce4affSfengbojiang 115*22ce4affSfengbojiang# cc-option-yn 116*22ce4affSfengbojiang# Usage: flag := $(call cc-option-yn,-march=winchip-c6) 117*22ce4affSfengbojiangcc-option-yn = $(call try-run,\ 118*22ce4affSfengbojiang $(CC) $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) $(1) -c -x c /dev/null -o "$$TMP",y,n) 119*22ce4affSfengbojiang 120*22ce4affSfengbojiang# cc-option-align 121*22ce4affSfengbojiang# Prefix align with either -falign or -malign 122*22ce4affSfengbojiangcc-option-align = $(subst -functions=0,,\ 123*22ce4affSfengbojiang $(call cc-option,-falign-functions=0,-malign-functions=0)) 124*22ce4affSfengbojiang 125*22ce4affSfengbojiang# cc-disable-warning 126*22ce4affSfengbojiang# Usage: cflags-y += $(call cc-disable-warning,unused-but-set-variable) 127*22ce4affSfengbojiangcc-disable-warning = $(call try-run,\ 128*22ce4affSfengbojiang $(CC) $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) -W$(strip $(1)) -c -x c /dev/null -o "$$TMP",-Wno-$(strip $(1))) 129*22ce4affSfengbojiang 130*22ce4affSfengbojiang# cc-version 131*22ce4affSfengbojiang# Usage gcc-ver := $(call cc-version) 132*22ce4affSfengbojiangcc-version = $(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-version.sh $(CC)) 133*22ce4affSfengbojiang 134*22ce4affSfengbojiang# cc-fullversion 135*22ce4affSfengbojiang# Usage gcc-ver := $(call cc-fullversion) 136*22ce4affSfengbojiangcc-fullversion = $(shell $(CONFIG_SHELL) \ 137*22ce4affSfengbojiang $(srctree)/scripts/gcc-version.sh -p $(CC)) 138*22ce4affSfengbojiang 139*22ce4affSfengbojiang# cc-ifversion 140*22ce4affSfengbojiang# Usage: EXTRA_CFLAGS += $(call cc-ifversion, -lt, 0402, -O1) 141*22ce4affSfengbojiangcc-ifversion = $(shell [ $(call cc-version, $(CC)) $(1) $(2) ] && echo $(3)) 142*22ce4affSfengbojiang 143*22ce4affSfengbojiang# cc-ldoption 144*22ce4affSfengbojiang# Usage: ldflags += $(call cc-ldoption, -Wl$(comma)--hash-style=both) 145*22ce4affSfengbojiangcc-ldoption = $(call try-run,\ 146*22ce4affSfengbojiang $(CC) $(1) -nostdlib -x c /dev/null -o "$$TMP",$(1),$(2)) 147*22ce4affSfengbojiang 148*22ce4affSfengbojiang# ld-option 149*22ce4affSfengbojiang# Usage: LDFLAGS += $(call ld-option, -X) 150*22ce4affSfengbojiangld-option = $(call try-run,\ 151*22ce4affSfengbojiang $(CC) /dev/null -c -o "$$TMPO" ; $(LD) $(1) "$$TMPO" -o "$$TMP",$(1),$(2)) 152*22ce4affSfengbojiang 153*22ce4affSfengbojiang# ar-option 154*22ce4affSfengbojiang# Usage: KBUILD_ARFLAGS := $(call ar-option,D) 155*22ce4affSfengbojiang# Important: no spaces around options 156*22ce4affSfengbojiangar-option = $(call try-run, $(AR) rc$(1) "$$TMP",$(1),$(2)) 157*22ce4affSfengbojiang 158*22ce4affSfengbojiang###### 159*22ce4affSfengbojiang 160*22ce4affSfengbojiang### 161*22ce4affSfengbojiang# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.build obj= 162*22ce4affSfengbojiang# Usage: 163*22ce4affSfengbojiang# $(Q)$(MAKE) $(build)=dir 164*22ce4affSfengbojiangbuild := -f $(if $(KBUILD_SRC),$(srctree)/)scripts/Makefile.build obj 165*22ce4affSfengbojiang 166*22ce4affSfengbojiang### 167*22ce4affSfengbojiang# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.modbuiltin obj= 168*22ce4affSfengbojiang# Usage: 169*22ce4affSfengbojiang# $(Q)$(MAKE) $(modbuiltin)=dir 170*22ce4affSfengbojiangmodbuiltin := -f $(if $(KBUILD_SRC),$(srctree)/)scripts/Makefile.modbuiltin obj 171*22ce4affSfengbojiang 172*22ce4affSfengbojiang# Prefix -I with $(srctree) if it is not an absolute path. 173*22ce4affSfengbojiang# skip if -I has no parameter 174*22ce4affSfengbojiangaddtree = $(if $(patsubst -I%,%,$(1)), \ 175*22ce4affSfengbojiang$(if $(filter-out -I/%,$(1)),$(patsubst -I%,-I$(srctree)/%,$(1))) $(1)) 176*22ce4affSfengbojiang 177*22ce4affSfengbojiang# Find all -I options and call addtree 178*22ce4affSfengbojiangflags = $(foreach o,$($(1)),$(if $(filter -I%,$(o)),$(call addtree,$(o)),$(o))) 179*22ce4affSfengbojiang 180*22ce4affSfengbojiang# echo command. 181*22ce4affSfengbojiang# Short version is used, if $(quiet) equals `quiet_', otherwise full one. 182*22ce4affSfengbojiangecho-cmd = $(if $($(quiet)cmd_$(1)),\ 183*22ce4affSfengbojiang echo ' $(call escsq,$($(quiet)cmd_$(1)))$(echo-why)';) 184*22ce4affSfengbojiang 185*22ce4affSfengbojiang# printing commands 186*22ce4affSfengbojiangcmd = @$(echo-cmd) $(cmd_$(1)) 187*22ce4affSfengbojiang 188*22ce4affSfengbojiang# Add $(obj)/ for paths that are not absolute 189*22ce4affSfengbojiangobjectify = $(foreach o,$(1),$(if $(filter /%,$(o)),$(o),$(obj)/$(o))) 190*22ce4affSfengbojiang 191*22ce4affSfengbojiang### 192*22ce4affSfengbojiang# if_changed - execute command if any prerequisite is newer than 193*22ce4affSfengbojiang# target, or command line has changed 194*22ce4affSfengbojiang# if_changed_dep - as if_changed, but uses fixdep to reveal dependencies 195*22ce4affSfengbojiang# including used config symbols 196*22ce4affSfengbojiang# if_changed_rule - as if_changed but execute rule instead 197*22ce4affSfengbojiang# See Documentation/kbuild/makefiles.txt for more info 198*22ce4affSfengbojiang 199*22ce4affSfengbojiangifneq ($(KBUILD_NOCMDDEP),1) 200*22ce4affSfengbojiang# Check if both arguments has same arguments. Result is empty string if equal. 201*22ce4affSfengbojiang# User may override this check using make KBUILD_NOCMDDEP=1 202*22ce4affSfengbojiangarg-check = $(strip $(filter-out $(cmd_$(1)), $(cmd_$@)) \ 203*22ce4affSfengbojiang $(filter-out $(cmd_$@), $(cmd_$(1))) ) 204*22ce4affSfengbojiangelse 205*22ce4affSfengbojiangarg-check = $(if $(strip $(cmd_$@)),,1) 206*22ce4affSfengbojiangendif 207*22ce4affSfengbojiang 208*22ce4affSfengbojiang# >'< substitution is for echo to work, 209*22ce4affSfengbojiang# >$< substitution to preserve $ when reloading .cmd file 210*22ce4affSfengbojiang# note: when using inline perl scripts [perl -e '...$$t=1;...'] 211*22ce4affSfengbojiang# in $(cmd_xxx) double $$ your perl vars 212*22ce4affSfengbojiangmake-cmd = $(subst \\,\\\\,$(subst \#,\\\#,$(subst $$,$$$$,$(call escsq,$(cmd_$(1)))))) 213*22ce4affSfengbojiang 214*22ce4affSfengbojiang# Find any prerequisites that is newer than target or that does not exist. 215*22ce4affSfengbojiang# PHONY targets skipped in both cases. 216*22ce4affSfengbojiangany-prereq = $(filter-out $(PHONY),$?) $(filter-out $(PHONY) $(wildcard $^),$^) 217*22ce4affSfengbojiang 218*22ce4affSfengbojiang# Execute command if command has changed or prerequisite(s) are updated. 219*22ce4affSfengbojiang# 220*22ce4affSfengbojiangif_changed = $(if $(strip $(any-prereq) $(arg-check)), \ 221*22ce4affSfengbojiang @set -e; \ 222*22ce4affSfengbojiang $(echo-cmd) $(cmd_$(1)); \ 223*22ce4affSfengbojiang echo 'cmd_$@ := $(make-cmd)' > $(dot-target).cmd) 224*22ce4affSfengbojiang 225*22ce4affSfengbojiang# Execute the command and also postprocess generated .d dependencies file. 226*22ce4affSfengbojiangif_changed_dep = $(if $(strip $(any-prereq) $(arg-check) ), \ 227*22ce4affSfengbojiang @set -e; \ 228*22ce4affSfengbojiang $(echo-cmd) $(cmd_$(1)); \ 229*22ce4affSfengbojiang scripts/basic/fixdep $(depfile) $@ '$(make-cmd)' > $(dot-target).tmp;\ 230*22ce4affSfengbojiang rm -f $(depfile); \ 231*22ce4affSfengbojiang mv -f $(dot-target).tmp $(dot-target).cmd) 232*22ce4affSfengbojiang 233*22ce4affSfengbojiang# Usage: $(call if_changed_rule,foo) 234*22ce4affSfengbojiang# Will check if $(cmd_foo) or any of the prerequisites changed, 235*22ce4affSfengbojiang# and if so will execute $(rule_foo). 236*22ce4affSfengbojiangif_changed_rule = $(if $(strip $(any-prereq) $(arg-check) ), \ 237*22ce4affSfengbojiang @set -e; \ 238*22ce4affSfengbojiang $(rule_$(1))) 239*22ce4affSfengbojiang 240*22ce4affSfengbojiang### 241*22ce4affSfengbojiang# why - tell why a a target got build 242*22ce4affSfengbojiang# enabled by make V=2 243*22ce4affSfengbojiang# Output (listed in the order they are checked): 244*22ce4affSfengbojiang# (1) - due to target is PHONY 245*22ce4affSfengbojiang# (2) - due to target missing 246*22ce4affSfengbojiang# (3) - due to: file1.h file2.h 247*22ce4affSfengbojiang# (4) - due to command line change 248*22ce4affSfengbojiang# (5) - due to missing .cmd file 249*22ce4affSfengbojiang# (6) - due to target not in $(targets) 250*22ce4affSfengbojiang# (1) PHONY targets are always build 251*22ce4affSfengbojiang# (2) No target, so we better build it 252*22ce4affSfengbojiang# (3) Prerequisite is newer than target 253*22ce4affSfengbojiang# (4) The command line stored in the file named dir/.target.cmd 254*22ce4affSfengbojiang# differed from actual command line. This happens when compiler 255*22ce4affSfengbojiang# options changes 256*22ce4affSfengbojiang# (5) No dir/.target.cmd file (used to store command line) 257*22ce4affSfengbojiang# (6) No dir/.target.cmd file and target not listed in $(targets) 258*22ce4affSfengbojiang# This is a good hint that there is a bug in the kbuild file 259*22ce4affSfengbojiangifeq ($(KBUILD_VERBOSE),2) 260*22ce4affSfengbojiangwhy = \ 261*22ce4affSfengbojiang $(if $(filter $@, $(PHONY)),- due to target is PHONY, \ 262*22ce4affSfengbojiang $(if $(wildcard $@), \ 263*22ce4affSfengbojiang $(if $(strip $(any-prereq)),- due to: $(any-prereq), \ 264*22ce4affSfengbojiang $(if $(arg-check), \ 265*22ce4affSfengbojiang $(if $(cmd_$@),- due to command line change, \ 266*22ce4affSfengbojiang $(if $(filter $@, $(targets)), \ 267*22ce4affSfengbojiang - due to missing .cmd file, \ 268*22ce4affSfengbojiang - due to $(notdir $@) not in $$(targets) \ 269*22ce4affSfengbojiang ) \ 270*22ce4affSfengbojiang ) \ 271*22ce4affSfengbojiang ) \ 272*22ce4affSfengbojiang ), \ 273*22ce4affSfengbojiang - due to target missing \ 274*22ce4affSfengbojiang ) \ 275*22ce4affSfengbojiang ) 276*22ce4affSfengbojiang 277*22ce4affSfengbojiangecho-why = $(call escsq, $(strip $(why))) 278*22ce4affSfengbojiangendif 279