我正在用c++写一个没有外部依赖项的小型库,除了测试我想使用catch2的地方。因此,我宁愿不包含该项目,并认为我可以在需要时使用cmake 来下载它,但到目前为止,它看起来仅在执行makefile时才下载cmake。
这是有问题的,因为CMakeList文件的其余部分取决于已经下载的文件,因此它可以利用catch2提供的cmake函数。没有它,构建将失败。
我的以下方法如下所示
./CMakeList.txt:cmake_minimum_required(VERSION 3.9)
project(projectname VERSION 0.0.1)
enable_language(CXX CUDA)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY bin)
add_definitions(-DNVRTC_GET_TYPE_NAME=1)
include_directories(include)
link_libraries(cuda nvrtc)
file(GLOB SOURCES "*.cpp")
add_subdirectory(examples)
add_subdirectory(test)
./examples/CMakeList.txt:if (CMAKE_BUILD_TYPE STREQUAL "Debug")
add_definitions(-Wall -Dcurrent_log_level=loglevel::DEBUG1)
endif ()
add_executable(example_saxpy ${SOURCES} example_saxpy.cpp)
add_executable(example_program ${SOURCES} example_program.cpp)
add_executable(example_template ${SOURCES} example_template.cpp)
./test/CMakeList.txt:file(GLOB TESTS "*.cpp")
add_executable(tests ${SOURCES} ${TESTS})
find_package(Catch2 CONFIG)
if (${Catch2_FOUND})
else ()
message(STATUS "downloading Catch2")
include(ExternalProject)
ExternalProject_Add(
catch2
PREFIX ${PROJECT_SOURCE_DIR}/lib/catch2
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
TIMEOUT 10
UPDATE_COMMAND ${GIT_EXECUTABLE} pull
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
LOG_DOWNLOAD ON
)
add_dependencies(tests catch2)
# Expose required variable (CATCH_INCLUDE_DIR) to parent scope
ExternalProject_Get_Property(catch2 source_dir)
set(CATCH_INCLUDE_DIR ${source_dir}/include CACHE INTERNAL "Path to include folder for Catch2")
add_subdirectory(${PROJECT_SOURCE_DIR}/lib/catch2)
include_directories(${CATCH_INCLUDE_DIR})
endif ()
enable_testing(true)
target_link_libraries(tests Catch2::Catch2)
include(CTest)
include(Catch)
catch_discover_tests(tests)
具有以下项目结构projectname
├── bin
├── build
├── CMakeLists.txt
├── examples
│ └── CMakeLists.txt
├── include
└── $projectname
├── src
└── test
└── CMakeLists.txt
现在这根本不起作用,因为我仍然误解了谁的变量在多个cmakefile之间作用域,但是将所有这些放在一个CmakeList中仍然不会让add_subdirectory发现Catch2。
我的另一个想法是将其包含在git子模块中,如果这样可以允许cmake在执行makefile文件之前下载catch2。
因此,总的来说,我想使gitrepo尽可能小,并且仅在需要它们时才获取依赖项。
最佳答案
我最终选择了子模块,因为fetchContent可以很好地工作,但是仅在cmake v3.11中受支持。
我遵循了Googletest https://cliutils.gitlab.io/modern-cmake/chapters/testing/googletest.html的指南
./test/CMakeList带有ext2 / catch2的catch2子模块的:
file(GLOB TESTS "*.cpp")
file(GLOB SOURCES "${PROJECT_SOURCE_DIR}/src/*.cpp")
add_executable(tests ${SOURCES} ${TESTS})
SET(catch2_dir ${PROJECT_SOURCE_DIR}/extern/catch2)
find_package(Git QUIET)
if(GIT_FOUND AND EXISTS "${PROJECT_SOURCE_DIR}/.git")
# Update submodules as needed
option(GIT_SUBMODULE "Check submodules during build" ON)
if(GIT_SUBMODULE)
message(STATUS "Submodule update")
execute_process(COMMAND ${GIT_EXECUTABLE} submodule update --init --recursive
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
RESULT_VARIABLE GIT_SUBMOD_RESULT)
if(NOT GIT_SUBMOD_RESULT EQUAL "0")
message(FATAL_ERROR "git submodule update --init failed with ${GIT_SUBMOD_RESULT}, please checkout submodules")
endif()
endif()
endif()
if(NOT EXISTS "${catch2_dir}/CMakeLists.txt")
message(FATAL_ERROR "The submodules were not downloaded! GIT_SUBMODULE was turned off or failed. Please update submodules and try again.")
endif()
add_subdirectory(${catch2_dir} ${CMAKE_CURRENT_SOURCE_DIR}/catch_build)
list(APPEND CMAKE_MODULE_PATH "${catch2_dir}/contrib/")
target_link_libraries(tests Catch2::Catch2)
include(CTest)
include(Catch)
catch_discover_tests(tests)
关于c++ - 通过cmake获取外部依赖,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59038582/