Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I set a CMake variable to the result of a boolean expression?

Consider the following CMake code:

if(VAR_1 STREQUAL VAR_2)
    set(VAR_3 ON)
else()
    set(VAR_3 OFF)
endif()

I want to write the same thing, but as a one-liner. I don't particularly care which boolean representation VAR_3 ends up using. So what I need is to set it to the result of the evaluation of a boolean expression.

Now, this doesn't work:

set(VAR_3 (VAR_1 STREQUAL VAR_2) )

nor does this:

set(VAR_3 (${VAR_1} STREQUAL ${VAR_2}) )

Rather, they get me something like ;ValueOfVar1Here;STREQUAL;ValueOfVar2Here; - which isn't what I want.

Can I get the evaluation I want somehow?

like image 547
einpoklum Avatar asked Oct 15 '22 02:10

einpoklum


1 Answers

No you can't. There is one cmake command on one line only. Each cmake script command invokation has to end with a ) (followed optionally by a comment) and followed by a newline (see cmake doc about source files). You can still write a macro that does what you want.

macro(assign_me_bool var)
     if(${ARGN})
         set(${var} ON)
     else()
         set(${var} OFF)
     endif()
endmacro()

assign_me_bool(VAR_3 VAR_1 STREQUAL VAR_2)
like image 188
KamilCuk Avatar answered Oct 21 '22 08:10

KamilCuk