myx/lib/FetchContentAdd.cmake

37 lines
1.0 KiB
CMake
Raw Normal View History

2022-10-01 13:42:36 +00:00
include_guard(GLOBAL)
function(FetchContent_Add NAME)
set(options "")
set(oneValueArgs GIT_REPOSITORY GIT_REMOTE GIT_PATH)
set(multiValueArgs "")
2022-10-01 20:34:07 +00:00
cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
2022-10-01 13:42:36 +00:00
find_package(Git)
2022-10-01 20:34:07 +00:00
if(GIT_FOUND AND ARG_GIT_REMOTE AND ARG_GIT_PATH)
execute_process(COMMAND ${GIT_EXECUTABLE} config --get remote.${ARG_GIT_REMOTE}.url OUTPUT_VARIABLE REMOTE_URL ERROR_QUIET)
2022-10-01 13:42:36 +00:00
if(REMOTE_URL)
string(REGEX REPLACE ":.*" "" SERVER ${REMOTE_URL})
string(FIND ${SERVER} "http" POS)
if(NOT POS EQUAL 0)
if(NOT SERVER STREQUAL REMOTE_URL)
2022-10-01 20:34:07 +00:00
set(ARG_GIT_REPOSITORY "${SERVER}:${ARG_GIT_PATH}")
2022-10-01 13:42:36 +00:00
endif()
endif()
endif()
endif()
FetchContent_Declare(
${NAME}
2022-10-01 20:34:07 +00:00
${ARG_UNPARSED_ARGUMENTS}
GIT_REPOSITORY ${ARG_GIT_REPOSITORY}
GIT_SHALLOW 1
2022-10-01 13:42:36 +00:00
)
if(NOT ${NAME}_POPULATED)
FetchContent_Populate(${NAME})
add_subdirectory(${${NAME}_SOURCE_DIR} ${${NAME}_BINARY_DIR})
endif()
endfunction()