Skip to content

Commit 12daf36

Browse files
Add SYSTEM option (#449)
* add system property for cpm_add_subdirectory * add test case for system property * lock CMake version in test workflow * refactor to make SYSTEM an extra config option and update tests * remove old comment change * use consistent CMake version and extension for all workflows * make warning more specific and try to trigger on windows * another attempt to trigger warning on MSVC * update readme * simplify test case and use git tag * add SYSTEM option to .cmake-format * forward system arg for source overrides * enable system implicitly for the single argument syntax * Use SYSTEM option for FetchContent and add_subdirectory (#441) * Use SYSTEM option for FetchContent and add_subdirectory * Add SYSTEM option to syntax and doku * Update CPM.cmake * Update .cmake-format --------- Co-authored-by: Lars Melchior <TheLartians@users.noreply.github.com> --------- Co-authored-by: Claus Klein <claus.klein@arcormail.de>
1 parent 0938e8f commit 12daf36

File tree

9 files changed

+100
-16
lines changed

9 files changed

+100
-16
lines changed

.cmake-format

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ parse:
3434
HTTP_USERNAME: 1
3535
HTTP_PASSWORD: 1
3636
EXCLUDE_FROM_ALL: 1
37+
SYSTEM: 1
3738
SOURCE_SUBDIR: 1
3839
OPTIONS: +
3940
cpmfindpackage:

.github/workflows/examples.yml renamed to .github/workflows/examples.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
gcc:
1313
runs-on: ubuntu-latest
1414
steps:
15-
- uses: actions/checkout@v2
15+
- uses: actions/checkout@v3
1616
- name: build all
1717
env:
1818
CC: gcc
@@ -22,7 +22,7 @@ jobs:
2222
clang:
2323
runs-on: ubuntu-latest
2424
steps:
25-
- uses: actions/checkout@v2
25+
- uses: actions/checkout@v3
2626
- name: build all
2727
env:
2828
CC: clang

.github/workflows/publish.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
name: Publish CPM.cmake
1111
runs-on: ubuntu-latest
1212
steps:
13-
- uses: actions/checkout@v1
13+
- uses: actions/checkout@v3
1414

1515
- name: Set CPM version by tag
1616
run: |

.github/workflows/style.yml renamed to .github/workflows/style.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ jobs:
1414
runs-on: ubuntu-latest
1515

1616
steps:
17-
- uses: actions/checkout@v1
17+
- uses: actions/checkout@v3
18+
19+
- name: Setup cmake
20+
uses: jwlawson/actions-setup-cmake@v1.13
21+
with:
22+
cmake-version: '3.25.x'
1823

1924
- name: Install format dependencies
2025
run: pip3 install clang-format==14.0.6 cmake_format==0.6.11 pyyaml

.github/workflows/test.yml renamed to .github/workflows/test.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ jobs:
2020

2121
steps:
2222
- name: clone
23-
uses: actions/checkout@v2
23+
uses: actions/checkout@v3
24+
25+
- name: Setup cmake
26+
uses: jwlawson/actions-setup-cmake@v1.13
27+
with:
28+
cmake-version: '3.25.x'
2429

2530
- name: unit tests
2631
run: |

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ On the other hand, if `VERSION` hasn't been explicitly specified, CPM can automa
6868

6969
If an additional optional parameter `EXCLUDE_FROM_ALL` is set to a truthy value, then any targets defined inside the dependency won't be built by default. See the [CMake docs](https://cmake.org/cmake/help/latest/prop_tgt/EXCLUDE_FROM_ALL.html) for details.
7070

71+
If an additional optional parameter `SYSTEM` is set to a truthy value, the SYSTEM directory property of the subdirectory added will be set to true.
72+
See the [add_subdirectory ](https://cmake.org/cmake/help/latest/command/add_subdirectory.html?highlight=add_subdirectory)
73+
and [SYSTEM](https://cmake.org/cmake/help/latest/prop_tgt/SYSTEM.html#prop_tgt:SYSTEM) target property for details.
74+
7175
A single-argument compact syntax is also supported:
7276

7377
```cmake

cmake/CPM.cmake

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -513,8 +513,8 @@ function(CPMAddPackage)
513513
if(argnLength EQUAL 1)
514514
cpm_parse_add_package_single_arg("${ARGN}" ARGN)
515515

516-
# The shorthand syntax implies EXCLUDE_FROM_ALL
517-
set(ARGN "${ARGN};EXCLUDE_FROM_ALL;YES")
516+
# The shorthand syntax implies EXCLUDE_FROM_ALL and SYSTEM
517+
set(ARGN "${ARGN};EXCLUDE_FROM_ALL;YES;SYSTEM;YES;")
518518
endif()
519519

520520
set(oneValueArgs
@@ -531,6 +531,7 @@ function(CPMAddPackage)
531531
DOWNLOAD_COMMAND
532532
FIND_PACKAGE_ARGUMENTS
533533
NO_CACHE
534+
SYSTEM
534535
GIT_SHALLOW
535536
EXCLUDE_FROM_ALL
536537
SOURCE_SUBDIR
@@ -626,6 +627,7 @@ function(CPMAddPackage)
626627
NAME "${CPM_ARGS_NAME}"
627628
SOURCE_DIR "${PACKAGE_SOURCE}"
628629
EXCLUDE_FROM_ALL "${CPM_ARGS_EXCLUDE_FROM_ALL}"
630+
SYSTEM "${CPM_ARGS_SYSTEM}"
629631
OPTIONS "${CPM_ARGS_OPTIONS}"
630632
SOURCE_SUBDIR "${CPM_ARGS_SOURCE_SUBDIR}"
631633
DOWNLOAD_ONLY "${DOWNLOAD_ONLY}"
@@ -739,9 +741,13 @@ function(CPMAddPackage)
739741
endif()
740742

741743
cpm_add_subdirectory(
742-
"${CPM_ARGS_NAME}" "${DOWNLOAD_ONLY}"
743-
"${${CPM_ARGS_NAME}_SOURCE_DIR}/${CPM_ARGS_SOURCE_SUBDIR}" "${${CPM_ARGS_NAME}_BINARY_DIR}"
744-
"${CPM_ARGS_EXCLUDE_FROM_ALL}" "${CPM_ARGS_OPTIONS}"
744+
"${CPM_ARGS_NAME}"
745+
"${DOWNLOAD_ONLY}"
746+
"${${CPM_ARGS_NAME}_SOURCE_DIR}/${CPM_ARGS_SOURCE_SUBDIR}"
747+
"${${CPM_ARGS_NAME}_BINARY_DIR}"
748+
"${CPM_ARGS_EXCLUDE_FROM_ALL}"
749+
"${CPM_ARGS_SYSTEM}"
750+
"${CPM_ARGS_OPTIONS}"
745751
)
746752
set(PACKAGE_INFO "${PACKAGE_INFO} at ${download_directory}")
747753

@@ -794,9 +800,13 @@ function(CPMAddPackage)
794800
endif()
795801
if(${populated})
796802
cpm_add_subdirectory(
797-
"${CPM_ARGS_NAME}" "${DOWNLOAD_ONLY}"
798-
"${${CPM_ARGS_NAME}_SOURCE_DIR}/${CPM_ARGS_SOURCE_SUBDIR}" "${${CPM_ARGS_NAME}_BINARY_DIR}"
799-
"${CPM_ARGS_EXCLUDE_FROM_ALL}" "${CPM_ARGS_OPTIONS}"
803+
"${CPM_ARGS_NAME}"
804+
"${DOWNLOAD_ONLY}"
805+
"${${CPM_ARGS_NAME}_SOURCE_DIR}/${CPM_ARGS_SOURCE_SUBDIR}"
806+
"${${CPM_ARGS_NAME}_BINARY_DIR}"
807+
"${CPM_ARGS_EXCLUDE_FROM_ALL}"
808+
"${CPM_ARGS_SYSTEM}"
809+
"${CPM_ARGS_OPTIONS}"
800810
)
801811
endif()
802812
cpm_get_fetch_properties("${CPM_ARGS_NAME}")
@@ -947,13 +957,18 @@ function(
947957
SOURCE_DIR
948958
BINARY_DIR
949959
EXCLUDE
960+
SYSTEM
950961
OPTIONS
951962
)
963+
952964
if(NOT DOWNLOAD_ONLY AND EXISTS ${SOURCE_DIR}/CMakeLists.txt)
965+
set(addSubdirectoryExtraArgs "")
953966
if(EXCLUDE)
954-
set(addSubdirectoryExtraArgs EXCLUDE_FROM_ALL)
955-
else()
956-
set(addSubdirectoryExtraArgs "")
967+
list(APPEND addSubdirectoryExtraArgs EXCLUDE_FROM_ALL)
968+
endif()
969+
if("${SYSTEM}" AND "${CMAKE_VERSION}" VERSION_GREATER_EQUAL "3.25")
970+
# https://cmake.org/cmake/help/latest/prop_dir/SYSTEM.html#prop_dir:SYSTEM
971+
list(APPEND addSubdirectoryExtraArgs SYSTEM)
957972
endif()
958973
if(OPTIONS)
959974
foreach(OPTION ${OPTIONS})
@@ -1084,6 +1099,7 @@ function(cpm_prettify_package_arguments OUT_VAR IS_IN_COMMENT)
10841099
DOWNLOAD_COMMAND
10851100
FIND_PACKAGE_ARGUMENTS
10861101
NO_CACHE
1102+
SYSTEM
10871103
GIT_SHALLOW
10881104
)
10891105
set(multiValueArgs OPTIONS)

test/integration/lib.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,11 @@ def assert_success(res)
157157
assert_block(msg) { res.status.success? }
158158
end
159159

160+
def assert_failure(res)
161+
msg = build_message(nil, "command status was expected to be a failure, but succeeded")
162+
assert_block(msg) { !res.status.success? }
163+
end
164+
160165
def assert_same_path(a, b)
161166
msg = build_message(nil, "<?> expected but was\n<?>", a, b)
162167
assert_block(msg) { File.identical? a, b }
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
require_relative './lib'
2+
3+
class SystemWarnings < IntegrationTest
4+
5+
def test_dependency_added_using_system
6+
for use_system in [true, false] do
7+
prj = make_project name: use_system ? "system" : "no_system", from_template: 'using-adder'
8+
prj.create_lists_from_default_template package: <<~PACK
9+
# this commit has a warning in a public header
10+
CPMAddPackage(
11+
NAME Adder
12+
GITHUB_REPOSITORY cpm-cmake/testpack-adder
13+
GIT_TAG v1.0.1-warnings
14+
SYSTEM #{use_system ? "YES" : "NO"}
15+
)
16+
# all packages using `adder` will error on warnings
17+
target_compile_options(adder INTERFACE
18+
$<$<CXX_COMPILER_ID:MSVC>:/Wall /WX>
19+
$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wall -Werror>
20+
)
21+
PACK
22+
23+
assert_success prj.configure
24+
if use_system
25+
assert_success prj.build
26+
else
27+
assert_failure prj.build
28+
end
29+
end
30+
end
31+
32+
def test_dependency_added_implicitly_using_system
33+
prj = make_project from_template: 'using-adder'
34+
prj.create_lists_from_default_template package: <<~PACK
35+
# this commit has a warning in a public header
36+
CPMAddPackage("gh:cpm-cmake/testpack-adder@1.0.1-warnings")
37+
# all packages using `adder` will error on warnings
38+
target_compile_options(adder INTERFACE
39+
$<$<CXX_COMPILER_ID:MSVC>:/Wall /WX>
40+
$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wall -Werror>
41+
)
42+
PACK
43+
44+
assert_success prj.configure
45+
assert_success prj.build
46+
end
47+
48+
end

0 commit comments

Comments
 (0)