diff --git a/rapids-cmake/cpm/find.cmake b/rapids-cmake/cpm/find.cmake index e65b47c4..52219c33 100644 --- a/rapids-cmake/cpm/find.cmake +++ b/rapids-cmake/cpm/find.cmake @@ -44,16 +44,6 @@ consistency. List all targets used by your project in `GLOBAL_TARGET`. .. note:: Requires :cmake:command:`rapids_cpm_init` to be called before usage -.. note:: - Adding an export set to :cmake:command:`rapids_cpm_find` has different behavior - for build and install. Build exports a respective CPM call, since - we presume other CPM packages don't generate a correct build directory - config module. While install exports a `find_dependency` call as - we expect projects to have a valid install setup. - - If you need different behavior you will need to use :cmake:command:`rapids_export_package()` - or :cmake:command:`rapids_export_cpm()`. - ``PackageName`` Name of the package to find. @@ -64,6 +54,13 @@ consistency. List all targets used by your project in `GLOBAL_TARGET`. Which targets from this package should be made global. This information will be propagated to any associated export set. + .. versionchanged:: v21.10.00 + If any targets listed in `GLOBAL_TARGET` exist when :cmake:command:`rapids_cpm_find` is called + no calls to `CPM` will be executed. This is done for the following reasons: + + - Removes the need for the calling code to do the conditional checks + - Allows `BUILD_EXPORT_SET` and `INSTALL_EXPORT_SET` tracking to happen correctly when targets had already been brought it by non-CPM means. + ``BUILD_EXPORT_SET`` Record that a :cmake:command:`CPMFindPackage( ...)` call needs to occur as part of our build directory export set. @@ -77,6 +74,16 @@ consistency. List all targets used by your project in `GLOBAL_TARGET`. be passed down to :cmake:command:`CPMFindPackage`. +.. note:: + Adding an export set to :cmake:command:`rapids_cpm_find` has different behavior + for build and install. Build exports a respective CPM call, since + we presume other CPM packages don't generate a correct build directory + config module. While install exports a `find_dependency` call as + we expect projects to have a valid install setup. + + If you need different behavior you will need to use :cmake:command:`rapids_export_package()` + or :cmake:command:`rapids_export_cpm()`. + #]=======================================================================] function(rapids_cpm_find name version) list(APPEND CMAKE_MESSAGE_CONTEXT "rapids.cpm.find") @@ -89,14 +96,25 @@ function(rapids_cpm_find name version) message(FATAL_ERROR "rapids_cpm_find requires you to specify CPM_ARGS before any CPM arguments") endif() - CPMFindPackage(NAME ${name} VERSION ${version} ${RAPIDS_UNPARSED_ARGUMENTS}) + set(package_needs_to_be_added TRUE) if(RAPIDS_GLOBAL_TARGETS) - include("${rapids-cmake-dir}/cmake/make_global.cmake") - rapids_cmake_make_global(RAPIDS_GLOBAL_TARGETS) + foreach(target IN LISTS RAPIDS_GLOBAL_TARGETS) + if(TARGET ${target}) + set(package_needs_to_be_added FALSE) + break() + endif() + endforeach() + endif() + + if(package_needs_to_be_added) + CPMFindPackage(NAME ${name} VERSION ${version} ${RAPIDS_UNPARSED_ARGUMENTS}) endif() set(extra_info) if(RAPIDS_GLOBAL_TARGETS) + include("${rapids-cmake-dir}/cmake/make_global.cmake") + rapids_cmake_make_global(RAPIDS_GLOBAL_TARGETS) + set(extra_info "GLOBAL_TARGETS") list(APPEND extra_info ${RAPIDS_GLOBAL_TARGETS}) endif() diff --git a/testing/cpm/CMakeLists.txt b/testing/cpm/CMakeLists.txt index 35a2cfea..0c46f8cc 100644 --- a/testing/cpm/CMakeLists.txt +++ b/testing/cpm/CMakeLists.txt @@ -16,4 +16,6 @@ add_cmake_config_test( rapids-cpm.cmake ) add_cmake_config_test( cpm_find-existing-build-dir ) +add_cmake_config_test( cpm_find-existing-target ) +add_cmake_config_test( cpm_find-existing-target-to-export-sets ) add_cmake_config_test( cpm_find-options-escaped ) diff --git a/testing/cpm/cpm_find-existing-target-to-export-sets/CMakeLists.txt b/testing/cpm/cpm_find-existing-target-to-export-sets/CMakeLists.txt new file mode 100644 index 00000000..7a827cd7 --- /dev/null +++ b/testing/cpm/cpm_find-existing-target-to-export-sets/CMakeLists.txt @@ -0,0 +1,82 @@ +#============================================================================= +# Copyright (c) 2018-2021, 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. +#============================================================================= +cmake_minimum_required(VERSION 3.20) + +include(${rapids-cmake-dir}/cpm/init.cmake) +include(${rapids-cmake-dir}/cpm/find.cmake) +include(${rapids-cmake-dir}/export/write_dependencies.cmake) + +project(rapids-existing-target LANGUAGES CXX) + +include("${rapids-cmake-testing-dir}/cpm/make_fake_project_build_dir_with_config.cmake") + +make_fake_project_build_dir_with_config(RapidsTestFind 2021.01.02 + RapidsTestFindConfig.cmake + RapidsTestFindConfigVersion.cmake) + +rapids_cpm_init() + +set(CMAKE_PREFIX_PATH + "${CMAKE_CURRENT_BINARY_DIR}/RapidsTestFind-build/" + ) + + +add_library(RapidsTest::RapidsTest IMPORTED INTERFACE) + +rapids_cpm_find(RapidsTestFind 2021.01.02 + GLOBAL_TARGETS RapidsTest::RapidsTest + INSTALL_EXPORT_SET setA) + +rapids_cpm_find(RapidsTestFind 2021.01.02 + GLOBAL_TARGETS RapidsTest::RapidsTest + INSTALL_EXPORT_SET setB) + +rapids_cpm_find(RapidsTestFind 2021.01.02 + GLOBAL_TARGETS RapidsTest::RapidsTest + BUILD_EXPORT_SET setB) + + +function(verify_dependency_file file_name to_match) + file(STRINGS "${file_name}" text) + set(package_dependency_found FALSE) + + foreach(line IN LISTS text) + if( line MATCHES ${to_match}) + set(package_dependency_found TRUE) + break() + endif() + endforeach() + + if(NOT package_dependency_found) + message(FATAL_ERROR "${file_name} failed to export RapidsTestFind properly") + endif() + +endfunction() + +set(file_path "${CMAKE_CURRENT_BINARY_DIR}/build_export_set.cmake") +file(REMOVE "${file_path}") +rapids_export_write_dependencies(build setB "${file_path}") +verify_dependency_file("${file_path}" "[=[NAME;RapidsTestFind;VERSION;2021.01.02]=]") + +set(file_path "${CMAKE_CURRENT_BINARY_DIR}/install_export_setA.cmake") +file(REMOVE "${CMAKE_CURRENT_BINARY_DIR}/install_export_setA.cmake") +rapids_export_write_dependencies(build setB "${file_path}") +verify_dependency_file("${file_path}" "[=[find_dependency(RapidsTestFind)]=]") + +set(file_path "${CMAKE_CURRENT_BINARY_DIR}/install_export_setB.cmake") +file(REMOVE "${CMAKE_CURRENT_BINARY_DIR}/install_export_setB.cmake") +rapids_export_write_dependencies(build setB "${file_path}") +verify_dependency_file("${file_path}" "[=[find_dependency(RapidsTestFind)]=]") diff --git a/testing/cpm/cpm_find-existing-target-to-export-sets/RapidsTestFindConfig.cmake b/testing/cpm/cpm_find-existing-target-to-export-sets/RapidsTestFindConfig.cmake new file mode 100644 index 00000000..3860c97e --- /dev/null +++ b/testing/cpm/cpm_find-existing-target-to-export-sets/RapidsTestFindConfig.cmake @@ -0,0 +1,22 @@ +#============================================================================= +# Copyright (c) 2018-2021, 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. +#============================================================================= +@PACKAGE_INIT@ + +include("${CMAKE_CURRENT_LIST_DIR}/RapidsTestFindConfigVersion.cmake") + +add_library(RapidsTest::RapidsTest IMPORTED INTERFACE GLOBAL) + +check_required_components(RapidsTest2) diff --git a/testing/cpm/cpm_find-existing-target/CMakeLists.txt b/testing/cpm/cpm_find-existing-target/CMakeLists.txt new file mode 100644 index 00000000..acac66c5 --- /dev/null +++ b/testing/cpm/cpm_find-existing-target/CMakeLists.txt @@ -0,0 +1,37 @@ +#============================================================================= +# Copyright (c) 2018-2021, 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/find.cmake) + +cmake_minimum_required(VERSION 3.20) +project(rapids-existing-target LANGUAGES CXX) + +include("${rapids-cmake-testing-dir}/cpm/make_fake_project_build_dir_with_config.cmake") + +make_fake_project_build_dir_with_config(RapidsTestFind 2021.01.02 + RapidsTestFindConfig.cmake + RapidsTestFindConfigVersion.cmake) + +rapids_cpm_init() + +set(CMAKE_PREFIX_PATH + "${CMAKE_CURRENT_BINARY_DIR}/RapidsTestFind-build/" + ) + + +add_library(RapidsTest::RapidsTest IMPORTED INTERFACE) + +rapids_cpm_find(RapidsTestFind 2021.01.02 GLOBAL_TARGETS RapidsTest::RapidsTest) diff --git a/testing/cpm/cpm_find-existing-target/RapidsTestFindConfig.cmake b/testing/cpm/cpm_find-existing-target/RapidsTestFindConfig.cmake new file mode 100644 index 00000000..3860c97e --- /dev/null +++ b/testing/cpm/cpm_find-existing-target/RapidsTestFindConfig.cmake @@ -0,0 +1,22 @@ +#============================================================================= +# Copyright (c) 2018-2021, 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. +#============================================================================= +@PACKAGE_INIT@ + +include("${CMAKE_CURRENT_LIST_DIR}/RapidsTestFindConfigVersion.cmake") + +add_library(RapidsTest::RapidsTest IMPORTED INTERFACE GLOBAL) + +check_required_components(RapidsTest2) diff --git a/testing/export/write_dependencies-duplicate-packages.cmake b/testing/export/write_dependencies-duplicate-packages.cmake index 38d21819..f6c4f5ce 100644 --- a/testing/export/write_dependencies-duplicate-packages.cmake +++ b/testing/export/write_dependencies-duplicate-packages.cmake @@ -29,7 +29,6 @@ rapids_export_write_dependencies(build test_set "${CMAKE_CURRENT_BINARY_DIR}/exp file(STRINGS "${CMAKE_CURRENT_BINARY_DIR}/export_set.cmake" text) -set(duplicate_package ) foreach(line IN LISTS text) # message(STATUS "1. line: ${line}") if( line MATCHES "find_dependency\\(ExactlyDuplicate\\)" )