# How to test for set env variables without getting warnings

**URL:** <https://discourse.cmake.org/t/how-to-test-for-set-env-variables-without-getting-warnings/11401>\
**Category:** Code\
**Created:** [August 7, 2024, 7:38am UTC](https://discourse.cmake.org/t/how-to-test-for-set-env-variables-without-getting-warnings/11401 "2024-08-07T07:38:26Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![nolange](https://discourse.cmake.org/user_avatar/discourse.cmake.org/nolange/32/4839_2.png) [@nolange](https://discourse.cmake.org/u/nolange)\
**Post date:** [August 7, 2024, 7:38am UTC](https://discourse.cmake.org/t/how-to-test-for-set-env-variables-without-getting-warnings/11401/1 "2024-08-07T07:38:26Z")

</div>

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

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

```

the warning is:

```auto
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.

---

<div class="post-metadata">

**Author:** ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)\
**Post date:** [August 7, 2024, 10:42pm UTC](https://discourse.cmake.org/t/how-to-test-for-set-env-variables-without-getting-warnings/11401/2 "2024-08-07T22:42:48Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![nolange](https://discourse.cmake.org/user_avatar/discourse.cmake.org/nolange/32/4839_2.png) [@nolange](https://discourse.cmake.org/u/nolange)\
**Post date:** [August 14, 2024, 1:38pm UTC](https://discourse.cmake.org/t/how-to-test-for-set-env-variables-without-getting-warnings/11401/3 "2024-08-14T13:38:47Z")

</div>

Nope

---

<div class="post-metadata">

**Author:** ![Aaron\_Cohen](https://discourse.cmake.org/user_avatar/discourse.cmake.org/aaron_cohen/32/4831_2.png) [@Aaron\_Cohen](https://discourse.cmake.org/u/Aaron_Cohen)\
**Post date:** [August 14, 2024, 1:50pm UTC](https://discourse.cmake.org/t/how-to-test-for-set-env-variables-without-getting-warnings/11401/4 "2024-08-14T13:50:55Z")

</div>

What about:

if(CMAKE\_HOST\_WIN32 AND NOT DEFINED ENV{ProgramFiles}) set(\_extra\_path PATHS “$ENV{ProgramFiles}/LLVM/bin”) endif()

---

<div class="post-metadata">

**Author:** ![nolange](https://discourse.cmake.org/user_avatar/discourse.cmake.org/nolange/32/4839_2.png) [@nolange](https://discourse.cmake.org/u/nolange)\
**Post date:** [August 14, 2024, 3:38pm UTC](https://discourse.cmake.org/t/how-to-test-for-set-env-variables-without-getting-warnings/11401/5 "2024-08-14T15:38:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)\
**Post date:** [August 15, 2024, 7:29am UTC](https://discourse.cmake.org/t/how-to-test-for-set-env-variables-without-getting-warnings/11401/6 "2024-08-15T07:29:51Z")

</div>

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:

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

```

---

<div class="post-metadata">

**Author:** ![scivision](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/a87d85/32.png) [@scivision](https://discourse.cmake.org/u/scivision)\
**Post date:** [August 15, 2024, 12:23pm UTC](https://discourse.cmake.org/t/how-to-test-for-set-env-variables-without-getting-warnings/11401/7 "2024-08-15T12:23:00Z")

</div>

Correct, if() is not short circuit

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)\
**Post date:** [July 25, 2025, 2:08pm UTC](https://discourse.cmake.org/t/how-to-test-for-set-env-variables-without-getting-warnings/11401/8 "2025-07-25T14:08:18Z")

</div>

I’m pretty sure it does short circuit the predicate evaluations. What is _not_ short-circuited is the variable evaluations literally present in the argument list. So `$ENV{ProgramFiles}` is evaluated (which is what fires the warning), but the `STREQUAL` is not.
