From ed5039f7b89ed556dabe7de3c6a63c31e6ed9a31 Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Tue, 10 Jan 2023 14:30:16 +0100 Subject: [PATCH] [dotnet] Ensure any trimmer components are restored properly. .NET 8 will load the ILLink component based on the target framework of the project file - so if .NET 8 is building a net7.0-ios app, the build will restore and use the ILLink component from .NET 7. There is a problem however: * The inclusion of the ILLink component is dpendent on the PublishTrimmed property - if PublishTrimmed is true, then the ILLink component is restored (which makes sense, why restore it if it's not going to be used?). * We always PublishTrimmed, because the linker must always be executed for our projects. So far so good - we can just always enable PublishTrimmed, right? * Nope, when building on Windows, we only enable PublishTrimmed when connected to a Mac, because that's where the ILLink target must be executed (and if we're not connected to a Mac, we can't run the ILLink target, and things fall apart - so just disable PublishTrimmed in that, since it won't work anyway). * Early on in the build we have no idea if we're connected to a Mac or not, which means we can only enable PublishTrimmed in a target, and not as an early-on default value. This is *way* to late for the ILLink component, which needs PublishTrimmed set quite early in the build process. * Fortunately, the ILLink inclusion is actually gated on a different variable (_IsTrimmingEnabled) - which is initialized from PublishTrimmed if it's not set. So the way out here is to set _IsTrimmingEnabled early enough, and now the ILLink component is included and restored. * The additional hurdle is that we need to set _IsTrimmingEnabled in our .NET 6 and .NET 7 workloads as well, it's not enough to set it in our .NET 8 workload (which isn't even loaded when building an earlier TFM). --- dotnet/targets/Xamarin.Shared.Sdk.props | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/dotnet/targets/Xamarin.Shared.Sdk.props b/dotnet/targets/Xamarin.Shared.Sdk.props index 77adbac40ab..a8dcdb5e045 100644 --- a/dotnet/targets/Xamarin.Shared.Sdk.props +++ b/dotnet/targets/Xamarin.Shared.Sdk.props @@ -39,6 +39,14 @@ true + + + <_IsTrimmingEnabled>true