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