xref: /optee_os/mk/checkconf.mk (revision cf458f13c9c3ad7167ad2f680e3ca143467a4b31)
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	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;					\
26	if [ -r $@ ] && cmp -s $@ $@.tmp; then			\
27		rm -f $@.tmp;					\
28	else							\
29		echo '  UPD     $@';				\
30		mv $@.tmp $@;					\
31	fi
32endef
33
34define build-conf-mk
35	$(q)set -e;						\
36	cnf="$(strip $(foreach var,				\
37		$(call cfg-vars-by-prefix,CFG_),		\
38		$(call cfg-make-variable,$(var))))";		\
39	mkdir -p $(dir $@);					\
40	echo "# auto-generated TEE configuration file" >$@.tmp; \
41	echo "# TEE version $${CFG_TEE_VERSION:-(undefined)}" >>$@.tmp; \
42	echo "ARCH=${ARCH}" >>$@.tmp;				\
43	echo "PLATFORM=${PLATFORM}" >>$@.tmp;			\
44	echo "PLATFORM_FLAVOR=${PLATFORM_FLAVOR}" >>$@.tmp; 	\
45	echo -n "$${cnf}" | sed 's/_nl_ */\n/g' >>$@.tmp;	\
46	echo '  UPD     $@';					\
47	mv $@.tmp $@;
48endef
49
50define cfg-vars-by-prefix
51	$(strip $(if $(1),$(call _cfg-vars-by-prefix,$(1)),
52			  $(call _cfg-vars-by-prefix,CFG_ _CFG_)))
53endef
54
55define _cfg-vars-by-prefix
56	$(sort $(foreach prefix,$(1),$(filter $(prefix)%,$(.VARIABLES))))
57endef
58
59# Convert a makefile variable to a #define
60# <undefined>, n => <undefined>
61# y              => 1
62# <other value>  => <other value>
63define cfg-make-define
64	$(strip $(if $(filter y,$($1)),
65		     #define $1 1 /* '$($1)' */_nl_,
66		     $(if $(filter xn x,x$($1)),
67			  /* $1 is not set ('$($1)') */_nl_,
68			  #define $1 $($1) /* '$($1)' */_nl_)))
69endef
70
71define cfg-make-variable
72	$(strip $(if $(filter xn x,x$($1)),
73			  # $1 is not set ('$($1)')_nl_,
74			  $1=$($1)_nl_))
75endef
76
77# Returns 'y' if at least one variable is 'y', empty otherwise
78# Example:
79# FOO_OR_BAR := $(call cfg-one-enabled, FOO BAR)
80cfg-one-enabled = $(if $(filter y, $(foreach var,$(1),$($(var)))),y,)
81
82# Returns 'y' if all variables are 'y', empty otherwise
83# Example:
84# FOO_AND_BAR := $(call cfg-all-enabled, FOO BAR)
85cfg-all-enabled =                                                             \
86    $(strip                                                                   \
87        $(if $(1),                                                            \
88            $(if $(filter y,$($(firstword $(1)))),                            \
89                $(call cfg-all-enabled,$(filter-out $(firstword $(1)),$(1))), \
90             ),                                                               \
91            y                                                                 \
92         )                                                                    \
93     )
94
95# Disable a configuration variable if some dependency is disabled
96# Example:
97# $(eval $(call cfg-depends-all,FOO,BAR BAZ))
98# Will clear FOO if it is initially 'y' and BAR or BAZ are not 'y'
99cfg-depends-all =                                                           \
100    $(strip                                                                 \
101        $(if $(filter y, $($(1))),                                          \
102            $(if $(call cfg-all-enabled,$(2)),                              \
103                ,                                                           \
104                $(warning Warning: Disabling $(1) [requires $(strip $(2))]) \
105                    override $(1) :=                                        \
106             )                                                              \
107         )                                                                  \
108     )
109
110# Disable a configuration variable if all dependencies are disabled
111# Example:
112# $(eval $(call cfg-depends-one,FOO,BAR BAZ))
113# Will clear FOO if it is initially 'y' and both BAR and BAZ are not 'y'
114cfg-depends-one =                                                                    \
115    $(strip                                                                          \
116        $(if $(filter y, $($(1))),                                                   \
117            $(if $(call cfg-one-enabled,$(2)),                                       \
118                ,                                                                    \
119                $(warning Warning: Disabling $(1) [requires (one of) $(strip $(2))]) \
120                    override $(1) :=                                                 \
121             )                                                                       \
122         )                                                                           \
123     )
124