diff --git a/rapids-cmake/test/add.cmake b/rapids-cmake/test/add.cmake index f36a64ad..47180d16 100644 --- a/rapids-cmake/test/add.cmake +++ b/rapids-cmake/test/add.cmake @@ -1,5 +1,5 @@ #============================================================================= -# Copyright (c) 2022-2023, NVIDIA CORPORATION. +# Copyright (c) 2022-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. @@ -70,7 +70,7 @@ function(rapids_test_add) list(APPEND CMAKE_MESSAGE_CONTEXT "rapids.test.add") set(options) - set(one_value NAME WORKING_DIRECTORY GPUS PERCENT INSTALL_COMPONENT_SET) + set(one_value NAME WORKING_DIRECTORY GPUS PERCENT INSTALL_COMPONENT_SET INSTALL_TARGET) set(multi_value COMMAND) cmake_parse_arguments(_RAPIDS_TEST "${options}" "${one_value}" "${multi_value}" ${ARGN}) @@ -89,6 +89,18 @@ function(rapids_test_add) if(TARGET ${command_or_target}) set(command "$") endif() + if(DEFINED _RAPIDS_TEST_INSTALL_TARGET) + if(NOT TARGET ${_RAPIDS_TEST_INSTALL_TARGET}) + message(FATAL_ERROR "rapids_add_test given INSTALL_TARGET \"${_RAPIDS_TEST_INSTALL_TARGET}\", which does not exist" + ) + endif() + set(target_to_install ${_RAPIDS_TEST_INSTALL_TARGET}) + else() + if(NOT TARGET ${command_or_target}) + message(VERBOSE "rapids_add_test could not infer a target to install") + endif() + set(target_to_install ${command_or_target}) + endif() if(NOT DEFINED _RAPIDS_TEST_WORKING_DIRECTORY) set(_RAPIDS_TEST_WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}") @@ -120,8 +132,8 @@ function(rapids_test_add) rapids_test_record_test_component(NAME ${_RAPIDS_TEST_NAME} COMPONENT ${_RAPIDS_TEST_INSTALL_COMPONENT_SET}) - if(TARGET ${command_or_target}) - rapids_test_record_install(TARGET ${command_or_target} COMPONENT + if(TARGET ${target_to_install}) + rapids_test_record_install(TARGET ${target_to_install} COMPONENT ${_RAPIDS_TEST_INSTALL_COMPONENT_SET}) endif() endif() diff --git a/testing/test/CMakeLists.txt b/testing/test/CMakeLists.txt index 6e6ad1ae..feacf0d5 100644 --- a/testing/test/CMakeLists.txt +++ b/testing/test/CMakeLists.txt @@ -50,6 +50,8 @@ add_cmake_config_test(install_relocatable-wrong-component.cmake SHOULD_FAIL "${w add_cmake_ctest_test(add-impossible-allocation SHOULD_FAIL "Insufficient resources for test") add_cmake_config_test(add-with-install-component.cmake) +add_cmake_config_test(add-with-install-target.cmake) +add_cmake_config_test(add-with-install-target-missing.cmake SHOULD_FAIL "rapids_add_test given INSTALL_TARGET \"noexist\", which does not exist") add_cmake_config_test(add-with-no-gpus.cmake) add_cmake_ctest_test(add-no-cuda-toolkit) diff --git a/testing/test/add-with-install-target-missing.cmake b/testing/test/add-with-install-target-missing.cmake new file mode 100644 index 00000000..0a725ac2 --- /dev/null +++ b/testing/test/add-with-install-target-missing.cmake @@ -0,0 +1,27 @@ +#============================================================================= +# Copyright (c) 2022-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}/test/init.cmake) +include(${rapids-cmake-dir}/test/add.cmake) + +enable_language(CUDA) + +rapids_test_init() + +file(WRITE "${CMAKE_BINARY_DIR}/main.cu" "int main(){return 0;}") +add_executable(verify_alloc "${CMAKE_BINARY_DIR}/main.cu") + +enable_testing() +rapids_test_add(NAME simple_test COMMAND verify_alloc GPUS 1 INSTALL_COMPONENT_SET testing INSTALL_TARGET noexist) diff --git a/testing/test/add-with-install-target.cmake b/testing/test/add-with-install-target.cmake new file mode 100644 index 00000000..5b4a9c71 --- /dev/null +++ b/testing/test/add-with-install-target.cmake @@ -0,0 +1,39 @@ +#============================================================================= +# Copyright (c) 2022-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}/test/init.cmake) +include(${rapids-cmake-dir}/test/add.cmake) + +enable_language(CUDA) + +rapids_test_init() + +file(WRITE "${CMAKE_BINARY_DIR}/main.cu" "int main(){return 0;}") +add_executable(verify_alloc "${CMAKE_BINARY_DIR}/main.cu") + +enable_testing() +rapids_test_add(NAME simple_test COMMAND ${CMAKE_COMMAND} -E env verify_alloc GPUS 1 INSTALL_COMPONENT_SET testing INSTALL_TARGET verify_alloc) + +# Verify that we have recorded `simple_test` as part of the `testing` component +get_target_property(names rapids_test_install_testing TESTS_TO_RUN) +if(NOT "simple_test" IN_LIST names) + message(FATAL_ERROR "Failed to record `simple_test` as part of the testing component") +endif() + +# Verify that `verify_alloc` is marked as to be installed +get_target_property(names rapids_test_install_testing TARGETS_TO_INSTALL) +if(NOT "verify_alloc" IN_LIST names) + message(FATAL_ERROR "Failed to record `verify_alloc` as a target to be installed in the testing component") +endif()