From 12daf366c79559861ec31d53222412e978e8a7c6 Mon Sep 17 00:00:00 2001 From: Lars Melchior Date: Tue, 21 Feb 2023 15:51:33 +0100 Subject: [PATCH] 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 --------- Co-authored-by: Claus Klein --- .cmake-format | 1 + .../workflows/{examples.yml => examples.yaml} | 4 +- .github/workflows/publish.yaml | 2 +- .github/workflows/{style.yml => style.yaml} | 7 ++- .github/workflows/{test.yml => test.yaml} | 7 ++- README.md | 4 ++ cmake/CPM.cmake | 38 ++++++++++----- test/integration/lib.rb | 5 ++ test/integration/test_system_warnings.rb | 48 +++++++++++++++++++ 9 files changed, 100 insertions(+), 16 deletions(-) rename .github/workflows/{examples.yml => examples.yaml} (86%) rename .github/workflows/{style.yml => style.yaml} (74%) rename .github/workflows/{test.yml => test.yaml} (83%) create mode 100644 test/integration/test_system_warnings.rb diff --git a/.cmake-format b/.cmake-format index fb6307b..24850af 100644 --- a/.cmake-format +++ b/.cmake-format @@ -34,6 +34,7 @@ parse: HTTP_USERNAME: 1 HTTP_PASSWORD: 1 EXCLUDE_FROM_ALL: 1 + SYSTEM: 1 SOURCE_SUBDIR: 1 OPTIONS: + cpmfindpackage: diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yaml similarity index 86% rename from .github/workflows/examples.yml rename to .github/workflows/examples.yaml index 8919834..951dc40 100644 --- a/.github/workflows/examples.yml +++ b/.github/workflows/examples.yaml @@ -12,7 +12,7 @@ jobs: gcc: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: build all env: CC: gcc @@ -22,7 +22,7 @@ jobs: clang: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: build all env: CC: clang diff --git a/.github/workflows/publish.yaml b/.github/workflows/publish.yaml index 9ebf94d..905342a 100644 --- a/.github/workflows/publish.yaml +++ b/.github/workflows/publish.yaml @@ -10,7 +10,7 @@ jobs: name: Publish CPM.cmake runs-on: ubuntu-latest steps: - - uses: actions/checkout@v1 + - uses: actions/checkout@v3 - name: Set CPM version by tag run: | diff --git a/.github/workflows/style.yml b/.github/workflows/style.yaml similarity index 74% rename from .github/workflows/style.yml rename to .github/workflows/style.yaml index c7b309d..b518fcc 100644 --- a/.github/workflows/style.yml +++ b/.github/workflows/style.yaml @@ -14,7 +14,12 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v1 + - uses: actions/checkout@v3 + + - name: Setup cmake + uses: jwlawson/actions-setup-cmake@v1.13 + with: + cmake-version: '3.25.x' - name: Install format dependencies run: pip3 install clang-format==14.0.6 cmake_format==0.6.11 pyyaml diff --git a/.github/workflows/test.yml b/.github/workflows/test.yaml similarity index 83% rename from .github/workflows/test.yml rename to .github/workflows/test.yaml index 393a698..b7fdc77 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yaml @@ -20,7 +20,12 @@ jobs: steps: - name: clone - uses: actions/checkout@v2 + uses: actions/checkout@v3 + + - name: Setup cmake + uses: jwlawson/actions-setup-cmake@v1.13 + with: + cmake-version: '3.25.x' - name: unit tests run: | diff --git a/README.md b/README.md index 815e76e..1000584 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,10 @@ On the other hand, if `VERSION` hasn't been explicitly specified, CPM can automa 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. +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. +See the [add_subdirectory ](https://cmake.org/cmake/help/latest/command/add_subdirectory.html?highlight=add_subdirectory) +and [SYSTEM](https://cmake.org/cmake/help/latest/prop_tgt/SYSTEM.html#prop_tgt:SYSTEM) target property for details. + A single-argument compact syntax is also supported: ```cmake diff --git a/cmake/CPM.cmake b/cmake/CPM.cmake index fbc0bd3..a84954b 100644 --- a/cmake/CPM.cmake +++ b/cmake/CPM.cmake @@ -513,8 +513,8 @@ function(CPMAddPackage) if(argnLength EQUAL 1) cpm_parse_add_package_single_arg("${ARGN}" ARGN) - # The shorthand syntax implies EXCLUDE_FROM_ALL - set(ARGN "${ARGN};EXCLUDE_FROM_ALL;YES") + # The shorthand syntax implies EXCLUDE_FROM_ALL and SYSTEM + set(ARGN "${ARGN};EXCLUDE_FROM_ALL;YES;SYSTEM;YES;") endif() set(oneValueArgs @@ -531,6 +531,7 @@ function(CPMAddPackage) DOWNLOAD_COMMAND FIND_PACKAGE_ARGUMENTS NO_CACHE + SYSTEM GIT_SHALLOW EXCLUDE_FROM_ALL SOURCE_SUBDIR @@ -626,6 +627,7 @@ function(CPMAddPackage) NAME "${CPM_ARGS_NAME}" SOURCE_DIR "${PACKAGE_SOURCE}" EXCLUDE_FROM_ALL "${CPM_ARGS_EXCLUDE_FROM_ALL}" + SYSTEM "${CPM_ARGS_SYSTEM}" OPTIONS "${CPM_ARGS_OPTIONS}" SOURCE_SUBDIR "${CPM_ARGS_SOURCE_SUBDIR}" DOWNLOAD_ONLY "${DOWNLOAD_ONLY}" @@ -739,9 +741,13 @@ function(CPMAddPackage) endif() cpm_add_subdirectory( - "${CPM_ARGS_NAME}" "${DOWNLOAD_ONLY}" - "${${CPM_ARGS_NAME}_SOURCE_DIR}/${CPM_ARGS_SOURCE_SUBDIR}" "${${CPM_ARGS_NAME}_BINARY_DIR}" - "${CPM_ARGS_EXCLUDE_FROM_ALL}" "${CPM_ARGS_OPTIONS}" + "${CPM_ARGS_NAME}" + "${DOWNLOAD_ONLY}" + "${${CPM_ARGS_NAME}_SOURCE_DIR}/${CPM_ARGS_SOURCE_SUBDIR}" + "${${CPM_ARGS_NAME}_BINARY_DIR}" + "${CPM_ARGS_EXCLUDE_FROM_ALL}" + "${CPM_ARGS_SYSTEM}" + "${CPM_ARGS_OPTIONS}" ) set(PACKAGE_INFO "${PACKAGE_INFO} at ${download_directory}") @@ -794,9 +800,13 @@ function(CPMAddPackage) endif() if(${populated}) cpm_add_subdirectory( - "${CPM_ARGS_NAME}" "${DOWNLOAD_ONLY}" - "${${CPM_ARGS_NAME}_SOURCE_DIR}/${CPM_ARGS_SOURCE_SUBDIR}" "${${CPM_ARGS_NAME}_BINARY_DIR}" - "${CPM_ARGS_EXCLUDE_FROM_ALL}" "${CPM_ARGS_OPTIONS}" + "${CPM_ARGS_NAME}" + "${DOWNLOAD_ONLY}" + "${${CPM_ARGS_NAME}_SOURCE_DIR}/${CPM_ARGS_SOURCE_SUBDIR}" + "${${CPM_ARGS_NAME}_BINARY_DIR}" + "${CPM_ARGS_EXCLUDE_FROM_ALL}" + "${CPM_ARGS_SYSTEM}" + "${CPM_ARGS_OPTIONS}" ) endif() cpm_get_fetch_properties("${CPM_ARGS_NAME}") @@ -947,13 +957,18 @@ function( SOURCE_DIR BINARY_DIR EXCLUDE + SYSTEM OPTIONS ) + if(NOT DOWNLOAD_ONLY AND EXISTS ${SOURCE_DIR}/CMakeLists.txt) + set(addSubdirectoryExtraArgs "") if(EXCLUDE) - set(addSubdirectoryExtraArgs EXCLUDE_FROM_ALL) - else() - set(addSubdirectoryExtraArgs "") + list(APPEND addSubdirectoryExtraArgs EXCLUDE_FROM_ALL) + endif() + if("${SYSTEM}" AND "${CMAKE_VERSION}" VERSION_GREATER_EQUAL "3.25") + # https://cmake.org/cmake/help/latest/prop_dir/SYSTEM.html#prop_dir:SYSTEM + list(APPEND addSubdirectoryExtraArgs SYSTEM) endif() if(OPTIONS) foreach(OPTION ${OPTIONS}) @@ -1084,6 +1099,7 @@ function(cpm_prettify_package_arguments OUT_VAR IS_IN_COMMENT) DOWNLOAD_COMMAND FIND_PACKAGE_ARGUMENTS NO_CACHE + SYSTEM GIT_SHALLOW ) set(multiValueArgs OPTIONS) diff --git a/test/integration/lib.rb b/test/integration/lib.rb index 53467ab..377f0f9 100644 --- a/test/integration/lib.rb +++ b/test/integration/lib.rb @@ -157,6 +157,11 @@ class IntegrationTest < Test::Unit::TestCase assert_block(msg) { res.status.success? } end + def assert_failure(res) + msg = build_message(nil, "command status was expected to be a failure, but succeeded") + assert_block(msg) { !res.status.success? } + end + def assert_same_path(a, b) msg = build_message(nil, " expected but was\n", a, b) assert_block(msg) { File.identical? a, b } diff --git a/test/integration/test_system_warnings.rb b/test/integration/test_system_warnings.rb new file mode 100644 index 0000000..202fe6c --- /dev/null +++ b/test/integration/test_system_warnings.rb @@ -0,0 +1,48 @@ +require_relative './lib' + +class SystemWarnings < IntegrationTest + + def test_dependency_added_using_system + for use_system in [true, false] do + prj = make_project name: use_system ? "system" : "no_system", from_template: 'using-adder' + prj.create_lists_from_default_template package: <<~PACK + # this commit has a warning in a public header + CPMAddPackage( + NAME Adder + GITHUB_REPOSITORY cpm-cmake/testpack-adder + GIT_TAG v1.0.1-warnings + SYSTEM #{use_system ? "YES" : "NO"} + ) + # all packages using `adder` will error on warnings + target_compile_options(adder INTERFACE + $<$:/Wall /WX> + $<$>:-Wall -Werror> + ) + PACK + + assert_success prj.configure + if use_system + assert_success prj.build + else + assert_failure prj.build + end + end + end + + def test_dependency_added_implicitly_using_system + prj = make_project from_template: 'using-adder' + prj.create_lists_from_default_template package: <<~PACK + # this commit has a warning in a public header + CPMAddPackage("gh:cpm-cmake/testpack-adder@1.0.1-warnings") + # all packages using `adder` will error on warnings + target_compile_options(adder INTERFACE + $<$:/Wall /WX> + $<$>:-Wall -Werror> + ) + PACK + + assert_success prj.configure + assert_success prj.build + end + +end