Skip to content

Commit 49652b0

Browse files
committed
Added relative git URL support
1 parent c58e98a commit 49652b0

File tree

1 file changed

+51
-2
lines changed

1 file changed

+51
-2
lines changed

cmake/CPM.cmake

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,9 @@ function(cpm_parse_add_package_single_arg arg outArgs)
316316
elseif(scheme STREQUAL "bb")
317317
set(out "BITBUCKET_REPOSITORY;${uri}")
318318
set(packageType "git")
319+
elseif(scheme STREQUAL "rel")
320+
set(out "RELATIVE_REPOSITORY;${uri}")
321+
set(packageType "git")
319322
# A CPM-specific scheme was not found. Looks like this is a generic URL so try to determine
320323
# type
321324
elseif(arg MATCHES ".git/?(@|#|$)")
@@ -365,11 +368,11 @@ function(cpm_parse_add_package_single_arg arg outArgs)
365368
)
366369
endfunction()
367370

371+
find_package(Git REQUIRED)
372+
368373
# Check that the working directory for a git repo is clean
369374
function(cpm_check_git_working_dir_is_clean repoPath gitTag isClean)
370375

371-
find_package(Git REQUIRED)
372-
373376
if(NOT GIT_EXECUTABLE)
374377
# No git executable, assume directory is clean
375378
set(${isClean}
@@ -427,6 +430,48 @@ function(cpm_check_git_working_dir_is_clean repoPath gitTag isClean)
427430

428431
endfunction()
429432

433+
# Convert a URI relative to that of the URL of the remote of our current Git repository
434+
# e.g. github.com/user/repo.git + ../other.git = github.com/user/other.git
435+
function(cpm_git_relative_uri_to_url relative_uri absolute_url)
436+
if (NOT Git_FOUND)
437+
message(SEND_ERROR "Git not found, cannot convert relative URI to absolute Git URL")
438+
return()
439+
endif()
440+
441+
# Get the list of remotes available
442+
execute_process(COMMAND ${GIT_EXECUTABLE} remote
443+
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
444+
OUTPUT_VARIABLE remotes_names
445+
OUTPUT_STRIP_TRAILING_WHITESPACE
446+
ERROR_QUIET)
447+
448+
string(REPLACE "\n" ";" remotes_names_list ${remotes_names})
449+
450+
list(LENGTH remotes_names_list remotes_count)
451+
452+
if (remotes_count EQUAL 0)
453+
message(ERROR "Repository located at ${CMAKE_CURRENT_SOURCE_DIR} has no remote, cannot use relative URLs")
454+
return()
455+
endif()
456+
457+
# Get the first remote
458+
list(GET remotes_names_list 0 remote_name)
459+
460+
# Get the fetch URL of that remote
461+
execute_process(COMMAND ${GIT_EXECUTABLE} remote get-url ${remote_name}
462+
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
463+
OUTPUT_VARIABLE remote_url
464+
OUTPUT_STRIP_TRAILING_WHITESPACE
465+
ERROR_QUIET)
466+
467+
string(CONCAT absolute_url_local "${remote_url}" "/" "${relative_uri}")
468+
469+
set(${absolute_url}
470+
"${absolute_url_local}"
471+
PARENT_SCOPE
472+
)
473+
endfunction()
474+
430475
# Download and add a package from source
431476
function(CPMAddPackage)
432477
list(LENGTH ARGN argnLength)
@@ -446,6 +491,7 @@ function(CPMAddPackage)
446491
GITHUB_REPOSITORY
447492
GITLAB_REPOSITORY
448493
BITBUCKET_REPOSITORY
494+
RELATIVE_REPOSITORY
449495
GIT_REPOSITORY
450496
SOURCE_DIR
451497
DOWNLOAD_COMMAND
@@ -480,6 +526,9 @@ function(CPMAddPackage)
480526
set(CPM_ARGS_GIT_REPOSITORY "https://gitlab.com/${CPM_ARGS_GITLAB_REPOSITORY}.git")
481527
elseif(DEFINED CPM_ARGS_BITBUCKET_REPOSITORY)
482528
set(CPM_ARGS_GIT_REPOSITORY "https://bitbucket.org/${CPM_ARGS_BITBUCKET_REPOSITORY}.git")
529+
elseif(DEFINED CPM_ARGS_RELATIVE_REPOSITORY)
530+
cpm_git_relative_uri_to_url("${CPM_ARGS_RELATIVE_REPOSITORY}" absolute_git_url)
531+
set(CPM_ARGS_GIT_REPOSITORY "${absolute_git_url}.git")
483532
endif()
484533

485534
if(DEFINED CPM_ARGS_GIT_REPOSITORY)

0 commit comments

Comments
 (0)