Skip to content

set(ENV{FOO} "") does not create new environment variable

If an environment variable FOO does not exist yet, set(ENV{FOO} "") does not export FOO.

I want to create an environmental variable FOO with empty value, and export it to control the behavior of a child process. But if FOO does not exist yet, set(ENV{FOO} "") does not export FOO to the environment for the child.

Suppose the following shell script checkFOO and cmake script envtest.cmake exist in the current directory:

#!/bin/sh
# filename: checkFOO
if env | grep -q FOO; then
    echo "FOO exists"
else
    echo "FOO not exists"
fi
# filename: envtest.cmake
set(ENV{FOO} "")
execute_process(COMMAND "./checkFOO")

Then running cmake -P envtest.cmake outputs "FOO not exists". If envtest.cmake is replaced by

set(ENV{FOO} "fake value")
set(ENV{FOO} "")
execute_process(COMMAND "./checkFOO")

Then it outputs "FOO exists". Is this an intended behavior?

To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information