-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Infer package name and version from URL (#220)
* Function to extract name and version from url. Some tests * Rewrite. Previous version was not safe enough. More tests * Allow underscore as a name-version separator (<name>_<ver>) * CPMAddPackage can infer name and version from url * Allow URL parse from single arg and uncomment tests * Info about shorthand syntax in README * Fix style * Fixed typo Co-authored-by: Lars Melchior <[email protected]> * Explicit hash algorithm in shorthand URL example. Also added tests which include a hash algorithm provided We can't document a default until it's confirmed here: https://gitlab.kitware.com/cmake/cmake/-/issues/21859 Co-authored-by: Lars Melchior <[email protected]>
- Loading branch information
1 parent
492e762
commit a3d1048
Showing
4 changed files
with
149 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,6 +62,17 @@ 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 coverted to `https://gitlab.com/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. | ||
|
||
The single-argument syntax also works for URLs: | ||
|
||
```cmake | ||
# An archive package from a given url. The version is inferred | ||
CPMAddPackage("https://example.com/my-package-1.2.3.zip") | ||
# An archive package from a given url with an MD5 hash provided | ||
CPMAddPackage("https://example.com/my-package-1.2.3.zip#MD5=68e20f674a48be38d60e129f600faf7d") | ||
# An archive package from a given url. The version is explicitly given | ||
CPMAddPackage("https://example.com/[email protected]") | ||
``` | ||
|
||
After calling `CPMAddPackage` or `CPMFindPackage`, the following variables are defined in the local scope, where `<dependency>` is the name of the dependency. | ||
|
||
- `<dependency>_SOURCE_DIR` is the path to the source of the dependency. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
cmake_minimum_required(VERSION 3.14 FATAL_ERROR) | ||
|
||
include(${CPM_PATH}/CPM.cmake) | ||
include(${CPM_PATH}/testing.cmake) | ||
|
||
cpm_package_name_and_ver_from_url("https://example.com/coolpack-1.2.3.zip" name ver) | ||
assert_equal("coolpack" ${name}) | ||
assert_equal("1.2.3" ${ver}) | ||
|
||
cpm_package_name_and_ver_from_url("https://example.com/cool-pack-v1.3.tar.gz" name ver) | ||
assert_equal("cool-pack" ${name}) | ||
assert_equal("1.3" ${ver}) | ||
|
||
cpm_package_name_and_ver_from_url( | ||
"https://subd.zip.com/download.php?Cool.Pack-v1.2.3rc0.tar" name ver | ||
) | ||
assert_equal("Cool.Pack" ${name}) | ||
assert_equal("1.2.3rc0" ${ver}) | ||
|
||
cpm_package_name_and_ver_from_url( | ||
"http://evil-1.2.tar.gz.com/Plan9_1.2.3a.tar.bz2?download" name ver | ||
) | ||
assert_equal("Plan9" ${name}) | ||
assert_equal("1.2.3a" ${ver}) | ||
|
||
cpm_package_name_and_ver_from_url( | ||
"http://evil-1.2.tar.gz.com/Plan_9-1.2.3a.tar.bz2?download" name ver | ||
) | ||
assert_equal("Plan_9" ${name}) | ||
assert_equal("1.2.3a" ${ver}) | ||
|
||
cpm_package_name_and_ver_from_url( | ||
"http://evil-1.2.tar.gz.com/Plan-9_1.2.3a.tar.bz2?download" name ver | ||
) | ||
assert_equal("Plan-9" ${name}) | ||
assert_equal("1.2.3a" ${ver}) | ||
|
||
cpm_package_name_and_ver_from_url("https://sf.com/distrib/SFLib-0.999.4.tar.gz/download" name ver) | ||
assert_equal("SFLib" ${name}) | ||
assert_equal("0.999.4" ${ver}) | ||
|
||
cpm_package_name_and_ver_from_url("https://example.com/coolpack/v5.6.5rc44.zip" name ver) | ||
assert_not_defined(name) | ||
assert_equal("5.6.5rc44" ${ver}) | ||
|
||
cpm_package_name_and_ver_from_url("evil-1.3.zip.com/coolpack/release999.000beta.ZIP" name ver) | ||
assert_not_defined(name) | ||
assert_equal("999.000beta" ${ver}) | ||
|
||
cpm_package_name_and_ver_from_url("https://example.com/Foo55.tar.gz" name ver) | ||
assert_equal("Foo55" ${name}) | ||
assert_not_defined(ver) | ||
|
||
cpm_package_name_and_ver_from_url("https://example.com/foo" name ver) | ||
assert_not_defined(name) | ||
assert_not_defined(ver) | ||
|
||
cpm_package_name_and_ver_from_url("example.zip.com/foo" name ver) | ||
assert_not_defined(name) | ||
assert_not_defined(ver) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,18 +47,19 @@ assert_equal( | |
"${args}" | ||
) | ||
|
||
# The following test cases are to be used in the future, once single-argument archives are supported | ||
cpm_parse_add_package_single_arg("https://example.org/foo.tar.gz" args) | ||
assert_equal("URL;https://example.org/foo.tar.gz" "${args}") | ||
|
||
# cpm_parse_add_package_single_arg("https://example.org/foo.tar.gz" args) | ||
cpm_parse_add_package_single_arg("https://example.org/foo.tar.gz#[email protected]" args) | ||
assert_equal("URL;https://example.org/foo.tar.gz;URL_HASH;baadf00d;VERSION;1.2.0" "${args}") | ||
|
||
# assert_equal("URL;https://example.org/foo.tar.gz" "${args}") | ||
cpm_parse_add_package_single_arg("https://example.org/foo.tar.gz#MD5=baadf00d" args) | ||
assert_equal("URL;https://example.org/foo.tar.gz;URL_HASH;MD5=baadf00d" "${args}") | ||
|
||
# cpm_parse_add_package_single_arg("https://example.org/foo.tar.gz#[email protected]" args) | ||
cpm_parse_add_package_single_arg("https://example.org/Foo.zip#SHA3_512=1337" args) | ||
assert_equal("URL;https://example.org/Foo.zip;URL_HASH;SHA3_512=1337" "${args}") | ||
|
||
# assert_equal("URL;https://example.org/foo.tar.gz;URL_HASH;baadf00d;VERSION;1.2.0" "${args}") | ||
|
||
# cpm_parse_add_package_single_arg("ftp://user:password@server/pathname.zip#fragment#0ddb411@0" | ||
# args) | ||
|
||
# assert_equal("URL;ftp://user:password@server/pathname.zip#fragment;URL_HASH;0ddb411;VERSION;0" | ||
# "${args}") | ||
cpm_parse_add_package_single_arg("ftp://user:pass@server/pathname.zip#fragment#0ddb411@0" args) | ||
assert_equal( | ||
"URL;ftp://user:pass@server/pathname.zip#fragment;URL_HASH;0ddb411;VERSION;0" "${args}" | ||
) |