xref: /optee_os/mk/checkconf.mk (revision 947ebd697b79a401f28a2371b8058069ca82297c)
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):
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-mk
30	$(q)set -e;						\
31	$(cmd-echo-silent) '  CHK     $@';			\
32	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;	\
42	$(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		$(cmd-echo-silent) '  UPD     $2';		\
51		mv $1 $2;					\
52	fi
53endef
54
55define cfg-vars-by-prefix
56	$(strip $(if $(1),$(call _cfg-vars-by-prefix,$(1)),
57			  $(call _cfg-vars-by-prefix,CFG_ _CFG_)))
58endef
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
75
76define cfg-make-variable
77	$(strip $(if $(filter xn x,x$($1)),
78			  # $1 is not set ('$($1)')_nl_,
79			  $1=$($1)_nl_))
80endef
81
82# 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     )
129
130
131# Enable all depend variables
132# Example:
133# $(eval $(call cfg-enable-all-depends,FOO,BAR BAZ))
134# Will enable BAR and BAZ if FOO is initially 'y'
135cfg-enable-all-depends =                                                                   \
136    $(strip                                                                                \
137        $(if $(2),                                                                         \
138            $(if $(filter y, $($(1))),                                                     \
139                $(if $(filter y,$($(firstword $(2)))),                                     \
140                    ,                                                                      \
141                    $(warning Warning: Enabling $(firstword $(2)) [required by $(1)])      \
142                        $(eval $(firstword $(2)) = y)                                      \
143                 )                                                                         \
144                 $(call cfg-enable-all-depends,$(1),$(filter-out $(firstword $(2)),$(2))), \
145             )                                                                             \
146             ,                                                                             \
147        )                                                                                  \
148     )
149