Browse Source

Merge topic 'vs-sdk-style-platform-override'

4ebedf246d VS: Fix SLNs for DOTNET_SDK targets with VS_GLOBAL_PlatformTarget

Acked-by: Kitware Robot <[email protected]>
Acked-by: Steven Boswell <[email protected]>
Merge-request: !11247
Brad King 2 months ago
parent
commit
c6934e0928

+ 13 - 6
Source/cmGlobalVisualStudioGenerator.cxx

@@ -1009,12 +1009,19 @@ cm::VS::Solution cmGlobalVisualStudioGenerator::CreateSolution(
         project->TypeId = Solution::Project::TypeIdDefault;
       }
 
-      project->Platform =
-        // On VS 19 and above, always map .NET SDK projects to "Any CPU".
-        (gt->IsDotNetSdkTarget() && this->Version >= VSVersion::VS16 &&
-         !cmGlobalVisualStudioGenerator::IsReservedTarget(gt->GetName()))
-        ? "Any CPU"
-        : solution.Platform;
+      if (gt->IsDotNetSdkTarget() &&
+          !cmGlobalVisualStudioGenerator::IsReservedTarget(gt->GetName())) {
+        cmValue platformTarget = gt->GetProperty("VS_GLOBAL_PlatformTarget");
+        if (!platformTarget.IsEmpty()) {
+          project->Platform = *platformTarget;
+        } else {
+          project->Platform =
+            // On VS 16 and above, always map .NET SDK projects to "Any CPU".
+            this->Version >= VSVersion::VS16 ? "Any CPU" : solution.Platform;
+        }
+      } else {
+        project->Platform = solution.Platform;
+      }
 
       // Add solution-level dependencies.
       TargetDependSet const& depends = this->GetTargetDirectDepends(gt);

+ 8 - 0
Tests/RunCMake/VsDotnetSdk/RunCMakeTest.cmake

@@ -1,11 +1,19 @@
+cmake_minimum_required(VERSION 4.0)
 include(RunCMake)
 
+if(RunCMake_GENERATOR MATCHES "Visual Studio 1[4-7]")
+  set(sln_ext "sln")
+else()
+  set(sln_ext "slnx")
+endif()
+
 run_cmake(VsDotnetSdkStartupObject)
 run_cmake(VsDotnetSdkDefines)
 run_cmake(DotnetSdkVariables)
 run_cmake(VsDotnetSdkXamlFiles)
 run_cmake(VsDotnetSdkAssemblyName)
 run_cmake(VsDotnetSdkConfigurations)
+run_cmake(VsDotnetSdkTargetPlatform)
 
 function(run_VsDotnetSdk)
   set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/VsDotnetSdk-build)

+ 27 - 0
Tests/RunCMake/VsDotnetSdk/VsDotnetSdkTargetPlatform-check-sln.cmake

@@ -0,0 +1,27 @@
+set(slnFile ${RunCMake_TEST_BINARY_DIR}/VsDotnetSdkTargetPlatform.sln)
+
+if(NOT EXISTS "${slnFile}")
+  string(APPEND RunCMake_TEST_FAILED
+    "Solution file:\n"
+    " ${slnFile}\n"
+    "does not exist."
+  )
+  return()
+endif()
+
+file(STRINGS "${slnFile}" lines)
+
+set(haveAnyCPU 0)
+foreach(line IN LISTS lines)
+  if(line MATCHES [[\.(ActiveCfg|Build\.0) = (Debug|Release|MinSizeRel|RelWithDebInfo)\|Any CPU]])
+    set(haveAnyCPU 1)
+  endif()
+endforeach()
+
+if(haveAnyCPU)
+  string(APPEND RunCMake_TEST_FAILED
+    "Solution file:\n"
+    " ${slnFile}\n"
+    "incorrectly maps to Any CPU."
+  )
+endif()

+ 23 - 0
Tests/RunCMake/VsDotnetSdk/VsDotnetSdkTargetPlatform-check-slnx.cmake

@@ -0,0 +1,23 @@
+RunCMake_check_slnx("${RunCMake_TEST_BINARY_DIR}/VsDotnetSdkTargetPlatform.slnx" [[
+^<\?xml version="1\.0" encoding="UTF-8"\?>
+<Solution>
+  <Configurations>
+    <BuildType Name="Debug"/>
+    <BuildType Name="Release"/>
+    <BuildType Name="MinSizeRel"/>
+    <BuildType Name="RelWithDebInfo"/>
+    <Platform Name="[^"]+"/>
+  </Configurations>
+  <Project Path="ALL_BUILD\.vcxproj" Id="[0-9a-f-]+">
+    <BuildDependency Project="ZERO_CHECK\.vcxproj"/>
+    <BuildDependency Project="foo\.csproj"/>
+    <Build Solution="Debug\|\*" Project="false"/>
+    <Build Solution="Release\|\*" Project="false"/>
+    <Build Solution="MinSizeRel\|\*" Project="false"/>
+    <Build Solution="RelWithDebInfo\|\*" Project="false"/>
+  </Project>
+  <Project Path="ZERO_CHECK\.vcxproj" Id="[0-9a-f-]+"/>
+  <Project Path="foo\.csproj" Id="[0-9a-f-]+">
+    <BuildDependency Project="ZERO_CHECK\.vcxproj"/>
+  </Project>
+</Solution>$]])

+ 1 - 0
Tests/RunCMake/VsDotnetSdk/VsDotnetSdkTargetPlatform-check.cmake

@@ -0,0 +1 @@
+include(${CMAKE_CURRENT_LIST_DIR}/VsDotnetSdkTargetPlatform-check-${sln_ext}.cmake)

+ 10 - 0
Tests/RunCMake/VsDotnetSdk/VsDotnetSdkTargetPlatform.cmake

@@ -0,0 +1,10 @@
+enable_language(CSharp)
+
+set(CMAKE_DOTNET_SDK "Microsoft.NET.Sdk")
+
+add_executable(foo csharponly.cs lib1.cs)
+
+set_target_properties(foo PROPERTIES
+  VS_GLOBAL_Platforms "${CMAKE_VS_PLATFORM_NAME}"
+  VS_GLOBAL_PlatformTarget "${CMAKE_VS_PLATFORM_NAME}"
+)