makefiles/utils: function to export variables for a target
This allows exporting variables only for some target. It will allow not exporting variables when not needed, and so prevent unnecessary evaluation.
This commit is contained in:
parent
b9c15c3084
commit
fcf8c4782d
19
makefiles/utils/test-variables.mk
Normal file
19
makefiles/utils/test-variables.mk
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
include variables.mk
|
||||||
|
|
||||||
|
# Timestamp in nanoseconds (to be an integer)
|
||||||
|
# OSx 'date' does not support 'date +%s%N' so rely on python instead
|
||||||
|
# It could be OSx specific but we do not have 'OS' defined here to differentiate
|
||||||
|
date_nanoseconds = $(shell python -c 'import time; print(int(time.time() * 1000000000))')
|
||||||
|
|
||||||
|
EXPORTED_VARIABLES = MY_VARIABLE CURRENT_TIME
|
||||||
|
MY_VARIABLE = my_variable
|
||||||
|
# Defered evaluation to the test
|
||||||
|
CURRENT_TIME = $(call date_nanoseconds)
|
||||||
|
|
||||||
|
$(call target-export-variables,test-exported-variables,$(EXPORTED_VARIABLES))
|
||||||
|
test-exported-variables:
|
||||||
|
$(Q)bash -c 'test "$(MY_VARIABLE)" = "$${MY_VARIABLE}" || { echo ERROR: "$(MY_VARIABLE)" != "$${MY_VARIABLE}"; exit 1; }'
|
||||||
|
$(Q)bash -c 'test $(PARSE_TIME) -lt $${CURRENT_TIME} || { echo ERROR: $(PARSE_TIME) \>= $${CURRENT_TIME} >&2; exit 1; }'
|
||||||
|
|
||||||
|
# Immediate evaluation for comparing
|
||||||
|
PARSE_TIME := $(call date_nanoseconds)
|
||||||
19
makefiles/utils/variables.mk
Normal file
19
makefiles/utils/variables.mk
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
# Utilities to set variables and environment for targets
|
||||||
|
# These functions should help replacing immediate evaluation and global 'export'
|
||||||
|
|
||||||
|
|
||||||
|
# Target specific export the variables for that target
|
||||||
|
#
|
||||||
|
# target-export-variables <target> <variables>
|
||||||
|
#
|
||||||
|
# Parameters
|
||||||
|
# target: name of target
|
||||||
|
# variables: the variables to export
|
||||||
|
#
|
||||||
|
# The variable will only be evaluated when executing the target as when
|
||||||
|
# doing export
|
||||||
|
target-export-variables = $(foreach var,$(2),$(call _target-export-variable,$1,$(var)))
|
||||||
|
|
||||||
|
# '$1: export $2' cannot be used alone
|
||||||
|
# By using '?=' the variable is evaluated at runtime only
|
||||||
|
_target-export-variable = $(eval $1: export $2?=)
|
||||||
@ -16,7 +16,8 @@ endef
|
|||||||
|
|
||||||
MAKEFILES_UTILS = $(RIOTMAKE)/utils
|
MAKEFILES_UTILS = $(RIOTMAKE)/utils
|
||||||
|
|
||||||
COMPILE_TESTS = test-ensure_value test-ensure_value-negative
|
COMPILE_TESTS += test-ensure_value test-ensure_value-negative
|
||||||
|
COMPILE_TESTS += test-exported-variables
|
||||||
|
|
||||||
# Tests will be run both in the host machine and in `docker`
|
# Tests will be run both in the host machine and in `docker`
|
||||||
all: build-system-utils-tests
|
all: build-system-utils-tests
|
||||||
@ -31,3 +32,6 @@ test-ensure_value:
|
|||||||
|
|
||||||
test-ensure_value-negative:
|
test-ensure_value-negative:
|
||||||
$(Q)$(call command_should_fail,"$(MAKE)" -C $(MAKEFILES_UTILS) -f test-checks.mk test-ensure_value-negative)
|
$(Q)$(call command_should_fail,"$(MAKE)" -C $(MAKEFILES_UTILS) -f test-checks.mk test-ensure_value-negative)
|
||||||
|
|
||||||
|
test-exported-variables:
|
||||||
|
$(Q)$(call command_should_succeed,"$(MAKE)" -C $(MAKEFILES_UTILS) -f test-variables.mk test-exported-variables)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user