Issue not finding BuildType.cmake when building via CMake's FetchContent
apthorpe opened this issue · 3 comments
I'm building a standalone application that depends on FLPR and I am using CMake for my build system. Recent-ish versions of CMake allow for downloading and building remote dependencies with the ExternalProject and FetchContent facilities. FetchContent is especially nice for including dependencies that also use CMake. So, for example, my application's CMakeFiles.txt
contains:
include(FetchContent)
set(FLPR_SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/FLPR-source")
FetchContent_Declare(FLPR
GIT_REPOSITORY https://github.com/lanl/FLPR.git
GIT_TAG HEAD
SOURCE_DIR "${FLPR_SOURCE_DIR}"
)
FetchContent_MakeAvailable(FLPR)
which will retrieve the FLPR source from GitHub and place it in build/FLPR-source
, then perform some CMake magic to build it. So far, so good: you should see this pull and attempt to build FLPR with CMake.
The issue seems to be one of CMake module detection and pathing; the build fails with:
CMake Error at build/FLPR-source/CMakeLists.txt:18 (include):
include could not find load file:
BuildType
CMake Error at build/FLPR-source/CMakeLists.txt:19 (include):
include could not find load file:
CompilerFlags
Both BuildType.cmake
and CompilerFlags.cmake
exist under build/FLPR-source/cmake
with appropriate permissions (as expected) but it seems like CMake is searching for them under <my project root>/cmake
instead (where it should look for these files when it's normally built as a standalone project). That is, the errors disappear if I copy BuildType.cmake
and CompilerFlags.cmake
to <my project root>/cmake
.
I believe the critical lines of FLPR's CMakeLists.txt
are
# Allow CMake to find some project include files
list (INSERT CMAKE_MODULE_PATH 0 "${CMAKE_SOURCE_DIR}/cmake")
but I don't have a good idea of how to appropriately set CMAKE_MODULE_PATH
in a submodule; what's needed is the child's version of "${CMAKE_SOURCE_DIR}/cmake"
, not the parent's.
I'm not sure if this configuration mismatch is on my side or not; I'm still digging into it.
This may be a simple fix; reviewing some of my other CMakeFiles.txt
configs, I find I'm including CMake scripts with:
include("${CMAKE_CURRENT_SOURCE_DIR}/cmake/FetchFLPR.cmake")
Maybe change CMAKE_SOURCE_DIR
to CMAKE_CURRENT_SOURCE_DIR
in FLPR's CMakeLists.txt
? I checked it locally and it doesn't seem to affect the standalone build.
Also, as an alternative/workaround, I have had success with including FLPR as an ExternalProject (see attachment)
BuildFLPR.zip
That is cool CMake feature! Seems nicer than doing a 'git submodule', which is what I'm doing to build an app that depends on FLPR.
I have had better luck with ExternalProject but I thought I'd give FetchContent another try. One nice thing about ExternalProject is that it can deal with projects that don't use CMake or have weird build processes beyond the standard configure && make && make install
. Especially on Windows. It's not exactly pleasant, but it is both possible and portable.