The behavior of if(something)
continues to be a source of ongoing gotchas, even with CMP0054 set to NEW. Consider this code sample (run it with cmake -P <filename>
):
cmake_minimum_required(VERSION 3.21)
# Show this just to confirm PATH is not empty
message("PATH = $ENV{PATH}")
# Should always print "Defined" because PATH will always be in the environment
if(DEFINED ENV{PATH})
message("Defined")
else()
message("Not defined")
endif()
# This always prints False <----- probably unexpected
if(ENV{PATH})
message("True")
else()
message("False")
endif()
# This always prints True
set(someVar "$ENV{PATH}")
if(someVar)
message("True")
else()
message("False")
endif()
As noted in the comments, if(ENV{PATH})
always evaluates to false. The relevant part of the docs for the if()
command state the following:
if(<variable|string>)
True if given a variable that is defined to a value that is not a false constant. False otherwise. (Note macro arguments are not variables.)
Technically speaking, ENV{PATH}
is not the name of a CMake variable, but it kinda feels like it should be treated as such. Nevertheless, we’ve had the current behavior for a long time and introducing yet another policy to change it is probably only going to add to the confusion for developers.
I’m mostly pointing this out after seeing a code review that fell victim to this. At the very least, we should highlight this specific case in the docs (which I’m happy to do), but thought it worth bringing up here for discussion first in case others feel like we should do something about it.