Skip to content

makefiles/utils/variables: add functions to help managing variables#11664

Merged
jcarrano merged 3 commits intoRIOT-OS:masterfrom
cladmi:pr/make/variables_lazy_evaluation
Jun 28, 2019
Merged

makefiles/utils/variables: add functions to help managing variables#11664
jcarrano merged 3 commits intoRIOT-OS:masterfrom
cladmi:pr/make/variables_lazy_evaluation

Conversation

@cladmi
Copy link
Copy Markdown
Contributor

@cladmi cladmi commented Jun 7, 2019

Contribution description

Add functions to set variables and environment for targets
These functions should help replacing immediate evaluation and global 'export'

target-export-variables

target-export-variables <target> <variables> function that exports a list of variables for some targets

This should help exporting variables only for the required targets so not evaluate shell calls for nothing.
Like make clean does not need CFLAGS but if the variable is exported, it will evaluate which options are supported in CFLAGS anyway.

memoized

VARIABLE = $(call memoized,VARIABLE,<value>) function that allows only evaluating value when the variable is used like a normal deferred variable, but further evaluations will use an immediate value.

This should help not assigning shell commands results with := to call it once only, as it result to evaluating even if not needed. Replacing by a direct = would result in the command be executed on each evaluation which is not wanted.
This gives the best of both worlds.

The implementation requires to repeat the variable name in the call memoized arguments.

Testing procedure

Review and run the test in tests/build_system_utils.

make -C tests/build_system_utils/

If it executes without error, it is working.

You can also run it with QUIET=0 to see the executed commands.

Issues/PRs references

Tracking: remove harmful use of export in make and immediate evaluation #10850

@cladmi cladmi added Type: enhancement The issue suggests enhanceable parts / The PR enhances parts of the codebase / documentation Area: build system Area: Build system Area: tools Area: Supplementary tools labels Jun 7, 2019
@cladmi cladmi added this to the Release 2019.07 milestone Jun 7, 2019
Copy link
Copy Markdown
Member

@smlng smlng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First, to me memoized sounds odd, but that's subjective and non-blocking - I would use stored or memorized instead.

Second, I get the following errors when building in docker:

Building application "tests_build_system_utils" for "native" with MCU "native".

"make" -C /data/riotbuild/riotbase/boards/native
"make" -C /data/riotbuild/riotbase/boards/native/drivers
"make" -C /data/riotbuild/riotbase/core
"make" -C /data/riotbuild/riotbase/cpu/native
"make" -C /data/riotbuild/riotbase/cpu/native/periph
"make" -C /data/riotbuild/riotbase/cpu/native/vfs
"make" -C /data/riotbuild/riotbase/drivers
"make" -C /data/riotbuild/riotbase/drivers/periph_common
"make" -C /data/riotbuild/riotbase/sys
"make" -C /data/riotbuild/riotbase/sys/auto_init
   text	   data	    bss	    dec	    hex	filename
  22770	    568	  47652	  70990	  1154e	/data/riotbuild/riotbase/tests/build_system_utils/bin/native/tests_build_system_utils.elf
bash: line 0: test: 1560237203N: integer expression expected
ERROR: 1560237203N >= 1560237203N
make[1]: *** [test-exported-variables] Error 1
Command '/Library/Developer/CommandLineTools/usr/bin/make -C /Volumes/devel/github/smlng/RIOT/makefiles/utils -f test-variables.mk test-exported-variables' failed
bash: line 0: test: 1560237203N: integer expression expected
ERROR: 1560237203N >= 1560237203N
make[1]: *** [test-exported-variables] Error 1
make: *** [test-exported-variables] Error 1

Third: see comments.

# These functions should help replacing immediate evaluation and global 'export'


# Evaluate a deferred variable only on once on its first usage
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: double on, remove before once

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see that I piggybacked that change from a wip branch a bit too fast :D

# variable = $(call memoized,<variable>,<value>)
#
# Parameters
# variable: name on the variable you set
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: /on/of/



# Evaluate a deferred variable only on once on its first usage
# Uses after will be as if it was an immediate evaluation
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wording: better to read: # Uses after [that] will be as ...

@cladmi
Copy link
Copy Markdown
Contributor Author

cladmi commented Jun 11, 2019

Damn it does not support date +%s%N I will see what is available and does not require 1s sleep.

Memoized comes from the concept https://en.wikipedia.org/wiki/Memoization but I welcome alternative names.

@aabadie
Copy link
Copy Markdown
Contributor

aabadie commented Jun 11, 2019

Memoized comes from the concept https://en.wikipedia.org/wiki/Memoization but I welcome alternative names.

I think this name suits perfectly, no need for another one.

@cladmi
Copy link
Copy Markdown
Contributor Author

cladmi commented Jun 11, 2019

bash: line 0: test: 1560237203N: integer expression expected
ERROR: 1560237203N >= 1560237203N
make[1]: *** [test-exported-variables] Error 1
Command '/Library/Developer/CommandLineTools/usr/bin/make -C /Volumes/devel/github/smlng/RIOT/makefiles/utils -f test-variables.mk test-exported-variables' failed
bash: line 0: test: 1560237203N: integer expression expected

The error is not in docker but in osx.
I could replace it with date; sleep 1 in osx but will see if I find something better.

@cladmi
Copy link
Copy Markdown
Contributor Author

cladmi commented Jun 11, 2019

@smlng I updated to use python to give the timestamp in nanoseconds and it worked on OSx 17.7.0. Does it work for you too ?

@smlng
Copy link
Copy Markdown
Member

smlng commented Jun 11, 2019

yep, python works on macOS ... alternative would be to use gdate on macOS (and other non-Linux systems), but that's not available by default.

@cladmi
Copy link
Copy Markdown
Contributor Author

cladmi commented Jun 11, 2019

yep, python works on macOS ... alternative would be to use gdate on macOS (and other non-Linux systems), but that's not available by default.

Here it is just a test, not the all applications workflow, so I did not look for performance optimization. And it gives a solution without asking for uname and if gdate is there or not.

Copy link
Copy Markdown
Contributor

@jcarrano jcarrano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good addition.

@cladmi
Copy link
Copy Markdown
Contributor Author

cladmi commented Jun 27, 2019

@smlng do you agree with the updated changes ?

@smlng
Copy link
Copy Markdown
Member

smlng commented Jun 27, 2019

yep, pleas squash

@smlng
Copy link
Copy Markdown
Member

smlng commented Jun 27, 2019

please 😄

cladmi added 3 commits June 28, 2019 11:33
This allows exporting variables only for some target.
It will allow not exporting variables when not needed, and so prevent
unnecessary evaluation.
This allow deferring a variable evaluation to its usage but still
benefit from only evaluating it once on multiple uses.
Import utils functions in Makefile.include to allow using them.
@cladmi cladmi force-pushed the pr/make/variables_lazy_evaluation branch from 3cbafa3 to 25a1bc4 Compare June 28, 2019 09:35
@cladmi
Copy link
Copy Markdown
Contributor Author

cladmi commented Jun 28, 2019

Squashed.

@cladmi cladmi added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Jun 28, 2019
@jcarrano jcarrano merged commit 50ea0d8 into RIOT-OS:master Jun 28, 2019
@cladmi
Copy link
Copy Markdown
Contributor Author

cladmi commented Jul 1, 2019

Thank you for the review. We need to start using it to remove more exports.

@cladmi cladmi deleted the pr/make/variables_lazy_evaluation branch July 1, 2019 12:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: build system Area: Build system Area: tools Area: Supplementary tools CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Type: enhancement The issue suggests enhanceable parts / The PR enhances parts of the codebase / documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants