blob: 77b3748ee1efc6eecf6b060c9ac526f5100869c3 [file] [log] [blame]
Jerome Forissierfe52b1f2014-11-06 17:54:51 +01001# 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_*
Jerome Forissier5ef74e72016-08-06 10:47:12 +02005# and _CFG_* variables plus PLATFORM_*):
Jerome Forissierfe52b1f2014-11-06 17:54:51 +01006#
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_)
Jerome Forissierfe52b1f2014-11-06 17:54:51 +010014define check-conf-h
15 $(q)set -e; \
Jens Wiklander62428632015-04-29 15:05:19 +020016 $(cmd-echo-silent) ' CHK $@'; \
Jerome Forissierbef37b82016-10-31 09:22:01 +010017 cnf='$(strip $(foreach var, \
Jerome Forissierfe52b1f2014-11-06 17:54:51 +010018 $(call cfg-vars-by-prefix,$1), \
Jerome Forissierbef37b82016-10-31 09:22:01 +010019 $(call cfg-make-define,$(var))))'; \
Etienne Carriere21c9a572018-02-09 15:57:30 +010020 guard="_`echo $@ | tr -- -/.+ _`_"; \
Jerome Forissierfe52b1f2014-11-06 17:54:51 +010021 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; \
Jerome Forissier3354f9b2015-04-15 14:06:04 +020026 $(call mv-if-changed,$@.tmp,$@)
Jerome Forissierfe52b1f2014-11-06 17:54:51 +010027endef
28
Jens Wiklanderb924c492018-02-08 14:50:07 +010029define 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))))'; \
Jens Wiklanderb924c492018-02-08 14:50:07 +010035 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
Jerome Forissier3354f9b2015-04-15 14:06:04 +020042define check-conf-mk
etienne carrieredde0e232015-02-26 10:29:27 +010043 $(q)set -e; \
Jens Wiklander62428632015-04-29 15:05:19 +020044 $(cmd-echo-silent) ' CHK $@'; \
Jerome Forissierbef37b82016-10-31 09:22:01 +010045 cnf='$(strip $(foreach var, \
etienne carrieredde0e232015-02-26 10:29:27 +010046 $(call cfg-vars-by-prefix,CFG_), \
Jerome Forissierbef37b82016-10-31 09:22:01 +010047 $(strip $(var)=$($(var))_nl_)))'; \
etienne carrieredde0e232015-02-26 10:29:27 +010048 mkdir -p $(dir $@); \
49 echo "# auto-generated TEE configuration file" >$@.tmp; \
Pascal Brandb5569a62016-01-08 15:14:30 +010050 echo "# TEE version ${TEE_IMPL_VERSION}" >>$@.tmp; \
etienne carrieredde0e232015-02-26 10:29:27 +010051 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; \
Jerome Forissier3354f9b2015-04-15 14:06:04 +020055 $(call mv-if-changed,$@.tmp,$@)
56endef
57
58# Rename $1 to $2 only if file content differs. Otherwise just delete $1.
59define mv-if-changed
Jerome Forissier39578632017-11-08 15:51:52 +010060 if cmp -s $2 $1; then \
Jerome Forissier3354f9b2015-04-15 14:06:04 +020061 rm -f $1; \
62 else \
Jens Wiklander62428632015-04-29 15:05:19 +020063 $(cmd-echo-silent) ' UPD $2'; \
Jerome Forissier3354f9b2015-04-15 14:06:04 +020064 mv $1 $2; \
65 fi
etienne carrieredde0e232015-02-26 10:29:27 +010066endef
67
Jerome Forissierfe52b1f2014-11-06 17:54:51 +010068define cfg-vars-by-prefix
69 $(strip $(if $(1),$(call _cfg-vars-by-prefix,$(1)),
Jerome Forissier5ef74e72016-08-06 10:47:12 +020070 $(call _cfg-vars-by-prefix,CFG_ _CFG_ PLATFORM_)))
Jerome Forissierfe52b1f2014-11-06 17:54:51 +010071endef
72
73define _cfg-vars-by-prefix
74 $(sort $(foreach prefix,$(1),$(filter $(prefix)%,$(.VARIABLES))))
75endef
76
77# Convert a makefile variable to a #define
78# <undefined>, n => <undefined>
79# y => 1
80# <other value> => <other value>
81define cfg-make-define
82 $(strip $(if $(filter y,$($1)),
Jerome Forissierbef37b82016-10-31 09:22:01 +010083 #define $1 1_nl_,
Jerome Forissierfe52b1f2014-11-06 17:54:51 +010084 $(if $(filter xn x,x$($1)),
Jerome Forissierbef37b82016-10-31 09:22:01 +010085 /* $1 is not set */_nl_,
86 #define $1 $($1)_nl_)))
Jerome Forissierfe52b1f2014-11-06 17:54:51 +010087endef
Jerome Forissier0a7f95b2014-11-14 17:13:40 +010088
Jens Wiklanderb924c492018-02-08 14:50:07 +010089# Convert a makefile variable to a cmake set statement
90# <undefined>, n => <undefined>
91# <other value> => <other value>
92define cfg-cmake-set
93 $(strip $(if $(filter xn x,x$($1)),
94 # $1 is not set _nl_,
95 set($1 $($1))_nl_))
96endef
97
Jerome Forissier9e8c8162019-09-26 08:48:25 +020098# Returns 'y' if at least one variable is 'y', 'n' otherwise
Jerome Forissier0a7f95b2014-11-14 17:13:40 +010099# Example:
100# FOO_OR_BAR := $(call cfg-one-enabled, FOO BAR)
Jerome Forissier9e8c8162019-09-26 08:48:25 +0200101cfg-one-enabled = $(if $(filter y, $(foreach var,$(1),$($(var)))),y,n)
Jerome Forissier0a7f95b2014-11-14 17:13:40 +0100102
Jerome Forissierb2a75cd2019-09-25 22:07:11 +0200103# Returns 'y' if all variables are 'y', 'n' otherwise
Jerome Forissier0a7f95b2014-11-14 17:13:40 +0100104# Example:
105# FOO_AND_BAR := $(call cfg-all-enabled, FOO BAR)
Jerome Forissierb2a75cd2019-09-25 22:07:11 +0200106cfg-all-enabled = $(if $(strip $(1)),$(if $(call _cfg-all-enabled,$(1)),y,n),n)
107_cfg-all-enabled = \
108 $(strip \
109 $(if $(1), \
110 $(if $(filter y,$($(firstword $(1)))), \
111 $(call _cfg-all-enabled,$(filter-out $(firstword $(1)),$(1))), \
112 ), \
113 y \
114 ) \
Jerome Forissier0a7f95b2014-11-14 17:13:40 +0100115 )
116
117# Disable a configuration variable if some dependency is disabled
118# Example:
119# $(eval $(call cfg-depends-all,FOO,BAR BAZ))
Jerome Forissier9ffea7b2019-09-25 22:22:54 +0200120# Will set FOO to 'n' if it is initially 'y' and BAR or BAZ are not 'y'
Jerome Forissier0a7f95b2014-11-14 17:13:40 +0100121cfg-depends-all = \
122 $(strip \
123 $(if $(filter y, $($(1))), \
Jerome Forissierb2a75cd2019-09-25 22:07:11 +0200124 $(if $(filter y,$(call cfg-all-enabled,$(2))), \
Jerome Forissier0a7f95b2014-11-14 17:13:40 +0100125 , \
126 $(warning Warning: Disabling $(1) [requires $(strip $(2))]) \
Jerome Forissier9ffea7b2019-09-25 22:22:54 +0200127 override $(1) := n \
Jerome Forissier0a7f95b2014-11-14 17:13:40 +0100128 ) \
129 ) \
130 )
131
132# Disable a configuration variable if all dependencies are disabled
133# Example:
134# $(eval $(call cfg-depends-one,FOO,BAR BAZ))
Jerome Forissier9ffea7b2019-09-25 22:22:54 +0200135# Will set FOO to 'n' if it is initially 'y' and both BAR and BAZ are not 'y'
Jerome Forissier0a7f95b2014-11-14 17:13:40 +0100136cfg-depends-one = \
137 $(strip \
138 $(if $(filter y, $($(1))), \
Jerome Forissier9e8c8162019-09-26 08:48:25 +0200139 $(if $(filter y,$(call cfg-one-enabled,$(2))), \
Jerome Forissier0a7f95b2014-11-14 17:13:40 +0100140 , \
141 $(warning Warning: Disabling $(1) [requires (one of) $(strip $(2))]) \
Jerome Forissier9ffea7b2019-09-25 22:22:54 +0200142 override $(1) := n \
Jerome Forissier0a7f95b2014-11-14 17:13:40 +0100143 ) \
144 ) \
145 )
James Kunga8224612015-03-16 16:25:23 +0800146
147
148# Enable all depend variables
149# Example:
150# $(eval $(call cfg-enable-all-depends,FOO,BAR BAZ))
151# Will enable BAR and BAZ if FOO is initially 'y'
152cfg-enable-all-depends = \
153 $(strip \
154 $(if $(2), \
155 $(if $(filter y, $($(1))), \
156 $(if $(filter y,$($(firstword $(2)))), \
157 , \
158 $(warning Warning: Enabling $(firstword $(2)) [required by $(1)]) \
Jerome Forissierc8212ba2015-09-23 14:44:49 -0700159 $(eval override $(firstword $(2)) := y) \
James Kunga8224612015-03-16 16:25:23 +0800160 ) \
161 $(call cfg-enable-all-depends,$(1),$(filter-out $(firstword $(2)),$(2))), \
162 ) \
163 , \
164 ) \
165 )
Jerome Forissierdffb0042015-10-23 17:30:26 +0200166
Jerome Forissierc20f0d12020-02-12 13:25:16 +0100167# Check if a configuration variable has an acceptable value
168# Example:
169# $(call cfg-check-value,FOO,foo bar)
170# Will do nothing if $(CFG_FOO) is either foo or bar, and error out otherwise.
171cfg-check-value = \
172 $(if $(filter-out $(2),$(CFG_$(1))), \
173 $(error CFG_$(1) is set to '$(CFG_$(1))', valid values are: $(2)))
174
Jerome Forissier10b79252015-10-28 13:56:57 +0100175# Set a variable or error out if it was previously set to a different value
176# The reason message (3rd parameter) is optional
Jerome Forissierdffb0042015-10-23 17:30:26 +0200177# Example:
Jerome Forissier10b79252015-10-28 13:56:57 +0100178# $(call force,CFG_FOO,foo,required by CFG_BAR)
Jerome Forissierdffb0042015-10-23 17:30:26 +0200179define force
Jerome Forissiere5834432020-12-11 15:53:30 +0100180$(eval $(call _force,$(strip $(1)),$(2),$(3)))
Jerome Forissierdffb0042015-10-23 17:30:26 +0200181endef
182
183define _force
184ifdef $(1)
185ifneq ($($(1)),$(2))
Jerome Forissier10b79252015-10-28 13:56:57 +0100186ifneq (,$(3))
187_reason := $$(_empty) [$(3)]
188endif
189$$(error $(1) is set to '$($(1))' (from $(origin $(1))) but its value must be '$(2)'$$(_reason))
Jerome Forissierdffb0042015-10-23 17:30:26 +0200190endif
191endif
192$(1) := $(2)
193endef