Environment Variables in Tupfiles
Is it possible to access environment variables inside Tupfiles? This need comes from referencing run command scripts. For example, if my environment variable name is SCRIPT_DIR, I'd like to be able to write a Tupfile like:
run $(SCRIPT_DIR)/my_script.py
Perhaps Tup already supports this, but I scoured the manual and couldn't find it.
A thought: what about adding and import command? export takes and environment variable and lets a script/command use it, import would then take an environment variable and let the Tupfile use it.
On Tue, Apr 16, 2013 at 4:24 PM, Brad Campbell [email protected]:
Is it possible to access environment variables inside Tupfiles? This need comes from referencing run command scripts. For example, if my environment variable name is SCRIPT_DIR, I'd like to be able to write a Tupfile like:
run $(SCRIPT_DIR)/my_script.py
Perhaps Tup already supports this, but I scoured the manual and couldn't find it.
— Reply to this email directly or view it on GitHubhttps://github.com/gittup/tup/issues/103 .
No, this isn't currently possible. Can you explain why you'd want to have an environment variable pick the script location rather than have a single script that potentially uses an environment variable? Keep in mind that environment variables can be undefined, so in your example if the user doesn't have SCRIPT_DIR defined, it will try to run /my_script.py. In general, relying on variables to be defined is a bad idea.
-Mike
I have a somewhat specific example of when this would have been useful for me:
I primarily to do embedded development, and for better or worse, that means I generally have several copies of toolchains. Unfortunately, the means of selecting a toolchain requires picking between arm-linux-none-gnueabi (Ubuntu), arm-linux-none-gnu (Fedora), arm-none-eabi (CodeSourcery), etc etc, that is, you can't rely on there being arm-gcc in your $PATH, nor can you use $PATH to select which compiler you'd like in use in your system.
The result is that the name of the arm compiler is machine-specific, not project-specific, and setting an environment variable $(ARM_CC) is very useful. Most of my build scripts search first for ARM_CC and then fail back to searching for a list of known toolchains. The point is that configuring the compiler every time I check out a project on a new machine would be annoying.
Environment variables solve the problem of machine-specific project-independent configuration. Perhaps not the most common need, but it does exist.
I am using such behaviour at work to define in a machine independent manner the location of the compilers, debuggers, and other such things that could installed in various places on different machines. Our project has support for multiple HW platforms and multiple compilers, and adding all of them in the PATH is not possible since different versions of the same compiler will, most likely have the same file name.
Please also note that the default GNU make behaviour is to inherit all environment variables and act as if they were passed to the build system (unless they are overrides from the command line), and this is a huge bonus for GNU make for us at the moment.
AFAIK, there's not an easy way to hook getenv, so tup would not be able to track which commands actually use which environment variables. In order to ensure correctness then, tup would have to re-run any time any environment variable changed, which could quickly become very obnoxious. I imagine this was the original motivation for running tup commands in a "clean" environment.
That said, I think the import command suggestion is a nice compromise. It allows tup to explicitly use and track environment variables that are needed / useful.
Extra bonus points if someone can come up with a clean syntax for "import"ing environment variables for only certain commands---it would be nice to limit the impact of environment variable changes to only the commands that really needed to be re-run.
@ppannuto How about this format?
: *.o @ CC |> $(CC) %f $(CFLAGS) $(EXTRAFLAGS) -o %o |> program
But I just realised that one major feature of make regarding environment variables is that they override the ones in the makefile, if they are defined, so if CC=~/arm-unknown-linux-uclibcgnueabi/bin/arm-unknown-linux-uclibcgnueabi-gcc in the environment and there is a definition in the makefile like cc=gcc, then the value used is the one dictated by the environment variable.
The rule of priority is: override in makefile > command line > environment > makefile definitions
For the feature to be useful, I think tup should provide the same mechanism.
PS: We use this mechanism extensively at my workplace.
I think you're referring to the:
$ make CC=special_gcc
syntax? But that's really not necessary if tup properly tracks the environment variables. You can simply run something like
$ CC=special_gcc tup upd
to set the environment variable for the duration of that command (like any shell command). And if tup imports the CC variable from the environment, then it will do the RightThing.
As for syntax, the @ symbol refers to tup config variables, so that could be confusing. Maybe we could do something more like:
: *.o |> $$(CC) %f $(CFLAGS) $(EXTRAFLAGS) -o %o |> program
to reference and environment variable (and use \$ to escape a $ if necessary instead of make's $$)
We could also consider allowing a default value if an environment variable isn't set, e.g.:
: *.o |> $$(CC,gcc) %f $(CFLAGS) $(EXTRAFLAGS) -o %o |> program
I like the more explicit import. import would set a tup variable to the value of the environment variable if the environment variable exists. So you could do this:
import CC
ifndef CC
CC=gcc
endif
COMPILER = $(CC)
: *.o |> $(COMPILER) %f $(CFLAGS) $(EXTRAFLAGS) -o %o |> program
import would then add that environment variable to tup's watch list and if it changed the tupfile would be reparsed. This way it is clear where CC is getting set from. But, it's a little verbose. To expand this, I propose:
import <environment variable> [as <variable name>] [with default <default variable setting>]
so that you could do
import CC as COMPILER with default gcc
I think that takes care of handling using environment variables in the most sane way. It also nicely mirrors the export command.
@bradjc : About this:
import CC ifndef CC CC=gcc endif COMPILER = $(CC)
: *.o |> $(COMPILER) %f $(CFLAGS) $(EXTRAFLAGS) -o %o |> program
That's a lot of wording for a simple thing like that. It looks like Microsoft's nmake stupid rules which, by default, overwrites the variable values set in the environment with the ones in the script in a totally back-asswards manner.
It should be something as simple as Make's syntax:
CC=gcc
program: %.o
$(CC) $^ $(CFLAGS) $(EXTRAFLAGS) -o $@
So, IMHO, it should be something as simple as this:
import CC
CC=gcc
: *.o |> $(CC) %f $(CFLAGS) $(EXTRAFLAGS) -o %o |> program
which could have the alternate form:
import CC=gcc
: *.o |> $(CC) %f $(CFLAGS) $(EXTRAFLAGS) -o %o |> program
I don't see why you'd need any special syntax in the rule itself. The information is already there, you already know you have to expand CC in the rule, so if you mentioned an imported variable in the rule, tup should figure out the target is invalid when the environment variable has changed. I think that needs to be done is to record the state of the environment var just after the import and add it as a dependency to the rules using it...
Or maybe the expanded rule itself should be recorded, so when that changes from a build to the next, the environment variable is already taken into account, if necessary.
@ppannuto:
I think you're referring to the:
$ make CC=special_gcc
syntax? But that's really not necessary if tup properly tracks the environment variables. You can simply run something like
$ CC=special_gcc tup upd
to set the environment variable for the duration of that command (like any shell command). And if tup imports the CC variable from the environment, then it will do the RightThing.
This is not OK. This only works in *nix shell. The make syntax works everywhere.
Also, the syntax make var1=bla1 var2=bla2 ... is used to parametrize very large scale builds so they produce the results only for the subset defined by the variables in the command line. This could mean that even if the build system could produce a grand total of 1000 elfs for a testing phase for a project running on a microcontroller and a script that loads and executes the elfs on the target system, when running with parameters, the script would contain instructions to run only a subset of, let's say, 20 elfs, so the developer can focus on bugs and issues where they apply.
I happen to maintain such large build systems and some of them can be parametrized even with 10 parameters due to the size of the results.
Maybe I am thinking too much in terms of GNU make, but the principle
command line > environment variables > script defaults
makes a lot of sense to me and, in practice, if the build system does NOT work in this way, there is a lot of extra work that must be done just to try to emulate this, or can even be impossible (if the make-like utility does not import environment variables at all, or imports only a fixed subset of them, which is entirely stupid, because it means you can't influence the build in a sane manner in distributed building system, multi-version compiler systems and so on).
I just realised, I don't think you need any syntax at all (or I am missing some point entirely).
Think about these rules:
CC=gcc
: *.o |> $(CC) %f $(CFLAGS) $(EXTRAFLAGS) -o %o |> program
If on every variable initialization, like the CC variable here, we probe the environment to see if such a variable is set there, and if it is, we ignore the default value, but we can the read value and record it at this point.
Second, in the rule, because the variable itself is used, we know there is a dependency on the variable which we recorded and we check if it has been changed since the last run (or since initialization).
OTOH, I think environment variable are just another type of dependency, which is visible for such cases:
CC=gcc
DISTCC=gcc
ifdef DEBUG
EXTRAFLAGS += -g -DDEBUGLEVEL=$(DEBUG)
endif
: *.c |> $(DISTCC) %f $(CFLAGS) $(EXTRAFLAGS) -o %o |> program
There are a few things here:
- DISTCC is indirectly dependent on the environment (which could be stored a dependency of CC)
- EXTRAFLAGS is initialized or augmented in the ifdef section if the environment/command line specifies DEBUG
So I think the recording the expanded command line in the rule is necessary.
I do not think the goal is just to recreate make but instead use the word tup. I don't think that having variables that just magically get set by environment variables is a good design choice. It also isn't clear which variables the tupfile creator intended the user to be able to override. Tup is all about being very explicit about input and output files and I think that attention to explicitness should be continued here.
The goals for using environment variables in tupfiles that we came up with were:
- Short (one line only)
- Explicit (clear as to what the tupfile writer was intending to someone who isn't an expert in tup)
- Ability to set the variable name different from the environment variable name
- Ability to have a default option
- Fit in with the existing tup syntax
We considered something like:
COMPILER = env(CC)
or
COMPILER = env(CC,gcc)
because that uses the existing "create a variable and assign it a value syntax". But setting the default is weird because it looks like you are requesting gcc from the environment somehow.
I really like the symmetry that import provides w.r.t the existing export. However there is no obvious way to set defaults or a custom name without being verbose like I suggested above:
import CC as COMPILER with default gcc
But it gets the job done and makes it very clear when we are leaving the tupfile to get data from the user's environment.
Also, w.r.t. your comment above about the make var1=bla1 var2=bla2 ... syntax being used to parametrize builds, if I understand your goal, the "tup way" to do that is with variants. I realize that variants do not yet work on windows, but the solution is to make variants work on windows, not work around it with an otherwise unneeded feature.
@bradjc: Not everything about make is bad. And deciding against some solution because make adopted it is not a good decision making process.
The usual expectation from any user is that local environment variables do influence applications that are ran on the system.
There are good reasons why it makes sense to treat definitions in the Tupfile as defaults that can be overridden by environment variables.
All this discussion is about what should the default be, and which option makes more sense.
First of all, influencing the build without needing to edit the files, just because some developer didn't think it made sense to be able to have a different compiler or different compiler options, pick up another symbol extractor than nm, other autotools set, other anything than what that arbitrary developer picked, leads to extra work and is an unexpected behaviour.
The worse thing is that the developer might have not even been aware they should have to do something to have the expected behaviour happen.
Second, it is a bad idea to think one can guess all future uses and variants of building a software, so asking ahead of time to be able to decide which variables are to be imported and which not, is a very bad design decision.
Third, if a developer has on a machine some variables set in the environment is because there was a conscious decision made by the developer to have that environment set that way, and the purpose was that it would influence all things happening on that machine, not that they would be ignored because a third party didn't think it would be useful or wasn't aware they had to think in advance of all possible future uses of all the future users of the build system.
Maybe I use a compiler cache which is not in the path, but the build system would be as slow as a snail because choosing other filename than gcc was not enabled by another person.
And yes, there are reasons not to put everything in path, multiple compiler versions, multiple compilers, multiple tools, none of which would make sense to be default.
Also, the fact is that not only compilers can be non-default, paths, tools, options, executable names etc.
Requiring to touch the code to have a functionality that is expected is not scalable. I have under maintenance, as I said before, build systems which have teens of parameters that customize their behaviour, and I might know or even discover at some point that for a system certain combinations don't make sense or work, so I set that system's environment accordingly. Requiring to touch the code just to accommodate that single machine might not be possible or even acceptable (close to release, third party software that can't be modified because of the license or internal policies, no commit access is available, etc.).
Taking into account the environment is predictable, and I don't think there are systems where changes in the environment appear from second to second or without good reason.
OTOH, I don't see any good reason why would the default policy of tup should be against what is usually expected, that the local environment takes precedence over somebody else's defaults. The local admin is, after all, the only person capable of deciding what defaults make sense locally in spite of what somebody else judgement was at a prior time in a different setting.
What would make sense instead would be to print the information about the source of the variables in the debug mode of tup.
Also, w.r.t. your comment above about the make var1=bla1 var2=bla2 ... syntax being used to parametrize builds, if I understand your goal, the "tup way" to do that is with variants. I realize that variants do not yet work on windows, but the solution is to make variants work on windows, not work around it with an otherwise unneeded feature.
I understand the way tup works, but there are 2 issues, other than windows variants not working:
- the complete build has to have a number of variants in the range 500-5000 which differ so much because the code is configured statically with a configuration generator, so changing a header file might trigger a rebuild of between 10% to 100% of the 10000-100000 object files and relinking of al those variants when I know that my changes are supposed to fix an issue in some feature which is used only in 10 of them, so at the time of the fix it does not make sense to rebuild everything since none of those 4990 elfs would be ran, and the complete revalidation with built will be done at a later moment
- the requirement to have all variants on the topmost level is also not scalable since: a) many file systems don't cope well with thousands of entries in a single directory and b) for a system such as ours where we support from the same code base 12 platforms with 3 compilers each, it does not make sense to build everything since that would mean 3000 elfs (on average) * 12 platforms * 3 compilers = 108.000 of variants and over 2 000 000 object files in any build.
I hope it is more clear now what kind of scale of issues we are talking about and why parametrized incomplete builds are a necessary feature, IMHO.
This is nice in theory, but unfortunately, this line is rarely true in practice:
Taking into account the environment is predictable, and I don't think there are systems where changes in the environment appear from second to second or without good reason.
The environment is often not predictable. Many other misguided tools {ab}use environment variables (at least they often have the common sense to prefix them, so you end up with FOOLISH_PROGRAM_LAST_RUN_TIME=1377294967), but the point is that depending on your system, your environment can change frequently.
As I pointed out earlier, there's no easy way for tup to hook getenv to validate which environment variables are used. If the environment as-is was made available to subprograms, things would have to be rebuilt entirely too often to guarantee correctness.
Now, with that out of the way, I think there are really two discussions here: 1.) The environment that tup commands are run in 2.) The variables available to tup when parsing Tupfiles
Hopefully, we can agree for reasons above that (1) must be a sanitary environment, and variables must be explicitly exported via the existing mechanism.
The issue then is (2), what variables form the local environment are available to tup as it's parsing tupfiles. Currently the answer is "none of them". It seems @bradjc is taking that opinion that you should explicitly list the set of environment variables you would like to "import" into the tup environment, while @eddyp is arguing that all of the environment variables should be implicitly available, and tup should detect whenever an environment variable is referenced and automatically track it.
I think either of these two approaches would work, however I am inclined to lean towards @bradjc, and I tend to favor "explicit over implicit" as a general rule. From an implementation standpoint, there isn't a great difference, when an undefined variable is referenced, it could be implicitly considered an environment variable (as if a @bradjc-esque import line with no default value had been placed right before it). That said, I think such an implicit mechanism would like to tup being too forgiving of things like typos --- I'd rather it error out with "undefined variable $(LFLAGS)" than make me debug when my linking is broken
... my 2c -pat
re parameterized builds: You don't have to update the world with tup. It can take a list of the specific variants (or targets) you'd like to rebuild. If you know that only 10 of your 1000 builds are affected, just pass that list to tup.
re nested variants: I've asked for those before too :). IIRC, Mike's answer there was: (paraphrasing) "that sounds do-able, but hard.. pull requests welcome".
@ppannuto According to the principle of least surprise any command run on the command line should work identically in the build system. So if my command works in by hand, pasted into tup it should work, too, by default.
But if by default tup does not propagate the environment where itself is ran to the commands it runs, then you are likely to get different behavior.
One such example is one of my build system where several tools can locate their licenses, libraries and home directories through the environment variables set when they were installed.
So your build fails by default with the same identical commands that you tested they worked in the command line. I can't even imagine the count of cocked heads this behaviour would generate.
I agree that environment variable cleanup should be possible, but only if required explicitly, while the default should be to propagate everything according to the principle of least surprise.
@ppannuto: regarding a abused variables, I think you forgot that my proposal was to:
- take into account as build dependency the variables which are used in the Tupfile
- declare as explicit dependency environment variables that are not used/declared in the Tupfile, but, if changed, should invalidate the rule where is specified as dependency
So taking into account these two facts, the abused environ var will NOT invalidate the build, unless you use it in the Tupfile, or if you declared it as an explicit dependency, which is exactly what should happen.