Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow defining custom prefixes #445

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why introduce CUSTOM_REPOSITORY at all? It seems somewhat redundant to me

Why not concat the string here: "GIT_REPOSITORY;${CMAKE_MATCH_2}/${uri}"?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because I didn't knew it better. This was the first time I fiddled with CMake functions.

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
9 changes: 9 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,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/[email protected]" args)
assert_equal("GIT_REPOSITORY;https://github.com/cpm-cmake/CPM.cmake.git;VERSION;0.30.5" "${args}")

Expand Down