From 4f593732c06ede376b3f02053d5e8ddf2899fe13 Mon Sep 17 00:00:00 2001 From: Andreas Engel Date: Sat, 11 Feb 2023 13:49:41 +0100 Subject: [PATCH] Allow defining custom prefixes In addition to the predefined prefixes 'gh', 'gl' and 'bb', allow defining your own prefixes for, e.g. internal hosts. --- README.md | 7 ++++ cmake/CPM.cmake | 41 ++++++++++++++------ test/unit/parse_add_package_single_arg.cmake | 9 +++++ 3 files changed, 45 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 815e76e2..43f4190f 100644 --- a/README.md +++ b/README.md @@ -81,6 +81,13 @@ CPMAddPackage("uri@version#tag") In the shorthand syntax if the URI is of the form `gh:user/name`, it is interpreted as GitHub URI and converted to `https://github.com/user/name.git`. If the URI is of the form `gl:user/name`, it is interpreted as a [GitLab](https://gitlab.com/explore/) URI and converted to `https://gitlab.com/user/name.git`. If the URI is of the form `bb:user/name`, it is interpreted as a [Bitbucket](https://bitbucket.org/) URI and converted to `https://bitbucket.org/user/name.git`. Otherwise the URI used verbatim as a git URL. All packages added using the shorthand syntax will be added using the [EXCLUDE_FROM_ALL](https://cmake.org/cmake/help/latest/prop_tgt/EXCLUDE_FROM_ALL.html) flag. +In addition to the predefined prefixes, it is also possible to define custom prefixes, e.g. +```cmake +set(CPM_CUSTOM_PREFIXES c1:https://c1.example.com c2:https://c2.example.com) +CPMAddPackage("c1:some/repo@1.2.3") +CPMAddPackage("c2:another/repo#v1.2.3") +``` + The single-argument syntax also works for URLs: ```cmake diff --git a/cmake/CPM.cmake b/cmake/CPM.cmake index 60b8fd6c..07744079 100644 --- a/cmake/CPM.cmake +++ b/cmake/CPM.cmake @@ -339,7 +339,8 @@ endfunction() # to: GITHUB_REPOSITORY;foo/bar;VERSION;1.2.3 function(cpm_parse_add_package_single_arg arg outArgs) # Look for a scheme - if("${arg}" MATCHES "^([a-zA-Z]+):(.+)$") + + if("${arg}" MATCHES "^([a-zA-Z0-9]+):(.+)$") string(TOLOWER "${CMAKE_MATCH_1}" scheme) set(uri "${CMAKE_MATCH_2}") @@ -353,19 +354,32 @@ function(cpm_parse_add_package_single_arg arg outArgs) elseif(scheme STREQUAL "bb") set(out "BITBUCKET_REPOSITORY;${uri}") set(packageType "git") + elseif(DEFINED CPM_CUSTOM_PREFIXES) + foreach(prefix ${CPM_CUSTOM_PREFIXES}) + if("${prefix}" MATCHES "([^:]+):(.+)") + if(scheme STREQUAL "${CMAKE_MATCH_1}") + set(out "GIT_REPOSITORY;${uri};CUSTOM_REPOSITORY;${CMAKE_MATCH_2}") + set(packageType "git") + break() + endif() + endif() + endforeach() + endif() + if("${out}" STREQUAL "") # A CPM-specific scheme was not found. Looks like this is a generic URL so try to determine # type - elseif(arg MATCHES ".git/?(@|#|$)") - set(out "GIT_REPOSITORY;${arg}") - set(packageType "git") - else() - # Fall back to a URL - set(out "URL;${arg}") - set(packageType "archive") - - # We could also check for SVN since FetchContent supports it, but SVN is so rare these days. - # We just won't bother with the additional complexity it will induce in this function. SVN is - # done by multi-arg + if(arg MATCHES ".git/?(@|#|$)") + set(out "GIT_REPOSITORY;${arg}") + set(packageType "git") + else() + # Fall back to a URL + set(out "URL;${arg}") + set(packageType "archive") + + # We could also check for SVN since FetchContent supports it, but SVN is so rare these days. + # We just won't bother with the additional complexity it will induce in this function. SVN + # is done by multi-arg + endif() endif() else() if(arg MATCHES ".git/?(@|#|$)") @@ -534,6 +548,7 @@ function(CPMAddPackage) GIT_SHALLOW EXCLUDE_FROM_ALL SOURCE_SUBDIR + CUSTOM_REPOSITORY ) set(multiValueArgs URL OPTIONS) @@ -560,6 +575,8 @@ function(CPMAddPackage) set(CPM_ARGS_GIT_REPOSITORY "https://gitlab.com/${CPM_ARGS_GITLAB_REPOSITORY}.git") elseif(DEFINED CPM_ARGS_BITBUCKET_REPOSITORY) set(CPM_ARGS_GIT_REPOSITORY "https://bitbucket.org/${CPM_ARGS_BITBUCKET_REPOSITORY}.git") + elseif(DEFINED CPM_ARGS_CUSTOM_REPOSITORY) + set(CPM_ARGS_GIT_REPOSITORY "${CPM_ARGS_CUSTOM_REPOSITORY}/${CPM_ARGS_GIT_REPOSITORY}.git") endif() if(DEFINED CPM_ARGS_GIT_REPOSITORY) diff --git a/test/unit/parse_add_package_single_arg.cmake b/test/unit/parse_add_package_single_arg.cmake index d774d0fc..ba4de675 100644 --- a/test/unit/parse_add_package_single_arg.cmake +++ b/test/unit/parse_add_package_single_arg.cmake @@ -36,6 +36,15 @@ assert_equal("BITBUCKET_REPOSITORY;foo/bar" "${args}") cpm_parse_add_package_single_arg("bb:foo/Bar" args) assert_equal("BITBUCKET_REPOSITORY;foo/Bar" "${args}") +set(CPM_CUSTOM_PREFIXES c1:https://c1.example.com c2:https://c2.example.com/mirror) +cpm_parse_add_package_single_arg("c1:foo/bar@13" args) +assert_equal("GIT_REPOSITORY;foo/bar;VERSION;13;CUSTOM_REPOSITORY;https://c1.example.com" "${args}") + +cpm_parse_add_package_single_arg("c2:foo/Bar#bla" args) +assert_equal( + "GIT_REPOSITORY;foo/Bar;GIT_TAG;bla;CUSTOM_REPOSITORY;https://c2.example.com/mirror" "${args}" +) + cpm_parse_add_package_single_arg("https://github.com/cpm-cmake/CPM.cmake.git@0.30.5" args) assert_equal("GIT_REPOSITORY;https://github.com/cpm-cmake/CPM.cmake.git;VERSION;0.30.5" "${args}")