[CMake] CMake Project Structure Out of Source Build

Michael Wild themiwi at gmail.com
Thu Mar 4 08:12:35 EST 2010


On 4. Mar, 2010, at 14:02 , Flo wrote:

> Hello,
> 
> For our Project, we need a structure where its possible to work/test/deploy
> a single module and also build products which uses some modules.
> 
> f.e
> 
> MODULE 1
>      /bin
>      /scr
>      /test-src
>     CMakeLists.txt
> 
> MODULE 2
>     /bin
>     /src
>     /test-src
>    CMakeLists.txt
> 
> PRODUCT 1
>     /bin
>     /src
>     /test-src
>    CMakeLists.txt
> 
> PRODUCT 1 uses MODULE 1 and Module 2
> MODULE 1 uses MODULE 2
> MODULE 2 needs no other
> 
> in all CMakeLists-Files you can define per Macros Tests, Examples, Libs
> etc... If some target is depending on some other, the CMakeLists File will
> be called and the necessary target will be linked.
> 
> f.e. add_host_test( testModule1
>                             DEPS MODULE2)
> 
> My Problem is, that PRODUCT 1 and MODULE 1 uses MODULE 2 and  both builds
> the same executables libs etc. but its not possible(and necessary) to build
> the same executable etc. twice.
> every module should be build once per cmake execution.
> 
> I tried to use guards. But guards didn't work for me, because there aren't
> valid in global scope.
> 
> A simmilar stucture is necessary, because we will build a platform solution
> with a lot of Products in there.
> 
> Has anyone a solution for this problem? Or another project sturcture, that
> is better for this type of project?
> 
> Thx
> Flo

Every target should be built by exactly one project. Then, in each of the projects (MODULE1, MODULE2 and PRODUCT1), you export the targets (see the INSTALL(EXPORT ...) signature of the install-command) and create a <Project>Config.cmake. The latter should contain definitions such as <PROJECT>_INCLUDE_DIR, <PROJECT>_LIBRARIES etc. and include the export-file. You can then use normal FIND_PACKAGE(<Project> REQUIRED) commands in the dependent projects.

If you want this to also work from the build tree, have a look at the EXPORT(TARGETS ...) and EXPORT(PACKAGE ...) commands. I usually create the files for the installation in ${CMAKE_BINARY_DIR}/InstallFiles/ and the ones for the build-tree in ${CMAKE_BINARY_DIR}.

If you want to study an example, take a look at how I do it: http://repo.or.cz/w/freefoam.git/tree/refs/heads/pu


HTH

Michael



More information about the CMake mailing list