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