Skip to content

Commit

Permalink
Revert "[generator] Disable [SupportedOSPlatform] until .NET 5/6. (#781
Browse files Browse the repository at this point in the history
…)" (#841)

Context: dotnet/android#5338

This reverts commit 00862ad.

Now that we are properly building with `$(TargetFramework)`=net6.0
(4d0cba6), we can re-enable the `[SupportedOSPlatform]` support,
as originally introduced in da12df4.

Emit the [`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.

[0]: https://docs.microsoft.com/en-us/dotnet/api/system.runtime.versioning.supportedosplatformattribute?view=net-5.0
  • Loading branch information
jpobst authored May 17, 2021
1 parent 23baf0b commit 412e974
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 27 deletions.
22 changes: 10 additions & 12 deletions tests/generator-Tests/Unit-Tests/CodeGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -276,20 +276,18 @@ class XAJavaInteropCodeGeneratorTests : CodeGeneratorTests
{
protected override CodeGenerationTarget Target => CodeGenerationTarget.XAJavaInterop1;

[Test]
public void SupportedOSPlatform ()
{
var klass = SupportTypeBuilder.CreateClass ("java.code.MyClass", options);
klass.ApiAvailableSince = 30;

// Disabled until we can properly build .NET 5/6 assemblies in our XA tree.
//[Test]
//public void SupportedOSPlatform ()
//{
// var klass = SupportTypeBuilder.CreateClass ("java.code.MyClass", options);
// klass.ApiAvailableSince = 30;

// generator.Context.ContextTypes.Push (klass);
// generator.WriteType (klass, string.Empty, new GenerationInfo ("", "", "MyAssembly"));
// generator.Context.ContextTypes.Pop ();
generator.Context.ContextTypes.Push (klass);
generator.WriteType (klass, string.Empty, new GenerationInfo ("", "", "MyAssembly"));
generator.Context.ContextTypes.Pop ();

// StringAssert.Contains ("[global::System.Runtime.Versioning.SupportedOSPlatformAttribute (\"android30.0\")]", builder.ToString (), "Should contain SupportedOSPlatform!");
//}
StringAssert.Contains ("[global::System.Runtime.Versioning.SupportedOSPlatformAttribute (\"android30.0\")]", builder.ToString (), "Should contain SupportedOSPlatform!");
}
}

[TestFixture]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,20 @@ public void Generate (CodeGenerationOptions opt, GenerationInfo gen_info)
foreach (var jni in opt.GetJniMarshalDelegates ())
sw.WriteLine ($"delegate {FromJniType (jni[jni.Length - 1])} {jni} (IntPtr jnienv, IntPtr klass{GetDelegateParameters (jni)});");

// Disabled until we can properly build .NET 5/6 assemblies in our XA tree.
// [SupportedOSPlatform] only exists in .NET 5.0+, so we need to generate a
// dummy one so earlier frameworks can compile.
//if (opt.CodeGenerationTarget == Xamarin.Android.Binder.CodeGenerationTarget.XAJavaInterop1) {
// sw.WriteLine ("#if !NET");
// sw.WriteLine ("namespace System.Runtime.Versioning {");
// sw.WriteLine (" [System.Diagnostics.Conditional(\"NEVER\")]");
// sw.WriteLine (" [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Constructor | AttributeTargets.Event | AttributeTargets.Method | AttributeTargets.Module | AttributeTargets.Property | AttributeTargets.Struct, AllowMultiple = true, Inherited = false)]");
// sw.WriteLine (" internal sealed class SupportedOSPlatformAttribute : Attribute {");
// sw.WriteLine (" public SupportedOSPlatformAttribute (string platformName) { }");
// sw.WriteLine (" }");
// sw.WriteLine ("}");
// sw.WriteLine ("#endif");
// sw.WriteLine ("");
//}
if (opt.CodeGenerationTarget == Xamarin.Android.Binder.CodeGenerationTarget.XAJavaInterop1) {
sw.WriteLine ("#if !NET");
sw.WriteLine ("namespace System.Runtime.Versioning {");
sw.WriteLine (" [System.Diagnostics.Conditional(\"NEVER\")]");
sw.WriteLine (" [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Constructor | AttributeTargets.Event | AttributeTargets.Method | AttributeTargets.Module | AttributeTargets.Property | AttributeTargets.Struct, AllowMultiple = true, Inherited = false)]");
sw.WriteLine (" internal sealed class SupportedOSPlatformAttribute : Attribute {");
sw.WriteLine (" public SupportedOSPlatformAttribute (string platformName) { }");
sw.WriteLine (" }");
sw.WriteLine ("}");
sw.WriteLine ("#endif");
sw.WriteLine ("");
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ public class SupportedOSPlatformAttr : AttributeWriter

public override void WriteAttribute (CodeWriter writer)
{
// Disabled until we can properly build .NET 5/6 assemblies in our XA tree.
//writer.WriteLine ($"[global::System.Runtime.Versioning.SupportedOSPlatformAttribute (\"android{Version}.0\")]");
writer.WriteLine ($"[global::System.Runtime.Versioning.SupportedOSPlatformAttribute (\"android{Version}.0\")]");
}
}
}

0 comments on commit 412e974

Please sign in to comment.