How do I use CMake presets environment variables in the CMake build step?

223 views Asked by At

I work with embedded software in Linux. Our vendor's compiler and linker require certain environment variables to be defined when building the binary.

What we do now is that we run a script in a shell to define these environment variables for us, then we run CMake in the same shell to build.

I started using cmake-presets(7) in the hope that I could define the environment variables under the "environment" key instead of running a script, like so:

// CMakePresets.json
{
  "version": 4,
  "configurePresets": [
    {
      "name": "default"
      "environment": {
        "FOO": "some-value",
        "BAR": "some-other-value"
      }
    }
  ]
}

This works well for the CMake configure step (which requires the variables to be defined):

$ cmake --preset default -S /path/to/project -B /path/to/project/build
Preset environment variables:

  FOO="some-value"
  BAR="some-other-value"

(...)

Build files have been written to: /path/to/project/build

However, the environment variables are no longer defined when building the project:

$ cmake --build /path/to/project/build --target all
[  0%] Building CXX...
(...)
[  1%] Linking C static library libBaz.a
[vendor-linker] Error: FOO and BAR are undefined

Why are the environment variables only defined in the configure step? How can I propagate them to the build step?

I apologize for being unable to create a minimum working example. I don't know how to make a linker/compiler that throws an error when an env variable is undefined.

0

There are 0 answers