[net9.0] Do not suppress trim analysis warnings when NativeAOT is enabled (#20767)

We noticed we weren't seeing trim analysis warnings in VS Code when
PublishAot was set to true. There was a recent change that correctly
disabled the suppressions when TrimMode is full. We need to make sure
that we're also getting the trim analysis warnings in dotnet build with
PublishAot but suppress them when publishing (in that case the warnings
will come later from ILC). This PR aligns the behavior of
PublishAot=true and TrimMode=true in debug builds.
This commit is contained in:
Šimon Rozsíval 2024-07-03 18:22:44 +02:00 коммит произвёл GitHub
Родитель d27731da2f
Коммит 68897de20f
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
15 изменённых файлов: 125 добавлений и 5 удалений

Просмотреть файл

@ -166,14 +166,15 @@
<PropertyGroup>
<!--
With NativeAOT we want to suppress trim warnings coming from ILLink and enable them only for ILC.
For this reason, in case of NativeAOT, we set SuppressTrimAnalysisWarnings to true by default and store the overwriten default in
With NativeAOT we want to suppress trim warnings coming from ILLink and enable them only for ILC when publishing.
For this reason, in case of NativeAOT while publishing, we set SuppressTrimAnalysisWarnings to true by default and store the overwriten default in
_OriginalSuppressTrimAnalysisWarnings property, which is later used to properly configure warning suppression for ILC.
-->
<_OriginalSuppressTrimAnalysisWarnings>$(SuppressTrimAnalysisWarnings)</_OriginalSuppressTrimAnalysisWarnings>
<SuppressTrimAnalysisWarnings Condition="'$(_UseNativeAot)' == 'true'">true</SuppressTrimAnalysisWarnings>
<!-- Otherwise suppress trimmer warnings unless we're trimming all assemblies -->
<SuppressTrimAnalysisWarnings Condition="'$(SuppressTrimAnalysisWarnings)' == '' And '$(TrimMode)' == 'full'">false</SuppressTrimAnalysisWarnings>
<SuppressTrimAnalysisWarnings Condition="'$(SuppressTrimAnalysisWarnings)' == '' And '$(PublishAot)' == 'true'">false</SuppressTrimAnalysisWarnings>
<SuppressTrimAnalysisWarnings Condition="'$(SuppressTrimAnalysisWarnings)' == ''">true</SuppressTrimAnalysisWarnings>
</PropertyGroup>

Просмотреть файл

@ -527,9 +527,13 @@
<!-- Yep, we want to run ILLink as well, because we need our custom steps to run (NativeAOT sets this to false, so set it back to true) -->
<RunILLink Condition="'$(PublishAot)' == 'true'">true</RunILLink>
<!-- Suppress trimmer warnings unless we're trimming all assemblies -->
<SuppressTrimAnalysisWarnings Condition="'$(SuppressTrimAnalysisWarnings)' == '' And '$(TrimMode)' == 'full'">false</SuppressTrimAnalysisWarnings>
<SuppressTrimAnalysisWarnings Condition="'$(SuppressTrimAnalysisWarnings)' == ''">true</SuppressTrimAnalysisWarnings>
<!--
In the case of NativeAOT builds, the trim warnings will be produced either by the ILLink roslyn analyzer in Debug builds
or by the ILC when publishing. We want to suppress the ILLink warnings in all build configurations to avoid duplicates.
We choose not to suppress the ILLink warnings when TrimMode is set to 'full' and we're not publishing. In this scenario
ILC will not run at a later stage of the build.
-->
<SuppressTrimAnalysisWarnings Condition="'$(PublishAot)' == 'true' And '$(_IsPublishing)' != 'true' And '$(TrimMode)' != 'full'">true</SuppressTrimAnalysisWarnings>
<!-- Pass the custom options to our custom steps -->
<_CustomLinkerOptionsFile>$([System.IO.Path]::GetFullPath('$(IntermediateOutputPath)custom-linker-options.txt'))</_CustomLinkerOptionsFile>
@ -652,6 +656,9 @@
<!-- If we're using NativeAOT, tell ILLink to not remove dependency attributes (DynamicDependencyAttribute), because NativeAOT's trimmer also needs to see them. -->
<_ExtraTrimmerArgs Condition="'$(_UseNativeAot)' == 'true'">$(_ExtraTrimmerArgs) --keep-dep-attributes</_ExtraTrimmerArgs>
<!-- Ensure ILLink respects the value of SuppressTrimAnalysisWarnings -->
<_ExtraTrimmerArgs Condition="'$(SuppressTrimAnalysisWarnings)' == 'true'">$(_ExtraTrimmerArgs) --notrimwarn</_ExtraTrimmerArgs>
<!-- We always want the linker to process debug symbols, even when building in Release mode, because the AOT compiler uses the managed debug symbols to output DWARF debugging symbols -->
<TrimmerRemoveSymbols Condition="'$(TrimmerRemoveSymbols)' == ''">false</TrimmerRemoveSymbols>

Просмотреть файл

@ -0,0 +1,29 @@
using System;
using System.Runtime.InteropServices;
using Foundation;
namespace MySimpleApp {
public class Program {
static int Main (string [] args)
{
GC.KeepAlive (typeof (NSObject)); // prevent linking away the platform assembly
Console.WriteLine (Environment.GetEnvironmentVariable ("MAGIC_WORD"));
var myObject = new MyClass (typeof (MyClass));
Console.WriteLine (myObject.X);
return args.Length;
}
}
internal class MyClass {
public object X { get; set; }
public MyClass (object x)
{
X = x.GetType ().GetProperties ();
}
}
}

Просмотреть файл

@ -0,0 +1 @@
include ../shared.mk

Просмотреть файл

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net$(BundledNETCoreAppTargetFrameworkVersion)-maccatalyst</TargetFramework>
</PropertyGroup>
<Import Project="..\shared.csproj" />
</Project>

Просмотреть файл

@ -0,0 +1,2 @@
TOP=../../..
include $(TOP)/tests/common/shared-dotnet-test.mk

Просмотреть файл

@ -0,0 +1 @@
include ../shared.mk

Просмотреть файл

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net$(BundledNETCoreAppTargetFrameworkVersion)-ios</TargetFramework>
</PropertyGroup>
<Import Project="..\shared.csproj" />
</Project>

Просмотреть файл

@ -0,0 +1 @@
include ../shared.mk

Просмотреть файл

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net$(BundledNETCoreAppTargetFrameworkVersion)-macos</TargetFramework>
</PropertyGroup>
<Import Project="..\shared.csproj" />
</Project>

Просмотреть файл

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<Project>
<PropertyGroup>
<OutputType>Exe</OutputType>
<ApplicationTitle>MyNativeAotAppWithTrimAnalysisWarning</ApplicationTitle>
<ApplicationId>com.xamarin.mynativeaotappwithtrimanalysiswarning</ApplicationId>
<ApplicationVersion>3.14</ApplicationVersion>
<PublishAot>true</PublishAot>
</PropertyGroup>
<Import Project="../../common/shared-dotnet.csproj" />
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)AppDelegate.cs" />
</ItemGroup>
</Project>

Просмотреть файл

@ -0,0 +1,3 @@
TOP=../../../..
TESTNAME=MyNativeAotAppWithTrimAnalysisWarning
include $(TOP)/tests/common/shared-dotnet.mk

Просмотреть файл

@ -0,0 +1 @@
include ../shared.mk

Просмотреть файл

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net$(BundledNETCoreAppTargetFrameworkVersion)-tvos</TargetFramework>
</PropertyGroup>
<Import Project="..\shared.csproj" />
</Project>

Просмотреть файл

@ -1649,6 +1649,34 @@ namespace Xamarin.Tests {
DotNet.AssertBuild (project_path, properties);
}
[Test]
[TestCase (ApplePlatform.iOS, "ios-arm64")]
[TestCase (ApplePlatform.iOS, "iossimulator-arm64")]
[TestCase (ApplePlatform.MacCatalyst, "maccatalyst-arm64")]
public void BuildMyNativeAotAppWithTrimAnalysisWarning (ApplePlatform platform, string runtimeIdentifiers)
{
var project = "MyNativeAotAppWithTrimAnalysisWarning";
Configuration.IgnoreIfIgnoredPlatform (platform);
Configuration.AssertRuntimeIdentifiersAvailable (platform, runtimeIdentifiers);
var project_path = GetProjectPath (project, runtimeIdentifiers: runtimeIdentifiers, platform: platform, out var appPath);
Clean (project_path);
var properties = GetDefaultProperties (runtimeIdentifiers);
var rv = DotNet.AssertBuild (project_path, properties);
// We expect to get a warning from the trim analzyer in Debug build
var warnings = BinLog.GetBuildLogWarnings (rv.BinLogPath).ToArray ();
// Ignore warnings we haven't fixed yet
if (platform == ApplePlatform.iOS) {
warnings = warnings.Where (w => w.Message?.Trim () != "Supported iPhone orientations have not been set").ToArray ();
}
Assert.AreEqual (1, warnings.Length, "Warning count");
Assert.AreEqual (warnings [0].Code, "IL2075", "Warning code");
Assert.AreEqual (warnings [0].Message, "'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicProperties' in call to 'System.Type.GetProperties()'. The return value of method 'System.Object.GetType()' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.");
}
[Test]
[TestCase (ApplePlatform.iOS, "ios-arm64")]
[TestCase (ApplePlatform.MacOSX, "osx-x64")]