[CMake] ADD_SUBDIRECTORY() with a higher level directory

Michael Hertling mhertling at online.de
Thu Nov 3 20:02:31 EDT 2011


On 11/03/2011 09:10 PM, Daniel Dekkers wrote:
> Hi,
> 
> We are creating a directory structure for distribution of an open-source
> library with examples included.
> It looks something like this:
> 
> + library <- the root directory
>   CMakeLists.txt <- create context for building (only) the library
>   + src <- contains the sources of the library
>     CMakeLists.txt <- actually add sources for (only) the library
>   + examples
>     CMakeLists.txt <- create context for building all the examples (and the
> library, once)
>     + example0
>       CMakeLists.txt <- create context for building example0 (and the
> library)
>       + src
>         CMakeLists.txt <- add sources for example0
>       + rsrc
>     + example1
>       CMakeLists.txt <- create context for building example1 (and the
> library)
>       + src
>         CMakeLists.txt <- add sources for example1 
>       + rsrc
>     + ...
> 
> The CMakeLists.txt in library, library/examples and
> library/examples/example0 and library/examples/example1 should all be
> possible entry points for a build. Maybe you want to build only the library,
> maybe you want to build the whole example suite, or maybe an individual
> example.
> 
> Of course, the CMakeLists.txt in examples (or example0 and example1) depends
> on the actual library, so, from that level, we would like to call
> ADD_SUBDIRECTORY() to the *higher level* library directory... which is
> unacceptable for ADD_SUBDIRECTORY() (and probably conflicts with the whole
> CMake structure).
> 
> If we would move examples "up" the hierarchy there wouldn't be a problem,
> but that doesn't give a nice distribution package.
> 
> Any hints on a proper way to do this?
> 
> Kind Regards,
> 
> Daniel Dekkers

IMO, you should set up your overall project roughly as usual, i.e.:

library/CMakeLists.txt:
ADD_SUBDIRECTORY(src)
ADD_SUBDIRECTORY(examples)

library/examples/CMakeLists.txt:
SET_DIRECTORY_PROPERTIES(PROPERTIES EXCLUDE_FROM_ALL TRUE)
ADD_SUBDIRECTORY(example0)
ADD_SUBDIRECTORY(example1)
...
ADD_CUSTOM_TARGET(examples)
ADD_DEPENDENCIES(examples example0 example1 ...)

library/examples/example0/CMakeLists.txt:
TARGET_LINK_LIBRARIES(<example0> <library>)

In this way, you will have all dependencies set up correctly, and the
examples are not built on behalf of "make" or "make all". In order to
build them, you might trigger the "examples" target or any individual
"example<i>" target, or you can jump into a target's directory under-
neath CMAKE_BINARY_DIR and issue "make" from within there. However,
you don't have an automatic target anymore to build everything,
so you would need to provide one on your own, e.g.:

library/CMakeLists.txt:
ADD_CUSTOM_TARGET(world)
ADD_DEPENDENCIES(world <library> examples)

If you want to exclude unwanted parts of your project already from the
configuration, you can use appropriate OPTION() commands and surround
the ADD_SUBDIRECTORY() commands by IF(<option>)...ENDIF(). Note that
you will have to handle the dependencies of your project's parts by
yourself in this case, i.e. you can't disable ADD_SUBDIRECTORY(src)
while enabling ADD_SUBDIRECTORY(examples) in library/CMakeLists.txt.

'hope that helps.

Regards,

Michael

PS: Please don't hijack foreign threads.


More information about the CMake mailing list