From dd77dec18d66a2e20480d9bf2ab06231765720a4 Mon Sep 17 00:00:00 2001 From: Raul Tambre Date: Sun, 25 Oct 2020 10:23:10 +0200 Subject: VS: Don't compute CUDA options unless necessary In the following scenario (with 3.18 policies): 1. A CXX target is created. 2. CUDA language is enabled. CMake 3.18 introduced CMP0104, which requires CUDA_ARCHITECTURES to be set. Because the CXX target was created before CUDA was enabled it wouldn't have it set. The Visual Studio generator would however end up computing CUDA compile options for the CXX target, which would result in a fatal error due to the policy violation. There doesn't seem to be a reason to do this for targets that don't actually use the CUDA language, so we can skip and generate the CXX target just fine. Fixes: #21341 --- Source/cmGeneratorTarget.cxx | 8 ++++++++ Source/cmGeneratorTarget.h | 2 ++ Source/cmVisualStudio10TargetGenerator.cxx | 6 ++++-- Tests/RunCMake/CMP0104/CMP0104-Common.cmake | 4 ++++ Tests/RunCMake/CMP0104/main.cxx | 3 +++ 5 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 Tests/RunCMake/CMP0104/main.cxx diff --git a/Source/cmGeneratorTarget.cxx b/Source/cmGeneratorTarget.cxx index ab6a26c..b7bf4a6 100644 --- a/Source/cmGeneratorTarget.cxx +++ b/Source/cmGeneratorTarget.cxx @@ -6914,6 +6914,14 @@ void cmGeneratorTarget::GetLanguages(std::set& languages, } } +bool cmGeneratorTarget::IsLanguageUsed(std::string const& language, + std::string const& config) const +{ + std::set languages; + this->GetLanguages(languages, config); + return languages.count(language); +} + bool cmGeneratorTarget::IsCSharpOnly() const { // Only certain target types may compile CSharp. diff --git a/Source/cmGeneratorTarget.h b/Source/cmGeneratorTarget.h index 3aedbf5..ea3a684 100644 --- a/Source/cmGeneratorTarget.h +++ b/Source/cmGeneratorTarget.h @@ -397,6 +397,8 @@ public: // until we have per-target object file properties. void GetLanguages(std::set& languages, std::string const& config) const; + bool IsLanguageUsed(std::string const& language, + std::string const& config) const; bool IsCSharpOnly() const; diff --git a/Source/cmVisualStudio10TargetGenerator.cxx b/Source/cmVisualStudio10TargetGenerator.cxx index f58c6fd..2ea2839 100644 --- a/Source/cmVisualStudio10TargetGenerator.cxx +++ b/Source/cmVisualStudio10TargetGenerator.cxx @@ -3018,7 +3018,8 @@ bool cmVisualStudio10TargetGenerator::ComputeCudaOptions() return true; } for (std::string const& c : this->Configurations) { - if (!this->ComputeCudaOptions(c)) { + if (this->GeneratorTarget->IsLanguageUsed("CUDA", c) && + !this->ComputeCudaOptions(c)) { return false; } } @@ -3158,7 +3159,8 @@ bool cmVisualStudio10TargetGenerator::ComputeCudaOptions( void cmVisualStudio10TargetGenerator::WriteCudaOptions( Elem& e1, std::string const& configName) { - if (!this->MSTools || !this->GlobalGenerator->IsCudaEnabled()) { + if (!this->MSTools || !this->GlobalGenerator->IsCudaEnabled() || + !this->GeneratorTarget->IsLanguageUsed("CUDA", configName)) { return; } Elem e2(e1, "CudaCompile"); diff --git a/Tests/RunCMake/CMP0104/CMP0104-Common.cmake b/Tests/RunCMake/CMP0104/CMP0104-Common.cmake index b3568f1..ca4c1aa2 100644 --- a/Tests/RunCMake/CMP0104/CMP0104-Common.cmake +++ b/Tests/RunCMake/CMP0104/CMP0104-Common.cmake @@ -1,2 +1,6 @@ +# Make sure CMP0104 isn't issued for CXX targets created prior to enabling CUDA. See #21341. +enable_language(CXX) +add_library(cxx main.cxx) + enable_language(CUDA) add_library(cuda main.cu) diff --git a/Tests/RunCMake/CMP0104/main.cxx b/Tests/RunCMake/CMP0104/main.cxx new file mode 100644 index 0000000..5047a34 --- /dev/null +++ b/Tests/RunCMake/CMP0104/main.cxx @@ -0,0 +1,3 @@ +int main() +{ +} -- cgit v0.12