Why does CMake syntax everywhere have red parentheses?

CMake if follow these steps:

 if (condition) ... else if (...) ... else (...) ... endif (...) 

With else if (...) tests (...) for a single condition.
Why else (...) , not just else ? Why endif (...) and not endif ?


Cmake functions are performed as follows:

 function(funcname ...) ... endfunction(funcname ...) 

Why endfunction(funcname ...) and not just endfunction ?


I can omit the contents of the backup brackets where they appear, for example: endif () . What is the purpose of this design?

+3
source share
1 answer

I believe the original intention was that by repeating in each sentence (e.g. the else ) the original expression (e.g. the one in the if ), it would be more clear which statement was actually closed, and the parser can check and warn that nothing bad is happening.

However, it turned out that you would have an expression like:

 if (VARIABLE matches "something") [...] #This is executed when above condition is true else (VARIABLE matches "something") #Looks very much like an elseif... [...] #This is executed when above condition is false! endif (VARIABLE matches "something") 

Which turned out to be confusing. I am talking about my everyday experience, for example. I am writing something, and someone else asks me: "What is this doing?"

So now CMake also allows an empty bracket, the above can be rewritten as:

 if (VARIABLE matches "something") [...] #This is executed when above condition is true else () [...] #This is executed when above condition is false endif () 

What can be considered more clear. The grammar above can still be used.

To fully answer your question, the brackets also remain with empty arguments, because conceptually else and endif in CMake are macros like if , and therefore they are called with this grammar a little (but not absolutely exactly) as a function.


Basically the same explanation is available in CMake's frequently asked questions: Isn't that the expression "Expression" in "ELSE (Expression)"?

To add a little bit of history, in CMake 2.4, repeating the expression was mandatory, and still in, at least according to the documentation. The documentation was ambiguous on else , but inflexible in endif :

Note that the same expression must be specified for if and endif.

The first attempt to remove this restriction was introduced already with CMake 2.4.3 (2006), where you could deactivate it by writing:

 set(CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS true) 

This restriction has become completely optional with

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

+5
source

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


All Articles