Skip to content

Commit 772c715

Browse files
authored
[net9.0] [dotnet] Delay computing trimming options until MAUI has had a chance to change the default. (#20971)
See comment in code for a deeper explanation. Partial fix for https://devdiv.visualstudio.com/DevDiv/_workitems/edit/2053707.
1 parent be5447e commit 772c715

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+2062
-41
lines changed

dotnet/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ $(1)_NUGET_TARGETS = \
3838
$(DOTNET_DESTDIR)/$($(1)_NUGET_SDK_NAME)/targets/Xamarin.Shared.Sdk.Publish.targets \
3939
$(DOTNET_DESTDIR)/$($(1)_NUGET_SDK_NAME)/targets/Xamarin.Shared.Sdk.TargetFrameworkInference.props \
4040
$(DOTNET_DESTDIR)/$($(1)_NUGET_SDK_NAME)/targets/Xamarin.Shared.Sdk.props \
41+
$(DOTNET_DESTDIR)/$($(1)_NUGET_SDK_NAME)/targets/Xamarin.Shared.Sdk.Trimming.props \
4142
$(DOTNET_DESTDIR)/$($(1)_NUGET_SDK_NAME)/targets/Xamarin.Shared.Sdk.targets \
4243

4344
endef
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
4+
<!--
5+
6+
We have a few requirements when computing trimming properties:
7+
8+
1. We must give MAUI a change to set MtouchLink, which they do in Microsoft.Maui.Controls.iOS.targets
9+
2. We must set SuppressTrimAnalysisWarnings before Microsoft.NET.ILLink.targets is included.
10+
11+
1: Microsoft.Maui.Controls.iOS.targets is included like this:
12+
a) Sdks/Microsoft.NET.Sdk/Sdk/Sdk.targets
13+
b) Microsoft.CSharp.targets
14+
c) Microsoft.CSharp.CurrentVersion.targets
15+
d) Microsoft.Common.targets
16+
e) <project path>/obj/*.csproj.nuget.g.targets
17+
f) packages/microsoft.maui.controls.build.tasks/<version>/buildTransitive/net6.0-ios10.0/Microsoft.Maui.Controls.Build.Tasks.targets
18+
g) packages/microsoft.maui.controls.build.tasks/<version>/buildTransitive/Microsoft.Maui.Controls.Build.Tasks.targets
19+
h) packages/microsoft.maui.controls.build.tasks/<version>/buildTransitive/netstandard2.0/Microsoft.Maui.Controls.Build.Tasks.targets
20+
i) packages/packages/microsoft.maui.controls.build.tasks/<version>/buildTransitive/netstandard2.0/Microsoft.Maui.Controls.Build.Tasks.Before.targets
21+
j) packages/packages/microsoft.maui.controls.build.tasks/<version>/buildTransitive/net6.0-ios10.0/Microsoft.Maui.Controls.iOS.targets
22+
23+
2: Microsoft.NET.ILLink.targets is included like this:
24+
a) Sdks/Microsoft.NET.Sdk/Sdk/Sdk.targets
25+
b) Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets
26+
c) packages/microsoft.net.illink.tasks/<version>/build/Microsoft.NET.ILLink.targets
27+
28+
* Microsoft.NET.Sdk.targets is loaded right after
29+
Microsoft.CSharp.targets, and Microsoft.NET.Sdk.targets doesn't
30+
provide any customization points, so we can't inject logic in
31+
Microsoft.NET.Sdk.targets before Microsoft.NET.ILLink.targets is
32+
loaded.
33+
* However, Microsoft.Common.targets has a few customization points
34+
that are loaded after build logic from NuGets is loaded (aka
35+
Microsoft.Maui.Controls.iOS.targets), of which
36+
CustomBeforeDirectoryBuildTargets and
37+
CustomAfterDirectoryBuildTargets seem to fit. I chose
38+
CustomAfterDirectoryBuildTargets because that makes it possible to
39+
set MtouchLink/LinkMode in Directory.Build.targets files if people
40+
wants to do so.
41+
42+
-->
43+
44+
<!-- Since we know if we're building for a simulator or not, we can determine the default trimming behavior -->
45+
<PropertyGroup Condition="'$(TrimMode)' != ''">
46+
<!-- If TrimMode is set, then that's the default link mode -->
47+
<_DefaultLinkMode>TrimMode</_DefaultLinkMode>
48+
</PropertyGroup>
49+
<PropertyGroup Condition="'$(TrimMode)' == ''">
50+
<!-- Linking is always on for all assemblies when using NativeAOT - this is because we need to modify all assemblies in the linker for them to be compatible with NativeAOT -->
51+
<_DefaultLinkMode Condition="'$(_UseNativeAot)' == 'true'">Full</_DefaultLinkMode>
52+
53+
<_DefaultLinkMode Condition="'$(_UseNativeAot)' != 'true' And '$(_PlatformName)' == 'macOS'">None</_DefaultLinkMode> <!-- Linking is off by default for macOS apps -->
54+
<_DefaultLinkMode Condition="'$(_UseNativeAot)' != 'true' And '$(_PlatformName)' == 'MacCatalyst' And '$(Configuration)' == 'Release'">SdkOnly</_DefaultLinkMode> <!-- Default linking is on for release builds for Mac Catalyst apps -->
55+
<_DefaultLinkMode Condition="'$(_UseNativeAot)' != 'true' And '$(_PlatformName)' == 'MacCatalyst' And '$(Configuration)' != 'Release'">None</_DefaultLinkMode> <!-- Default linking is off for non-release builds for Mac Catalyst apps -->
56+
<_DefaultLinkMode Condition="'$(_UseNativeAot)' != 'true' And '$(_PlatformName)' != 'macOS' And '$(_PlatformName)' != 'MacCatalyst' And '$(_SdkIsSimulator)' == 'true'">None</_DefaultLinkMode> <!-- Linking is off by default in the simulator -->
57+
<_DefaultLinkMode Condition="'$(_UseNativeAot)' != 'true' And '$(_PlatformName)' != 'macOS' And '$(_PlatformName)' != 'MacCatalyst' And '$(_SdkIsSimulator)' != 'true'">SdkOnly</_DefaultLinkMode> <!-- Linking is SdkOnly for iOS/tvOS/watchOS apps on device -->
58+
</PropertyGroup>
59+
<PropertyGroup>
60+
<_LinkMode Condition="'$(_LinkMode)' == '' And '$(_PlatformName)' == 'macOS'">$(LinkMode)</_LinkMode>
61+
<_LinkMode Condition="'$(_LinkMode)' == '' And '$(_PlatformName)' != 'macOS'">$(MtouchLink)</_LinkMode>
62+
<_LinkMode Condition="'$(_LinkMode)' == ''">$(_DefaultLinkMode)</_LinkMode>
63+
<_LinkMode Condition="'$(_LinkMode)' == '' And '$(_PlatformName)' == 'macOS'">None</_LinkMode> <!-- Linking is off by default for macOS apps -->
64+
<_LinkMode Condition="'$(_LinkMode)' == '' And '$(_PlatformName)' != 'macOS'">SdkOnly</_LinkMode> <!-- Default linking is SdkOnly for iOS/tvOS/watchOS apps -->
65+
66+
<!-- TrimMode specifies what the linker will do with framework assemblies -->
67+
<TrimMode Condition="'$(_LinkMode)' == 'TrimMode'">$(TrimMode)</TrimMode>
68+
<TrimMode Condition="'$(_LinkMode)' == 'None'">copy</TrimMode>
69+
<TrimMode Condition="'$(_LinkMode)' == 'SdkOnly'">partial</TrimMode>
70+
<TrimMode Condition="'$(_LinkMode)' == 'Full'">full</TrimMode>
71+
<!-- For None link mode we also need to set TrimMode for all assemblies. This is done later -->
72+
</PropertyGroup>
73+
74+
<PropertyGroup>
75+
<!--
76+
With NativeAOT we want to suppress trim warnings coming from ILLink and enable them only for ILC when publishing.
77+
For this reason, in case of NativeAOT while publishing, we set SuppressTrimAnalysisWarnings to true by default and store the overwriten default in
78+
_OriginalSuppressTrimAnalysisWarnings property, which is later used to properly configure warning suppression for ILC.
79+
-->
80+
<_OriginalSuppressTrimAnalysisWarnings>$(SuppressTrimAnalysisWarnings)</_OriginalSuppressTrimAnalysisWarnings>
81+
<SuppressTrimAnalysisWarnings Condition="'$(_UseNativeAot)' == 'true'">true</SuppressTrimAnalysisWarnings>
82+
<!-- Otherwise suppress trimmer warnings unless we're trimming all assemblies -->
83+
<SuppressTrimAnalysisWarnings Condition="'$(SuppressTrimAnalysisWarnings)' == '' And '$(TrimMode)' == 'full'">false</SuppressTrimAnalysisWarnings>
84+
<SuppressTrimAnalysisWarnings Condition="'$(SuppressTrimAnalysisWarnings)' == '' And '$(PublishAot)' == 'true'">false</SuppressTrimAnalysisWarnings>
85+
<SuppressTrimAnalysisWarnings Condition="'$(SuppressTrimAnalysisWarnings)' == ''">true</SuppressTrimAnalysisWarnings>
86+
</PropertyGroup>
87+
88+
</Project>

dotnet/targets/Xamarin.Shared.Sdk.props

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -136,48 +136,9 @@
136136
<_SdkIsSimulator Condition="'$(_SdkIsSimulator)' == ''">false</_SdkIsSimulator>
137137
</PropertyGroup>
138138

139-
<!-- Since we know if we're building for a simulator or not, we can determine the default trimming behavior -->
140-
<PropertyGroup Condition="'$(TrimMode)' != ''">
141-
<!-- If TrimMode is set, then that's the default link mode -->
142-
<_DefaultLinkMode>TrimMode</_DefaultLinkMode>
143-
</PropertyGroup>
144-
<PropertyGroup Condition="'$(TrimMode)' == ''">
145-
<!-- Linking is always on for all assemblies when using NativeAOT - this is because we need to modify all assemblies in the linker for them to be compatible with NativeAOT -->
146-
<_DefaultLinkMode Condition="'$(_UseNativeAot)' == 'true'">Full</_DefaultLinkMode>
147-
148-
<_DefaultLinkMode Condition="'$(_UseNativeAot)' != 'true' And '$(_PlatformName)' == 'macOS'">None</_DefaultLinkMode> <!-- Linking is off by default for macOS apps -->
149-
<_DefaultLinkMode Condition="'$(_UseNativeAot)' != 'true' And '$(_PlatformName)' == 'MacCatalyst' And '$(Configuration)' == 'Release'">SdkOnly</_DefaultLinkMode> <!-- Default linking is on for release builds for Mac Catalyst apps -->
150-
<_DefaultLinkMode Condition="'$(_UseNativeAot)' != 'true' And '$(_PlatformName)' == 'MacCatalyst' And '$(Configuration)' != 'Release'">None</_DefaultLinkMode> <!-- Default linking is off for non-release builds for Mac Catalyst apps -->
151-
<_DefaultLinkMode Condition="'$(_UseNativeAot)' != 'true' And '$(_PlatformName)' != 'macOS' And '$(_PlatformName)' != 'MacCatalyst' And '$(_SdkIsSimulator)' == 'true'">None</_DefaultLinkMode> <!-- Linking is off by default in the simulator -->
152-
<_DefaultLinkMode Condition="'$(_UseNativeAot)' != 'true' And '$(_PlatformName)' != 'macOS' And '$(_PlatformName)' != 'MacCatalyst' And '$(_SdkIsSimulator)' != 'true'">SdkOnly</_DefaultLinkMode> <!-- Linking is SdkOnly for iOS/tvOS/watchOS apps on device -->
153-
</PropertyGroup>
154139
<PropertyGroup>
155-
<_LinkMode Condition="'$(_LinkMode)' == '' And '$(_PlatformName)' == 'macOS'">$(LinkMode)</_LinkMode>
156-
<_LinkMode Condition="'$(_LinkMode)' == '' And '$(_PlatformName)' != 'macOS'">$(MtouchLink)</_LinkMode>
157-
<_LinkMode Condition="'$(_LinkMode)' == ''">$(_DefaultLinkMode)</_LinkMode>
158-
<_LinkMode Condition="'$(_LinkMode)' == '' And '$(_PlatformName)' == 'macOS'">None</_LinkMode> <!-- Linking is off by default for macOS apps -->
159-
<_LinkMode Condition="'$(_LinkMode)' == '' And '$(_PlatformName)' != 'macOS'">SdkOnly</_LinkMode> <!-- Default linking is SdkOnly for iOS/tvOS/watchOS apps -->
160-
161-
<!-- TrimMode specifies what the linker will do with framework assemblies -->
162-
<TrimMode Condition="'$(_LinkMode)' == 'TrimMode'">$(TrimMode)</TrimMode>
163-
<TrimMode Condition="'$(_LinkMode)' == 'None'">copy</TrimMode>
164-
<TrimMode Condition="'$(_LinkMode)' == 'SdkOnly'">partial</TrimMode>
165-
<TrimMode Condition="'$(_LinkMode)' == 'Full'">full</TrimMode>
166-
<!-- For None link mode we also need to set TrimMode for all assemblies. This is done later -->
167-
</PropertyGroup>
168-
169-
<PropertyGroup>
170-
<!--
171-
With NativeAOT we want to suppress trim warnings coming from ILLink and enable them only for ILC when publishing.
172-
For this reason, in case of NativeAOT while publishing, we set SuppressTrimAnalysisWarnings to true by default and store the overwriten default in
173-
_OriginalSuppressTrimAnalysisWarnings property, which is later used to properly configure warning suppression for ILC.
174-
-->
175-
<_OriginalSuppressTrimAnalysisWarnings>$(SuppressTrimAnalysisWarnings)</_OriginalSuppressTrimAnalysisWarnings>
176-
<SuppressTrimAnalysisWarnings Condition="'$(_UseNativeAot)' == 'true'">true</SuppressTrimAnalysisWarnings>
177-
<!-- Otherwise suppress trimmer warnings unless we're trimming all assemblies -->
178-
<SuppressTrimAnalysisWarnings Condition="'$(SuppressTrimAnalysisWarnings)' == '' And '$(TrimMode)' == 'full'">false</SuppressTrimAnalysisWarnings>
179-
<SuppressTrimAnalysisWarnings Condition="'$(SuppressTrimAnalysisWarnings)' == '' And '$(PublishAot)' == 'true'">false</SuppressTrimAnalysisWarnings>
180-
<SuppressTrimAnalysisWarnings Condition="'$(SuppressTrimAnalysisWarnings)' == ''">true</SuppressTrimAnalysisWarnings>
140+
<!-- See Xamarin.Shared.Sdk.Trimming.props for an explanation why we use CustomAfterDirectoryBuildTargets to compute trimming options -->
141+
<CustomAfterDirectoryBuildTargets>$(CustomAfterDirectoryBuildTargets);$(MSBuildThisFileDirectory)Xamarin.Shared.Sdk.Trimming.props</CustomAfterDirectoryBuildTargets>
181142
</PropertyGroup>
182143

183144
<!-- We're never using any app hosts -->

tests/common/DotNet.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ public static ExecutionResult InstallWorkload (params string [] workloads)
119119
args.Add ("workload");
120120
args.Add ("install");
121121
args.AddRange (workloads);
122+
args.Add ("-v");
123+
args.Add ("diag");
124+
args.Add ("--skip-manifest-update");
122125

123126
var env = new Dictionary<string, string?> ();
124127
env ["MSBuildSDKsPath"] = null;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version = "1.0" encoding = "UTF-8" ?>
2+
<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4+
xmlns:local="clr-namespace:MyMauiApp"
5+
x:Class="MyMauiApp.App">
6+
<Application.Resources>
7+
<ResourceDictionary>
8+
<ResourceDictionary.MergedDictionaries>
9+
<ResourceDictionary Source="Resources/Styles/Colors.xaml" />
10+
<ResourceDictionary Source="Resources/Styles/Styles.xaml" />
11+
</ResourceDictionary.MergedDictionaries>
12+
</ResourceDictionary>
13+
</Application.Resources>
14+
</Application>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace MyMauiApp;
2+
3+
public partial class App : Application {
4+
public App ()
5+
{
6+
InitializeComponent ();
7+
8+
MainPage = new AppShell ();
9+
}
10+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<Shell
3+
x:Class="MyMauiApp.AppShell"
4+
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
5+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
6+
xmlns:local="clr-namespace:MyMauiApp"
7+
Shell.FlyoutBehavior="Disabled"
8+
Title="MyMauiApp">
9+
10+
<ShellContent
11+
Title="Home"
12+
ContentTemplate="{DataTemplate local:MainPage}"
13+
Route="MainPage" />
14+
15+
</Shell>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace MyMauiApp;
2+
3+
public partial class AppShell : Shell {
4+
public AppShell ()
5+
{
6+
InitializeComponent ();
7+
}
8+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4+
x:Class="MyMauiApp.MainPage">
5+
6+
<ScrollView>
7+
<VerticalStackLayout
8+
Padding="30,0"
9+
Spacing="25">
10+
<Image
11+
Source="dotnet_bot.png"
12+
HeightRequest="185"
13+
Aspect="AspectFit"
14+
SemanticProperties.Description="dot net bot in a race car number eight" />
15+
16+
<Label
17+
Text="Hello, World!"
18+
Style="{StaticResource Headline}"
19+
SemanticProperties.HeadingLevel="Level1" />
20+
21+
<Label
22+
Text="Welcome to &#10;.NET Multi-platform App UI"
23+
Style="{StaticResource SubHeadline}"
24+
SemanticProperties.HeadingLevel="Level2"
25+
SemanticProperties.Description="Welcome to dot net Multi platform App U I" />
26+
27+
<Button
28+
x:Name="CounterBtn"
29+
Text="Click me"
30+
SemanticProperties.Hint="Counts the number of times you click"
31+
Clicked="OnCounterClicked"
32+
HorizontalOptions="Fill" />
33+
</VerticalStackLayout>
34+
</ScrollView>
35+
36+
</ContentPage>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace MyMauiApp;
2+
3+
public partial class MainPage : ContentPage {
4+
int count = 0;
5+
6+
public MainPage ()
7+
{
8+
InitializeComponent ();
9+
}
10+
11+
private void OnCounterClicked (object sender, EventArgs e)
12+
{
13+
count++;
14+
15+
if (count == 1)
16+
CounterBtn.Text = $"Clicked {count} time";
17+
else
18+
CounterBtn.Text = $"Clicked {count} times";
19+
20+
SemanticScreenReader.Announce (CounterBtn.Text);
21+
}
22+
}
23+

0 commit comments

Comments
 (0)