From 7e57e6ae123439d5101ae1fc3ce593652b408b0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20St=C3=BCrmer?= Date: Tue, 29 Aug 2017 10:46:54 +0200 Subject: VS: Do not reference output assemblies if not possible for CSharp target Since commit v3.9.0-rc4~4^2 (Vs: allow CSharp targets to be linked to CXX targets, 2017-06-20) CSharp targets get `ProjectReference` entries to their dependencies. This causes VS to also reference the dependency's output assembly by default, which is incorrect for non-managed targets. Fix this by setting `ReferenceOutputAssembly` to `false` for targets that can't provide output assemblies. Unmanaged C++ targets (shared libs & executables) can still be referenced and a warning will be shown in the IDE but the build will not break anymore. Fixes: #17172 --- Source/cmGlobalVisualStudioGenerator.cxx | 13 +++++++++++++ Source/cmGlobalVisualStudioGenerator.h | 3 +++ Source/cmVisualStudio10TargetGenerator.cxx | 7 +++++++ Tests/CSharpLinkToCxx/CMakeLists.txt | 6 ++++++ Tests/CSharpLinkToCxx/cpp_native.cpp | 10 ++++++++++ Tests/CSharpLinkToCxx/cpp_native.hpp | 9 +++++++++ 6 files changed, 48 insertions(+) create mode 100644 Tests/CSharpLinkToCxx/cpp_native.cpp create mode 100644 Tests/CSharpLinkToCxx/cpp_native.hpp diff --git a/Source/cmGlobalVisualStudioGenerator.cxx b/Source/cmGlobalVisualStudioGenerator.cxx index 87a22d1..bbff48e 100644 --- a/Source/cmGlobalVisualStudioGenerator.cxx +++ b/Source/cmGlobalVisualStudioGenerator.cxx @@ -778,6 +778,19 @@ bool cmGlobalVisualStudioGenerator::TargetIsCSharpOnly( return false; } +bool cmGlobalVisualStudioGenerator::TargetCanBeReferenced( + cmGeneratorTarget const* gt) +{ + if (this->TargetIsCSharpOnly(gt)) { + return true; + } + if (gt->GetType() != cmStateEnums::SHARED_LIBRARY && + gt->GetType() != cmStateEnums::EXECUTABLE) { + return false; + } + return true; +} + bool cmGlobalVisualStudioGenerator::TargetCompare::operator()( cmGeneratorTarget const* l, cmGeneratorTarget const* r) const { diff --git a/Source/cmGlobalVisualStudioGenerator.h b/Source/cmGlobalVisualStudioGenerator.h index c12a933..3c43ccd 100644 --- a/Source/cmGlobalVisualStudioGenerator.h +++ b/Source/cmGlobalVisualStudioGenerator.h @@ -85,6 +85,9 @@ public: // return true if target is C# only static bool TargetIsCSharpOnly(cmGeneratorTarget const* gt); + // return true if target can be referenced by C# targets + bool TargetCanBeReferenced(cmGeneratorTarget const* gt); + /** Get the top-level registry key for this VS version. */ std::string GetRegistryBase(); diff --git a/Source/cmVisualStudio10TargetGenerator.cxx b/Source/cmVisualStudio10TargetGenerator.cxx index 88fa19c..dee153f 100644 --- a/Source/cmVisualStudio10TargetGenerator.cxx +++ b/Source/cmVisualStudio10TargetGenerator.cxx @@ -3502,6 +3502,13 @@ void cmVisualStudio10TargetGenerator::WriteProjectReferences() (*this->BuildFileStream) << "\n"; this->WriteString("", 3); (*this->BuildFileStream) << name << "\n"; + if (csproj == this->ProjectType) { + if (!static_cast(this->GlobalGenerator) + ->TargetCanBeReferenced(dt)) { + this->WriteString( + "false\n", 3); + } + } this->WriteString("\n", 2); } this->WriteString("\n", 1); diff --git a/Tests/CSharpLinkToCxx/CMakeLists.txt b/Tests/CSharpLinkToCxx/CMakeLists.txt index c4269e0..153c57c 100644 --- a/Tests/CSharpLinkToCxx/CMakeLists.txt +++ b/Tests/CSharpLinkToCxx/CMakeLists.txt @@ -15,3 +15,9 @@ target_compile_options(CLIApp PRIVATE "/clr") add_executable(CSharpLinkToCxx csharp.cs) target_link_libraries(CSharpLinkToCxx CLIApp) + +# this unmanaged C++ library will be added to the C#/.NET +# references of CSharpLinkToCxx but it will show a warning +# because it is unmanaged +add_library(CppNativeApp SHARED cpp_native.hpp cpp_native.cpp) +target_link_libraries(CSharpLinkToCxx CppNativeApp) diff --git a/Tests/CSharpLinkToCxx/cpp_native.cpp b/Tests/CSharpLinkToCxx/cpp_native.cpp new file mode 100644 index 0000000..dc7670f --- /dev/null +++ b/Tests/CSharpLinkToCxx/cpp_native.cpp @@ -0,0 +1,10 @@ +#include "cpp_native.hpp" + +#include + +namespace CppApp { +void MyCpp::testMyCpp() +{ + std::cout << "#message from CppApp" << std::endl; +} +} diff --git a/Tests/CSharpLinkToCxx/cpp_native.hpp b/Tests/CSharpLinkToCxx/cpp_native.hpp new file mode 100644 index 0000000..0fa1a3b --- /dev/null +++ b/Tests/CSharpLinkToCxx/cpp_native.hpp @@ -0,0 +1,9 @@ +#pragma once + +namespace CppApp { +class MyCpp +{ +public: + void testMyCpp(); +}; +} -- cgit v0.12