[CMake] conditions and included subprojects

Cornelis Bockemühl cornelis at bockemuehl.ch
Mon Jun 11 04:20:03 EDT 2018


Dear CMake users,

Maybe my question is trivial for most, but still I do not find an
answer on my own!

If you have a project and some sub-project (or module or whatever the
jargon is) that are both managed with CMake, they should be in separate
directories (directory trees), and each of them have a CMakeLists.txt
in the root directory, where the sub-project is "included" into the
main project with an ADD_DIRECTORY() etc. So far so clear.

But then I learned that it is good practice to ensure that the sub-
project would also be "buildable" with cmake separately. My problem is
about how to pass options from the main to the sub project in such a
way that this is working properly, i.e.:

- if the sub project is part of the main project it would simply take
over the option from the main project level
- but if the sub project is being built standalone, the option is not
defined and should be set e.g. in cmake-gui

If I write now in the sub project

IF(THAT_OPTION)
    ...

this will be true if THAT_OPTION was set to ON in the main project. So
far so good. But the expression is now so "clever" that it cannot
distinguish between THAT_OPTION being set to OFF in the main project,
or THAT_OPTION not being defined at all - like in a standalone build!

One way would be to have an additional option or variable
BUILDING_MAINPROJECT which is set to ON or TRUE in the main project and
can be checked. However, I have so far not seen any CMakeLists.txt with
such a code, so ... what are the experts doing in such a case?

Thanks and regards,
Cornelis
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20180611/86edfa6e/attachment.html>


More information about the CMake mailing list