Skip to content

if("non empty string value") is false

Hello,

The documentation says this about if(string):

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.)

But it seems like I'm getting some different behavior. I'm not sure if this is a bug or some weird edge case.

Test code:

project(testproj)
cmake_minimum_required(VERSION 3.17)

if("tomato")
    message("\"tomato\" is true")
endif()

if(NOT "tomato")
    message("NOT \"tomato\" is true")
endif()

if(tomato)
    message("tomato is true")
endif()

if(NOT tomato)
    message("NOT tomato is true")
endif()

message("")
set(tomato 85)
message("set tomato")

if("tomato")
    message("\"tomato\" is true")
endif()

if(NOT "tomato")
    message("NOT \"tomato\" is true")
endif()

if(tomato)
    message("tomato is true")
endif()

if(NOT tomato)
    message("NOT tomato is true")
endif()

Which gives the output:

NOT "tomato" is true
NOT tomato is true

set tomato
NOT "tomato" is true
tomato is true

My interpretation of this:

  • Since cmake does occasionally treat value and "value" the same, at first I thought if("tomato") and if(tomato) were equivalent, but it appears that this is not the case; I'd like a bit more information on this if somebody has time.
  • I am quite confused as to why "non empty string" evaluates to false, though. This is what made me want to report this as a bug, because it seems to deviate from what the docs say.

Hypothetically if "tomato" was evaluated as a constant, the documentation says:

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.)

If this is a bug, I'm using a fairly recent version I built myself, cmake version 3.17.20200309-gefa3002

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