VS: C# Compiler Detection fails in VS 2022 due to outdated framework version in CompilerIdCSharp
Starting with Visual Studio 2022, Visual Studio no longer includes .NET Framework components for .NET Framework 4.0 - 4.5.1 because these versions are no longer supported. Visual Studio 2022 and later versions can't build apps that target .NET Framework 4.0 through .NET Framework 4.5.1. To continue building these apps, you can use Visual Studio 2019 or an earlier version.
See: https://docs.microsoft.com/en-us/dotnet/framework/install/guide-for-developers
My testing is below:
Running CMake 3.22-rc2, Visual Studio 2022 RC3
CMakeLists.txt:
project(MyTestProject LANGUAGES CSharp)
command line:
cmake -G "Visual Studio 17 2022" .
Gives output:
-- Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.19043.
-- The CSharp compiler identification is unknown
-- The CSharp compiler version is
CMake Error at CMakeLists.txt:1 (project):
No CMAKE_CSharp_COMPILER could be found.
-- Configuring incomplete, errors occurred!
See also "C:/Users/Remy/Documents/src/test/CMakeFiles/CMakeOutput.log".
See also "C:/Users/Remy/Documents/src/test/CMakeFiles/CMakeError.log".
CMakeError.log:
Compiling the CSharp compiler identification source file "CMakeCSharpCompilerId.cs" failed.
Compiler:
Build flags:
Id flags:
The output was:
1
Microsoft (R) Build Engine version 17.0.0-preview-21515-03+d66a44095 for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.
Build started 11/1/2021 7:30:05 PM.
Project "C:\Users\Remy\Documents\src\test\CMakeFiles\3.22.0-rc2\CompilerIdCSharp\CompilerIdCSharp.csproj" on node 1 (def
ault targets).
C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\amd64\Microsoft.Common.CurrentVersion.target
s(1217,5): error MSB3644: The reference assemblies for .NETFramework,Version=v4.0 were not found. To resolve this, insta
ll the Developer Pack (SDK/Targeting Pack) for this framework version or retarget your application. You can download .NE
T Framework Developer Packs at https://aka.ms/msbuild/developerpacks [C:\Users\Remy\Documents\src\test\CMakeFiles\3.22.0
-rc2\CompilerIdCSharp\CompilerIdCSharp.csproj]
_CleanRecordFileWrites:
Creating directory "obj\x64\Debug\".
Done Building Project "C:\Users\Remy\Documents\src\test\CMakeFiles\3.22.0-rc2\CompilerIdCSharp\CompilerIdCSharp.csproj"
(default targets) -- FAILED.
Build FAILED.
"C:\Users\Remy\Documents\src\test\CMakeFiles\3.22.0-rc2\CompilerIdCSharp\CompilerIdCSharp.csproj" (default target) (1) -
>
(GetReferenceAssemblyPaths target) ->
C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\amd64\Microsoft.Common.CurrentVersion.targ
ets(1217,5): error MSB3644: The reference assemblies for .NETFramework,Version=v4.0 were not found. To resolve this, ins
tall the Developer Pack (SDK/Targeting Pack) for this framework version or retarget your application. You can download .
NET Framework Developer Packs at https://aka.ms/msbuild/developerpacks [C:\Users\Remy\Documents\src\test\CMakeFiles\3.22
.0-rc2\CompilerIdCSharp\CompilerIdCSharp.csproj]
0 Warning(s)
1 Error(s)
Time Elapsed 00:00:00.06
Attempting to open CMakeFiles\3.22.0-rc2\CompilerIdCSharp\CompilerIdCSharp.csproj in the Visual Studio shows the following dialog:
And finally, opening up CMakeFiles/3.22.0-rc2/CompilerIdCSharp/CompilerIdCSharp.csproj
indeed shows
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">`
Verified as well on GitHub Actions: https://github.com/remyjette/CMakeCSharpTest/runs/4075622514