Skip to content

Commit

Permalink
Allow defining custom prefixes
Browse files Browse the repository at this point in the history
In addition to the predefined prefixes 'gh', 'gl' and 'bb', allow
defining your own prefixes for, e.g. internal hosts.
  • Loading branch information
papperlapapp committed Feb 11, 2023
1 parent 09b056a commit 30aae82
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]")
CPMAddPackage("c2:another/repo#v1.2.3")
```

The single-argument syntax also works for URLs:

```cmake
Expand Down
41 changes: 29 additions & 12 deletions cmake/CPM.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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}")

Expand All @@ -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/?(@|#|$)")
Expand Down Expand Up @@ -534,6 +548,7 @@ function(CPMAddPackage)
GIT_SHALLOW
EXCLUDE_FROM_ALL
SOURCE_SUBDIR
CUSTOM_REPOSITORY
)

set(multiValueArgs URL OPTIONS)
Expand All @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions test/unit/parse_add_package_single_arg.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ 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/[email protected]" args)
assert_equal("GIT_REPOSITORY;https://github.com/cpm-cmake/CPM.cmake.git;VERSION;0.30.5" "${args}")

Expand Down

0 comments on commit 30aae82

Please sign in to comment.