diff --git a/cmake-format-rapids-cmake.json b/cmake-format-rapids-cmake.json index b09ce5e5..d57f9fa2 100644 --- a/cmake-format-rapids-cmake.json +++ b/cmake-format-rapids-cmake.json @@ -195,6 +195,11 @@ "nargs": 1 } }, + "rapids_cuda_set_runtime": { + "pargs": { + "nargs": 3 + } + }, "rapids_export_cpm": { "pargs": { diff --git a/docs/api.rst b/docs/api.rst index f91c58f5..6612f609 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -107,6 +107,7 @@ require. rapids_cuda_init_architectures rapids_cuda_init_runtime rapids_cuda_patch_toolkit + rapids_cuda_set_runtime rapids_cuda_set_architectures [Advanced] diff --git a/docs/command/rapids_cuda_set_runtime.rst b/docs/command/rapids_cuda_set_runtime.rst new file mode 100644 index 00000000..16f04f57 --- /dev/null +++ b/docs/command/rapids_cuda_set_runtime.rst @@ -0,0 +1 @@ +.. cmake-module:: ../../rapids-cmake/cuda/set_runtime.cmake diff --git a/rapids-cmake/cuda/set_runtime.cmake b/rapids-cmake/cuda/set_runtime.cmake new file mode 100644 index 00000000..1b110f2a --- /dev/null +++ b/rapids-cmake/cuda/set_runtime.cmake @@ -0,0 +1,72 @@ +#============================================================================= +# Copyright (c) 2023, 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_guard(GLOBAL) + +#[=======================================================================[.rst: +rapids_cuda_set_runtime +------------------------------- + +.. versionadded:: v23.08.00 + +Establish what CUDA runtime library should be used by a single target + + .. code-block:: cmake + + rapids_cuda_set_runtime( target USE_STATIC (TRUE|FALSE) ) + + Establishes what CUDA runtime will be used for a target, via + the :cmake:prop_tgt:`CUDA_RUNTIME_LIBRARY ` + and by linking to `CUDA::cudart` or `CUDA::cudart_static` if the :cmake:module:`find_package(CUDAToolkit) + ` has been called. + + The linking to the `CUDA::cudart` or `CUDA::cudart_static` will have the following + usage behavior: + + - For `INTERFACE` targets the linking will be `INTERFACE` + - For all other targets the linking will be `PRIVATE` + + .. note:: + If using the deprecated `FindCUDA.cmake` you must use the + :cmake:command:`rapids_cuda_init_runtime` method to properly establish the default + mode. + + When `USE_STATIC TRUE` is provided the target will link to a + statically-linked CUDA runtime library. + + When `USE_STATIC FALSE` is provided the target will link to a + shared-linked CUDA runtime library. + + +#]=======================================================================] +function(rapids_cuda_set_runtime target use_static value) + list(APPEND CMAKE_MESSAGE_CONTEXT "rapids.cuda.set_runtime") + + get_target_property(type ${target} TYPE) + if(type STREQUAL "INTERFACE_LIBRARY") + set(mode INTERFACE) + else() + set(mode PRIVATE) + endif() + + if(${value}) + set_target_properties(${target} PROPERTIES CUDA_RUNTIME_LIBRARY STATIC) + target_link_libraries(${target} ${mode} $) + else() + set_target_properties(${target} PROPERTIES CUDA_RUNTIME_LIBRARY SHARED) + target_link_libraries(${target} ${mode} $) + endif() + +endfunction() diff --git a/testing/cuda/CMakeLists.txt b/testing/cuda/CMakeLists.txt index 65859361..212dd237 100644 --- a/testing/cuda/CMakeLists.txt +++ b/testing/cuda/CMakeLists.txt @@ -20,6 +20,10 @@ add_cmake_config_test( init_runtime-multiple.cmake ) add_cmake_config_test( init_runtime-shared.cmake ) add_cmake_config_test( init_runtime-static.cmake ) +add_cmake_config_test( set_runtime-shared.cmake ) +add_cmake_config_test( set_runtime-static.cmake ) +add_cmake_config_test( set_runtime-on-interface-target.cmake ) + add_cmake_config_test( init_arch-all-via-undef ) add_cmake_config_test( init_arch-existing-project-flags.cmake ) add_cmake_config_test( init_arch-native.cmake ) diff --git a/testing/cuda/set_runtime-on-interface-target.cmake b/testing/cuda/set_runtime-on-interface-target.cmake new file mode 100644 index 00000000..73b6f859 --- /dev/null +++ b/testing/cuda/set_runtime-on-interface-target.cmake @@ -0,0 +1,24 @@ +#============================================================================= +# Copyright (c) 2023, 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}/cuda/set_runtime.cmake) + +add_library(uses_cuda INTERFACE) +rapids_cuda_set_runtime(uses_cuda USE_STATIC TRUE) + +get_target_property(linked_libs uses_cuda INTERFACE_LINK_LIBRARIES) +if(NOT "$" IN_LIST linked_libs) + message(FATAL_ERROR "rapids_cuda_set_runtime shouldn't set CUDA::cudart_static in target linked libraries correctly") +endif() diff --git a/testing/cuda/set_runtime-shared.cmake b/testing/cuda/set_runtime-shared.cmake new file mode 100644 index 00000000..f1e77dea --- /dev/null +++ b/testing/cuda/set_runtime-shared.cmake @@ -0,0 +1,31 @@ +#============================================================================= +# Copyright (c) 2023, 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}/cuda/set_runtime.cmake) + +file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/empty.cpp" " ") +add_library(uses_cuda SHARED ${CMAKE_CURRENT_BINARY_DIR}/empty.cpp) +rapids_cuda_set_runtime(uses_cuda USE_STATIC FALSE) + + +get_target_property(runtime_state uses_cuda CUDA_RUNTIME_LIBRARY) +if( NOT runtime_state STREQUAL "SHARED") + message(FATAL_ERROR "rapids_cuda_set_runtime didn't correctly set CUDA_RUNTIME_LIBRARY") +endif() + +get_target_property(linked_libs uses_cuda LINK_LIBRARIES) +if(NOT "$" IN_LIST linked_libs) + message(FATAL_ERROR "rapids_cuda_set_runtime didn't privately link to CUDA::cudart_static") +endif() diff --git a/testing/cuda/set_runtime-static.cmake b/testing/cuda/set_runtime-static.cmake new file mode 100644 index 00000000..c7a1c502 --- /dev/null +++ b/testing/cuda/set_runtime-static.cmake @@ -0,0 +1,30 @@ +#============================================================================= +# Copyright (c) 2023, 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}/cuda/set_runtime.cmake) + +file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/empty.cpp" " ") +add_library(uses_cuda SHARED ${CMAKE_CURRENT_BINARY_DIR}/empty.cpp) +rapids_cuda_set_runtime(uses_cuda USE_STATIC TRUE) + +get_target_property(runtime_state uses_cuda CUDA_RUNTIME_LIBRARY) +if( NOT runtime_state STREQUAL "STATIC") + message(FATAL_ERROR "rapids_cuda_set_runtime didn't correctly set CUDA_RUNTIME_LIBRARY") +endif() + +get_target_property(linked_libs uses_cuda LINK_LIBRARIES) +if(NOT "$" IN_LIST linked_libs) + message(FATAL_ERROR "rapids_cuda_set_runtime didn't privately link to CUDA::cudart_static") +endif()