Skip to content

Commit f584566

Browse files
committed
Allow defining custom prefixes
In addition to the predefined prefixes 'gh', 'gl' and 'bb', allow defining your own prefixes for, e.g. internal hosts.
1 parent 09b056a commit f584566

File tree

3 files changed

+43
-12
lines changed

3 files changed

+43
-12
lines changed

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ CPMAddPackage("uri@version#tag")
8181

8282
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.
8383

84+
In addition to the predefined prefixes, it is also possible to define custom prefixes, e.g.
85+
```cmake
86+
set(CPM_CUSTOM_PREFIXES c1:https://c1.example.com c2:https://c2.example.com)
87+
CPMAddPackage("c1:some/[email protected]")
88+
CPMAddPackage("c2:another/repo#v1.2.3")
89+
```
90+
8491
The single-argument syntax also works for URLs:
8592

8693
```cmake

cmake/CPM.cmake

+29-12
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,8 @@ endfunction()
339339
# to: GITHUB_REPOSITORY;foo/bar;VERSION;1.2.3
340340
function(cpm_parse_add_package_single_arg arg outArgs)
341341
# Look for a scheme
342-
if("${arg}" MATCHES "^([a-zA-Z]+):(.+)$")
342+
343+
if("${arg}" MATCHES "^([a-zA-Z0-9]+):(.+)$")
343344
string(TOLOWER "${CMAKE_MATCH_1}" scheme)
344345
set(uri "${CMAKE_MATCH_2}")
345346

@@ -353,19 +354,32 @@ function(cpm_parse_add_package_single_arg arg outArgs)
353354
elseif(scheme STREQUAL "bb")
354355
set(out "BITBUCKET_REPOSITORY;${uri}")
355356
set(packageType "git")
357+
elseif(DEFINED CPM_CUSTOM_PREFIXES)
358+
foreach(prefix ${CPM_CUSTOM_PREFIXES})
359+
if("${prefix}" MATCHES "([^:]+):(.+)")
360+
if(scheme STREQUAL "${CMAKE_MATCH_1}")
361+
set(out "GIT_REPOSITORY;${uri};CUSTOM_REPOSITORY;${CMAKE_MATCH_2}")
362+
set(packageType "git")
363+
break()
364+
endif()
365+
endif()
366+
endforeach()
367+
endif()
368+
if("${out}" STREQUAL "")
356369
# A CPM-specific scheme was not found. Looks like this is a generic URL so try to determine
357370
# type
358-
elseif(arg MATCHES ".git/?(@|#|$)")
359-
set(out "GIT_REPOSITORY;${arg}")
360-
set(packageType "git")
361-
else()
362-
# Fall back to a URL
363-
set(out "URL;${arg}")
364-
set(packageType "archive")
365-
366-
# We could also check for SVN since FetchContent supports it, but SVN is so rare these days.
367-
# We just won't bother with the additional complexity it will induce in this function. SVN is
368-
# done by multi-arg
371+
if(arg MATCHES ".git/?(@|#|$)")
372+
set(out "GIT_REPOSITORY;${arg}")
373+
set(packageType "git")
374+
else()
375+
# Fall back to a URL
376+
set(out "URL;${arg}")
377+
set(packageType "archive")
378+
379+
# We could also check for SVN since FetchContent supports it, but SVN is so rare these days.
380+
# We just won't bother with the additional complexity it will induce in this function. SVN
381+
# is done by multi-arg
382+
endif()
369383
endif()
370384
else()
371385
if(arg MATCHES ".git/?(@|#|$)")
@@ -534,6 +548,7 @@ function(CPMAddPackage)
534548
GIT_SHALLOW
535549
EXCLUDE_FROM_ALL
536550
SOURCE_SUBDIR
551+
CUSTOM_REPOSITORY
537552
)
538553

539554
set(multiValueArgs URL OPTIONS)
@@ -560,6 +575,8 @@ function(CPMAddPackage)
560575
set(CPM_ARGS_GIT_REPOSITORY "https://gitlab.com/${CPM_ARGS_GITLAB_REPOSITORY}.git")
561576
elseif(DEFINED CPM_ARGS_BITBUCKET_REPOSITORY)
562577
set(CPM_ARGS_GIT_REPOSITORY "https://bitbucket.org/${CPM_ARGS_BITBUCKET_REPOSITORY}.git")
578+
elseif(DEFINED CPM_ARGS_CUSTOM_REPOSITORY)
579+
set(CPM_ARGS_GIT_REPOSITORY "${CPM_ARGS_CUSTOM_REPOSITORY}/${CPM_ARGS_GIT_REPOSITORY}.git")
563580
endif()
564581

565582
if(DEFINED CPM_ARGS_GIT_REPOSITORY)

test/unit/parse_add_package_single_arg.cmake

+7
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ assert_equal("BITBUCKET_REPOSITORY;foo/bar" "${args}")
3636
cpm_parse_add_package_single_arg("bb:foo/Bar" args)
3737
assert_equal("BITBUCKET_REPOSITORY;foo/Bar" "${args}")
3838

39+
set(CPM_CUSTOM_PREFIXES c1:https://c1.example.com c2:https://c2.example.com/mirror)
40+
cpm_parse_add_package_single_arg("c1:foo/bar@13" args)
41+
assert_equal("GIT_REPOSITORY;foo/bar;VERSION;13;CUSTOM_REPOSITORY;https://c1.example.com" "${args}")
42+
43+
cpm_parse_add_package_single_arg("c2:foo/Bar#bla" args)
44+
assert_equal("GIT_REPOSITORY;foo/Bar;GIT_TAG;bla;CUSTOM_REPOSITORY;https://c2.example.com/mirror" "${args}")
45+
3946
cpm_parse_add_package_single_arg("https://github.com/cpm-cmake/[email protected]" args)
4047
assert_equal("GIT_REPOSITORY;https://github.com/cpm-cmake/CPM.cmake.git;VERSION;0.30.5" "${args}")
4148

0 commit comments

Comments
 (0)