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