|
| 1 | +cmake_minimum_required(VERSION 3.13) |
| 2 | + |
| 3 | +# include this file to check submodule include |
| 4 | + |
| 5 | +# set here as only |
| 6 | +set(warthog_https_warthog-core "https://github.com/ShortestPathLab/warthog-core.git" CACHE INTERNAL "") |
| 7 | +set(warthog_https_warthog-jps "https://github.com/ShortestPathLab/warthog-jps.git" CACHE INTERNAL "") |
| 8 | + |
| 9 | +if(COMMAND warthog_module) |
| 10 | + return() # do not redefine |
| 11 | +endif() |
| 12 | + |
| 13 | +include(FetchContent) |
| 14 | + |
| 15 | +set(WARTHOG_MODULE_ROOT_ONLY ON CACHE BOOL "add submodule if present only allowed for root project") |
| 16 | + |
| 17 | +function(warthog_top_level) |
| 18 | +set(_is_top OFF) |
| 19 | +if(${CMAKE_VERSION} VERSION_GREATER_EQUAL 3.21) |
| 20 | + if(${PROJECT_IS_TOP_LEVEL}) |
| 21 | + set(_is_top ON) |
| 22 | + endif() |
| 23 | +else() |
| 24 | + if(${CMAKE_SOURCE_DIR} STREQUAL ${PROJECT_SOURCE_DIR}) |
| 25 | + set(_is_top ON) |
| 26 | + endif() |
| 27 | +endif() |
| 28 | +set(_warthog_is_top ${_is_top} PARENT_SCOPE) |
| 29 | +endfunction() |
| 30 | + |
| 31 | +# warthog_module module [override_top] |
| 32 | +# adds a module to warthog |
| 33 | +# has checks to insure module is only added once |
| 34 | +# if called from the top level project (or optional variable override_top is true) |
| 35 | +# then if extern/${module}/CMakeLists.txt exists, adds that version instead |
| 36 | +# intended for use with git module/subtree. |
| 37 | +# otherwise, fetchcontent ${module} is made available |
| 38 | +# user must add FetchContent_Declare |
| 39 | +function(warthog_module module) |
| 40 | +get_property(_module_added GLOBAL PROPERTY WARTHOG_${module} SET) |
| 41 | +if(${_module_added}) # module already added |
| 42 | + return() |
| 43 | +endif() |
| 44 | +if((${ARGC} GREATER 1) AND (${ARGV1})) |
| 45 | + set(is_top_level ON) |
| 46 | +else() |
| 47 | + warthog_top_level() |
| 48 | + set(is_top_level ${_warthog_is_top}) |
| 49 | +endif() |
| 50 | +if(${is_top_level}) |
| 51 | + if(EXISTS "${PROJECT_SOURCE_DIR}/extern/${module}/CMakeLists.txt") |
| 52 | + # module or subtree, include and exit |
| 53 | + add_subdirectory("${PROJECT_SOURCE_DIR}/extern/${module}") |
| 54 | + set_property(GLOBAL PROPERTY WARTHOG_${module} ON) |
| 55 | + return() |
| 56 | + endif() |
| 57 | +endif() |
| 58 | +# failed to add module, fetch if able |
| 59 | +warthog_module_declare(${module}) |
| 60 | +FetchContent_MakeAvailable(${module}) |
| 61 | +set_property(GLOBAL PROPERTY WARTHOG_${module} ON) |
| 62 | +endfunction() |
| 63 | + |
| 64 | +function(warthog_module_declare module) |
| 65 | +if(NOT DEFINED warthog_https_${module}) |
| 66 | + return() |
| 67 | +endif() |
| 68 | +if((${ARGC} GREATER 1)) |
| 69 | + set(git_tag ${ARGV1}) |
| 70 | +else() |
| 71 | + set(git_tag "main") |
| 72 | +endif() |
| 73 | +FetchContent_Declare(${module} |
| 74 | + GIT_REPOSITORY ${warthog_https_${module}} |
| 75 | + GIT_TAG ${git_tag}) |
| 76 | +endfunction() |
0 commit comments