FetchContent and package requirements
I am trying to move some packages into my CMake process to simplify dependency handling (I use multiple C++ compilers and am getting ready to do some cross compiling, so this lets CMake worry about managing the toolchains for me).
FetchContent is a pretty convenient: use a third party CMake package by simply doing
FetchContent_Declare(somelib URL https://example.com/somelib.tgz)
FetchContent_MakeAvailable(somelib)
Unfortunately it isn't yet the same as using an externally-installed package. A couple of things that have been an issue for me:
-
find_package (somelib REQUIRED)
doesn't work -- CMake can't find the Findxxx.cmake files, even if they have been built. -
Since the loading of .cmake files is not well defined in this case, behavior of installation is hit or miss (e.g. using fetchcontent to load the fmtlib library exports fmt::fmt while the library spdlog's alias is not exposed, even though both packages' CMakeLists.txt have an ALIAS directive).
I guess I don't really understand what add_subdirectory is really doing.
Thus, I have a couple of questions:
-
Is
cmake --install
invoked in this case? It's not clear to me -- it looks like it has been run but the location of the output files is inconsistent, presumably due to assumptions by the authors of the various Cmakelists.txts -
Are there "best practice" conventions that lib developers (I am one myself) should do so that their package can install properly and consistently in each use case: built as a standalone package, built via FetchContent, and possibly via ExternalProject. (It's not clear that ExternalProject matters when the whole point of FetchContent when you have a CMakeLists.txt is to have the package info available at config time.)
-
Does add_subdirectory need a third argument, which would be the install location?)