Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 133 additions & 5 deletions src/Cli/dotnet/Commands/Run/CSharpCompilerCommand.Generated.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Text.Json;

namespace Microsoft.DotNet.Cli.Commands.Run;

// Generated by test `RunFileTests.CscArguments`.
partial class CSharpCompilerCommand
{
private IEnumerable<string> GetCscArguments(
string fileNameWithoutExtension,
string objDir,
string binDir)
{
Expand All @@ -29,24 +30,24 @@ .. GetFrameworkReferenceArguments(),
"/debug:portable",
"/filealign:512",
"/optimize-",
$"/out:{binDir}/{fileNameWithoutExtension}.dll",
$"/out:{binDir}/{FileNameWithoutExtension}.dll",
"/target:exe",
"/warnaserror-",
"/utf8output",
"/deterministic+",
"/langversion:14.0",
"/features:FileBasedProgram",
$"/analyzerconfig:{objDir}/{fileNameWithoutExtension}.GeneratedMSBuildEditorConfig.editorconfig",
$"/analyzerconfig:{objDir}/{FileNameWithoutExtension}.GeneratedMSBuildEditorConfig.editorconfig",
$"/analyzerconfig:{SdkPath}/Sdks/Microsoft.NET.Sdk/analyzers/build/config/analysislevel_10_default.globalconfig",
$"/analyzer:{SdkPath}/Sdks/Microsoft.NET.Sdk/targets/../analyzers/Microsoft.CodeAnalysis.CSharp.NetAnalyzers.dll",
$"/analyzer:{SdkPath}/Sdks/Microsoft.NET.Sdk/targets/../analyzers/Microsoft.CodeAnalysis.NetAnalyzers.dll",
$"/analyzer:{NuGetCachePath}/microsoft.net.illink.tasks/{RuntimeVersion}/analyzers/dotnet/cs/ILLink.CodeFixProvider.dll",
$"/analyzer:{NuGetCachePath}/microsoft.net.illink.tasks/{RuntimeVersion}/analyzers/dotnet/cs/ILLink.RoslynAnalyzer.dll",
.. GetFrameworkAnalyzerArguments(),
$"{EntryPointFileFullPath}",
$"{objDir}/{fileNameWithoutExtension}.GlobalUsings.g.cs",
$"{objDir}/{FileNameWithoutExtension}.GlobalUsings.g.cs",
$"{objDir}/.NETCoreApp,Version=v10.0.AssemblyAttributes.cs",
$"{objDir}/{fileNameWithoutExtension}.AssemblyInfo.cs",
$"{objDir}/{FileNameWithoutExtension}.AssemblyInfo.cs",
"/warnaserror+:NU1605,SYSLIB0011",
];
}
Expand All @@ -62,4 +63,131 @@ public static IEnumerable<string> GetPathsOfCscInputsFromNuGetCache()
$"{NuGetCachePath}/microsoft.net.illink.tasks/{RuntimeVersion}/analyzers/dotnet/cs/ILLink.RoslynAnalyzer.dll",
];
}

private string GetRuntimeConfigContent()
{
return $$"""
{
"runtimeOptions": {
"tfm": {{JsonSerializer.Serialize(TargetFramework)}},
"framework": {
"name": "Microsoft.NETCore.App",
"version": {{JsonSerializer.Serialize(DefaultRuntimeVersion)}}
},
"configProperties": {
"EntryPointFilePath": {{JsonSerializer.Serialize(EntryPointFileFullPath)}},
"EntryPointFileDirectoryPath": {{JsonSerializer.Serialize(BaseDirectory)}},
"Microsoft.Extensions.DependencyInjection.VerifyOpenGenericServiceTrimmability": true,
"System.ComponentModel.DefaultValueAttribute.IsSupported": false,
"System.ComponentModel.Design.IDesignerHost.IsSupported": false,
"System.ComponentModel.TypeConverter.EnableUnsafeBinaryFormatterInDesigntimeLicenseContextSerialization": false,
"System.ComponentModel.TypeDescriptor.IsComObjectDescriptorSupported": false,
"System.Data.DataSet.XmlSerializationIsSupported": false,
"System.Diagnostics.Tracing.EventSource.IsSupported": false,
"System.Linq.Enumerable.IsSizeOptimized": true,
"System.Net.SocketsHttpHandler.Http3Support": false,
"System.Reflection.Metadata.MetadataUpdater.IsSupported": false,
"System.Resources.ResourceManager.AllowCustomResourceTypes": false,
"System.Resources.UseSystemResourceKeys": false,
"System.Runtime.CompilerServices.RuntimeFeature.IsDynamicCodeSupported": false,
"System.Runtime.InteropServices.BuiltInComInterop.IsSupported": false,
"System.Runtime.InteropServices.EnableConsumingManagedCodeFromNativeHosting": false,
"System.Runtime.InteropServices.EnableCppCLIHostActivation": false,
"System.Runtime.InteropServices.Marshalling.EnableGeneratedComInterfaceComImportInterop": false,
"System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization": false,
"System.StartupHookProvider.IsSupported": false,
"System.Text.Encoding.EnableUnsafeUTF7Encoding": false,
"System.Text.Json.JsonSerializer.IsReflectionEnabledByDefault": false,
"System.Threading.Thread.EnableAutoreleasePool": false,
"System.Linq.Expressions.CanEmitObjectArrayDelegate": false
}
}
}
""";
}

private string GetAssemblyAttributesContent()
{
return $"""
// <autogenerated />
using System;
using System.Reflection;
[assembly: global::System.Runtime.Versioning.TargetFrameworkAttribute(".NETCoreApp,Version=v{TargetFrameworkVersion}", FrameworkDisplayName = ".NET {TargetFrameworkVersion}")]

""";
}

private string GetAssemblyInfoContent()
{
return $"""
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System;
using System.Reflection;

[assembly: System.Reflection.AssemblyCompanyAttribute("{FileNameWithoutExtension}")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")]
[assembly: System.Reflection.AssemblyProductAttribute("{FileNameWithoutExtension}")]
[assembly: System.Reflection.AssemblyTitleAttribute("{FileNameWithoutExtension}")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]

// Generated by the MSBuild WriteCodeFragment class.


""";
}

private string GetGeneratedMSBuildEditorConfigContent()
{
return $"""
is_global = true
build_property.EnableAotAnalyzer = true
build_property.EnableSingleFileAnalyzer = true
build_property.EnableTrimAnalyzer = true
build_property.IncludeAllContentForSelfExtract =
build_property.VerifyReferenceTrimCompatibility =
build_property.VerifyReferenceAotCompatibility =
build_property.TargetFramework = net{TargetFrameworkVersion}
build_property.TargetFrameworkIdentifier = .NETCoreApp
build_property.TargetFrameworkVersion = v{TargetFrameworkVersion}
build_property.TargetPlatformMinVersion =
build_property.UsingMicrosoftNETSdkWeb =
build_property.ProjectTypeGuids =
build_property.InvariantGlobalization =
build_property.PlatformNeutralAssembly =
build_property.EnforceExtendedAnalyzerRules =
build_property._SupportedPlatformList = Linux,macOS,Windows
build_property.RootNamespace = {FileNameWithoutExtension}
build_property.ProjectDir = {BaseDirectoryWithTrailingSeparator}
build_property.EnableComHosting =
build_property.EnableGeneratedComInterfaceComImportInterop = false
build_property.EffectiveAnalysisLevelStyle = {TargetFrameworkVersion}
build_property.EnableCodeStyleSeverity =

""";
}

private string GetGlobalUsingsContent()
{
return """
// <auto-generated/>
global using System;
global using System.Collections.Generic;
global using System.IO;
global using System.Linq;
global using System.Net.Http;
global using System.Threading;
global using System.Threading.Tasks;

""";
}
}
135 changes: 16 additions & 119 deletions src/Cli/dotnet/Commands/Run/CSharpCompilerCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,17 @@ internal sealed partial class CSharpCompilerCommand
private static string ClientDirectory => field ??= Path.Combine(SdkPath, "Roslyn", "bincore");
private static string NuGetCachePath => field ??= SettingsUtility.GetGlobalPackagesFolder(Settings.LoadDefaultSettings(null));
internal static string RuntimeVersion => field ??= RuntimeInformation.FrameworkDescription.Split(' ').Last();
private static string DefaultRuntimeVersion => field ??= GetDefaultRuntimeVersion();
private static string TargetFrameworkVersion => Product.TargetFrameworkVersion;
internal static string DefaultRuntimeVersion => field ??= GetDefaultRuntimeVersion();
internal static string TargetFrameworkVersion => Product.TargetFrameworkVersion;
internal static string TargetFramework => field ??= $"net{TargetFrameworkVersion}";

public required string EntryPointFileFullPath { get; init; }
public required string ArtifactsPath { get; init; }
public required bool CanReuseAuxiliaryFiles { get; init; }

public string BaseDirectory => field ??= Path.GetDirectoryName(EntryPointFileFullPath)!;
internal string BaseDirectoryWithTrailingSeparator => field ??= BaseDirectory + Path.DirectorySeparatorChar;
internal string FileNameWithoutExtension => field ??= Path.GetFileNameWithoutExtension(EntryPointFileFullPath);

/// <summary>
/// Compiler command line arguments to use. If empty, default arguments are used.
Expand Down Expand Up @@ -209,9 +212,6 @@ private void PrepareAuxiliaryFiles(out string rspPath)
return;
}

string fileDirectory = Path.GetDirectoryName(EntryPointFileFullPath) ?? string.Empty;
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(EntryPointFileFullPath);

// Note that Release builds won't go through this optimized code path because `-c Release` translates to global property `Configuration=Release`
// and customizing global properties triggers a full MSBuild run.
string objDir = Path.Join(ArtifactsPath, "obj", "debug");
Expand All @@ -222,151 +222,48 @@ private void PrepareAuxiliaryFiles(out string rspPath)
string assemblyAttributes = Path.Join(objDir, $".NETCoreApp,Version=v{TargetFrameworkVersion}.AssemblyAttributes.cs");
if (ShouldEmit(assemblyAttributes))
{
File.WriteAllText(assemblyAttributes, /* lang=C#-test */ $"""
// <autogenerated />
using System;
using System.Reflection;
[assembly: global::System.Runtime.Versioning.TargetFrameworkAttribute(".NETCoreApp,Version=v{TargetFrameworkVersion}", FrameworkDisplayName = ".NET {TargetFrameworkVersion}")]

""");
File.WriteAllText(assemblyAttributes, GetAssemblyAttributesContent());
}

string globalUsings = Path.Join(objDir, $"{fileNameWithoutExtension}.GlobalUsings.g.cs");
string globalUsings = Path.Join(objDir, $"{FileNameWithoutExtension}.GlobalUsings.g.cs");
if (ShouldEmit(globalUsings))
{
File.WriteAllText(globalUsings, /* lang=C#-test */ """
// <auto-generated/>
global using System;
global using System.Collections.Generic;
global using System.IO;
global using System.Linq;
global using System.Net.Http;
global using System.Threading;
global using System.Threading.Tasks;

""");
File.WriteAllText(globalUsings, GetGlobalUsingsContent());
}

string assemblyInfo = Path.Join(objDir, $"{fileNameWithoutExtension}.AssemblyInfo.cs");
string assemblyInfo = Path.Join(objDir, $"{FileNameWithoutExtension}.AssemblyInfo.cs");
if (ShouldEmit(assemblyInfo))
{
File.WriteAllText(assemblyInfo, /* lang=C#-test */ $"""
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System;
using System.Reflection;

[assembly: System.Reflection.AssemblyCompanyAttribute("{fileNameWithoutExtension}")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")]
[assembly: System.Reflection.AssemblyProductAttribute("{fileNameWithoutExtension}")]
[assembly: System.Reflection.AssemblyTitleAttribute("{fileNameWithoutExtension}")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]

// Generated by the MSBuild WriteCodeFragment class.


""");
File.WriteAllText(assemblyInfo, GetAssemblyInfoContent());
}

string editorconfig = Path.Join(objDir, $"{fileNameWithoutExtension}.GeneratedMSBuildEditorConfig.editorconfig");
string editorconfig = Path.Join(objDir, $"{FileNameWithoutExtension}.GeneratedMSBuildEditorConfig.editorconfig");
if (ShouldEmit(editorconfig))
{
File.WriteAllText(editorconfig, $"""
is_global = true
build_property.EnableAotAnalyzer = true
build_property.EnableSingleFileAnalyzer = true
build_property.EnableTrimAnalyzer = true
build_property.IncludeAllContentForSelfExtract =
build_property.VerifyReferenceTrimCompatibility =
build_property.VerifyReferenceAotCompatibility =
build_property.TargetFramework = net{TargetFrameworkVersion}
build_property.TargetFrameworkIdentifier = .NETCoreApp
build_property.TargetFrameworkVersion = v{TargetFrameworkVersion}
build_property.TargetPlatformMinVersion =
build_property.UsingMicrosoftNETSdkWeb =
build_property.ProjectTypeGuids =
build_property.InvariantGlobalization =
build_property.PlatformNeutralAssembly =
build_property.EnforceExtendedAnalyzerRules =
build_property._SupportedPlatformList = Linux,macOS,Windows
build_property.RootNamespace = {fileNameWithoutExtension}
build_property.ProjectDir = {fileDirectory}{Path.DirectorySeparatorChar}
build_property.EnableComHosting =
build_property.EnableGeneratedComInterfaceComImportInterop = false
build_property.EffectiveAnalysisLevelStyle = {TargetFrameworkVersion}
build_property.EnableCodeStyleSeverity =

""");
File.WriteAllText(editorconfig, GetGeneratedMSBuildEditorConfigContent());
}

var apphostTarget = Path.Join(binDir, $"{fileNameWithoutExtension}{FileNameSuffixes.CurrentPlatform.Exe}");
var apphostTarget = Path.Join(binDir, $"{FileNameWithoutExtension}{FileNameSuffixes.CurrentPlatform.Exe}");
if (ShouldEmit(apphostTarget))
{
var rid = RuntimeInformation.RuntimeIdentifier;
var apphostSource = Path.Join(SdkPath, "..", "..", "packs", $"Microsoft.NETCore.App.Host.{rid}", RuntimeVersion, "runtimes", rid, "native", $"apphost{FileNameSuffixes.CurrentPlatform.Exe}");
HostWriter.CreateAppHost(
appHostSourceFilePath: apphostSource,
appHostDestinationFilePath: apphostTarget,
appBinaryFilePath: $"{fileNameWithoutExtension}.dll",
appBinaryFilePath: $"{FileNameWithoutExtension}.dll",
enableMacOSCodeSign: OperatingSystem.IsMacOS());
}

var runtimeConfig = Path.Join(binDir, $"{fileNameWithoutExtension}{FileNameSuffixes.RuntimeConfigJson}");
var runtimeConfig = Path.Join(binDir, $"{FileNameWithoutExtension}{FileNameSuffixes.RuntimeConfigJson}");
if (ShouldEmit(runtimeConfig))
{
File.WriteAllText(runtimeConfig, $$"""
{
"runtimeOptions": {
"tfm": "net{{TargetFrameworkVersion}}",
"framework": {
"name": "Microsoft.NETCore.App",
"version": {{JsonSerializer.Serialize(DefaultRuntimeVersion)}}
},
"configProperties": {
"EntryPointFilePath": {{JsonSerializer.Serialize(EntryPointFileFullPath)}},
"EntryPointFileDirectoryPath": {{JsonSerializer.Serialize(fileDirectory)}},
"Microsoft.Extensions.DependencyInjection.VerifyOpenGenericServiceTrimmability": true,
"System.ComponentModel.DefaultValueAttribute.IsSupported": false,
"System.ComponentModel.Design.IDesignerHost.IsSupported": false,
"System.ComponentModel.TypeConverter.EnableUnsafeBinaryFormatterInDesigntimeLicenseContextSerialization": false,
"System.ComponentModel.TypeDescriptor.IsComObjectDescriptorSupported": false,
"System.Data.DataSet.XmlSerializationIsSupported": false,
"System.Diagnostics.Tracing.EventSource.IsSupported": false,
"System.Linq.Enumerable.IsSizeOptimized": true,
"System.Net.SocketsHttpHandler.Http3Support": false,
"System.Reflection.Metadata.MetadataUpdater.IsSupported": false,
"System.Resources.ResourceManager.AllowCustomResourceTypes": false,
"System.Resources.UseSystemResourceKeys": false,
"System.Runtime.CompilerServices.RuntimeFeature.IsDynamicCodeSupported": false,
"System.Runtime.InteropServices.BuiltInComInterop.IsSupported": false,
"System.Runtime.InteropServices.EnableConsumingManagedCodeFromNativeHosting": false,
"System.Runtime.InteropServices.EnableCppCLIHostActivation": false,
"System.Runtime.InteropServices.Marshalling.EnableGeneratedComInterfaceComImportInterop": false,
"System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization": false,
"System.StartupHookProvider.IsSupported": false,
"System.Text.Encoding.EnableUnsafeUTF7Encoding": false,
"System.Text.Json.JsonSerializer.IsReflectionEnabledByDefault": false,
"System.Threading.Thread.EnableAutoreleasePool": false,
"System.Linq.Expressions.CanEmitObjectArrayDelegate": false
}
}
}
""");
File.WriteAllText(runtimeConfig, GetRuntimeConfigContent());
}

if (ShouldEmit(rspPath))
{
IEnumerable<string> args = GetCscArguments(
fileNameWithoutExtension: fileNameWithoutExtension,
objDir: objDir,
binDir: binDir);

Expand Down
Loading