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