Assigning a CMake ENDIF Expression

In CMake, the ELSE and ENDIF control functions accept expressions as arguments. This is optional according to the documentation. What is their purpose? Should you just make the original IF expression more understandable for maintenance purposes or provide some features?

+6
source share
4 answers

These expressions are optional, as you said, and they are useful when you have nested if() . cmake will warn you when expr in endif() does not match expr in the nearest if() .

Same thing for else() .

Simple - this protects you from errors in if() else() endif() nested chains.

+8
source

The arguments else () and endif () were needed before version 2.6.0. From CMake Help :

As with CMake 2.6.0, the ELSE () and ENDIF () constructs may be empty. The same is true for closing constructions in ENDMACRO (), ENDFUNCTION (), and ENDFOREACH (). If you need 2.4.x compatibility, CMake 2.4.3 or higher recognizes the CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS parameter (which is superfluous in 2.6.0)

Besides helping with readability, they don't seem to have any function. See also this excellent answer .

+4
source

Optional arguments make it easy to find matching parts of if / else / endif, so for better readability.

I personally do not use arguments, as I find the expression else else(condition) really confusing, as in

 if(condition) // do something else(condition) // do something else endif(condition) 

I often misread else(condition) as elseif(condition) .

+3
source

It is not that else and endif are optional. The expression in () is optional. From the documentation:

Note that the expression in the else clause and endif is optional.

Previous versions of cmake required you to repeat the condition in another and endif:

 if(FOO) ... else(FOO) ... endif(FOO) 
+1
source

Source: https://habr.com/ru/post/984745/


All Articles