blob: 579b5fdc941c14439b649558baf62fccc872f83e [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 Forissier0a7f95b2014-11-14 17:13:40 +01005# and _CFG_* variables):
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; \
16 echo ' 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; \
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; \
Jerome Forissier3354f9b2015-04-15 14:06:04 +020031 echo ' CHK $@'; \
etienne carrieredde0e232015-02-26 10:29:27 +010032 cnf="$(strip $(foreach var, \
33 $(call cfg-vars-by-prefix,CFG_), \
34 $(call cfg-make-variable,$(var))))"; \
35 mkdir -p $(dir $@); \
36 echo "# auto-generated TEE configuration file" >$@.tmp; \
37 echo "# TEE version $${CFG_TEE_VERSION:-(undefined)}" >>$@.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; \
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
47 if [ -r $2 ] && cmp -s $2 $1; then \
48 rm -f $1; \
49 else \
50 echo ' UPD $2'; \
51 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 Forissier0a7f95b2014-11-14 17:13:40 +010057 $(call _cfg-vars-by-prefix,CFG_ _CFG_)))
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)),
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
Jerome Forissier0a7f95b2014-11-14 17:13:40 +010075
etienne carrieredde0e232015-02-26 10:29:27 +010076define cfg-make-variable
77 $(strip $(if $(filter xn x,x$($1)),
78 # $1 is not set ('$($1)')_nl_,
79 $1=$($1)_nl_))
80endef
81
Jerome Forissier0a7f95b2014-11-14 17:13:40 +010082# Returns 'y' if at least one variable is 'y', empty otherwise
83# Example:
84# FOO_OR_BAR := $(call cfg-one-enabled, FOO BAR)
85cfg-one-enabled = $(if $(filter y, $(foreach var,$(1),$($(var)))),y,)
86
87# Returns 'y' if all variables are 'y', empty otherwise
88# Example:
89# FOO_AND_BAR := $(call cfg-all-enabled, FOO BAR)
90cfg-all-enabled = \
91 $(strip \
92 $(if $(1), \
93 $(if $(filter y,$($(firstword $(1)))), \
94 $(call cfg-all-enabled,$(filter-out $(firstword $(1)),$(1))), \
95 ), \
96 y \
97 ) \
98 )
99
100# Disable a configuration variable if some dependency is disabled
101# Example:
102# $(eval $(call cfg-depends-all,FOO,BAR BAZ))
103# Will clear FOO if it is initially 'y' and BAR or BAZ are not 'y'
104cfg-depends-all = \
105 $(strip \
106 $(if $(filter y, $($(1))), \
107 $(if $(call cfg-all-enabled,$(2)), \
108 , \
109 $(warning Warning: Disabling $(1) [requires $(strip $(2))]) \
110 override $(1) := \
111 ) \
112 ) \
113 )
114
115# Disable a configuration variable if all dependencies are disabled
116# Example:
117# $(eval $(call cfg-depends-one,FOO,BAR BAZ))
118# Will clear FOO if it is initially 'y' and both BAR and BAZ are not 'y'
119cfg-depends-one = \
120 $(strip \
121 $(if $(filter y, $($(1))), \
122 $(if $(call cfg-one-enabled,$(2)), \
123 , \
124 $(warning Warning: Disabling $(1) [requires (one of) $(strip $(2))]) \
125 override $(1) := \
126 ) \
127 ) \
128 )