1# Generate/check/update a .h file to reflect the values of Makefile 2# variables 3# 4# Example usage (by default, check-conf-h will consider all CFG_* 5# and _CFG_* variables): 6# 7# path/to/conf.h: FORCE 8# $(call check-conf-h) 9# 10# Or, to include only the variables with the given prefix(es): 11# 12# path/to/crypto_config.h: FORCE 13# $(call check-conf-h,CFG_CRYPTO_ CRYPTO_) 14define check-conf-h 15 $(q)set -e; \ 16 $(cmd-echo-silent) ' CHK $@'; \ 17 cnf="$(strip $(foreach var, \ 18 $(call cfg-vars-by-prefix,$1), \ 19 $(call cfg-make-define,$(var))))"; \ 20 guard="_`echo $@ | tr -- -/. ___`_"; \ 21 mkdir -p $(dir $@); \ 22 echo "#ifndef $${guard}" >$@.tmp; \ 23 echo "#define $${guard}" >>$@.tmp; \ 24 echo -n "$${cnf}" | sed 's/_nl_ */\n/g' >>$@.tmp; \ 25 echo "#endif" >>$@.tmp; \ 26 $(call mv-if-changed,$@.tmp,$@) 27endef 28 29define check-conf-mk 30 $(q)set -e; \ 31 $(cmd-echo-silent) ' CHK $@'; \ 32 cnf="$(strip $(foreach var, \ 33 $(call cfg-vars-by-prefix,CFG_), \ 34 $(strip $(var)=$($(var))_nl_)))"; \ 35 mkdir -p $(dir $@); \ 36 echo "# auto-generated TEE configuration file" >$@.tmp; \ 37 echo "# TEE version ${TEE_IMPL_VERSION}" >>$@.tmp; \ 38 echo "ARCH=${ARCH}" >>$@.tmp; \ 39 echo "PLATFORM=${PLATFORM}" >>$@.tmp; \ 40 echo "PLATFORM_FLAVOR=${PLATFORM_FLAVOR}" >>$@.tmp; \ 41 echo -n "$${cnf}" | sed 's/_nl_ */\n/g' >>$@.tmp; \ 42 $(call mv-if-changed,$@.tmp,$@) 43endef 44 45# Rename $1 to $2 only if file content differs. Otherwise just delete $1. 46define mv-if-changed 47 if [ -r $2 ] && cmp -s $2 $1; then \ 48 rm -f $1; \ 49 else \ 50 $(cmd-echo-silent) ' UPD $2'; \ 51 mv $1 $2; \ 52 fi 53endef 54 55define cfg-vars-by-prefix 56 $(strip $(if $(1),$(call _cfg-vars-by-prefix,$(1)), 57 $(call _cfg-vars-by-prefix,CFG_ _CFG_))) 58endef 59 60define _cfg-vars-by-prefix 61 $(sort $(foreach prefix,$(1),$(filter $(prefix)%,$(.VARIABLES)))) 62endef 63 64# Convert a makefile variable to a #define 65# <undefined>, n => <undefined> 66# y => 1 67# <other value> => <other value> 68define cfg-make-define 69 $(strip $(if $(filter y,$($1)), 70 #define $1 1 /* '$($1)' */_nl_, 71 $(if $(filter xn x,x$($1)), 72 /* $1 is not set ('$($1)') */_nl_, 73 #define $1 $($1) /* '$($1)' */_nl_))) 74endef 75 76# Returns 'y' if at least one variable is 'y', empty otherwise 77# Example: 78# FOO_OR_BAR := $(call cfg-one-enabled, FOO BAR) 79cfg-one-enabled = $(if $(filter y, $(foreach var,$(1),$($(var)))),y,) 80 81# Returns 'y' if all variables are 'y', empty otherwise 82# Example: 83# FOO_AND_BAR := $(call cfg-all-enabled, FOO BAR) 84cfg-all-enabled = \ 85 $(strip \ 86 $(if $(1), \ 87 $(if $(filter y,$($(firstword $(1)))), \ 88 $(call cfg-all-enabled,$(filter-out $(firstword $(1)),$(1))), \ 89 ), \ 90 y \ 91 ) \ 92 ) 93 94# Disable a configuration variable if some dependency is disabled 95# Example: 96# $(eval $(call cfg-depends-all,FOO,BAR BAZ)) 97# Will clear FOO if it is initially 'y' and BAR or BAZ are not 'y' 98cfg-depends-all = \ 99 $(strip \ 100 $(if $(filter y, $($(1))), \ 101 $(if $(call cfg-all-enabled,$(2)), \ 102 , \ 103 $(warning Warning: Disabling $(1) [requires $(strip $(2))]) \ 104 override $(1) := \ 105 ) \ 106 ) \ 107 ) 108 109# Disable a configuration variable if all dependencies are disabled 110# Example: 111# $(eval $(call cfg-depends-one,FOO,BAR BAZ)) 112# Will clear FOO if it is initially 'y' and both BAR and BAZ are not 'y' 113cfg-depends-one = \ 114 $(strip \ 115 $(if $(filter y, $($(1))), \ 116 $(if $(call cfg-one-enabled,$(2)), \ 117 , \ 118 $(warning Warning: Disabling $(1) [requires (one of) $(strip $(2))]) \ 119 override $(1) := \ 120 ) \ 121 ) \ 122 ) 123 124 125# Enable all depend variables 126# Example: 127# $(eval $(call cfg-enable-all-depends,FOO,BAR BAZ)) 128# Will enable BAR and BAZ if FOO is initially 'y' 129cfg-enable-all-depends = \ 130 $(strip \ 131 $(if $(2), \ 132 $(if $(filter y, $($(1))), \ 133 $(if $(filter y,$($(firstword $(2)))), \ 134 , \ 135 $(warning Warning: Enabling $(firstword $(2)) [required by $(1)]) \ 136 $(eval override $(firstword $(2)) := y) \ 137 ) \ 138 $(call cfg-enable-all-depends,$(1),$(filter-out $(firstword $(2)),$(2))), \ 139 ) \ 140 , \ 141 ) \ 142 ) 143 144# Set a variable or error out if it was previously set to a different value 145# The reason message (3rd parameter) is optional 146# Example: 147# $(call force,CFG_FOO,foo,required by CFG_BAR) 148define force 149$(eval $(call _force,$(1),$(2),$(3))) 150endef 151 152define _force 153ifdef $(1) 154ifneq ($($(1)),$(2)) 155ifneq (,$(3)) 156_reason := $$(_empty) [$(3)] 157endif 158$$(error $(1) is set to '$($(1))' (from $(origin $(1))) but its value must be '$(2)'$$(_reason)) 159endif 160endif 161$(1) := $(2) 162endef 163