4

When using CMakePresets.json the documentation states that I can use either $env{<variable-name>} or $penv{<variable-name>} to query environment variables.

Is it possible to specify a default value in case the environment variable is not set?

Note:

  • Since the variable that I am trying to set is also inside the presets, I can't handle the issue inside a CMake script.
  • If default values are not supported is there any workaround with which I could achieve the same inside the presets file?
cbandera
  • 105
  • 9
  • If you're still wondering about this, I don't have an answer, but I'm interested in hearing more about your use-case and trying to brainstorm workarounds. what are you trying to set? what's the environment variable? In the particular case of trying to set a cache variable, it probably wouldn't be too difficult to come up with a workaround. – starball Aug 09 '22 at 23:13
  • The background was trying to place as much information as possible into the CMakePresets in order to use it as a single source of truth for delivering the same look&feel through console and the "CMake Tools" plugin for VSCode. In the cli I use direnv to make sure some env vars are set. However the VSCode plugin runs in a different environment, so it won't have these env vars set and consequently configures the CMake project differently. Trying to provide a workaround for unset env vars was workaround to fix this.. but I guess the best solution would be to handle it differently from the start – cbandera Aug 14 '22 at 12:54
  • I have never used direnv before, but have you tried using a [vscode direnv plugin](https://marketplace.visualstudio.com/items?itemName=Rubymaniac.vscode-direnv)? Not sure if the env vars will get passed on to VS Code but it's worth a shot. – starball Aug 14 '22 at 17:52
  • Also, you should be able to set defaults in the CMakeLists.txt file instead: check if an environment variable is defined, and if not, [set it](https://cmake.org/cmake/help/latest/command/set.html#set-environment-variable). But it's important to clarify here whether you only want to _define_ these env variables in the presets, or if you actually need to _use_ their values to define other fields of the presets (in which case this solution doesn't work). – starball Aug 14 '22 at 17:57
  • Self-correction. I just realized you said "_Since the variable that I am trying to set is also inside the presets, I can't handle the issue inside a CMake script._". Do you mean that you want to use the environment variable to set the value of a cache variable in the presets file? In that case, is there a reason you can't just set the cache variable inside you CMakeLists.txt file and also handle setting a default value to the cache and env variables in the CMakeLists.txt file? – starball Oct 27 '22 at 04:36

0 Answers0