From 7b7421346194896109a304ff088630f37ab2bd9e Mon Sep 17 00:00:00 2001 From: Brad King Date: Wed, 5 Dec 2018 11:57:47 -0500 Subject: CUDA: Fix crash on linking to a CUDA target without CUDA enabled Do not try to device link or add CUDA runtime libraries if the language is not enabled. Fixes: #18673 Issue: #18614 --- Source/cmMakefileExecutableTargetGenerator.cxx | 4 ++++ Source/cmNinjaNormalTargetGenerator.cxx | 4 ++++ Source/cmVisualStudio10TargetGenerator.cxx | 3 ++- Tests/RunCMake/Languages/ExternalCUDA.cmake | 8 ++++++++ Tests/RunCMake/Languages/RunCMakeTest.cmake | 2 ++ Tests/RunCMake/Languages/empty.c | 0 6 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 Tests/RunCMake/Languages/ExternalCUDA.cmake create mode 100644 Tests/RunCMake/Languages/empty.c diff --git a/Source/cmMakefileExecutableTargetGenerator.cxx b/Source/cmMakefileExecutableTargetGenerator.cxx index 08bb2ce..846b12c 100644 --- a/Source/cmMakefileExecutableTargetGenerator.cxx +++ b/Source/cmMakefileExecutableTargetGenerator.cxx @@ -84,6 +84,10 @@ void cmMakefileExecutableTargetGenerator::WriteDeviceExecutableRule( bool relink) { #ifdef CMAKE_BUILD_WITH_CMAKE + if (!this->GlobalGenerator->GetLanguageEnabled("CUDA")) { + return; + } + const std::string cuda_lang("CUDA"); cmGeneratorTarget::LinkClosure const* closure = this->GeneratorTarget->GetLinkClosure(this->ConfigName); diff --git a/Source/cmNinjaNormalTargetGenerator.cxx b/Source/cmNinjaNormalTargetGenerator.cxx index 6436969..8909e06 100644 --- a/Source/cmNinjaNormalTargetGenerator.cxx +++ b/Source/cmNinjaNormalTargetGenerator.cxx @@ -558,6 +558,10 @@ std::vector cmNinjaNormalTargetGenerator::ComputeLinkCmd() void cmNinjaNormalTargetGenerator::WriteDeviceLinkStatement() { + if (!this->GetGlobalGenerator()->GetLanguageEnabled("CUDA")) { + return; + } + cmGeneratorTarget& genTarget = *this->GetGeneratorTarget(); // determine if we need to do any device linking for this target diff --git a/Source/cmVisualStudio10TargetGenerator.cxx b/Source/cmVisualStudio10TargetGenerator.cxx index 334c15b..d5e834b 100644 --- a/Source/cmVisualStudio10TargetGenerator.cxx +++ b/Source/cmVisualStudio10TargetGenerator.cxx @@ -3331,7 +3331,8 @@ bool cmVisualStudio10TargetGenerator::ComputeLinkOptions( std::vector vsTargetVec; this->AddLibraries(cli, libVec, vsTargetVec, config); if (std::find(linkClosure->Languages.begin(), linkClosure->Languages.end(), - "CUDA") != linkClosure->Languages.end()) { + "CUDA") != linkClosure->Languages.end() && + this->CudaOptions[config] != nullptr) { switch (this->CudaOptions[config]->GetCudaRuntime()) { case cmVisualStudioGeneratorOptions::CudaRuntimeStatic: libVec.push_back("cudadevrt.lib"); diff --git a/Tests/RunCMake/Languages/ExternalCUDA.cmake b/Tests/RunCMake/Languages/ExternalCUDA.cmake new file mode 100644 index 0000000..3afa93e --- /dev/null +++ b/Tests/RunCMake/Languages/ExternalCUDA.cmake @@ -0,0 +1,8 @@ +enable_language(C) + +add_library(ext_cuda IMPORTED STATIC) +set_property(TARGET ext_cuda PROPERTY IMPORTED_LOCATION "/does_not_exist") +set_property(TARGET ext_cuda PROPERTY IMPORTED_LINK_INTERFACE_LANGUAGES "CUDA") + +add_executable(main empty.c) +target_link_libraries(main ext_cuda) diff --git a/Tests/RunCMake/Languages/RunCMakeTest.cmake b/Tests/RunCMake/Languages/RunCMakeTest.cmake index 732baae..2a534b3 100644 --- a/Tests/RunCMake/Languages/RunCMakeTest.cmake +++ b/Tests/RunCMake/Languages/RunCMakeTest.cmake @@ -6,3 +6,5 @@ run_cmake(link-libraries-TARGET_FILE-genex) run_cmake(link-libraries-TARGET_FILE-genex-ok) run_cmake(DetermineFail) + +run_cmake(ExternalCUDA) diff --git a/Tests/RunCMake/Languages/empty.c b/Tests/RunCMake/Languages/empty.c new file mode 100644 index 0000000..e69de29 -- cgit v0.12