xref: /optee_os/mk/checkconf.mk (revision d85162020b1326753d75fcea37f9f34088cba34c)
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 cfg-vars-by-prefix
35	$(strip $(if $(1),$(call _cfg-vars-by-prefix,$(1)),
36			  $(call _cfg-vars-by-prefix,CFG_ _CFG_)))
37endef
38
39define _cfg-vars-by-prefix
40	$(sort $(foreach prefix,$(1),$(filter $(prefix)%,$(.VARIABLES))))
41endef
42
43# Convert a makefile variable to a #define
44# <undefined>, n => <undefined>
45# y              => 1
46# <other value>  => <other value>
47define cfg-make-define
48	$(strip $(if $(filter y,$($1)),
49		     #define $1 1 /* '$($1)' */_nl_,
50		     $(if $(filter xn x,x$($1)),
51			  /* $1 is not set ('$($1)') */_nl_,
52			  #define $1 $($1) /* '$($1)' */_nl_)))
53endef
54
55# Returns 'y' if at least one variable is 'y', empty otherwise
56# Example:
57# FOO_OR_BAR := $(call cfg-one-enabled, FOO BAR)
58cfg-one-enabled = $(if $(filter y, $(foreach var,$(1),$($(var)))),y,)
59
60# Returns 'y' if all variables are 'y', empty otherwise
61# Example:
62# FOO_AND_BAR := $(call cfg-all-enabled, FOO BAR)
63cfg-all-enabled =                                                             \
64    $(strip                                                                   \
65        $(if $(1),                                                            \
66            $(if $(filter y,$($(firstword $(1)))),                            \
67                $(call cfg-all-enabled,$(filter-out $(firstword $(1)),$(1))), \
68             ),                                                               \
69            y                                                                 \
70         )                                                                    \
71     )
72
73# Disable a configuration variable if some dependency is disabled
74# Example:
75# $(eval $(call cfg-depends-all,FOO,BAR BAZ))
76# Will clear FOO if it is initially 'y' and BAR or BAZ are not 'y'
77cfg-depends-all =                                                           \
78    $(strip                                                                 \
79        $(if $(filter y, $($(1))),                                          \
80            $(if $(call cfg-all-enabled,$(2)),                              \
81                ,                                                           \
82                $(warning Warning: Disabling $(1) [requires $(strip $(2))]) \
83                    override $(1) :=                                        \
84             )                                                              \
85         )                                                                  \
86     )
87
88# Disable a configuration variable if all dependencies are disabled
89# Example:
90# $(eval $(call cfg-depends-one,FOO,BAR BAZ))
91# Will clear FOO if it is initially 'y' and both BAR and BAZ are not 'y'
92cfg-depends-one =                                                                    \
93    $(strip                                                                          \
94        $(if $(filter y, $($(1))),                                                   \
95            $(if $(call cfg-one-enabled,$(2)),                                       \
96                ,                                                                    \
97                $(warning Warning: Disabling $(1) [requires (one of) $(strip $(2))]) \
98                    override $(1) :=                                                 \
99             )                                                                       \
100         )                                                                           \
101     )
102