How to test for set env variables without getting warnings

I have trouble silencing warnings, the code in question is this:

if(CMAKE_HOST_WIN32 AND NOT $ENV{ProgramFiles} STREQUAL "")
  set(_extra_path PATHS "$ENV{ProgramFiles}/LLVM/bin")
endif()

the warning is:

CMake Warning (dev) at cmake/tools-clang.cmake:4 (if):
  uninitialized variable 'ProgramFiles'

Note that even CMAKE_HOST_WIN32 is false, and I would like to support VMake versions before 3.14 so DEFINED ENV{ProgramFiles} is no option.

Does the behavior change if you put quotes around the $ENV{ProgramFiles}?

if(CMAKE_HOST_WIN32 AND NOT "$ENV{ProgramFiles}" STREQUAL "")
  set(_extra_path PATHS "$ENV{ProgramFiles}/LLVM/bin")
endif()

Nope

What about:

if(CMAKE_HOST_WIN32 AND NOT DEFINED ENV{ProgramFiles}) set(_extra_path PATHS “$ENV{ProgramFiles}/LLVM/bin”) endif()

Doesn’t work with Versions before 3.14, as I said in the opening post.

From what I recall, CMake does not short-circuit the expression in an if() command. So if you have if(expr1 AND expr2), and expr1 is false, it will still evaluate expr2. If you were trying to use CMAKE_HOST_WIN32 to prevent evaluation of $ENV{ProgramFiles}, that’s not going to work as a single expression. You would have to write it as two separate commands:

if(CMAKE_HOST_WIN32)
    if(NOT "$ENV{ProgramFiles}" STREQUAL "")
        set(_extra_path PATHS "$ENV{ProgramFiles}/LLVM/bin")
    endif()
endif()

Correct, if() is not short circuit