I am trying to use a small subproject in a cmake master project using the FetchContent
method. Specifically, in the CMakeLists.txt
of my master project, I have the lines
include(FetchContent)
FetchContent_Declare(subproject GIT_REPOSITORY https://url/to/my_subproject)
FetchContent_MakeAvailable(subproject)
When I enter cmake
in the master project, the subproject is cloned, but the configuration fails.
Specifically, the ${CMAKE_SOURCE_DIR}
variable in the subproject contains the source directory of the master project, rather than that of the subproject, so directories relative to it are incorrect. The ${PROJECT_SOURCE_DIR}
variable correctly points to the subproject root, and the ${CMAKE_CURRENT_SOURCE_DIR}
variable correctly points to the current directory in the subproject.
My questions are:
- Is this a developer error / bug in the subproject (should references to the
${CMAKE_SOURCE_DIR}
variable be replaced with${PROJECT_SOURCE_DIR}
)? - Is there a workaround for the master project (Such as using
cmake -P
?)