0

Intel's OneAPI distribution (which includes many components such as ICC) has a script named setvars.sh, which checks for the presence of various stuff, then runs components' individual scripts for setting environment variables. It takes over a second to run and prints some stuff to the terminal.

Now, I'm willing to run it once, but I certainly don't want to run it whenever I start a shell. How can I "cache" its results, in the form of the delta it adds (or removes?) from my environment before its invocation? So that I could just source a script with:

export PATH="whatever:$PATH"
export TBBROOT="whatever"
# etc. etc.

?

Naturally, I would need to re-run setvars.sh if my system configuration changed in any way, but I'm ok with that.


PS - It has to be said: OneAPI is the epitome of mis-naming. It's the never-going-to-be-used-by-anybody-other-than-us, one-of-many API.

einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • How does it normally set vars in the parent shell's environment? Do you have to `source` it rather than running it and `eval`ing the output? If the former, probably you can just capture the result of `set` or `env` before/after and take a diff to see what env vars it set, and adapt that into something simple. `set`'s output includes shell functions, not just env vars, at least in bash. – Peter Cordes Dec 09 '21 at 10:10
  • @PeterCordes: You have to source it. It would have been much nicer and robust of them to produce a script you can `eval`, but they don't do that... :-( – einpoklum Dec 09 '21 at 10:20

1 Answers1

1

Here's how to do it (bash assumed, this will work for other shells mutatis mutandis):

  1. Disable sourcing setvars.sh in your .bashrc, .bash_profile and whatever you run on login.
  2. After logging in and being in a bash session, execute env > /tmp/env_before.txt
  3. Source the setvars.sh script.
  4. execute env > /tmp/env_after.txt
  5. execute diff /tmp/env_before.txt /tmp/env_after.txt /tmp/precache.diff

Now, all the lines which have been added in the diff are new environment variable which were not set before. You can cache them as

export FOO="bar"

lines (make sure to add the parentheses, just to be on the safe side, if they're missing).

the lines which have been changed are changes to existing environment variables. Those will be very few and maybe even just $PATH. For those, your cache needs lines such as:

export PATH="/bunch/of:/intel/stuff:$PATH"

which prepends the Intel OneAPI entries to existing ones. You will need to check whether any entries you had before were actually removed, but I doubt this will happen. Anyway, do these lines manually; the other lines can probably be transformed all at once with a regexp.

The result is a shell script containing a few dozen export commands. Source that instead of setvars.sh, but remember to re-generate it whenever your system configuration / directory structure changes and when you update versions.

einpoklum
  • 118,144
  • 57
  • 340
  • 684