From ed209ef291c253e9ffab42d80f24efb6a8f920ff Mon Sep 17 00:00:00 2001 From: Robert Maynard Date: Wed, 6 Nov 2024 11:13:23 -0500 Subject: [PATCH] Allow for cpm override via file and direct calls to rapids_cpm_package_override (#714) The override file provided on the command line is now just the first override file always used. Therefore other calls to `rapids_cpm_package_override` with no intersection will still work. Fixes https://github.com/rapidsai/rapids-cmake/issues/713 Authors: - Robert Maynard (https://github.com/robertmaynard) Approvers: - Kyle Edwards (https://github.com/KyleFromNVIDIA) URL: https://github.com/rapidsai/rapids-cmake/pull/714 --- rapids-cmake/cpm/init.cmake | 10 ++- rapids-cmake/cpm/package_override.cmake | 17 ++--- testing/cpm/CMakeLists.txt | 1 + testing/cpm/cpm_init-override-multiple.cmake | 2 +- ..._package_override-multiple-cmake-var.cmake | 69 +++++++++++++++++++ .../cpm/cpm_package_override-multiple.cmake | 4 +- 6 files changed, 85 insertions(+), 18 deletions(-) create mode 100644 testing/cpm/cpm_package_override-multiple-cmake-var.cmake diff --git a/rapids-cmake/cpm/init.cmake b/rapids-cmake/cpm/init.cmake index ead56cf9..13052f41 100644 --- a/rapids-cmake/cpm/init.cmake +++ b/rapids-cmake/cpm/init.cmake @@ -78,8 +78,9 @@ in the build tree of the calling project .. versionadded:: v24.06.00 If the variable :cmake:variable:`RAPIDS_CMAKE_CPM_OVERRIDE_VERSION_FILE` is specified it will be used - in all calls to ``rapids_cpm_init``. Any existing explicit `OVERRIDE` files will be ignored, and - all other calls will be treated as if this file was specified as the override. + in all calls to ``rapids_cpm_init`` no matter the arguments. Any existing + ``rapids_cpm_init(OVERRIDE`` files will be ignored, and all other calls will be treated as if this file was specified + as the override. .. versionadded:: v24.04.00 ``` @@ -118,7 +119,10 @@ function(rapids_cpm_init) rapids_cpm_load_preset_versions() endif() - if(_RAPIDS_OVERRIDE OR RAPIDS_CMAKE_CPM_OVERRIDE_VERSION_FILE) + if(RAPIDS_CMAKE_CPM_OVERRIDE_VERSION_FILE) + include("${rapids-cmake-dir}/cpm/package_override.cmake") + rapids_cpm_package_override("${RAPIDS_CMAKE_CPM_OVERRIDE_VERSION_FILE}") + elseif(_RAPIDS_OVERRIDE) include("${rapids-cmake-dir}/cpm/package_override.cmake") rapids_cpm_package_override("${_RAPIDS_OVERRIDE}") endif() diff --git a/rapids-cmake/cpm/package_override.cmake b/rapids-cmake/cpm/package_override.cmake index d39e8d7a..1ebe71a3 100644 --- a/rapids-cmake/cpm/package_override.cmake +++ b/rapids-cmake/cpm/package_override.cmake @@ -43,16 +43,16 @@ for that project will occur. This is done to make sure that the requested modifi version is used. If a project is listed in multiple override files, the first file values will be used, -and all later calls for that packaged will be ignored. This "first to record, wins" +and all later calls for that package will be ignored. This "first to record, wins" approach is used to match FetchContent, and allows parent projects to override child projects. .. versionadded:: v24.06.00 If the variable :cmake:variable:`RAPIDS_CMAKE_CPM_OVERRIDE_VERSION_FILE` is specified it will be used -in all calls to ``rapids_cpm_init``. Any existing explicit `OVERRIDE` files will be ignored, and -all other calls will be treated as if this file was specified as the override. - +in all calls to ``rapids_cpm_init`` no matter the arguments. Any existing +``rapids_cpm_init(OVERRIDE`` files will be ignored, and all other calls will be treated as if this file was specified +as the override. .. note:: @@ -72,12 +72,6 @@ all other calls will be treated as if this file was specified as the override. function(rapids_cpm_package_override _rapids_override_filepath) list(APPEND CMAKE_MESSAGE_CONTEXT "rapids.cpm.rapids_cpm_package_override") - # The `RAPIDS_CMAKE_CPM_OVERRIDE_VERSION_FILE` must be loaded instead of any explicit file path - # when it is set - if(DEFINED RAPIDS_CMAKE_CPM_OVERRIDE_VERSION_FILE) - set(_rapids_override_filepath "${RAPIDS_CMAKE_CPM_OVERRIDE_VERSION_FILE}") - endif() - if(NOT EXISTS "${_rapids_override_filepath}") message(FATAL_ERROR "rapids_cpm_package_override can't load '${_rapids_override_filepath}', verify it exists" ) @@ -96,8 +90,7 @@ function(rapids_cpm_package_override _rapids_override_filepath) string(JSON package_name MEMBER "${json_data}" packages ${index}) string(TOLOWER "${package_name}" normalized_pkg_name) get_property(override_exists GLOBAL PROPERTY rapids_cpm_${normalized_pkg_name}_override_json - DEFINED) - + SET) if(override_exists OR DEFINED CPM_${package_name}_SOURCE) continue() endif() diff --git a/testing/cpm/CMakeLists.txt b/testing/cpm/CMakeLists.txt index 9656335a..00f876c5 100644 --- a/testing/cpm/CMakeLists.txt +++ b/testing/cpm/CMakeLists.txt @@ -62,6 +62,7 @@ add_cmake_config_test( cpm_package_override-defaults-with-different-casing-warni add_cmake_config_test( cpm_package_override-empty-patches.cmake ) add_cmake_config_test( cpm_package_override-empty.cmake ) add_cmake_config_test( cpm_package_override-env-var-support.cmake ) +add_cmake_config_test( cpm_package_override-multiple-cmake-var.cmake ) add_cmake_config_test( cpm_package_override-multiple.cmake ) add_cmake_config_test( cpm_package_override-no-version-value.cmake SHOULD_FAIL "rapids_cmake can't parse") add_cmake_config_test( cpm_package_override-obey-cpm-source-var.cmake ) diff --git a/testing/cpm/cpm_init-override-multiple.cmake b/testing/cpm/cpm_init-override-multiple.cmake index 562f86b7..25851352 100644 --- a/testing/cpm/cpm_init-override-multiple.cmake +++ b/testing/cpm/cpm_init-override-multiple.cmake @@ -18,7 +18,7 @@ include(${rapids-cmake-dir}/cpm/init.cmake) function(expect_fetch_content_details project expected) string(TOLOWER ${project} project) set(internal_fetch_content_var_name "_FetchContent_${project}_savedDetails") - get_property(exists GLOBAL PROPERTY ${internal_fetch_content_var_name} DEFINED) + get_property(exists GLOBAL PROPERTY ${internal_fetch_content_var_name} SET) if(expected AND NOT exists) message(FATAL_ERROR "FetchContent expected ${project} doesn't match expected[${exists}!=${expected})") elseif(NOT expected AND exists) diff --git a/testing/cpm/cpm_package_override-multiple-cmake-var.cmake b/testing/cpm/cpm_package_override-multiple-cmake-var.cmake new file mode 100644 index 00000000..6c70e3d7 --- /dev/null +++ b/testing/cpm/cpm_package_override-multiple-cmake-var.cmake @@ -0,0 +1,69 @@ +#============================================================================= +# Copyright (c) 2024, NVIDIA CORPORATION. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +#============================================================================= +include(${rapids-cmake-dir}/cpm/init.cmake) +include(${rapids-cmake-dir}/cpm/package_override.cmake) + +# Need to write out an override file +file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/override1.json + [=[ +{ + "packages": { + "nvbench": { + "git_tag": "my_tag" + }, + "gtest": { + "version": "2.99" + } + } +} + ]=]) + +file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/override2.json + [=[ +{ + "packages": { + "rmm": { + "git_tag": "new_rmm_tag" + }, + "GTest": { + "version": "3.99" + } + } +} + ]=]) + +set(RAPIDS_CMAKE_CPM_OVERRIDE_VERSION_FILE "${CMAKE_CURRENT_BINARY_DIR}/override1.json") +rapids_cpm_init() +rapids_cpm_package_override(${CMAKE_CURRENT_BINARY_DIR}/override2.json) + +# Verify that the override works +rapids_cpm_package_details(nvbench version repository tag shallow exclude) +if(NOT tag STREQUAL "my_tag") + message(FATAL_ERROR "custom git_tag field was ignored. ${tag} found instead of my_url") +endif() + +rapids_cpm_package_details(GTest version repository tag shallow exclude) +if(NOT version STREQUAL "2.99") + message(FATAL_ERROR "custom version field was removed. ${version} was found instead") +endif() +if(NOT tag MATCHES "2.99") + message(FATAL_ERROR "custom version field not used when computing git_tag value. ${tag} was found instead") +endif() + +rapids_cpm_package_details(rmm version repository tag shallow exclude) +if(NOT tag MATCHES "new_rmm_tag") + message(FATAL_ERROR "custom version field not used when computing git_tag value. ${tag} was found instead") +endif() diff --git a/testing/cpm/cpm_package_override-multiple.cmake b/testing/cpm/cpm_package_override-multiple.cmake index d4fc1f1d..e67e943e 100644 --- a/testing/cpm/cpm_package_override-multiple.cmake +++ b/testing/cpm/cpm_package_override-multiple.cmake @@ -69,10 +69,10 @@ if(NOT tag STREQUAL "my_tag") endif() rapids_cpm_package_details(GTest version repository tag shallow exclude) -if(NOT version STREQUAL "3.99") +if(NOT version STREQUAL "2.99") message(FATAL_ERROR "custom version field was removed. ${version} was found instead") endif() -if(NOT tag MATCHES "3.99") +if(NOT tag MATCHES "2.99") message(FATAL_ERROR "custom version field not used when computing git_tag value. ${tag} was found instead") endif()