Skip to content

Commit 6b665dc

Browse files
[tests] verify trimmer warnings where appropriate (#9076)
One change we need in the Android workload is to make sure that trimmer warnings are displayed if a project sets `$(IsAotCompatible)`. Customers would likely want this set for all platforms if they are using NativeAOT on iOS or MacCatalyst. I also wrote a test with somewhat complicated parameters to verify we get warnings. First, I can create a warning for both `IL2055` and `IL3050`: // Member field Type type = typeof (List<>); // ... // Later in OnCreate Console.WriteLine (type.MakeGenericType (typeof (object))); The combinations of tests are: | Configuration | Property | Warning(s) | | ------------- | ---------------------------------- | ----------------- | | Debug | (defaults) | None | | Release | (defaults) | None | | Debug | TrimMode=full | None | | Release | TrimMode=full | IL2055(2) | | Release | SuppressTrimAnalysisWarnings=false | IL2055(2) | | Debug | IsAotCompatible=true | IL2055, IL3050 | | Release | IsAotCompatible=false | IL2055(2), IL3050 | Some of the cases receive duplicate warnings, but this is expected as the same behavior occurs in the simplest case: * `dotnet new console` * Add the above code to `Program.cs` * `dotnet publish -c Release -r win-x64 -p:PublishAot=true` * Receive warnings from both the Roslyn analyzer and ILC (NativeAOT compiler) In a future PR, I might try to "fix" the duplicate warnings.
1 parent 54a9c8c commit 6b665dc

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed

src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.DefaultProperties.targets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
<!-- For compat with user code not marked trimmable, only trim opt-in by default. -->
8686
<TrimMode Condition=" '$(TrimMode)' == '' and '$(AndroidLinkMode)' == 'Full' ">full</TrimMode>
8787
<TrimMode Condition="'$(TrimMode)' == ''">partial</TrimMode>
88-
<SuppressTrimAnalysisWarnings Condition=" '$(SuppressTrimAnalysisWarnings)' == '' and '$(TrimMode)' == 'full' ">false</SuppressTrimAnalysisWarnings>
88+
<SuppressTrimAnalysisWarnings Condition=" '$(SuppressTrimAnalysisWarnings)' == '' and ('$(TrimMode)' == 'full' or '$(IsAotCompatible)' == 'true') ">false</SuppressTrimAnalysisWarnings>
8989
<SuppressTrimAnalysisWarnings Condition=" '$(SuppressTrimAnalysisWarnings)' == '' ">true</SuppressTrimAnalysisWarnings>
9090
<!-- Prefer $(RuntimeIdentifiers) plural -->
9191
<RuntimeIdentifiers Condition=" '$(RuntimeIdentifier)' == '' And '$(RuntimeIdentifiers)' == '' ">android-arm;android-arm64;android-x86;android-x64</RuntimeIdentifiers>

src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/BuildTest2.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,48 @@ public void BuildHasNoWarnings (bool isRelease, bool xamarinForms, bool multidex
269269
}
270270
}
271271

272+
[Test]
273+
[TestCase ("", new string [0], false)]
274+
[TestCase ("", new string [0], true)]
275+
[TestCase ("SuppressTrimAnalysisWarnings=false", new string [] { "IL2055" }, true, 2)]
276+
[TestCase ("TrimMode=full", new string [0], false)]
277+
[TestCase ("TrimMode=full", new string [] { "IL2055" }, true, 2)]
278+
[TestCase ("IsAotCompatible=true", new string [] { "IL2055", "IL3050" }, false)]
279+
[TestCase ("IsAotCompatible=true", new string [] { "IL2055", "IL3050" }, true, 3)]
280+
public void BuildHasTrimmerWarnings (string properties, string [] codes, bool isRelease, int? totalWarnings = null)
281+
{
282+
var proj = new XamarinAndroidApplicationProject {
283+
IsRelease = isRelease,
284+
};
285+
proj.SetRuntimeIdentifier ("arm64-v8a");
286+
proj.MainActivity = proj.DefaultMainActivity
287+
.Replace ("//${FIELDS}", "Type type = typeof (List<>);")
288+
.Replace ("//${AFTER_ONCREATE}", "Console.WriteLine (type.MakeGenericType (typeof (object)));");
289+
proj.SetProperty ("TrimmerSingleWarn", "false");
290+
291+
if (!string.IsNullOrEmpty (properties)) {
292+
foreach (var property in properties.Split (';')) {
293+
int index = property.IndexOf ('=');
294+
if (index != -1) {
295+
proj.SetProperty (property [..index], property [(index + 1)..]);
296+
}
297+
}
298+
}
299+
300+
using var b = CreateApkBuilder (Path.Combine ("temp", TestName));
301+
Assert.IsTrue (b.Build (proj), "Build should have succeeded.");
302+
303+
if (codes.Length == 0) {
304+
b.AssertHasNoWarnings ();
305+
} else {
306+
totalWarnings ??= codes.Length;
307+
Assert.True (StringAssertEx.ContainsText (b.LastBuildOutput, $"{totalWarnings} Warning(s)"), $"Should receive {totalWarnings} warnings");
308+
foreach (var code in codes) {
309+
Assert.True (StringAssertEx.ContainsText (b.LastBuildOutput, code), $"Should receive {code} warning");
310+
}
311+
}
312+
}
313+
272314
[Test]
273315
[TestCase ("AndroidFastDeploymentType", "Assemblies", true, false)]
274316
[TestCase ("AndroidFastDeploymentType", "Assemblies", false, false)]

src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Resources/Base/MainActivity.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ namespace ${ROOT_NAMESPACE}
1313
[Register ("${JAVA_PACKAGENAME}.MainActivity"), Activity (Label = "${PROJECT_NAME}", MainLauncher = true, Icon = "@drawable/icon")]
1414
public class MainActivity : Activity
1515
{
16+
//${FIELDS}
1617
int count = 1;
1718

1819
protected override void OnCreate (Bundle bundle)

src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Resources/DotNet/MainActivity.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ namespace ${ROOT_NAMESPACE}
44
[Android.Runtime.Register ("${JAVA_PACKAGENAME}.MainActivity"), Activity (Label = "${PROJECT_NAME}", MainLauncher = true, Icon = "@drawable/icon")]
55
public class MainActivity : Activity
66
{
7+
//${FIELDS}
78
int count = 1;
89

910
protected override void OnCreate (Bundle? bundle)

0 commit comments

Comments
 (0)