Skip to content

Commit d1f79fb

Browse files
committed
Fetch dependencies from submodules potentially reusing already provided ones
1 parent 6f73292 commit d1f79fb

File tree

4 files changed

+30
-11
lines changed

4 files changed

+30
-11
lines changed

.gitmodules

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[submodule "external/fmt"]
2+
path = external/fmt
3+
url = [email protected]:fmtlib/fmt.git
4+
[submodule "external/Catch2"]
5+
path = external/Catch2
6+
url = [email protected]:catchorg/Catch2.git

CMakeLists.txt

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,33 @@ function(libenvpp_set_compiler_parameters TARGET)
5454
endfunction()
5555

5656
# External dependencies.
57+
include(FetchContent)
58+
5759
if(LIBENVPP_INSTALL)
5860
set(FMT_INSTALL ON CACHE BOOL "" FORCE)
5961
endif()
60-
include(FetchContent)
61-
FetchContent_Declare(fmt
62-
GIT_REPOSITORY https://github.com/fmtlib/fmt.git
63-
GIT_TAG 11.0.2
64-
)
65-
FetchContent_MakeAvailable(fmt)
62+
63+
macro(fetch_content_from_submodule DEPNAME RELPATH)
64+
# Dependencies may have already been satisfied by the parent project, in this case the already provided dependency will be used.
65+
# The parent project is responsible for ensuring that the provided dependency version is compatible.
66+
if(${DEPNAME}_FOUND OR TARGET ${DEPNAME})
67+
message(STATUS "Depndency ${DEPNAME} already provided, reusing it")
68+
else()
69+
message(STATUS "Fetching ${DEPNAME} from submodule")
70+
if(NOT EXISTS "${PROJECT_SOURCE_DIR}/${RELPATH}/CMakeLists.txt")
71+
message(FATAL_ERROR "The git submodule ${RELPATH} is missing.\nTry running `git submodule update --init`")
72+
endif()
73+
FetchContent_Declare(${DEPNAME}
74+
URL "${PROJECT_SOURCE_DIR}/${RELPATH}"
75+
)
76+
FetchContent_MakeAvailable(${DEPNAME})
77+
endif()
78+
endmacro()
79+
80+
fetch_content_from_submodule(fmt external/fmt)
6681

6782
if(LIBENVPP_TESTS)
68-
FetchContent_Declare(Catch2
69-
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
70-
GIT_TAG v3.7.1
71-
)
72-
FetchContent_MakeAvailable(Catch2)
83+
fetch_content_from_submodule(Catch2 external/Catch2)
7384
list(APPEND CMAKE_MODULE_PATH ${catch2_SOURCE_DIR}/extras)
7485
endif()
7586

external/Catch2

Submodule Catch2 added at fa43b77

external/fmt

Submodule fmt added at 0c9fce2

0 commit comments

Comments
 (0)