Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/building-apps/build-properties.md
Original file line number Diff line number Diff line change
Expand Up @@ -1299,6 +1299,12 @@ See [TrimMode](/dotnet/core/deploying/trimming/trimming-options) for a bit more
> to `false` - to disable trimming, set `TrimMode=copy` instead (a build error
> will be raised if `PublishTrimmed` is set to `false`).

> [!NOTE]
> Due to [a known issue](https://github.com/dotnet/runtime/issues/108269), setting `PublishTrimmed`
> to `true` may cause confusing problems, so the build will report an error if this
> is detected (the solution is to not set `PublishTrimmed` at all).


The `TrimMode` property is equivalent to the existing
[MtouchLink](#mtouchlink) (for iOS, tvOS and Mac Catalyst) and
[LinkMode](#linkmode) (for macOS) properties, but the valid properties values
Expand Down
20 changes: 16 additions & 4 deletions dotnet/targets/Xamarin.Shared.Sdk.targets
Original file line number Diff line number Diff line change
Expand Up @@ -291,13 +291,25 @@
<!-- PublishTrimmed must be calculated as part of a target because IsMacEnabled on Windows will be set after connecting to the Mac -->
<Target Name="_ComputePublishTrimmed">
<PropertyGroup>
<_MustTrim Condition="'$(_MustTrim)' == '' And '$(RuntimeIdentifier)' != '' And ($(_ProjectType.EndsWith('ExecutableProject')) Or $(_ProjectType.EndsWith('AppExtensionProject'))) And '$(IsMacEnabled)' == 'true'">true</_MustTrim>
<PublishTrimmed Condition="'$(PublishTrimmed)' == '' And '$(_MustTrim)' == 'true'">true</PublishTrimmed>

<_LinkModeProperty Condition="'$(_PlatformName)' == 'macOS'">LinkMode</_LinkModeProperty>
<_LinkModeProperty Condition="'$(_PlatformName)' != 'macOS'">MtouchLink</_LinkModeProperty>

<!-- The trimmer must be enabled, so if it was explicitly disabled, show an error. -->
<_PublishTrimmedError Condition="'$(_PublishTrimmedError)' == '' And '$(PublishTrimmed)' != 'true' And '$(PublishTrimmed)' != ''">true</_PublishTrimmedError>
<!-- Also disallow explicitly enabling the trimmer, because it causes problems (https://github.com/dotnet/runtime/issues/108269),
except then PublishAot=true, because then the .NET SDK sets PublishTrimmed=true before we get to here, so we can't distinguish
between developers setting it and the .NET SDK setting it -->
<_PublishTrimmedError Condition="'$(_PublishTrimmedError)' == '' And '$(PublishTrimmed)' != '' And '$(PublishAot)' != 'true'">true</_PublishTrimmedError>
</PropertyGroup>
<Error Condition="'$(_MustTrim)' == 'true' And '$(PublishTrimmed)' != 'true'" Text="$(_PlatformName) projects must build with PublishTrimmed=true. Current value: $(PublishTrimmed). Set '$(_LinkModeProperty)=None' instead to disable trimming for all assemblies." />

<Error Condition="'$(_PublishTrimmedError)' == 'true'" Text="$(_PlatformName) projects do not support setting 'PublishTrimmed' to any value (current value: $(PublishTrimmed)). Use the '$(_LinkModeProperty)' property to configure trimming behavior instead." />

<PropertyGroup>
<_MustTrim Condition="'$(_MustTrim)' == '' And '$(RuntimeIdentifier)' != '' And ($(_ProjectType.EndsWith('ExecutableProject')) Or $(_ProjectType.EndsWith('AppExtensionProject'))) And '$(IsMacEnabled)' == 'true'">true</_MustTrim>
<PublishTrimmed Condition="'$(PublishTrimmed)' == '' And '$(_MustTrim)' == 'true'">true</PublishTrimmed>
</PropertyGroup>

<!-- Show a warning if the trimmer was disabled because there's no connection to a Mac -->
<PropertyGroup Condition="'$(PublishTrimmed)' != '' And '$(IsMacEnabled)' != 'true'">
<_PreviousPublishTrimmedValue>$(PublishTrimmed)</_PreviousPublishTrimmedValue>
<PublishTrimmed />
Expand Down
2 changes: 1 addition & 1 deletion tests/dotnet/UnitTests/ProjectTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ public void InvalidRuntimeIdentifiers (ApplePlatform platform, string runtimeIde
[TestCase (ApplePlatform.iOS, "iossimulator-x64", false)]
[TestCase (ApplePlatform.iOS, "ios-arm64", true)]
[TestCase (ApplePlatform.iOS, "ios-arm64", true, null, "Release")]
[TestCase (ApplePlatform.iOS, "ios-arm64", true, "PublishTrimmed=true;UseInterpreter=true")]
[TestCase (ApplePlatform.iOS, "ios-arm64", true, "UseInterpreter=true")]
[TestCase (ApplePlatform.MacCatalyst, "maccatalyst-arm64;maccatalyst-x64", false)]
[Category ("WindowsInclusive")]
public void IsNotMacBuild (ApplePlatform platform, string runtimeIdentifiers, bool isDeviceBuild, string? extraProperties = null, string configuration = "Debug")
Expand Down
24 changes: 14 additions & 10 deletions tests/dotnet/UnitTests/PublishTrimmedTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ namespace Xamarin.Tests {
[TestFixture]
public class PublishTrimmedTest : TestBaseClass {
[Test]
[TestCase (ApplePlatform.iOS, "ios-arm64")]
[TestCase (ApplePlatform.TVOS, "tvos-arm64")]
[TestCase (ApplePlatform.MacCatalyst, "maccatalyst-arm64")]
[TestCase (ApplePlatform.MacCatalyst, "maccatalyst-arm64;maccatalyst-x64")]
[TestCase (ApplePlatform.MacOSX, "osx-x64")]
[TestCase (ApplePlatform.MacOSX, "osx-arm64;osx-x64")]
public void DisableLinker (ApplePlatform platform, string runtimeIdentifiers)
[TestCase (ApplePlatform.iOS, "ios-arm64", "false")]
[TestCase (ApplePlatform.TVOS, "tvos-arm64", "false")]
[TestCase (ApplePlatform.MacCatalyst, "maccatalyst-arm64", "false")]
[TestCase (ApplePlatform.MacCatalyst, "maccatalyst-arm64;maccatalyst-x64", "false")]
[TestCase (ApplePlatform.MacOSX, "osx-x64", "false")]
[TestCase (ApplePlatform.MacOSX, "osx-arm64;osx-x64", "false")]

[TestCase (ApplePlatform.iOS, "ios-arm64", "true")]
[TestCase (ApplePlatform.TVOS, "tvos-arm64", "true")]
[TestCase (ApplePlatform.MacCatalyst, "maccatalyst-arm64", "true")]
[TestCase (ApplePlatform.MacOSX, "osx-arm64;osx-x64", "true")]
Comment thread
rolfbjarne marked this conversation as resolved.
public void PublishTrimmedNotSupported (ApplePlatform platform, string runtimeIdentifiers, string value)
{
var project = "MySimpleApp";
Configuration.IgnoreIfIgnoredPlatform (platform);
Expand All @@ -17,13 +22,12 @@ public void DisableLinker (ApplePlatform platform, string runtimeIdentifiers)
var project_path = GetProjectPath (project, platform: platform);
Clean (project_path);
var properties = GetDefaultProperties (runtimeIdentifiers);
properties ["PublishTrimmed"] = "false";
properties ["PublishTrimmed"] = value;

var rv = DotNet.AssertBuildFailure (project_path, properties);
var errors = BinLog.GetBuildLogErrors (rv.BinLogPath).ToArray ();
Assert.AreEqual (1, errors.Length, "Error count");
var linkModeName = platform == ApplePlatform.MacOSX ? "LinkMode" : "MtouchLink";
Assert.AreEqual ($"{platform.AsString ()} projects must build with PublishTrimmed=true. Current value: false. Set '{linkModeName}=None' instead to disable trimming for all assemblies.", errors [0].Message, "Error message");
AssertErrorMessages (errors, $"{platform.AsString ()} projects do not support setting 'PublishTrimmed' to any value (current value: {value}). Use the '{linkModeName}' property to configure trimming behavior instead.");
}
}
}
Loading