Skip to content

Commit d761438

Browse files
feature: allow URI to use shorthand syntax with additional options (#617)
* feature: add URI to use shorthand syntax with additional options This allows to combine the shorthand syntax with URI and additional arguments: ``` CPMAddPackage(URI "gh:nlohmann/json@3.9.1" OPTIONS "JSON_BUildTests OFF") ``` This is much shorter than the longer syntax way of writing: ``` CPMAddPackage( NAME nlohmann_json VERSION 3.9.1 GITHUB_REPOSITORY nlohmann/json OPTIONS "JSON_BuildTests OFF" ) ``` * fix: use shorthand syntax in examples * test: add test for shorthand syntax with options * doc: extend README mentioning shorthand syntax with options * feat: URI keyword also sets EXCLUDE_FROM AND SYSTEM * doc: more explicit about the behavior of URI * doc: adjust README accordingly to PR-Review * test: fix inline documentation of test_simple * move URI comment * added new test for shorthand syntax * reset simple test * add that URI must be the first argument --------- Co-authored-by: Lars Melchior <lars.melchior@gmail.com>
1 parent 97023e8 commit d761438

File tree

3 files changed

+121
-20
lines changed

3 files changed

+121
-20
lines changed

README.md

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ If an additional optional parameter `SYSTEM` is set to a truthy value, the SYSTE
8888
See the [add_subdirectory ](https://cmake.org/cmake/help/latest/command/add_subdirectory.html?highlight=add_subdirectory)
8989
and [SYSTEM](https://cmake.org/cmake/help/latest/prop_tgt/SYSTEM.html#prop_tgt:SYSTEM) target property for details.
9090

91-
A single-argument compact syntax is also supported:
91+
A shorthand syntax is also supported:
9292

9393
```cmake
9494
# A git package from a given uri with a version
@@ -112,6 +112,19 @@ CPMAddPackage("https://example.com/my-package-1.2.3.zip#MD5=68e20f674a48be38d60e
112112
CPMAddPackage("https://example.com/my-package.zip@1.2.3")
113113
```
114114

115+
Additionally, if needed, extra arguments can be provided while using single argument syntax by using the shorthand syntax with the `URI` specifier.
116+
117+
```cmake
118+
CPMAddPackage(
119+
URI "gh:nlohmann/json@3.9.1"
120+
OPTIONS "JSON_BuildTests OFF"
121+
)
122+
```
123+
124+
The `URI` argument must be the first argument to `CPMAddPackage`.
125+
`URI` automatically sets `EXCLUDE_FROM_ALL YES` and `SYSTEM YES`.
126+
If this is not desired, `EXCLUDE_FROM_ALL NO` and `SYSTEM NO` can be set afterwards.
127+
115128
After calling `CPMAddPackage`, the following variables are defined in the local scope, where `<dependency>` is the name of the dependency.
116129

117130
- `<dependency>_SOURCE_DIR` is the path to the source of the dependency.
@@ -412,11 +425,8 @@ CPMAddPackage("gh:jbeder/yaml-cpp#yaml-cpp-0.6.3@0.6.3")
412425

413426
```cmake
414427
CPMAddPackage(
415-
NAME nlohmann_json
416-
VERSION 3.9.1
417-
GITHUB_REPOSITORY nlohmann/json
418-
OPTIONS
419-
"JSON_BuildTests OFF"
428+
URI "gh:nlohmann/json@3.9.1"
429+
OPTIONS "JSON_BuildTests OFF"
420430
)
421431
```
422432

@@ -446,8 +456,7 @@ For a working example of using CPM to download and configure the Boost C++ Libra
446456
```cmake
447457
# the install option has to be explicitly set to allow installation
448458
CPMAddPackage(
449-
GITHUB_REPOSITORY jarro2783/cxxopts
450-
VERSION 2.2.1
459+
URI "gh:jarro2783/cxxopts@2.2.1"
451460
OPTIONS "CXXOPTS_BUILD_EXAMPLES NO" "CXXOPTS_BUILD_TESTS NO" "CXXOPTS_ENABLE_INSTALL YES"
452461
)
453462
```
@@ -456,9 +465,7 @@ CPMAddPackage(
456465

457466
```cmake
458467
CPMAddPackage(
459-
NAME benchmark
460-
GITHUB_REPOSITORY google/benchmark
461-
VERSION 1.5.2
468+
URI "gh:google/benchmark@1.5.2"
462469
OPTIONS "BENCHMARK_ENABLE_TESTING Off"
463470
)
464471

cmake/CPM.cmake

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -594,14 +594,6 @@ endfunction()
594594
function(CPMAddPackage)
595595
cpm_set_policies()
596596

597-
list(LENGTH ARGN argnLength)
598-
if(argnLength EQUAL 1)
599-
cpm_parse_add_package_single_arg("${ARGN}" ARGN)
600-
601-
# The shorthand syntax implies EXCLUDE_FROM_ALL and SYSTEM
602-
set(ARGN "${ARGN};EXCLUDE_FROM_ALL;YES;SYSTEM;YES;")
603-
endif()
604-
605597
set(oneValueArgs
606598
NAME
607599
FORCE
@@ -624,10 +616,26 @@ function(CPMAddPackage)
624616

625617
set(multiValueArgs URL OPTIONS DOWNLOAD_COMMAND PATCHES)
626618

619+
list(LENGTH ARGN argnLength)
620+
621+
# Parse single shorthand argument
622+
if(argnLength EQUAL 1)
623+
cpm_parse_add_package_single_arg("${ARGN}" ARGN)
624+
625+
# The shorthand syntax implies EXCLUDE_FROM_ALL and SYSTEM
626+
set(ARGN "${ARGN};EXCLUDE_FROM_ALL;YES;SYSTEM;YES;")
627+
628+
# Parse URI shorthand argument
629+
elseif(argnLength GREATER 1 AND "${ARGV0}" STREQUAL "URI")
630+
list(REMOVE_AT ARGN 0 1) # remove "URI gh:<...>@version#tag"
631+
cpm_parse_add_package_single_arg("${ARGV1}" ARGV0)
632+
633+
set(ARGN "${ARGV0};EXCLUDE_FROM_ALL;YES;SYSTEM;YES;${ARGN}")
634+
endif()
635+
627636
cmake_parse_arguments(CPM_ARGS "" "${oneValueArgs}" "${multiValueArgs}" "${ARGN}")
628637

629638
# Set default values for arguments
630-
631639
if(NOT DEFINED CPM_ARGS_VERSION)
632640
if(DEFINED CPM_ARGS_GIT_TAG)
633641
cpm_get_version_from_git_tag("${CPM_ARGS_GIT_TAG}" CPM_ARGS_VERSION)
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
require_relative './lib'
2+
3+
class TestShorthandSyntax < IntegrationTest
4+
5+
def get_project_binaries prj
6+
exe_dir = File.join(prj.bin_dir, 'bin')
7+
assert File.directory? exe_dir
8+
return Dir[exe_dir + '/**/*'].filter {
9+
# on multi-configuration generators (like Visual Studio) the executables will be in bin/<Config>
10+
# also filter-out other artifacts like .pdb or .dsym
11+
!File.directory?(_1) && File.stat(_1).executable?
12+
}.map {
13+
# remove .exe extension if any (there will be one on Windows)
14+
File.basename(_1, '.exe')
15+
}.sort
16+
end
17+
18+
def test_create_with_commit_sha
19+
prj = make_project from_template: 'using-adder'
20+
prj.create_lists_from_default_template package:
21+
'CPMAddPackage("gh:cpm-cmake/testpack-adder#cad1cd4b4cdf957c5b59e30bc9a1dd200dbfc716")'
22+
assert_success prj.configure
23+
24+
cache = prj.read_cache
25+
assert_equal 1, cache.packages.size
26+
assert_equal '0', cache.packages['testpack-adder'].ver
27+
28+
assert_success prj.build
29+
exes = get_project_binaries prj
30+
# No adder projects were built as EXCLUDE_FROM_ALL is implicitly set
31+
assert_equal ['using-adder'], exes
32+
end
33+
34+
def test_create_with_version
35+
prj = make_project from_template: 'using-adder'
36+
prj.create_lists_from_default_template package:
37+
'CPMAddPackage("gh:cpm-cmake/testpack-adder@1.0.0")'
38+
assert_success prj.configure
39+
40+
cache = prj.read_cache
41+
assert_equal 1, cache.packages.size
42+
assert_equal '1.0.0', cache.packages['testpack-adder'].ver
43+
44+
assert_success prj.build
45+
exes = get_project_binaries prj
46+
assert_equal ['using-adder'], exes
47+
end
48+
49+
def test_create_with_all
50+
prj = make_project from_template: 'using-adder'
51+
prj.create_lists_from_default_template package:
52+
'CPMAddPackage(
53+
URI "gh:cpm-cmake/testpack-adder@1.0.0"
54+
EXCLUDE_FROM_ALL false
55+
)'
56+
assert_success prj.configure
57+
58+
cache = prj.read_cache
59+
assert_equal cache.packages.size, 1
60+
assert_equal cache.packages['testpack-adder'].ver, '1.0.0'
61+
62+
assert_success prj.build
63+
exes = get_project_binaries prj
64+
assert_equal exes, ['simple', 'test-adding', 'using-adder']
65+
end
66+
67+
def test_create_with_tests_but_without_examples
68+
prj = make_project from_template: 'using-adder'
69+
prj.create_lists_from_default_template package:
70+
'CPMAddPackage(
71+
URI "gh:cpm-cmake/testpack-adder@1.0.0"
72+
OPTIONS "ADDER_BUILD_EXAMPLES OFF" "ADDER_BUILD_TESTS TRUE"
73+
EXCLUDE_FROM_ALL false
74+
)'
75+
assert_success prj.configure
76+
77+
cache = prj.read_cache
78+
assert_equal cache.packages.size, 1
79+
assert_equal cache.packages['testpack-adder'].ver, '1.0.0'
80+
81+
assert_success prj.build
82+
exes = get_project_binaries prj
83+
assert_equal exes, ['test-adding', 'using-adder']
84+
end
85+
86+
end

0 commit comments

Comments
 (0)