1*4882a593Smuzhiyun#### 2*4882a593Smuzhiyun# kbuild: Generic definitions 3*4882a593Smuzhiyun 4*4882a593Smuzhiyun# Convenient variables 5*4882a593Smuzhiyuncomma := , 6*4882a593Smuzhiyunquote := " 7*4882a593Smuzhiyunsquote := ' 8*4882a593Smuzhiyunempty := 9*4882a593Smuzhiyunspace := $(empty) $(empty) 10*4882a593Smuzhiyunpound := \# 11*4882a593Smuzhiyun 12*4882a593Smuzhiyun### 13*4882a593Smuzhiyun# Name of target with a '.' as filename prefix. foo/bar.o => foo/.bar.o 14*4882a593Smuzhiyundot-target = $(dir $@).$(notdir $@) 15*4882a593Smuzhiyun 16*4882a593Smuzhiyun### 17*4882a593Smuzhiyun# The temporary file to save gcc -MD generated dependencies must not 18*4882a593Smuzhiyun# contain a comma 19*4882a593Smuzhiyundepfile = $(subst $(comma),_,$(dot-target).d) 20*4882a593Smuzhiyun 21*4882a593Smuzhiyun### 22*4882a593Smuzhiyun# filename of target with directory and extension stripped 23*4882a593Smuzhiyunbasetarget = $(basename $(notdir $@)) 24*4882a593Smuzhiyun 25*4882a593Smuzhiyun### 26*4882a593Smuzhiyun# filename of first prerequisite with directory and extension stripped 27*4882a593Smuzhiyunbaseprereq = $(basename $(notdir $<)) 28*4882a593Smuzhiyun 29*4882a593Smuzhiyun### 30*4882a593Smuzhiyun# Escape single quote for use in echo statements 31*4882a593Smuzhiyunescsq = $(subst $(squote),'\$(squote)',$1) 32*4882a593Smuzhiyun 33*4882a593Smuzhiyun### 34*4882a593Smuzhiyun# Easy method for doing a status message 35*4882a593Smuzhiyun kecho := : 36*4882a593Smuzhiyun quiet_kecho := echo 37*4882a593Smuzhiyunsilent_kecho := : 38*4882a593Smuzhiyunkecho := $($(quiet)kecho) 39*4882a593Smuzhiyun 40*4882a593Smuzhiyun### 41*4882a593Smuzhiyun# filechk is used to check if the content of a generated file is updated. 42*4882a593Smuzhiyun# Sample usage: 43*4882a593Smuzhiyun# define filechk_sample 44*4882a593Smuzhiyun# echo $KERNELRELEASE 45*4882a593Smuzhiyun# endef 46*4882a593Smuzhiyun# version.h : Makefile 47*4882a593Smuzhiyun# $(call filechk,sample) 48*4882a593Smuzhiyun# The rule defined shall write to stdout the content of the new file. 49*4882a593Smuzhiyun# The existing file will be compared with the new one. 50*4882a593Smuzhiyun# - If no file exist it is created 51*4882a593Smuzhiyun# - If the content differ the new file is used 52*4882a593Smuzhiyun# - If they are equal no change, and no timestamp update 53*4882a593Smuzhiyun# - stdin is piped in from the first prerequisite ($<) so one has 54*4882a593Smuzhiyun# to specify a valid file as first prerequisite (often the kbuild file) 55*4882a593Smuzhiyundefine filechk 56*4882a593Smuzhiyun $(Q)set -e; \ 57*4882a593Smuzhiyun $(kecho) ' CHK $@'; \ 58*4882a593Smuzhiyun mkdir -p $(dir $@); \ 59*4882a593Smuzhiyun $(filechk_$(1)) < $< > $@.tmp; \ 60*4882a593Smuzhiyun if [ -r $@ ] && cmp -s $@ $@.tmp; then \ 61*4882a593Smuzhiyun rm -f $@.tmp; \ 62*4882a593Smuzhiyun else \ 63*4882a593Smuzhiyun $(kecho) ' UPD $@'; \ 64*4882a593Smuzhiyun mv -f $@.tmp $@; \ 65*4882a593Smuzhiyun fi 66*4882a593Smuzhiyunendef 67*4882a593Smuzhiyun 68*4882a593Smuzhiyun###### 69*4882a593Smuzhiyun# gcc support functions 70*4882a593Smuzhiyun# See documentation in Documentation/kbuild/makefiles.txt 71*4882a593Smuzhiyun 72*4882a593Smuzhiyun# cc-cross-prefix 73*4882a593Smuzhiyun# Usage: CROSS_COMPILE := $(call cc-cross-prefix, m68k-linux-gnu- m68k-linux-) 74*4882a593Smuzhiyun# Return first prefix where a prefix$(CC) is found in PATH. 75*4882a593Smuzhiyun# If no $(CC) found in PATH with listed prefixes return nothing 76*4882a593Smuzhiyuncc-cross-prefix = \ 77*4882a593Smuzhiyun $(word 1, $(foreach c,$(1), \ 78*4882a593Smuzhiyun $(shell set -e; \ 79*4882a593Smuzhiyun if (which $(strip $(c))$(CC)) > /dev/null 2>&1 ; then \ 80*4882a593Smuzhiyun echo $(c); \ 81*4882a593Smuzhiyun fi))) 82*4882a593Smuzhiyun 83*4882a593Smuzhiyun# output directory for tests below 84*4882a593SmuzhiyunTMPOUT := $(if $(KBUILD_EXTMOD),$(firstword $(KBUILD_EXTMOD))/) 85*4882a593Smuzhiyun 86*4882a593Smuzhiyun# try-run 87*4882a593Smuzhiyun# Usage: option = $(call try-run, $(CC)...-o "$$TMP",option-ok,otherwise) 88*4882a593Smuzhiyun# Exit code chooses option. "$$TMP" is can be used as temporary file and 89*4882a593Smuzhiyun# is automatically cleaned up. 90*4882a593Smuzhiyun# modifed for U-Boot: prevent cc-option from leaving .*.su files 91*4882a593Smuzhiyuntry-run = $(shell set -e; \ 92*4882a593Smuzhiyun TMP="$(TMPOUT).$$$$.tmp"; \ 93*4882a593Smuzhiyun TMPO="$(TMPOUT).$$$$.o"; \ 94*4882a593Smuzhiyun TMPSU="$(TMPOUT).$$$$.su"; \ 95*4882a593Smuzhiyun if ($(1)) >/dev/null 2>&1; \ 96*4882a593Smuzhiyun then echo "$(2)"; \ 97*4882a593Smuzhiyun else echo "$(3)"; \ 98*4882a593Smuzhiyun fi; \ 99*4882a593Smuzhiyun rm -f "$$TMP" "$$TMPO" "$$TMPSU") 100*4882a593Smuzhiyun 101*4882a593Smuzhiyun# as-option 102*4882a593Smuzhiyun# Usage: cflags-y += $(call as-option,-Wa$(comma)-isa=foo,) 103*4882a593Smuzhiyun 104*4882a593Smuzhiyunas-option = $(call try-run,\ 105*4882a593Smuzhiyun $(CC) $(KBUILD_CFLAGS) $(1) -c -x assembler /dev/null -o "$$TMP",$(1),$(2)) 106*4882a593Smuzhiyun 107*4882a593Smuzhiyun# as-instr 108*4882a593Smuzhiyun# Usage: cflags-y += $(call as-instr,instr,option1,option2) 109*4882a593Smuzhiyun 110*4882a593Smuzhiyunas-instr = $(call try-run,\ 111*4882a593Smuzhiyun printf "%b\n" "$(1)" | $(CC) $(KBUILD_AFLAGS) -c -x assembler -o "$$TMP" -,$(2),$(3)) 112*4882a593Smuzhiyun 113*4882a593Smuzhiyun# cc-option 114*4882a593Smuzhiyun# Usage: cflags-y += $(call cc-option,-march=winchip-c6,-march=i586) 115*4882a593Smuzhiyun 116*4882a593Smuzhiyuncc-option = $(call try-run,\ 117*4882a593Smuzhiyun $(CC) -Werror $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) $(1) -c -x c /dev/null -o "$$TMP",$(1),$(2)) 118*4882a593Smuzhiyun 119*4882a593Smuzhiyun# cc-option-yn 120*4882a593Smuzhiyun# Usage: flag := $(call cc-option-yn,-march=winchip-c6) 121*4882a593Smuzhiyuncc-option-yn = $(call try-run,\ 122*4882a593Smuzhiyun $(CC) -Werror $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) $(1) -c -x c /dev/null -o "$$TMP",y,n) 123*4882a593Smuzhiyun 124*4882a593Smuzhiyun# cc-option-align 125*4882a593Smuzhiyun# Prefix align with either -falign or -malign 126*4882a593Smuzhiyuncc-option-align = $(subst -functions=0,,\ 127*4882a593Smuzhiyun $(call cc-option,-falign-functions=0,-malign-functions=0)) 128*4882a593Smuzhiyun 129*4882a593Smuzhiyun# cc-disable-warning 130*4882a593Smuzhiyun# Usage: cflags-y += $(call cc-disable-warning,unused-but-set-variable) 131*4882a593Smuzhiyuncc-disable-warning = $(call try-run,\ 132*4882a593Smuzhiyun $(CC) $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) -W$(strip $(1)) -c -x c /dev/null -o "$$TMP",-Wno-$(strip $(1))) 133*4882a593Smuzhiyun 134*4882a593Smuzhiyun# cc-name 135*4882a593Smuzhiyun# Expands to either gcc or clang 136*4882a593Smuzhiyuncc-name = $(shell $(CC) -v 2>&1 | grep -q "clang version" && echo clang || echo gcc) 137*4882a593Smuzhiyun 138*4882a593Smuzhiyun# cc-version 139*4882a593Smuzhiyuncc-version = $(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-version.sh $(CC)) 140*4882a593Smuzhiyun 141*4882a593Smuzhiyun# cc-fullversion 142*4882a593Smuzhiyuncc-fullversion = $(shell $(CONFIG_SHELL) \ 143*4882a593Smuzhiyun $(srctree)/scripts/gcc-version.sh -p $(CC)) 144*4882a593Smuzhiyun 145*4882a593Smuzhiyun# cc-ifversion 146*4882a593Smuzhiyun# Usage: EXTRA_CFLAGS += $(call cc-ifversion, -lt, 0402, -O1) 147*4882a593Smuzhiyuncc-ifversion = $(shell [ $(cc-version) $(1) $(2) ] && echo $(3) || echo $(4)) 148*4882a593Smuzhiyun 149*4882a593Smuzhiyun# added for U-Boot 150*4882a593Smuzhiyunbinutils-version = $(shell $(CONFIG_SHELL) $(srctree)/scripts/binutils-version.sh $(AS)) 151*4882a593Smuzhiyundtc-version = $(shell $(CONFIG_SHELL) $(srctree)/scripts/dtc-version.sh $(DTC)) 152*4882a593Smuzhiyun 153*4882a593Smuzhiyun# cc-ldoption 154*4882a593Smuzhiyun# Usage: ldflags += $(call cc-ldoption, -Wl$(comma)--hash-style=both) 155*4882a593Smuzhiyuncc-ldoption = $(call try-run,\ 156*4882a593Smuzhiyun $(CC) $(1) -nostdlib -x c /dev/null -o "$$TMP",$(1),$(2)) 157*4882a593Smuzhiyun 158*4882a593Smuzhiyun# ld-option 159*4882a593Smuzhiyun# Usage: LDFLAGS += $(call ld-option, -X) 160*4882a593Smuzhiyunld-option = $(call try-run,\ 161*4882a593Smuzhiyun $(CC) -x c /dev/null -c -o "$$TMPO" ; $(LD) $(1) "$$TMPO" -o "$$TMP",$(1),$(2)) 162*4882a593Smuzhiyun 163*4882a593Smuzhiyun# ar-option 164*4882a593Smuzhiyun# Usage: KBUILD_ARFLAGS := $(call ar-option,D) 165*4882a593Smuzhiyun# Important: no spaces around options 166*4882a593Smuzhiyunar-option = $(call try-run, $(AR) rc$(1) "$$TMP",$(1),$(2)) 167*4882a593Smuzhiyun 168*4882a593Smuzhiyun# ld-version 169*4882a593Smuzhiyun# Note this is mainly for HJ Lu's 3 number binutil versions 170*4882a593Smuzhiyunld-version = $(shell $(LD) --version | $(srctree)/scripts/ld-version.sh) 171*4882a593Smuzhiyun 172*4882a593Smuzhiyun# ld-ifversion 173*4882a593Smuzhiyun# Usage: $(call ld-ifversion, -ge, 22252, y) 174*4882a593Smuzhiyunld-ifversion = $(shell [ $(ld-version) $(1) $(2) ] && echo $(3) || echo $(4)) 175*4882a593Smuzhiyun 176*4882a593Smuzhiyun###### 177*4882a593Smuzhiyun 178*4882a593Smuzhiyun### 179*4882a593Smuzhiyun# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.build obj= 180*4882a593Smuzhiyun# Usage: 181*4882a593Smuzhiyun# $(Q)$(MAKE) $(build)=dir 182*4882a593Smuzhiyunbuild := -f $(srctree)/scripts/Makefile.build obj 183*4882a593Smuzhiyun 184*4882a593Smuzhiyun### 185*4882a593Smuzhiyun# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.modbuiltin obj= 186*4882a593Smuzhiyun# Usage: 187*4882a593Smuzhiyun# $(Q)$(MAKE) $(modbuiltin)=dir 188*4882a593Smuzhiyunmodbuiltin := -f $(srctree)/scripts/Makefile.modbuiltin obj 189*4882a593Smuzhiyun 190*4882a593Smuzhiyun### 191*4882a593Smuzhiyun# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.dtbinst obj= 192*4882a593Smuzhiyun# Usage: 193*4882a593Smuzhiyun# $(Q)$(MAKE) $(dtbinst)=dir 194*4882a593Smuzhiyundtbinst := -f $(if $(KBUILD_SRC),$(srctree)/)scripts/Makefile.dtbinst obj 195*4882a593Smuzhiyun 196*4882a593Smuzhiyun### 197*4882a593Smuzhiyun# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.clean obj= 198*4882a593Smuzhiyun# Usage: 199*4882a593Smuzhiyun# $(Q)$(MAKE) $(clean)=dir 200*4882a593Smuzhiyunclean := -f $(srctree)/scripts/Makefile.clean obj 201*4882a593Smuzhiyun 202*4882a593Smuzhiyun### 203*4882a593Smuzhiyun# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.headersinst obj= 204*4882a593Smuzhiyun# Usage: 205*4882a593Smuzhiyun# $(Q)$(MAKE) $(hdr-inst)=dir 206*4882a593Smuzhiyunhdr-inst := -f $(srctree)/scripts/Makefile.headersinst obj 207*4882a593Smuzhiyun 208*4882a593Smuzhiyun# Prefix -I with $(srctree) if it is not an absolute path. 209*4882a593Smuzhiyun# skip if -I has no parameter 210*4882a593Smuzhiyunaddtree = $(if $(patsubst -I%,%,$(1)), \ 211*4882a593Smuzhiyun$(if $(filter-out -I/%,$(1)),$(patsubst -I%,-I$(srctree)/%,$(1))) $(1)) 212*4882a593Smuzhiyun 213*4882a593Smuzhiyun# Find all -I options and call addtree 214*4882a593Smuzhiyunflags = $(foreach o,$($(1)),$(if $(filter -I%,$(o)),$(call addtree,$(o)),$(o))) 215*4882a593Smuzhiyun 216*4882a593Smuzhiyun# echo command. 217*4882a593Smuzhiyun# Short version is used, if $(quiet) equals `quiet_', otherwise full one. 218*4882a593Smuzhiyunecho-cmd = $(if $($(quiet)cmd_$(1)),\ 219*4882a593Smuzhiyun echo ' $(call escsq,$($(quiet)cmd_$(1)))$(echo-why)';) 220*4882a593Smuzhiyun 221*4882a593Smuzhiyun# printing commands 222*4882a593Smuzhiyuncmd = @$(echo-cmd) $(cmd_$(1)) 223*4882a593Smuzhiyun 224*4882a593Smuzhiyun# Add $(obj)/ for paths that are not absolute 225*4882a593Smuzhiyunobjectify = $(foreach o,$(1),$(if $(filter /%,$(o)),$(o),$(obj)/$(o))) 226*4882a593Smuzhiyun 227*4882a593Smuzhiyun### 228*4882a593Smuzhiyun# if_changed - execute command if any prerequisite is newer than 229*4882a593Smuzhiyun# target, or command line has changed 230*4882a593Smuzhiyun# if_changed_dep - as if_changed, but uses fixdep to reveal dependencies 231*4882a593Smuzhiyun# including used config symbols 232*4882a593Smuzhiyun# if_changed_rule - as if_changed but execute rule instead 233*4882a593Smuzhiyun# See Documentation/kbuild/makefiles.txt for more info 234*4882a593Smuzhiyun 235*4882a593Smuzhiyunifneq ($(KBUILD_NOCMDDEP),1) 236*4882a593Smuzhiyun# Check if both arguments has same arguments. Result is empty string if equal. 237*4882a593Smuzhiyun# User may override this check using make KBUILD_NOCMDDEP=1 238*4882a593Smuzhiyunarg-check = $(strip $(filter-out $(cmd_$(1)), $(cmd_$@)) \ 239*4882a593Smuzhiyun $(filter-out $(cmd_$@), $(cmd_$(1))) ) 240*4882a593Smuzhiyunelse 241*4882a593Smuzhiyunarg-check = $(if $(strip $(cmd_$@)),,1) 242*4882a593Smuzhiyunendif 243*4882a593Smuzhiyun 244*4882a593Smuzhiyun# Replace >$< with >$$< to preserve $ when reloading the .cmd file 245*4882a593Smuzhiyun# (needed for make) 246*4882a593Smuzhiyun# Replace >#< with >$(pound)< to avoid starting a comment in the .cmd file 247*4882a593Smuzhiyun# (needed for make) 248*4882a593Smuzhiyun# Replace >'< with >'\''< to be able to enclose the whole string in '...' 249*4882a593Smuzhiyun# (needed for the shell) 250*4882a593Smuzhiyunmake-cmd = $(call escsq,$(subst $(pound),$$(pound),$(subst $$,$$$$,$(cmd_$(1))))) 251*4882a593Smuzhiyun 252*4882a593Smuzhiyun# Find any prerequisites that is newer than target or that does not exist. 253*4882a593Smuzhiyun# PHONY targets skipped in both cases. 254*4882a593Smuzhiyunany-prereq = $(filter-out $(PHONY),$?) $(filter-out $(PHONY) $(wildcard $^),$^) 255*4882a593Smuzhiyun 256*4882a593Smuzhiyun# Execute command if command has changed or prerequisite(s) are updated. 257*4882a593Smuzhiyun# 258*4882a593Smuzhiyunif_changed = $(if $(strip $(any-prereq) $(arg-check)), \ 259*4882a593Smuzhiyun @set -e; \ 260*4882a593Smuzhiyun $(echo-cmd) $(cmd_$(1)); \ 261*4882a593Smuzhiyun printf '%s\n' 'cmd_$@ := $(make-cmd)' > $(dot-target).cmd) 262*4882a593Smuzhiyun 263*4882a593Smuzhiyun# Execute the command and also postprocess generated .d dependencies file. 264*4882a593Smuzhiyunif_changed_dep = $(if $(strip $(any-prereq) $(arg-check) ), \ 265*4882a593Smuzhiyun @set -e; \ 266*4882a593Smuzhiyun $(echo-cmd) $(cmd_$(1)); \ 267*4882a593Smuzhiyun scripts/basic/fixdep $(depfile) $@ '$(make-cmd)' > $(dot-target).tmp;\ 268*4882a593Smuzhiyun rm -f $(depfile); \ 269*4882a593Smuzhiyun mv -f $(dot-target).tmp $(dot-target).cmd) 270*4882a593Smuzhiyun 271*4882a593Smuzhiyun# Usage: $(call if_changed_rule,foo) 272*4882a593Smuzhiyun# Will check if $(cmd_foo) or any of the prerequisites changed, 273*4882a593Smuzhiyun# and if so will execute $(rule_foo). 274*4882a593Smuzhiyunif_changed_rule = $(if $(strip $(any-prereq) $(arg-check) ), \ 275*4882a593Smuzhiyun @set -e; \ 276*4882a593Smuzhiyun $(rule_$(1))) 277*4882a593Smuzhiyun 278*4882a593Smuzhiyun### 279*4882a593Smuzhiyun# why - tell why a a target got build 280*4882a593Smuzhiyun# enabled by make V=2 281*4882a593Smuzhiyun# Output (listed in the order they are checked): 282*4882a593Smuzhiyun# (1) - due to target is PHONY 283*4882a593Smuzhiyun# (2) - due to target missing 284*4882a593Smuzhiyun# (3) - due to: file1.h file2.h 285*4882a593Smuzhiyun# (4) - due to command line change 286*4882a593Smuzhiyun# (5) - due to missing .cmd file 287*4882a593Smuzhiyun# (6) - due to target not in $(targets) 288*4882a593Smuzhiyun# (1) PHONY targets are always build 289*4882a593Smuzhiyun# (2) No target, so we better build it 290*4882a593Smuzhiyun# (3) Prerequisite is newer than target 291*4882a593Smuzhiyun# (4) The command line stored in the file named dir/.target.cmd 292*4882a593Smuzhiyun# differed from actual command line. This happens when compiler 293*4882a593Smuzhiyun# options changes 294*4882a593Smuzhiyun# (5) No dir/.target.cmd file (used to store command line) 295*4882a593Smuzhiyun# (6) No dir/.target.cmd file and target not listed in $(targets) 296*4882a593Smuzhiyun# This is a good hint that there is a bug in the kbuild file 297*4882a593Smuzhiyunifeq ($(KBUILD_VERBOSE),2) 298*4882a593Smuzhiyunwhy = \ 299*4882a593Smuzhiyun $(if $(filter $@, $(PHONY)),- due to target is PHONY, \ 300*4882a593Smuzhiyun $(if $(wildcard $@), \ 301*4882a593Smuzhiyun $(if $(strip $(any-prereq)),- due to: $(any-prereq), \ 302*4882a593Smuzhiyun $(if $(arg-check), \ 303*4882a593Smuzhiyun $(if $(cmd_$@),- due to command line change, \ 304*4882a593Smuzhiyun $(if $(filter $@, $(targets)), \ 305*4882a593Smuzhiyun - due to missing .cmd file, \ 306*4882a593Smuzhiyun - due to $(notdir $@) not in $$(targets) \ 307*4882a593Smuzhiyun ) \ 308*4882a593Smuzhiyun ) \ 309*4882a593Smuzhiyun ) \ 310*4882a593Smuzhiyun ), \ 311*4882a593Smuzhiyun - due to target missing \ 312*4882a593Smuzhiyun ) \ 313*4882a593Smuzhiyun ) 314*4882a593Smuzhiyun 315*4882a593Smuzhiyunecho-why = $(call escsq, $(strip $(why))) 316*4882a593Smuzhiyunendif 317*4882a593Smuzhiyun 318*4882a593Smuzhiyunifdef CONFIG_SPL_BUILD 319*4882a593SmuzhiyunSPL_ := SPL_ 320*4882a593Smuzhiyunifeq ($(CONFIG_TPL_BUILD),y) 321*4882a593SmuzhiyunSPL_TPL_ := TPL_ 322*4882a593Smuzhiyunelse 323*4882a593SmuzhiyunSPL_TPL_ := SPL_ 324*4882a593Smuzhiyunendif 325*4882a593Smuzhiyunelse 326*4882a593SmuzhiyunSPL_ := 327*4882a593SmuzhiyunSPL_TPL_ := 328*4882a593Smuzhiyunendif 329