Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GNU Make recursively expanded variables examples

Could somebody provide a real-world example of using recursively expanded variables (REV)? In the docs or various blog posts people give only useless toy examples, like

foo = $(bar)
bar = $(ugh)
ugh = Huh?

I cannot find a real use for REV besides creating custom functions with $(call). I also found that in the past people were using REV to supply additional parameters to a compiler for specific targets but that trick is considered outdated now because GNU Make has target-specific variables.

like image 885
Alexander Gromnitsky Avatar asked Aug 29 '26 02:08

Alexander Gromnitsky


1 Answers

Both recursively expanded variables and simply expanded variables recurse their expansions. The major difference is when that expansion happens.

So your example above works just fine with simply expanded variables if you invert the assignments:

ugh := Huh?
bar := $(ugh)
foo := $(bar)

So the major thing that recursively expanded variables get you is the freedom to assign values in whatever order you need (which means you don't need to worry about inclusion order for included makefiles, etc.).

In a project at work we have a dozen or so included makefiles that have inter-dependent relationships. These are expressed through the usage of known-format variable names (e.g. module A generates an A_provides variable, etc.) Modules that need to utilize the things that module A provides can then list $(A_provides) in their makefiles.

With simply expanded variables (which we had been using until somewhat recently) this meant that the inclusion of the generated makefiles required a manually sorted order to force the inclusion of assigning makefiles before consuming makefiles (into other variables).

With recursively expanded variables this order does not matter. (This would not be the case if the variables were used in any immediately evaluated context in these makefiles but luckily they are not, they only set variables that are used later in the main makefiles.)

like image 121
Etan Reisner Avatar answered Aug 31 '26 08:08

Etan Reisner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!