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 plus PLATFORM_*): 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-cmake 30 $(q)set -e; \ 31 $(cmd-echo-silent) ' CHK $@'; \ 32 cnf='$(strip $(foreach var, \ 33 $(call cfg-vars-by-prefix,$1), \ 34 $(call cfg-cmake-set,$(var))))'; \ 35 mkdir -p $(dir $@); \ 36 echo "# auto-generated TEE configuration file" >$@.tmp; \ 37 echo "# TEE version ${TEE_IMPL_VERSION}" >>$@.tmp; \ 38 echo -n "$${cnf}" | sed 's/_nl_ */\n/g' >>$@.tmp; \ 39 $(call mv-if-changed,$@.tmp,$@) 40endef 41 42define check-conf-mk 43 $(q)set -e; \ 44 $(cmd-echo-silent) ' CHK $@'; \ 45 cnf='$(strip $(foreach var, \ 46 $(call cfg-vars-by-prefix,CFG_), \ 47 $(strip $(var)=$($(var))_nl_)))'; \ 48 mkdir -p $(dir $@); \ 49 echo "# auto-generated TEE configuration file" >$@.tmp; \ 50 echo "# TEE version ${TEE_IMPL_VERSION}" >>$@.tmp; \ 51 echo "ARCH=${ARCH}" >>$@.tmp; \ 52 echo "PLATFORM=${PLATFORM}" >>$@.tmp; \ 53 echo "PLATFORM_FLAVOR=${PLATFORM_FLAVOR}" >>$@.tmp; \ 54 echo -n "$${cnf}" | sed 's/_nl_ */\n/g' >>$@.tmp; \ 55 $(call mv-if-changed,$@.tmp,$@) 56endef 57 58define cfg-vars-by-prefix 59 $(strip $(if $(1),$(call _cfg-vars-by-prefix,$(1)), 60 $(call _cfg-vars-by-prefix,CFG_ _CFG_ PLATFORM_))) 61endef 62 63define _cfg-vars-by-prefix 64 $(sort $(foreach prefix,$(1),$(filter $(prefix)%,$(.VARIABLES)))) 65endef 66 67# Convert a makefile variable to a #define 68# <undefined>, n => <undefined> 69# y => 1 70# <other value> => <other value> 71define cfg-make-define 72 $(strip $(if $(filter y,$($1)), 73 #define $1 1_nl_, 74 $(if $(filter xn x,x$($1)), 75 /* $1 is not set */_nl_, 76 #define $1 $($1)_nl_))) 77endef 78 79# Convert a makefile variable to a cmake set statement 80# <undefined>, n => <undefined> 81# <other value> => <other value> 82define cfg-cmake-set 83 $(strip $(if $(filter xn x,x$($1)), 84 # $1 is not set _nl_, 85 set($1 $($1))_nl_)) 86endef 87 88# Returns 'y' if at least one variable is 'y', 'n' otherwise 89# Example: 90# FOO_OR_BAR := $(call cfg-one-enabled, FOO BAR) 91cfg-one-enabled = $(if $(filter y, $(foreach var,$(1),$($(var)))),y,n) 92 93# Returns 'y' if all variables are 'y', 'n' otherwise 94# Example: 95# FOO_AND_BAR := $(call cfg-all-enabled, FOO BAR) 96cfg-all-enabled = $(if $(strip $(1)),$(if $(call _cfg-all-enabled,$(1)),y,n),n) 97_cfg-all-enabled = \ 98 $(strip \ 99 $(if $(1), \ 100 $(if $(filter y,$($(firstword $(1)))), \ 101 $(call _cfg-all-enabled,$(filter-out $(firstword $(1)),$(1))), \ 102 ), \ 103 y \ 104 ) \ 105 ) 106 107# Disable a configuration variable if some dependency is disabled 108# Example: 109# $(eval $(call cfg-depends-all,FOO,BAR BAZ)) 110# Will set FOO to 'n' if it is initially 'y' and BAR or BAZ are not 'y' 111cfg-depends-all = \ 112 $(strip \ 113 $(if $(filter y, $($(1))), \ 114 $(if $(filter y,$(call cfg-all-enabled,$(2))), \ 115 , \ 116 $(warning Warning: Disabling $(1) [requires $(strip $(2))]) \ 117 override $(1) := n \ 118 ) \ 119 ) \ 120 ) 121 122# Disable a configuration variable if all dependencies are disabled 123# Example: 124# $(eval $(call cfg-depends-one,FOO,BAR BAZ)) 125# Will set FOO to 'n' if it is initially 'y' and both BAR and BAZ are not 'y' 126cfg-depends-one = \ 127 $(strip \ 128 $(if $(filter y, $($(1))), \ 129 $(if $(filter y,$(call cfg-one-enabled,$(2))), \ 130 , \ 131 $(warning Warning: Disabling $(1) [requires (one of) $(strip $(2))]) \ 132 override $(1) := n \ 133 ) \ 134 ) \ 135 ) 136 137 138# Enable all depend variables 139# Example: 140# $(eval $(call cfg-enable-all-depends,FOO,BAR BAZ)) 141# Will enable BAR and BAZ if FOO is initially 'y' 142cfg-enable-all-depends = \ 143 $(strip \ 144 $(if $(2), \ 145 $(if $(filter y, $($(1))), \ 146 $(if $(filter y,$($(firstword $(2)))), \ 147 , \ 148 $(warning Warning: Enabling $(firstword $(2)) [required by $(1)]) \ 149 $(eval override $(firstword $(2)) := y) \ 150 ) \ 151 $(call cfg-enable-all-depends,$(1),$(filter-out $(firstword $(2)),$(2))), \ 152 ) \ 153 , \ 154 ) \ 155 ) 156 157# Check if a configuration variable has an acceptable value 158# Example: 159# $(call cfg-check-value,FOO,foo bar) 160# Will do nothing if $(CFG_FOO) is either foo or bar, and error out otherwise. 161cfg-check-value = \ 162 $(if $(filter-out $(2),$(CFG_$(1))), \ 163 $(error CFG_$(1) is set to '$(CFG_$(1))', valid values are: $(2))) 164 165# Set a variable or error out if it was previously set to a different value 166# The reason message (3rd parameter) is optional 167# Example: 168# $(call force,CFG_FOO,foo,required by CFG_BAR) 169define force 170$(eval $(call _force,$(strip $(1)),$(2),$(3))) 171endef 172 173define _force 174ifdef $(1) 175ifneq ($($(1)),$(2)) 176ifneq (,$(3)) 177_reason := $$(_empty) [$(3)] 178endif 179$$(error $(1) is set to '$($(1))' (from $(origin $(1))) but its value must be '$(2)'$$(_reason)) 180endif 181endif 182$(1) := $(2) 183endef 184