Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake: Dependencies of an external project

I'm wondering how to manage dependencies of an external project. Let's say I have three projects, each one in a different repository:

  1. Core: library exporting a lot of useful stuff.
  2. Lib1: library with some specific classes (depends on Core).
  3. Executable: uses stuff from Core and also from Lib1.

Each project with its CMakeLists.txt. Using find_package and defining paths on CMake I can build each project without problems and install it (I followed this tuto to build .cmake files for libraries)


Now I'm trying to make this process as easy as possible and I'm introducing the ExternalProject_Add macro, this way people will only have to clone Executable repository and with the help of CMake files Lib1 and Core are also downloaded, compiled and linked.

The problem is that Executable and Lib1 depends on Core but I want it to be cloned (and compiled) only once. Here is the problem:

  • In Executable CMake clones and builds Core with the ExternalProject_Add features.
  • Then Cmake clones Lib1 and look for Core but it hasn't been already installed so there is no core-targets.cmake file (core-config.cmake is available)... and building fails...

I don't know if there is a documented way to work with this kind of project tree or shall I write some kind of workaround when Core is included as dependency of an external project in order to avoid the use of core-targets.cmake file.

Thanks!


Solution

I realized that when using ExternalProject_Add it downloads, builds and INSTALL the project so, in the install-folder, I already have all the files needed to compile its dependants.

So I can manage this issue following these steps (also for build order):

  • *ExternalProject_Add* for Core
  • *ExternalProject_Add* for Lib1 passing Core install_dir as argument
  • ...and Executable.
like image 229
jgsogo Avatar asked Nov 11 '22 22:11

jgsogo


1 Answers

I would add two convenience projects to Executable, which would be contained in subfolders and make use of ExternalProject_Add.

add_subdirectory( CoreWrapper ) # Use ExternalProject_Add here
add_subdirectory( Lib1Wrapper ) # Use ExternalProject_Add here

Now you can either add dependencies to Executable, so that everything gets installed before Executable is build or you work directly with the binaries contained in the build folders.

like image 199
ToniBig Avatar answered Nov 15 '22 04:11

ToniBig