1

I am trying to move from an own written tool to cmake for Windows to simplify our build environment.

We have a tool in the build process that requires to have set environment variables. I would like to set them with cmake but don't know how. What I have tried:

set(ENV{MYENVVAR} "My-Content")

I would expect that I can read this environment variable again with

message($ENV{MYENVVAR})

But this does not work. Even setting a variable before invoking cmake (instead of setting it in cmake) does not help, I cant read the variable in cmake.

What do I do wrong?

Edit:

  • I do invoke cmake again after changing CMakeLists.exe.
  • I do set environment variables with set MYENVVAR=My-Content in Windows cmd.exe and then invoke cmake from exactly this console.
  • cmake is version 3.20.0 for Windows.
Zoe
  • 27,060
  • 21
  • 118
  • 148
Teddy
  • 993
  • 10
  • 20
  • 1
    "Even setting a variable before invoking cmake (instead of setting it in cmake) does not help, I cant read the variable in cmake." - CMake definitely is able to read environment variable. If `$ENV{MYENVVAR}` is empty, then either you don't set an environment variable or you set environment variable for some other process. As for setting environment variable in CMake, it definitely works too. But this setting is active only during the configuration process (When `CMakeLists.txt` is processed). You need to be more detailed about your problem. – Tsyvarev May 20 '21 at 14:12
  • @Tsyvarev: What do you meen with "But this setting is active only during the configuration process"? I invoke cmake, then it should be available (and therefore be visible in message), but it is not available anymore when I call make afterwards? – Teddy May 20 '21 at 14:30
  • 2
    `I invoke cmake, then it should be available (and therefore be visible in message), but it is not available anymore when I call make afterwards?` Yes. – KamilCuk May 20 '21 at 14:33
  • 1
    https://stackoverflow.com/questions/35029277/how-to-modify-environment-variables-passed-to-custom-cmake-target This is answer your question? https://stackoverflow.com/questions/44047611/how-to-set-environment-variable-for-build-time-in-cmake https://stackoverflow.com/questions/46266649/cmake-set-environment-variable – KamilCuk May 23 '21 at 08:11

1 Answers1

2

You're going to need to produce a minimal, reproducible example. Here's my best attempt based on your question and unfortunately, the best I can say is "works for me":

alex@alex-ubuntu:~$ export MYENVVAR=FooBarBaz
alex@alex-ubuntu:~$ cat test.cmake 
cmake_minimum_required(VERSION 3.20)

message($ENV{MYENVVAR})

set(ENV{MYENVVAR} "My-Content")
message($ENV{MYENVVAR})

alex@alex-ubuntu:~$ cmake -P test.cmake 
FooBarBaz
My-Content
alex@alex-ubuntu:~$ echo $MYENVVAR 
FooBarBaz

So CMake can both read the environment variable and write to it in-process, but it does not affect the controlling process's (in this case bash; in your case cmd.exe) environment.

Alex Reinking
  • 16,724
  • 5
  • 52
  • 86