blob: 52a8952e628a242957990fc23d3b98c7da44ca2d [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))))'; \
Jerome Forissierfe52b1f2014-11-06 17:54:51 +010020 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; \
Jerome Forissier3354f9b2015-04-15 14:06:04 +020026 $(call mv-if-changed,$@.tmp,$@)
Jerome Forissierfe52b1f2014-11-06 17:54:51 +010027endef
28
Jerome Forissier3354f9b2015-04-15 14:06:04 +020029define check-conf-mk
etienne carrieredde0e232015-02-26 10:29:27 +010030 $(q)set -e; \
Jens Wiklander62428632015-04-29 15:05:19 +020031 $(cmd-echo-silent) ' CHK $@'; \
Jerome Forissierbef37b82016-10-31 09:22:01 +010032 cnf='$(strip $(foreach var, \
etienne carrieredde0e232015-02-26 10:29:27 +010033 $(call cfg-vars-by-prefix,CFG_), \
Jerome Forissierbef37b82016-10-31 09:22:01 +010034 $(strip $(var)=$($(var))_nl_)))'; \
etienne carrieredde0e232015-02-26 10:29:27 +010035 mkdir -p $(dir $@); \
36 echo "# auto-generated TEE configuration file" >$@.tmp; \
Pascal Brandb5569a62016-01-08 15:14:30 +010037 echo "# TEE version ${TEE_IMPL_VERSION}" >>$@.tmp; \
etienne carrieredde0e232015-02-26 10:29:27 +010038 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; \
Jerome Forissier3354f9b2015-04-15 14:06:04 +020042 $(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
Jerome Forissier39578632017-11-08 15:51:52 +010047 if cmp -s $2 $1; then \
Jerome Forissier3354f9b2015-04-15 14:06:04 +020048 rm -f $1; \
49 else \
Jens Wiklander62428632015-04-29 15:05:19 +020050 $(cmd-echo-silent) ' UPD $2'; \
Jerome Forissier3354f9b2015-04-15 14:06:04 +020051 mv $1 $2; \
52 fi
etienne carrieredde0e232015-02-26 10:29:27 +010053endef
54
Jerome Forissierfe52b1f2014-11-06 17:54:51 +010055define cfg-vars-by-prefix
56 $(strip $(if $(1),$(call _cfg-vars-by-prefix,$(1)),
Jerome Forissier5ef74e72016-08-06 10:47:12 +020057 $(call _cfg-vars-by-prefix,CFG_ _CFG_ PLATFORM_)))
Jerome Forissierfe52b1f2014-11-06 17:54:51 +010058endef
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)),
Jerome Forissierbef37b82016-10-31 09:22:01 +010070 #define $1 1_nl_,
Jerome Forissierfe52b1f2014-11-06 17:54:51 +010071 $(if $(filter xn x,x$($1)),
Jerome Forissierbef37b82016-10-31 09:22:01 +010072 /* $1 is not set */_nl_,
73 #define $1 $($1)_nl_)))
Jerome Forissierfe52b1f2014-11-06 17:54:51 +010074endef
Jerome Forissier0a7f95b2014-11-14 17:13:40 +010075
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 )
James Kunga8224612015-03-16 16:25:23 +0800123
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)]) \
Jerome Forissierc8212ba2015-09-23 14:44:49 -0700136 $(eval override $(firstword $(2)) := y) \
James Kunga8224612015-03-16 16:25:23 +0800137 ) \
138 $(call cfg-enable-all-depends,$(1),$(filter-out $(firstword $(2)),$(2))), \
139 ) \
140 , \
141 ) \
142 )
Jerome Forissierdffb0042015-10-23 17:30:26 +0200143
Jerome Forissier10b79252015-10-28 13:56:57 +0100144# Set a variable or error out if it was previously set to a different value
145# The reason message (3rd parameter) is optional
Jerome Forissierdffb0042015-10-23 17:30:26 +0200146# Example:
Jerome Forissier10b79252015-10-28 13:56:57 +0100147# $(call force,CFG_FOO,foo,required by CFG_BAR)
Jerome Forissierdffb0042015-10-23 17:30:26 +0200148define force
Jerome Forissier10b79252015-10-28 13:56:57 +0100149$(eval $(call _force,$(1),$(2),$(3)))
Jerome Forissierdffb0042015-10-23 17:30:26 +0200150endef
151
152define _force
153ifdef $(1)
154ifneq ($($(1)),$(2))
Jerome Forissier10b79252015-10-28 13:56:57 +0100155ifneq (,$(3))
156_reason := $$(_empty) [$(3)]
157endif
158$$(error $(1) is set to '$($(1))' (from $(origin $(1))) but its value must be '$(2)'$$(_reason))
Jerome Forissierdffb0042015-10-23 17:30:26 +0200159endif
160endif
161$(1) := $(2)
162endef