xref: /optee_os/mk/checkconf.mk (revision 9b94299a676c0534ada76e3e008d1a39f9103891)
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	guard="_`echo $@ | tr -- -/. ___`_";			\
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
59# Rename $1 to $2 only if file content differs. Otherwise just delete $1.
60define mv-if-changed
61	if cmp -s $2 $1; then					\
62		rm -f $1;					\
63	else							\
64		$(cmd-echo-silent) '  UPD     $2';		\
65		mv $1 $2;					\
66	fi
67endef
68
69define cfg-vars-by-prefix
70	$(strip $(if $(1),$(call _cfg-vars-by-prefix,$(1)),
71			  $(call _cfg-vars-by-prefix,CFG_ _CFG_ PLATFORM_)))
72endef
73
74define _cfg-vars-by-prefix
75	$(sort $(foreach prefix,$(1),$(filter $(prefix)%,$(.VARIABLES))))
76endef
77
78# Convert a makefile variable to a #define
79# <undefined>, n => <undefined>
80# y              => 1
81# <other value>  => <other value>
82define cfg-make-define
83	$(strip $(if $(filter y,$($1)),
84		     #define $1 1_nl_,
85		     $(if $(filter xn x,x$($1)),
86			  /* $1 is not set */_nl_,
87			  #define $1 $($1)_nl_)))
88endef
89
90# Convert a makefile variable to a cmake set statement
91# <undefined>, n => <undefined>
92# <other value>  => <other value>
93define cfg-cmake-set
94	$(strip $(if $(filter xn x,x$($1)),
95		  # $1 is not set _nl_,
96		  set($1 $($1))_nl_))
97endef
98
99# Returns 'y' if at least one variable is 'y', empty otherwise
100# Example:
101# FOO_OR_BAR := $(call cfg-one-enabled, FOO BAR)
102cfg-one-enabled = $(if $(filter y, $(foreach var,$(1),$($(var)))),y,)
103
104# Returns 'y' if all variables are 'y', empty otherwise
105# Example:
106# FOO_AND_BAR := $(call cfg-all-enabled, FOO BAR)
107cfg-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         )                                                                    \
115     )
116
117# Disable a configuration variable if some dependency is disabled
118# Example:
119# $(eval $(call cfg-depends-all,FOO,BAR BAZ))
120# Will clear FOO if it is initially 'y' and BAR or BAZ are not 'y'
121cfg-depends-all =                                                           \
122    $(strip                                                                 \
123        $(if $(filter y, $($(1))),                                          \
124            $(if $(call cfg-all-enabled,$(2)),                              \
125                ,                                                           \
126                $(warning Warning: Disabling $(1) [requires $(strip $(2))]) \
127                    override $(1) :=                                        \
128             )                                                              \
129         )                                                                  \
130     )
131
132# Disable a configuration variable if all dependencies are disabled
133# Example:
134# $(eval $(call cfg-depends-one,FOO,BAR BAZ))
135# Will clear FOO if it is initially 'y' and both BAR and BAZ are not 'y'
136cfg-depends-one =                                                                    \
137    $(strip                                                                          \
138        $(if $(filter y, $($(1))),                                                   \
139            $(if $(call cfg-one-enabled,$(2)),                                       \
140                ,                                                                    \
141                $(warning Warning: Disabling $(1) [requires (one of) $(strip $(2))]) \
142                    override $(1) :=                                                 \
143             )                                                                       \
144         )                                                                           \
145     )
146
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)])      \
159                        $(eval override $(firstword $(2)) := y)                            \
160                 )                                                                         \
161                 $(call cfg-enable-all-depends,$(1),$(filter-out $(firstword $(2)),$(2))), \
162             )                                                                             \
163             ,                                                                             \
164        )                                                                                  \
165     )
166
167# Set a variable or error out if it was previously set to a different value
168# The reason message (3rd parameter) is optional
169# Example:
170# $(call force,CFG_FOO,foo,required by CFG_BAR)
171define force
172$(eval $(call _force,$(1),$(2),$(3)))
173endef
174
175define _force
176ifdef $(1)
177ifneq ($($(1)),$(2))
178ifneq (,$(3))
179_reason := $$(_empty) [$(3)]
180endif
181$$(error $(1) is set to '$($(1))' (from $(origin $(1))) but its value must be '$(2)'$$(_reason))
182endif
183endif
184$(1) := $(2)
185endef
186