xref: /optee_os/mk/checkconf.mk (revision 4e77495e8caa9fd9d05e28ef675fb64f8fcdedec)
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# 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_)
14
15define check-conf-h
16	$(q)set -e;						\
17	echo '  CHK     $@';					\
18	cnf="$(strip $(foreach var,				\
19		$(call cfg-vars-by-prefix,$1),			\
20		$(call cfg-make-define,$(var))))";		\
21	guard="_`echo $@ | tr -- -/. ___`_";			\
22	mkdir -p $(dir $@);					\
23	echo "#ifndef $${guard}" >$@.tmp;			\
24	echo "#define $${guard}" >>$@.tmp;			\
25	echo -n "$${cnf}" | sed 's/_nl_ */\n/g' >>$@.tmp;	\
26	echo "#endif" >>$@.tmp;					\
27	if [ -r $@ ] && cmp -s $@ $@.tmp; then			\
28		rm -f $@.tmp;					\
29	else							\
30		echo '  UPD     $@';				\
31		mv $@.tmp $@;					\
32	fi
33endef
34
35define cfg-vars-by-prefix
36	$(strip $(if $(1),$(call _cfg-vars-by-prefix,$(1)),
37			  $(call _cfg-vars-by-prefix,CFG_)))
38endef
39
40define _cfg-vars-by-prefix
41	$(sort $(foreach prefix,$(1),$(filter $(prefix)%,$(.VARIABLES))))
42endef
43
44# Convert a makefile variable to a #define
45# <undefined>, n => <undefined>
46# y              => 1
47# <other value>  => <other value>
48define cfg-make-define
49	$(strip $(if $(filter y,$($1)),
50		     #define $1 1 /* '$($1)' */_nl_,
51		     $(if $(filter xn x,x$($1)),
52			  /* $1 is not set ('$($1)') */_nl_,
53			  #define $1 $($1) /* '$($1)' */_nl_)))
54endef
55