Setting environment variables in a batch file called using execute_process.

Say I have a batch file that looks like this.

set varA=valueA

I want my CMakeLists.txt file to make use of this environment variable with the syntax provided by CMake.

$ENV{varA}

Before that can be done, I need to run the batch script to set that environment variable. So, I run it using execute_process like so.

execute_process(COMMAND var.bat)
# varA should now be set to the value specified in var.bat.
add_executable($ENV{varA})

This does not work. Running the batch script using execute_process does not update the environment variable. Is it necessary to run the batch script before invoking CMake or is there something I am missing that makes this work?

execute_process starts a new process that sets the environment variable for itself. There’s no way for the child process to set environment variables for the parent (which is what you’re trying to do here).

So yes - you have to set the environment before running CMake.

Other possibility would be for your bat script to print the desired environment variables so that you could capture that output and process however you want.