Jerome Forissier | fe52b1f | 2014-11-06 17:54:51 +0100 | [diff] [blame] | 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_* |
Jerome Forissier | 0a7f95b | 2014-11-14 17:13:40 +0100 | [diff] [blame] | 5 | # and _CFG_* variables): |
Jerome Forissier | fe52b1f | 2014-11-06 17:54:51 +0100 | [diff] [blame] | 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_) |
Jerome Forissier | fe52b1f | 2014-11-06 17:54:51 +0100 | [diff] [blame] | 14 | define 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 |
| 32 | endef |
| 33 | |
etienne carriere | dde0e23 | 2015-02-26 10:29:27 +0100 | [diff] [blame^] | 34 | define 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 $@; |
| 48 | endef |
| 49 | |
Jerome Forissier | fe52b1f | 2014-11-06 17:54:51 +0100 | [diff] [blame] | 50 | define cfg-vars-by-prefix |
| 51 | $(strip $(if $(1),$(call _cfg-vars-by-prefix,$(1)), |
Jerome Forissier | 0a7f95b | 2014-11-14 17:13:40 +0100 | [diff] [blame] | 52 | $(call _cfg-vars-by-prefix,CFG_ _CFG_))) |
Jerome Forissier | fe52b1f | 2014-11-06 17:54:51 +0100 | [diff] [blame] | 53 | endef |
| 54 | |
| 55 | define _cfg-vars-by-prefix |
| 56 | $(sort $(foreach prefix,$(1),$(filter $(prefix)%,$(.VARIABLES)))) |
| 57 | endef |
| 58 | |
| 59 | # Convert a makefile variable to a #define |
| 60 | # <undefined>, n => <undefined> |
| 61 | # y => 1 |
| 62 | # <other value> => <other value> |
| 63 | define 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_))) |
| 69 | endef |
Jerome Forissier | 0a7f95b | 2014-11-14 17:13:40 +0100 | [diff] [blame] | 70 | |
etienne carriere | dde0e23 | 2015-02-26 10:29:27 +0100 | [diff] [blame^] | 71 | define cfg-make-variable |
| 72 | $(strip $(if $(filter xn x,x$($1)), |
| 73 | # $1 is not set ('$($1)')_nl_, |
| 74 | $1=$($1)_nl_)) |
| 75 | endef |
| 76 | |
Jerome Forissier | 0a7f95b | 2014-11-14 17:13:40 +0100 | [diff] [blame] | 77 | # Returns 'y' if at least one variable is 'y', empty otherwise |
| 78 | # Example: |
| 79 | # FOO_OR_BAR := $(call cfg-one-enabled, FOO BAR) |
| 80 | cfg-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) |
| 85 | cfg-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' |
| 99 | cfg-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' |
| 114 | cfg-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 | ) |