aboutsummaryrefslogtreecommitdiff
path: root/mk/checkconf.mk
diff options
context:
space:
mode:
authorJerome Forissier <jerome.forissier@linaro.org>2014-11-06 17:54:51 +0100
committerJerome Forissier <jerome.forissier@linaro.org>2014-11-07 17:46:17 +0100
commitfe52b1f588403d8145ef1de326985460a3b1a2da (patch)
tree0bceba29a251f3c5405aa5e2ec2e698277d738df /mk/checkconf.mk
parentf8baf58212bbc46b21fb305419ddc0315ab1568d (diff)
core: create conf.h from CFG_* Makefile variables
Simplify the use of makefile configuration variables from C code. With this patch, one can #include <generated/conf.h> instead of adding CPP flags definitions to the .mk files. - CFG_* variables that are set to 'y' are converted to: #define CFG_FOO 1 - Undefined variables, or variables set to 'n' remain undefined in conf.h - CFG_* variables with any other value are output unchanged Signed-off-by: Jerome Forissier <jerome.forissier@linaro.org> Reviewed-by: Jens Wiklander <jens.wiklander@linaro.org> Reviewed-by: Pascal Brand <pascal.brand@linaro.org>
Diffstat (limited to 'mk/checkconf.mk')
-rw-r--r--mk/checkconf.mk54
1 files changed, 54 insertions, 0 deletions
diff --git a/mk/checkconf.mk b/mk/checkconf.mk
new file mode 100644
index 00000000..863b81ad
--- /dev/null
+++ b/mk/checkconf.mk
@@ -0,0 +1,54 @@
+# Generate/check/update a .h file to reflect the values of Makefile
+# variables
+#
+# Example usage (by default, check-conf-h will consider all CFG_*
+# variables):
+#
+# path/to/conf.h: FORCE
+# $(call check-conf-h)
+#
+# Or, to include only the variables with the given prefix(es):
+#
+# path/to/crypto_config.h: FORCE
+# $(call check-conf-h,CFG_CRYPTO_ CRYPTO_)
+
+define check-conf-h
+ $(q)set -e; \
+ echo ' CHK $@'; \
+ cnf="$(strip $(foreach var, \
+ $(call cfg-vars-by-prefix,$1), \
+ $(call cfg-make-define,$(var))))"; \
+ guard="_`echo $@ | tr -- -/. ___`_"; \
+ mkdir -p $(dir $@); \
+ echo "#ifndef $${guard}" >$@.tmp; \
+ echo "#define $${guard}" >>$@.tmp; \
+ echo -n "$${cnf}" | sed 's/_nl_ */\n/g' >>$@.tmp; \
+ echo "#endif" >>$@.tmp; \
+ if [ -r $@ ] && cmp -s $@ $@.tmp; then \
+ rm -f $@.tmp; \
+ else \
+ echo ' UPD $@'; \
+ mv $@.tmp $@; \
+ fi
+endef
+
+define cfg-vars-by-prefix
+ $(strip $(if $(1),$(call _cfg-vars-by-prefix,$(1)),
+ $(call _cfg-vars-by-prefix,CFG_)))
+endef
+
+define _cfg-vars-by-prefix
+ $(sort $(foreach prefix,$(1),$(filter $(prefix)%,$(.VARIABLES))))
+endef
+
+# Convert a makefile variable to a #define
+# <undefined>, n => <undefined>
+# y => 1
+# <other value> => <other value>
+define cfg-make-define
+ $(strip $(if $(filter y,$($1)),
+ #define $1 1 /* '$($1)' */_nl_,
+ $(if $(filter xn x,x$($1)),
+ /* $1 is not set ('$($1)') */_nl_,
+ #define $1 $($1) /* '$($1)' */_nl_)))
+endef