-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[generator] Add [SupportedOSPlatform] in assemblies using ApiSince (#773
) Context: dotnet/android#5338 .NET 5 provides a new [`System.Runtime.Versioning.SupportedOSPlatformAttribute`][0] custom attribute which is used to specify on which platforms and platform versions an API is available. This is used to build analyzers to give users warnings if they are trying to use an API when it will not be available on their target platform. `SupportedOSPlatformAttribute` is fundamentally the same as our existing `RegisterAttribute.ApiSince` property, except tooling has actually been built to consume the information. As such, we need `generator` support to put this information into `Mono.Android.dll`: partial class Activity { // Metadata.xml XPath method reference: path="/api/package[@name='android.app']/class[@name='Activity']/method[@name='dismissKeyboardShortcutsHelper' and count(parameter)=0]" [global::System.Runtime.Versioning.SupportedOSPlatformAttribute ("android24.0")] [Register ("dismissKeyboardShortcutsHelper", "()V", "", ApiSince = 24)] public unsafe void DismissKeyboardShortcutsHelper () { … } } Some interesting notes: - `SupportedOSPlatformAttribute` is only available in .NET 5+, so we include a local version for earlier frameworks so we can compile without needing to `#ifdef` every attribute use. - The local version is marked as `[Conditional ("NEVER")]` so the attributes will not actually get compiled into the resulting pre-NET5.0 assembly. - The attribute cannot be placed on interfaces or fields, so we aren't able to annotate them. - Our minimum supported API for .NET 6 is 21, so we only write attributes for API added in versions newer than 21, as API added earlier are always available. [0]: https://docs.microsoft.com/en-us/dotnet/api/system.runtime.versioning.supportedosplatformattribute?view=net-5.0
- Loading branch information
Showing
15 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
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
21 changes: 21 additions & 0 deletions
21
tools/generator/SourceWriters/Attributes/SupportedOSPlatformAttr.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,21 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Xamarin.SourceWriter; | ||
|
||
namespace generator.SourceWriters | ||
{ | ||
public class SupportedOSPlatformAttr : AttributeWriter | ||
{ | ||
public int Version { get; } | ||
|
||
public SupportedOSPlatformAttr (int version) => Version = version; | ||
|
||
public override void WriteAttribute (CodeWriter writer) | ||
{ | ||
writer.WriteLine ($"[global::System.Runtime.Versioning.SupportedOSPlatformAttribute (\"android{Version}.0\")]"); | ||
} | ||
} | ||
} |
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
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
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
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