0

I've got 2 adjacent repositories containing projectA and projectB

projectA uses cmake to construct 3 libraries, if I build it by itself, I get 3 outputs in the build folder, 1 library for each library sub-project. so after running cmake I'll have something like this:

build/
  lib1/*.a
  lib2/*.a
  lib3/*.a

projectB uses cmake to construct 1 executable. It uses 2 of the libraries from projectA to do that. Instead of linking against the already built library files, I'm obtaining those libraries with add_subdirectory(${CMAKE_SOURCE_DIR}/../projectA projectA) and then calling target_link_libraries(${PROJECT_NAME} lib1 lib3) with their names. This allows me to make them as a sort of dependency for projectB so that they will be built first if they are not already and yields something like this:

build/
  projectA/
    lib1/*.a
    lib2/*.a
    lib3/*.a
  projectB

however it also means that if I now wanted to make another adjacent repository with executable, projectC, that wants to use those libraries, it would end up duplicating the build. It also means if I run make all in projectB (or C), the libraries I'm not using in that particular executable gets built anyways since the entirety of projectA is now considered part of them.

So, my question is: How can I add the libraries from projectA to projectB where they will only get built inside of projectA, but projectB (or C) determines what gets built and when (only the one nedeed, only if not yet built, if src changed)?

starball
  • 20,030
  • 7
  • 43
  • 238

1 Answers1

0

I suppose in your project B and C, you could create a custom command that runs configuration for project A, and a custom command for building each library that project B or C uses from project A, making sure to specfify the DEPENDS and OUTPUT fields properly, and then in project B and C, add those libraries as IMPORTED libraries and specify the IMPORTED_LOCATION properties.

For specifying the outputs of the custom target building commands, you'll probably need to use How can I get a target's output-file's name during CMake's configuration phase (not the generation phase)?.

If that sounds like a pain, you could implement exporting / installation for project A and then add a COMMAND to your custom build targets to install that individual target, and instead of adding IMPORTED libraries manually, include the generated ProjectATargets.cmake file.

This would be very much like how ExternalProject works, so you could look into that too, but I'm not sure how applicable it is to this use-case with two projects sharing a build directory for an external project. I've never tried this and things might get weird.

starball
  • 20,030
  • 7
  • 43
  • 238