diff --git a/makefiles/utils/test-variables.mk b/makefiles/utils/test-variables.mk new file mode 100644 index 0000000000..d0040ff24d --- /dev/null +++ b/makefiles/utils/test-variables.mk @@ -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) diff --git a/makefiles/utils/variables.mk b/makefiles/utils/variables.mk new file mode 100644 index 0000000000..6d02061ae8 --- /dev/null +++ b/makefiles/utils/variables.mk @@ -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 +# +# 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?=) diff --git a/tests/build_system_utils/Makefile b/tests/build_system_utils/Makefile index 2be3981c8e..22e4af7d2a 100644 --- a/tests/build_system_utils/Makefile +++ b/tests/build_system_utils/Makefile @@ -16,7 +16,8 @@ endef 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` all: build-system-utils-tests @@ -31,3 +32,6 @@ test-ensure_value: 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)