CMake's if
s go like this:
if (condition)
...
else if (...)
...
else (...)
...
endif (...)
With else if (...)
the (...)
tests for a separate condition.
Why else (...)
and not just else
? Why endif (...)
and not endif
?
Cmake's functions go like this:
function(funcname ...)
...
endfunction(funcname ...)
Why endfunction(funcname ...)
and not simply endfunction
?
I can omit the contents of the redundant parenthesis where they appear, like so: endif ()
. What's the purpose of this construct?
I believe the initial intention was that, by repeating at each clause (for example,
else
statement) the initial expression (for example, the one at theif
statement) it would make more clear which statement was actually closed, and the parser could verify and warn nothing wrong was going on.However, it turned out that you would have expression like:
Which turned out to be confusing. I am speaking about my every day experience, e.g. I write something and somebody else come to ask me "What does this does?"
So, now CMake allows also to put empty parenthesis, the above can be rewritten as:
Which can be considered clearer. The grammar above can still be used.
To completely answer to your question, the parenthesis stay also with empty arguments because conceptually
else
andendif
in CMake are macros likeif
, and therefore they are invoked with this grammar, a little bit (but not at all exactly) as functions.Basically the same explanation is available in CMake FAQs: Isn't the "Expression" in the "ELSE (Expression)" confusing?.
To add a little bit of history, in CMake 2.4 repeating the expression was obligatory, and still in CMake 2.6, at least according to the documentation. The documentation was ambiguous on
else
, but adamant onendif
:The first try to remove this constraint was introduced already with CMake 2.4.3 (year 2006), where it was possible to deactivate it by writing:
This constraint became fully optional with CMake 2.8