1#### 2# kbuild: Generic definitions 3 4# Convinient variables 5comma := , 6squote := ' 7quote := " 8empty := 9space := $(empty) $(empty) 10 11### 12# The temporary file to save gcc -MD generated dependencies must not 13# contain a comma 14depfile = $(subst $(comma),_,$(@D)/.$(@F).d) 15 16### 17# Escape single quote for use in echo statements 18escsq = $(subst $(squote),'\$(squote)',$1) 19 20### 21# filechk is used to check if the content of a generated file is updated. 22# Sample usage: 23# define filechk_sample 24# echo $KERNELRELEASE 25# endef 26# version.h : Makefile 27# $(call filechk,sample) 28# The rule defined shall write to stdout the content of the new file. 29# The existing file will be compared with the new one. 30# - If no file exist it is created 31# - If the content differ the new file is used 32# - If they are equal no change, and no timestamp update 33# - stdin is piped in from the first prerequisite ($<) so one has 34# to specify a valid file as first prerequisite (often the kbuild file) 35define filechk 36 $(Q)set -e; \ 37 echo ' CHK $@'; \ 38 mkdir -p $(dir $@); \ 39 $(filechk_$(1)) < $< > $@.tmp; \ 40 if [ -r $@ ] && cmp -s $@ $@.tmp; then \ 41 rm -f $@.tmp; \ 42 else \ 43 echo ' UPD $@'; \ 44 mv -f $@.tmp $@; \ 45 fi 46endef 47 48###### 49# gcc support functions 50# See documentation in Documentation/kbuild/makefiles.txt 51 52# as-option 53# Usage: cflags-y += $(call as-option, -Wa$(comma)-isa=foo,) 54 55as-option = $(shell if $(CC) $(CFLAGS) $(1) -Wa,-Z -c -o /dev/null \ 56 -xassembler /dev/null > /dev/null 2>&1; then echo "$(1)"; \ 57 else echo "$(2)"; fi ;) 58 59# cc-option 60# Usage: cflags-y += $(call cc-option, -march=winchip-c6, -march=i586) 61 62cc-option = $(shell if $(CC) $(CFLAGS) $(1) -S -o /dev/null -xc /dev/null \ 63 > /dev/null 2>&1; then echo "$(1)"; else echo "$(2)"; fi ;) 64 65# hostcc-option 66# Usage: hostcflags-y += $(call hostcc-option, -march=winchip-c6, -march=i586) 67 68hostcc-option = $(shell if $(HOSTCC) $(HOSTCFLAGS) $(1) -S -o /dev/null -xc /dev/null \ 69 > /dev/null 2>&1; then echo "$(1)"; else echo "$(2)"; fi ;) 70 71# cc-option-yn 72# Usage: flag := $(call cc-option-yn, -march=winchip-c6) 73cc-option-yn = $(shell if $(CC) $(CFLAGS) $(1) -S -o /dev/null -xc /dev/null \ 74 > /dev/null 2>&1; then echo "y"; else echo "n"; fi;) 75 76# cc-option-align 77# Prefix align with either -falign or -malign 78cc-option-align = $(subst -functions=0,,\ 79 $(call cc-option,-falign-functions=0,-malign-functions=0)) 80 81# cc-version 82# Usage gcc-ver := $(call cc-version, $(CC)) 83cc-version = $(shell PATH="$(PATH)" $(CONFIG_SHELL) $(srctree)/scripts/gcc-version.sh \ 84 $(if $(1), $(1), $(CC))) 85 86# cc-ifversion 87# Usage: EXTRA_CFLAGS += $(call cc-ifversion, -lt, 0402, -O1) 88cc-ifversion = $(shell if [ $(call cc-version, $(CC)) $(1) $(2) ]; then \ 89 echo $(3); fi;) 90 91### 92# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.build obj= 93# Usage: 94# $(Q)$(MAKE) $(build)=dir 95build := -f $(if $(KBUILD_SRC),$(srctree)/)scripts/Makefile.build obj 96 97# Prefix -I with $(srctree) if it is not an absolute path 98addtree = $(if $(filter-out -I/%,$(1)),$(patsubst -I%,-I$(srctree)/%,$(1))) $(1) 99# Find all -I options and call addtree 100flags = $(foreach o,$($(1)),$(if $(filter -I%,$(o)),$(call addtree,$(o)),$(o))) 101 102# If quiet is set, only print short version of command 103cmd = @$(echo-cmd) $(cmd_$(1)) 104 105# Add $(obj)/ for paths that is not absolute 106objectify = $(foreach o,$(1),$(if $(filter /%,$(o)),$(o),$(obj)/$(o))) 107 108### 109# if_changed - execute command if any prerequisite is newer than 110# target, or command line has changed 111# if_changed_dep - as if_changed, but uses fixdep to reveal dependencies 112# including used config symbols 113# if_changed_rule - as if_changed but execute rule instead 114# See Documentation/kbuild/makefiles.txt for more info 115 116ifneq ($(KBUILD_NOCMDDEP),1) 117# Check if both arguments has same arguments. Result in empty string if equal 118# User may override this check using make KBUILD_NOCMDDEP=1 119arg-check = $(strip $(filter-out $(1), $(2)) $(filter-out $(2), $(1)) ) 120endif 121 122# echo command. Short version is $(quiet) equals quiet, otherwise full command 123echo-cmd = $(if $($(quiet)cmd_$(1)), \ 124 echo ' $(call escsq,$($(quiet)cmd_$(1)))';) 125 126make-cmd = $(subst \#,\\\#,$(subst $$,$$$$,$(call escsq,$(cmd_$(1))))) 127 128# function to only execute the passed command if necessary 129# >'< substitution is for echo to work, >$< substitution to preserve $ when reloading .cmd file 130# note: when using inline perl scripts [perl -e '...$$t=1;...'] in $(cmd_xxx) double $$ your perl vars 131# 132if_changed = $(if $(strip $(filter-out $(PHONY),$?) \ 133 $(call arg-check, $(cmd_$(1)), $(cmd_$@)) ), \ 134 @set -e; \ 135 $(echo-cmd) $(cmd_$(1)); \ 136 echo 'cmd_$@ := $(make-cmd)' > $(@D)/.$(@F).cmd) 137 138# execute the command and also postprocess generated .d dependencies 139# file 140if_changed_dep = $(if $(strip $(filter-out $(PHONY),$?) \ 141 $(filter-out FORCE $(wildcard $^),$^) \ 142 $(call arg-check, $(cmd_$(1)), $(cmd_$@)) ), \ 143 @set -e; \ 144 $(echo-cmd) $(cmd_$(1)); \ 145 scripts/basic/fixdep $(depfile) $@ '$(make-cmd)' > $(@D)/.$(@F).tmp; \ 146 rm -f $(depfile); \ 147 mv -f $(@D)/.$(@F).tmp $(@D)/.$(@F).cmd) 148 149# Usage: $(call if_changed_rule,foo) 150# will check if $(cmd_foo) changed, or any of the prequisites changed, 151# and if so will execute $(rule_foo) 152if_changed_rule = $(if $(strip $(filter-out $(PHONY),$?) \ 153 $(call arg-check, $(cmd_$(1)), $(cmd_$@)) ),\ 154 @set -e; \ 155 $(rule_$(1))) 156