forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch to using the .NET SDK properties that are defined to provide t…
…he TFM instead of using the assembly that defines System.Object (dotnet#80646) Fixes dotnet#80621
- Loading branch information
1 parent
ae3b8b9
commit 434d4f1
Showing
15 changed files
with
257 additions
and
85 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
src/libraries/Common/tests/SourceGenerators/GlobalOptionsOnlyProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Diagnostics; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Reflection; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Microsoft.Interop; | ||
using Microsoft.Interop.UnitTests; | ||
using SourceGenerators.Tests; | ||
|
||
namespace SourceGenerators.Tests | ||
{ | ||
/// <summary> | ||
/// An implementation of <see cref="AnalyzerConfigOptionsProvider"/> that provides configuration in code | ||
/// of global options. | ||
/// </summary> | ||
internal class GlobalOptionsOnlyProvider : AnalyzerConfigOptionsProvider | ||
{ | ||
public GlobalOptionsOnlyProvider(AnalyzerConfigOptions globalOptions) | ||
{ | ||
GlobalOptions = globalOptions; | ||
} | ||
|
||
public sealed override AnalyzerConfigOptions GlobalOptions { get; } | ||
|
||
public sealed override AnalyzerConfigOptions GetOptions(SyntaxTree tree) | ||
{ | ||
return EmptyOptions.Instance; | ||
} | ||
|
||
public sealed override AnalyzerConfigOptions GetOptions(AdditionalText textFile) | ||
{ | ||
return EmptyOptions.Instance; | ||
} | ||
|
||
private sealed class EmptyOptions : AnalyzerConfigOptions | ||
{ | ||
public override bool TryGetValue(string key, [NotNullWhen(true)] out string? value) | ||
{ | ||
value = null; | ||
return false; | ||
} | ||
|
||
public static AnalyzerConfigOptions Instance = new EmptyOptions(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
...InteropServices/gen/Microsoft.Interop.SourceGeneration/AnalyzerConfigOptionsExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using System.Text; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
|
||
namespace Microsoft.Interop | ||
{ | ||
// This type is a record to get the generated equality and hashing operators | ||
// which will be faster than the reflection-based ones. | ||
public readonly record struct TargetFrameworkSettings(TargetFramework TargetFramework, Version Version); | ||
|
||
public static class AnalyzerConfigOptionsExtensions | ||
{ | ||
private static readonly Version FirstNonCoreVersion = new(5, 0); | ||
|
||
// Parse from the informational version as that is the only version that always matches the TFM version | ||
// even in debug builds. | ||
private static readonly Version ThisAssemblyVersion = Version.Parse( | ||
typeof(IncrementalGeneratorInitializationContextExtensions).Assembly | ||
.GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion.Split('-')[0]); | ||
|
||
public static TargetFrameworkSettings GetTargetFrameworkSettings(this AnalyzerConfigOptions options) | ||
{ | ||
// Our generator only runs in the following scenarios: | ||
// - In the dotnet/runtime repository. | ||
// - In a .NET SDK for the same TFM that matches the version of this assembly. | ||
// We'll try to pull the TFM information from the build, but if it is not present, | ||
// then we'll assume we're in the ref pack as the TFM information will always be present in the dotnet/runtime build. | ||
options.TryGetValue("build_property.TargetFrameworkIdentifier", out string? frameworkIdentifier); | ||
options.TryGetValue("build_property.TargetFrameworkVersion", out string? versionString); | ||
// TargetFrameworkVersion starts with a 'v'. | ||
Version? version = versionString is not null ? Version.Parse(versionString.Substring(1)) : null; | ||
return new TargetFrameworkSettings( | ||
frameworkIdentifier switch | ||
{ | ||
".NETStandard" => TargetFramework.Standard, | ||
".NETCoreApp" when version is not null && version < FirstNonCoreVersion => TargetFramework.Core, | ||
".NETCoreApp" => TargetFramework.Net, | ||
// If the TFM is not specified, we'll infer it from this assembly. | ||
// Since we only ship this assembly as part of the Microsoft.NETCore.App TFM, | ||
// the down-level support only matters for the repo where this project is built. | ||
// In all other cases, we will only be used from the TFM with the matching version as our assembly. | ||
null => TargetFramework.Net, | ||
// Assume that all unknown target framework identifiers are .NET Framework. | ||
// All legacy target frameworks will have effectively the same feature set as we provide for .NET Framework | ||
// for our purposes. | ||
_ => TargetFramework.Framework | ||
}, | ||
// If the version is not specified, we'll infer it from this assembly. | ||
version ?? ThisAssemblyVersion); | ||
} | ||
} | ||
} |
42 changes: 0 additions & 42 deletions
42
...m.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/CompilationExtensions.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
src/libraries/System.Runtime.InteropServices/tests/Common/TargetFrameworkOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Diagnostics; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Reflection; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Microsoft.Interop; | ||
using Microsoft.Interop.UnitTests; | ||
using SourceGenerators.Tests; | ||
|
||
namespace Microsoft.Interop.UnitTests | ||
{ | ||
/// <summary> | ||
/// An implementation of <see cref="AnalyzerConfigOptions"/> that provides configuration in code | ||
/// of the target framework options. Used when testing interop source generators. | ||
/// </summary> | ||
public class TargetFrameworkConfigOptions : AnalyzerConfigOptions | ||
{ | ||
private static readonly string _liveTargetFrameworkVersion; | ||
private readonly string _targetFrameworkIdentifier; | ||
private readonly string _targetFrameworkVersion; | ||
|
||
static TargetFrameworkConfigOptions() | ||
{ | ||
Version liveVersion = Version.Parse( | ||
typeof(TargetFrameworkConfigOptions) | ||
.Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()! | ||
.InformationalVersion.Split('-')[0]); | ||
_liveTargetFrameworkVersion = $"v{liveVersion.ToString(2)}"; | ||
} | ||
|
||
public TargetFrameworkConfigOptions(TestTargetFramework targetFramework) | ||
{ | ||
_targetFrameworkIdentifier = targetFramework switch | ||
{ | ||
TestTargetFramework.Framework => ".NETFramework", | ||
TestTargetFramework.Standard => ".NETStandard", | ||
_ => ".NETCoreApp" | ||
}; | ||
_targetFrameworkVersion = targetFramework switch | ||
{ | ||
TestTargetFramework.Framework => "v4.8", | ||
TestTargetFramework.Standard => "v2.1", | ||
TestTargetFramework.Core => "v3.1", | ||
TestTargetFramework.Net6 => "v6.0", | ||
TestTargetFramework.Net => _liveTargetFrameworkVersion, | ||
_ => throw new UnreachableException() | ||
}; | ||
} | ||
|
||
public override bool TryGetValue(string key, [NotNullWhen(true)] out string? value) | ||
{ | ||
switch (key) | ||
{ | ||
case "build_property.TargetFrameworkIdentifier": | ||
value = _targetFrameworkIdentifier; | ||
return true; | ||
|
||
case "build_property.TargetFrameworkVersion": | ||
value = _targetFrameworkVersion; | ||
return true; | ||
|
||
default: | ||
value = null; | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.