From cfacbb86534f6973022cc0391563af2f0dc671bd Mon Sep 17 00:00:00 2001 From: BlackMark Date: Mon, 7 Oct 2024 13:53:20 +0200 Subject: [PATCH] Fetch dependencies from submodules potentially reusing already provided ones --- .gitmodules | 6 ++++++ CMakeLists.txt | 33 ++++++++++++++++++++++----------- external/Catch2 | 1 + external/fmt | 1 + 4 files changed, 30 insertions(+), 11 deletions(-) create mode 100644 .gitmodules create mode 160000 external/Catch2 create mode 160000 external/fmt diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..15d7c7b --- /dev/null +++ b/.gitmodules @@ -0,0 +1,6 @@ +[submodule "external/fmt"] + path = external/fmt + url = git@github.com:fmtlib/fmt.git +[submodule "external/Catch2"] + path = external/Catch2 + url = git@github.com:catchorg/Catch2.git diff --git a/CMakeLists.txt b/CMakeLists.txt index 4e7f0ff..128a26c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -54,22 +54,33 @@ function(libenvpp_set_compiler_parameters TARGET) endfunction() # External dependencies. +include(FetchContent) + if(LIBENVPP_INSTALL) set(FMT_INSTALL ON CACHE BOOL "" FORCE) endif() -include(FetchContent) -FetchContent_Declare(fmt - GIT_REPOSITORY https://github.com/fmtlib/fmt.git - GIT_TAG 11.0.2 -) -FetchContent_MakeAvailable(fmt) + +macro(fetch_content_from_submodule DEPNAME RELPATH) + # Dependencies may have already been satisfied by the parent project, in this case the already provided dependency will be used. + # The parent project is responsible for ensuring that the provided dependency version is compatible. + if(${DEPNAME}_FOUND) + message(STATUS "Using system ${DEPNAME} in ${${DEPNAME}_DIR}") + else() + message(STATUS "Fetching ${DEPNAME} from submodule") + if(NOT EXISTS "${PROJECT_SOURCE_DIR}/${RELPATH}/CMakeLists.txt") + message(FATAL_ERROR "The git submodule ${RELPATH} is missing.\nTry running `git submodule update --init`.") + endif() + FetchContent_Declare(${DEPNAME} + URL "${PROJECT_SOURCE_DIR}/${RELPATH}" + ) + FetchContent_MakeAvailable(${DEPNAME}) + endif() +endmacro() + +fetch_content_from_submodule(fmt external/fmt) if(LIBENVPP_TESTS) - FetchContent_Declare(Catch2 - GIT_REPOSITORY https://github.com/catchorg/Catch2.git - GIT_TAG v3.7.1 - ) - FetchContent_MakeAvailable(Catch2) + fetch_content_from_submodule(Catch2 external/Catch2) list(APPEND CMAKE_MODULE_PATH ${catch2_SOURCE_DIR}/extras) endif() diff --git a/external/Catch2 b/external/Catch2 new file mode 160000 index 0000000..fa43b77 --- /dev/null +++ b/external/Catch2 @@ -0,0 +1 @@ +Subproject commit fa43b77429ba76c462b1898d6cd2f2d7a9416b14 diff --git a/external/fmt b/external/fmt new file mode 160000 index 0000000..0c9fce2 --- /dev/null +++ b/external/fmt @@ -0,0 +1 @@ +Subproject commit 0c9fce2ffefecfdce794e1859584e25877b7b592