xref: /optee_os/mk/checkconf.mk (revision a1cbb728630308fcf902a8953a32cc972d14757e)
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
58# Rename $1 to $2 only if file content differs. Otherwise just delete $1.
59define mv-if-changed
60	if cmp -s $2 $1; then					\
61		rm -f $1;					\
62	else							\
63		$(cmd-echo-silent) '  UPD     $2';		\
64		mv $1 $2;					\
65	fi
66endef
67
68define cfg-vars-by-prefix
69	$(strip $(if $(1),$(call _cfg-vars-by-prefix,$(1)),
70			  $(call _cfg-vars-by-prefix,CFG_ _CFG_ PLATFORM_)))
71endef
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)),
83		     #define $1 1_nl_,
84		     $(if $(filter xn x,x$($1)),
85			  /* $1 is not set */_nl_,
86			  #define $1 $($1)_nl_)))
87endef
88
89# 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
98# Returns 'y' if at least one variable is 'y', empty otherwise
99# Example:
100# FOO_OR_BAR := $(call cfg-one-enabled, FOO BAR)
101cfg-one-enabled = $(if $(filter y, $(foreach var,$(1),$($(var)))),y,)
102
103# Returns 'y' if all variables are 'y', empty otherwise
104# Example:
105# FOO_AND_BAR := $(call cfg-all-enabled, FOO BAR)
106cfg-all-enabled =                                                             \
107    $(strip                                                                   \
108        $(if $(1),                                                            \
109            $(if $(filter y,$($(firstword $(1)))),                            \
110                $(call cfg-all-enabled,$(filter-out $(firstword $(1)),$(1))), \
111             ),                                                               \
112            y                                                                 \
113         )                                                                    \
114     )
115
116# Disable a configuration variable if some dependency is disabled
117# Example:
118# $(eval $(call cfg-depends-all,FOO,BAR BAZ))
119# Will clear FOO if it is initially 'y' and BAR or BAZ are not 'y'
120cfg-depends-all =                                                           \
121    $(strip                                                                 \
122        $(if $(filter y, $($(1))),                                          \
123            $(if $(call cfg-all-enabled,$(2)),                              \
124                ,                                                           \
125                $(warning Warning: Disabling $(1) [requires $(strip $(2))]) \
126                    override $(1) :=                                        \
127             )                                                              \
128         )                                                                  \
129     )
130
131# Disable a configuration variable if all dependencies are disabled
132# Example:
133# $(eval $(call cfg-depends-one,FOO,BAR BAZ))
134# Will clear FOO if it is initially 'y' and both BAR and BAZ are not 'y'
135cfg-depends-one =                                                                    \
136    $(strip                                                                          \
137        $(if $(filter y, $($(1))),                                                   \
138            $(if $(call cfg-one-enabled,$(2)),                                       \
139                ,                                                                    \
140                $(warning Warning: Disabling $(1) [requires (one of) $(strip $(2))]) \
141                    override $(1) :=                                                 \
142             )                                                                       \
143         )                                                                           \
144     )
145
146
147# Enable all depend variables
148# Example:
149# $(eval $(call cfg-enable-all-depends,FOO,BAR BAZ))
150# Will enable BAR and BAZ if FOO is initially 'y'
151cfg-enable-all-depends =                                                                   \
152    $(strip                                                                                \
153        $(if $(2),                                                                         \
154            $(if $(filter y, $($(1))),                                                     \
155                $(if $(filter y,$($(firstword $(2)))),                                     \
156                    ,                                                                      \
157                    $(warning Warning: Enabling $(firstword $(2)) [required by $(1)])      \
158                        $(eval override $(firstword $(2)) := y)                            \
159                 )                                                                         \
160                 $(call cfg-enable-all-depends,$(1),$(filter-out $(firstword $(2)),$(2))), \
161             )                                                                             \
162             ,                                                                             \
163        )                                                                                  \
164     )
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,$(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