From a4ea2931532d7b5b468bd2670a5f23198763a24e Mon Sep 17 00:00:00 2001 From: Robert Maynard Date: Mon, 18 May 2020 14:34:32 -0400 Subject: Help: Correct CMAKE_CUDA_RUNTIME_LIBRARY applicability --- Help/variable/CMAKE_CUDA_RUNTIME_LIBRARY.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Help/variable/CMAKE_CUDA_RUNTIME_LIBRARY.rst b/Help/variable/CMAKE_CUDA_RUNTIME_LIBRARY.rst index ea1c1b8..698cae7 100644 --- a/Help/variable/CMAKE_CUDA_RUNTIME_LIBRARY.rst +++ b/Help/variable/CMAKE_CUDA_RUNTIME_LIBRARY.rst @@ -1,7 +1,7 @@ CMAKE_CUDA_RUNTIME_LIBRARY -------------------------- -Select the CUDA runtime library for use by compilers targeting the MSVC ABI. +Select the CUDA runtime library for use when compiling and linking CUDA. This variable is used to initialize the :prop_tgt:`CUDA_RUNTIME_LIBRARY` property on all targets as they are created. -- cgit v0.12 From 1086e930dc8ad94847f17f627673db8f7a3fb3ab Mon Sep 17 00:00:00 2001 From: Robert Maynard Date: Mon, 18 May 2020 10:54:11 -0400 Subject: CUDA: Propagate CMAKE_CUDA_RUNTIME_LIBRARY state to try_compile --- Help/command/try_compile.rst | 1 + Source/cmCoreTryCompile.cxx | 3 +++ 2 files changed, 4 insertions(+) diff --git a/Help/command/try_compile.rst b/Help/command/try_compile.rst index edbf92c..323077a 100644 --- a/Help/command/try_compile.rst +++ b/Help/command/try_compile.rst @@ -135,6 +135,7 @@ If set, the following variables are passed in to the generated try_compile CMakeLists.txt to initialize compile target properties with default values: +* :variable:`CMAKE_CUDA_RUNTIME_LIBRARY` * :variable:`CMAKE_ENABLE_EXPORTS` * :variable:`CMAKE_LINK_SEARCH_START_STATIC` * :variable:`CMAKE_LINK_SEARCH_END_STATIC` diff --git a/Source/cmCoreTryCompile.cxx b/Source/cmCoreTryCompile.cxx index da04396..73f099b 100644 --- a/Source/cmCoreTryCompile.cxx +++ b/Source/cmCoreTryCompile.cxx @@ -40,6 +40,8 @@ static std::string const kCMAKE_CXX_LINK_NO_PIE_SUPPORTED = "CMAKE_CXX_LINK_NO_PIE_SUPPORTED"; static std::string const kCMAKE_CXX_LINK_PIE_SUPPORTED = "CMAKE_CXX_LINK_PIE_SUPPORTED"; +static std::string const kCMAKE_CUDA_RUNTIME_LIBRARY = + "CMAKE_CUDA_RUNTIME_LIBRARY"; static std::string const kCMAKE_ENABLE_EXPORTS = "CMAKE_ENABLE_EXPORTS"; static std::string const kCMAKE_LINK_SEARCH_END_STATIC = "CMAKE_LINK_SEARCH_END_STATIC"; @@ -719,6 +721,7 @@ int cmCoreTryCompile::TryCompileCode(std::vector const& argv, vars.insert(kCMAKE_C_COMPILER_TARGET); vars.insert(kCMAKE_CXX_COMPILER_EXTERNAL_TOOLCHAIN); vars.insert(kCMAKE_CXX_COMPILER_TARGET); + vars.insert(kCMAKE_CUDA_RUNTIME_LIBRARY); vars.insert(kCMAKE_ENABLE_EXPORTS); vars.insert(kCMAKE_LINK_SEARCH_END_STATIC); vars.insert(kCMAKE_LINK_SEARCH_START_STATIC); -- cgit v0.12 From e55b21e24ee81f5433b8ec778aa341c485d6e149 Mon Sep 17 00:00:00 2001 From: Robert Maynard Date: Mon, 18 May 2020 15:29:18 -0400 Subject: CUDA: Compute CMAKE_CUDA_RUNTIME_LIBRARY default from toolchain Since commit 0d0145138f (CUDA: Add abstraction for cuda runtime selection, 2019-11-29, v3.17.0-rc1~83^2) we add CUDA runtime library selection flags by default. To maintain backwards compatibility the default CUDA runtime library needs to be computed based on what libraries are found on the initial compiler invocation. For example a toolchain could establish initial flags that have all CUDA compilations using the runtime version, and if we don't detect this we will try to link to both the static and shared runtime. Co-Author: Brad King Fixes: #20708 --- Help/prop_tgt/CUDA_RUNTIME_LIBRARY.rst | 4 ++-- Help/variable/CMAKE_CUDA_RUNTIME_LIBRARY.rst | 3 ++- Modules/CMakeCUDACompiler.cmake.in | 2 ++ Modules/CMakeDetermineCUDACompiler.cmake | 28 ++++++++++++++++++++++ Modules/CMakeDetermineCompilerId.cmake | 6 ++++- Modules/Compiler/NVIDIA-CUDA.cmake | 1 - Modules/Platform/Windows-NVIDIA-CUDA.cmake | 1 - Tests/CudaOnly/CMakeLists.txt | 1 + .../SharedRuntimeViaCUDAFlags/CMakeLists.txt | 15 ++++++++++++ Tests/CudaOnly/SharedRuntimeViaCUDAFlags/main.cu | 5 ++++ 10 files changed, 60 insertions(+), 6 deletions(-) create mode 100644 Tests/CudaOnly/SharedRuntimeViaCUDAFlags/CMakeLists.txt create mode 100644 Tests/CudaOnly/SharedRuntimeViaCUDAFlags/main.cu diff --git a/Help/prop_tgt/CUDA_RUNTIME_LIBRARY.rst b/Help/prop_tgt/CUDA_RUNTIME_LIBRARY.rst index 0782765..11b344c 100644 --- a/Help/prop_tgt/CUDA_RUNTIME_LIBRARY.rst +++ b/Help/prop_tgt/CUDA_RUNTIME_LIBRARY.rst @@ -10,8 +10,8 @@ The allowed case insensitive values are: Contents of ``CUDA_RUNTIME_LIBRARY`` may use :manual:`generator expressions `. -If this property is not set then CMake uses the default value -``Static`` to select the CUDA runtime library. +If that property is not set then CMake uses an appropriate default +value based on the compiler to select the CUDA runtime library. .. note:: diff --git a/Help/variable/CMAKE_CUDA_RUNTIME_LIBRARY.rst b/Help/variable/CMAKE_CUDA_RUNTIME_LIBRARY.rst index 698cae7..e3205d3 100644 --- a/Help/variable/CMAKE_CUDA_RUNTIME_LIBRARY.rst +++ b/Help/variable/CMAKE_CUDA_RUNTIME_LIBRARY.rst @@ -14,7 +14,8 @@ Contents of ``CMAKE_CUDA_RUNTIME_LIBRARY`` may use If this variable is not set then the :prop_tgt:`CUDA_RUNTIME_LIBRARY` target property will not be set automatically. If that property is not set then -CMake uses the default value ``Static`` to select the CUDA runtime library. +CMake uses an appropriate default value based on the compiler to select the +CUDA runtime library. .. note:: diff --git a/Modules/CMakeCUDACompiler.cmake.in b/Modules/CMakeCUDACompiler.cmake.in index 4a615a3..04a4c42 100644 --- a/Modules/CMakeCUDACompiler.cmake.in +++ b/Modules/CMakeCUDACompiler.cmake.in @@ -53,5 +53,7 @@ set(CMAKE_CUDA_IMPLICIT_LINK_LIBRARIES "@CMAKE_CUDA_IMPLICIT_LINK_LIBRARIES@") set(CMAKE_CUDA_IMPLICIT_LINK_DIRECTORIES "@CMAKE_CUDA_IMPLICIT_LINK_DIRECTORIES@") set(CMAKE_CUDA_IMPLICIT_LINK_FRAMEWORK_DIRECTORIES "@CMAKE_CUDA_IMPLICIT_LINK_FRAMEWORK_DIRECTORIES@") +@_SET_CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT@ + set(CMAKE_LINKER "@CMAKE_LINKER@") set(CMAKE_MT "@CMAKE_MT@") diff --git a/Modules/CMakeDetermineCUDACompiler.cmake b/Modules/CMakeDetermineCUDACompiler.cmake index 7d7fb9a..baf1501 100644 --- a/Modules/CMakeDetermineCUDACompiler.cmake +++ b/Modules/CMakeDetermineCUDACompiler.cmake @@ -86,6 +86,20 @@ if(${CMAKE_GENERATOR} MATCHES "Visual Studio") set(CMAKE_CUDA_HOST_IMPLICIT_LINK_LIBRARIES "") set(CMAKE_CUDA_HOST_IMPLICIT_LINK_DIRECTORIES "") set(CMAKE_CUDA_HOST_IMPLICIT_LINK_FRAMEWORK_DIRECTORIES "") + + # We do not currently detect CMAKE_CUDA_HOST_IMPLICIT_LINK_LIBRARIES but we + # do need to detect CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT from the compiler by + # looking at which cudart library exists in the implicit link libraries passed + # to the host linker. + if(CMAKE_CUDA_COMPILER_PRODUCED_OUTPUT MATCHES "link\\.exe [^\n]*cudart_static\\.lib") + set(CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT "STATIC") + elseif(CMAKE_CUDA_COMPILER_PRODUCED_OUTPUT MATCHES "link\\.exe [^\n]*cudart\\.lib") + set(CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT "SHARED") + else() + set(CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT "NONE") + endif() + set(_SET_CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT + "set(CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT \"${CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT}\")") elseif(CMAKE_CUDA_COMPILER_ID STREQUAL NVIDIA) set(_nvcc_log "") string(REPLACE "\r" "" _nvcc_output_orig "${CMAKE_CUDA_COMPILER_PRODUCED_OUTPUT}") @@ -179,6 +193,20 @@ elseif(CMAKE_CUDA_COMPILER_ID STREQUAL NVIDIA) log "${CMAKE_CUDA_IMPLICIT_OBJECT_REGEX}") + # Detect CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT from the compiler by looking at which + # cudart library exists in the implicit link libraries passed to the host linker. + # This is required when a project sets the cuda runtime library as part of the + # initial flags. + if(";${CMAKE_CUDA_HOST_IMPLICIT_LINK_LIBRARIES};" MATCHES [[;cudart_static(\.lib)?;]]) + set(CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT "STATIC") + elseif(";${CMAKE_CUDA_HOST_IMPLICIT_LINK_LIBRARIES};" MATCHES [[;cudart(\.lib)?;]]) + set(CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT "SHARED") + else() + set(CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT "NONE") + endif() + set(_SET_CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT + "set(CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT \"${CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT}\")") + file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log "Parsed CUDA nvcc implicit link information from above output:\n${_nvcc_log}\n${log}\n\n") else() diff --git a/Modules/CMakeDetermineCompilerId.cmake b/Modules/CMakeDetermineCompilerId.cmake index d3ed45b..9eeb7a1 100644 --- a/Modules/CMakeDetermineCompilerId.cmake +++ b/Modules/CMakeDetermineCompilerId.cmake @@ -381,7 +381,11 @@ Id flags: ${testflags} ${CMAKE_${lang}_COMPILER_ID_FLAGS_ALWAYS} if(CMAKE_VS_PLATFORM_NAME STREQUAL x64) set(id_ItemDefinitionGroup_entry "64%(AdditionalOptions)-v") endif() - set(id_Link_AdditionalDependencies "cudart.lib") + if(CMAKE_CUDA_FLAGS MATCHES "(^| )-cudart +shared( |$)") + set(id_Link_AdditionalDependencies "cudart.lib") + else() + set(id_Link_AdditionalDependencies "cudart_static.lib") + endif() endif() configure_file(${CMAKE_ROOT}/Modules/CompilerId/VS-${v}.${ext}.in ${id_dir}/CompilerId${lang}.${ext} @ONLY) diff --git a/Modules/Compiler/NVIDIA-CUDA.cmake b/Modules/Compiler/NVIDIA-CUDA.cmake index 1f4d54d..671468d 100644 --- a/Modules/Compiler/NVIDIA-CUDA.cmake +++ b/Modules/Compiler/NVIDIA-CUDA.cmake @@ -43,7 +43,6 @@ endif() set(CMAKE_SHARED_LIBRARY_CREATE_CUDA_FLAGS -shared) set(CMAKE_INCLUDE_SYSTEM_FLAG_CUDA -isystem=) -set(CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT "STATIC") set(CMAKE_CUDA_RUNTIME_LIBRARY_LINK_OPTIONS_STATIC "cudadevrt;cudart_static") set(CMAKE_CUDA_RUNTIME_LIBRARY_LINK_OPTIONS_SHARED "cudadevrt;cudart") set(CMAKE_CUDA_RUNTIME_LIBRARY_LINK_OPTIONS_NONE "") diff --git a/Modules/Platform/Windows-NVIDIA-CUDA.cmake b/Modules/Platform/Windows-NVIDIA-CUDA.cmake index f809094..98cd5ef 100644 --- a/Modules/Platform/Windows-NVIDIA-CUDA.cmake +++ b/Modules/Platform/Windows-NVIDIA-CUDA.cmake @@ -69,7 +69,6 @@ else() endif() unset(_cmp0092) -set(CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT "STATIC") set(CMAKE_CUDA_RUNTIME_LIBRARY_LINK_OPTIONS_STATIC "cudadevrt;cudart_static") set(CMAKE_CUDA_RUNTIME_LIBRARY_LINK_OPTIONS_SHARED "cudadevrt;cudart") set(CMAKE_CUDA_RUNTIME_LIBRARY_LINK_OPTIONS_NONE "") diff --git a/Tests/CudaOnly/CMakeLists.txt b/Tests/CudaOnly/CMakeLists.txt index cc1ee1a..3e3e44c 100644 --- a/Tests/CudaOnly/CMakeLists.txt +++ b/Tests/CudaOnly/CMakeLists.txt @@ -6,6 +6,7 @@ ADD_TEST_MACRO(CudaOnly.GPUDebugFlag CudaOnlyGPUDebugFlag) ADD_TEST_MACRO(CudaOnly.ResolveDeviceSymbols CudaOnlyResolveDeviceSymbols) ADD_TEST_MACRO(CudaOnly.SeparateCompilation CudaOnlySeparateCompilation) ADD_TEST_MACRO(CudaOnly.SharedRuntimePlusToolkit CudaOnlySharedRuntimePlusToolkit) +ADD_TEST_MACRO(CudaOnly.SharedRuntimeViaCUDAFlags CudaOnlySharedRuntimeViaCUDAFlags) ADD_TEST_MACRO(CudaOnly.Standard98 CudaOnlyStandard98) ADD_TEST_MACRO(CudaOnly.Toolkit CudaOnlyToolkit) ADD_TEST_MACRO(CudaOnly.WithDefs CudaOnlyWithDefs) diff --git a/Tests/CudaOnly/SharedRuntimeViaCUDAFlags/CMakeLists.txt b/Tests/CudaOnly/SharedRuntimeViaCUDAFlags/CMakeLists.txt new file mode 100644 index 0000000..24ff478 --- /dev/null +++ b/Tests/CudaOnly/SharedRuntimeViaCUDAFlags/CMakeLists.txt @@ -0,0 +1,15 @@ +cmake_minimum_required(VERSION 3.17) +project(SharedRuntimeViaCUDAFlags NONE) + +set(CMAKE_CUDA_FLAGS "") +string(APPEND CMAKE_CUDA_FLAGS "-cudart shared") + +enable_language(CUDA) + +add_executable(CudaOnlySharedRuntimeViaCUDAFlags main.cu) + +if(UNIX) + # Help the shared cuda runtime find libcudart as it is not located + # in a default system searched location + set_property(TARGET CudaOnlySharedRuntimeViaCUDAFlags PROPERTY BUILD_RPATH ${CMAKE_CUDA_IMPLICIT_LINK_DIRECTORIES}) +endif() diff --git a/Tests/CudaOnly/SharedRuntimeViaCUDAFlags/main.cu b/Tests/CudaOnly/SharedRuntimeViaCUDAFlags/main.cu new file mode 100644 index 0000000..766b775 --- /dev/null +++ b/Tests/CudaOnly/SharedRuntimeViaCUDAFlags/main.cu @@ -0,0 +1,5 @@ + +int main() +{ + return 0; +} -- cgit v0.12