Skip to content

Commit

Permalink
Enable additional ways to opt in to trimming (#16094)
Browse files Browse the repository at this point in the history
* Enable additional ways to opt in to trimming

This implements the SDK side of the behavior described at https://github.com/mono/linker/blob/main/docs/design/trimmed-assemblies.md#net-6.

This includes a linker update with dotnet/linker#1839.

* PR feedback
- use JoinItems task :)
- always set _TrimmerDefaultAction
  • Loading branch information
sbomer authored Mar 1, 2021
1 parent 8cf31f0 commit b8b3f10
Show file tree
Hide file tree
Showing 4 changed files with 335 additions and 17 deletions.
8 changes: 4 additions & 4 deletions eng/Version.Details.xml
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,13 @@
<Uri>https://github.com/microsoft/vstest</Uri>
<Sha>0447617600851a20f1d75a2ff86da4c0fc67d999</Sha>
</Dependency>
<Dependency Name="Microsoft.NET.ILLink.Tasks" Version="6.0.100-preview.2.21124.3">
<Dependency Name="Microsoft.NET.ILLink.Tasks" Version="6.0.100-preview.2.21125.1">
<Uri>https://github.com/mono/linker</Uri>
<Sha>6b3a3050c70577bd1b3fd7611eef56679e22a4f1</Sha>
<Sha>44907d98e524f65db0a0edc2cab8afe077ba812a</Sha>
</Dependency>
<Dependency Name="Microsoft.NET.ILLink.Analyzers" Version="6.0.100-preview.2.21124.3">
<Dependency Name="Microsoft.NET.ILLink.Analyzers" Version="6.0.100-preview.2.21125.1">
<Uri>https://github.com/mono/linker</Uri>
<Sha>6b3a3050c70577bd1b3fd7611eef56679e22a4f1</Sha>
<Sha>44907d98e524f65db0a0edc2cab8afe077ba812a</Sha>
</Dependency>
<Dependency Name="System.CodeDom" Version="6.0.0-preview.3.21125.3">
<Uri>https://github.com/dotnet/runtime</Uri>
Expand Down
2 changes: 1 addition & 1 deletion eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
</PropertyGroup>
<PropertyGroup>
<!-- Dependencies from https://github.com/mono/linker -->
<MicrosoftNETILLinkTasksPackageVersion>6.0.100-preview.2.21124.3</MicrosoftNETILLinkTasksPackageVersion>
<MicrosoftNETILLinkTasksPackageVersion>6.0.100-preview.2.21125.1</MicrosoftNETILLinkTasksPackageVersion>
<MicrosoftNETILLinkAnalyzerPackageVersion>$(MicrosoftNETILLinkTasksPackageVersion)</MicrosoftNETILLinkAnalyzerPackageVersion>
</PropertyGroup>
<PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ Copyright (c) .NET Foundation. All rights reserved.
ReferenceAssemblyPaths="@(ReferencePath)"
RootAssemblyNames="@(TrimmerRootAssembly)"
TrimMode="$(TrimMode)"
DefaultAction="$(_TrimmerDefaultAction)"
RemoveSymbols="$(TrimmerRemoveSymbols)"
FeatureSettings="@(_TrimmerFeatureSettings)"
CustomData="@(_TrimmerCustomData)"
Expand Down Expand Up @@ -171,14 +172,20 @@ Copyright (c) .NET Foundation. All rights reserved.
in some cases. -->
<NETSdkError Condition="'$(SelfContained)' != 'true'" ResourceName="ILLinkNotSupportedError" />

<PropertyGroup>
<ILLinkWarningLevel Condition=" '$(ILLinkWarningLevel)' == '' And
'$(EffectiveAnalysisLevel)' != '' And
<PropertyGroup Condition=" '$(ILLinkWarningLevel)' == '' ">
<ILLinkWarningLevel Condition=" '$(EffectiveAnalysisLevel)' != '' And
$([MSBuild]::VersionGreaterThanOrEquals($(EffectiveAnalysisLevel), '5.0')) ">5</ILLinkWarningLevel>
<ILLinkWarningLevel Condition=" '$(ILLinkWarningLevel)' == '' ">0</ILLinkWarningLevel>
</PropertyGroup>

<PropertyGroup>
<ILLinkTreatWarningsAsErrors Condition=" '$(ILLinkTreatWarningsAsErrors)' == '' ">$(TreatWarningsAsErrors)</ILLinkTreatWarningsAsErrors>
<_ExtraTrimmerArgs>--skip-unresolved true $(_ExtraTrimmerArgs)</_ExtraTrimmerArgs>
<TrimMode Condition=" '$(TrimMode)' == '' ">copyused</TrimMode>
<!-- For .NET < 6, the defaults are the same regardless of whether the assembly has an IsTrimmable attribute. (The attribute didn't exist until .NET 6.) -->
<_TrimmerDefaultAction Condition=" $([MSBuild]::VersionLessThan('$(TargetFrameworkVersion)', '6.0')) ">$(TrimMode)</_TrimmerDefaultAction>
<!-- For .NET 6+, assemblies without IsTrimmable attribute get the "copy" action. -->
<_TrimmerDefaultAction Condition=" '$(_TrimmerDefaultAction)' == '' ">copy</_TrimmerDefaultAction>
</PropertyGroup>

<!-- Suppress warnings produced by the linker. Any warnings shared with the analyzer should be
Expand Down Expand Up @@ -220,18 +227,47 @@ Copyright (c) .NET Foundation. All rights reserved.
<TrimmerRemoveSymbols Condition=" '$(DebuggerSupport)' != 'false' ">false</TrimmerRemoveSymbols>
</PropertyGroup>

<!-- Set IsTrimmable for any assemblies that already have customized TrimMode. -->
<ItemGroup>
<!-- Treat any assemblies that already have customized TrimMode as trimmable. -->
<ManagedAssemblyToLink Condition=" '%(ManagedAssemblyToLink.TrimMode)' != '' ">
<IsTrimmable>true</IsTrimmable>
</ManagedAssemblyToLink>
<!-- Root and copy non-trimmable assemblies. -->
</ItemGroup>

<!-- SetIsTrimmable for any assemblies listed in TrimmableAssembly. -->
<JoinItems Left="@(ManagedAssemblyToLink)" LeftKey="FileName" LeftMetadata="*"
Right="@(TrimmableAssembly)"
ItemSpecToUse="Left">
<Output TaskParameter="JoinResult" ItemName="_TrimmableManagedAssemblyToLink" />
</JoinItems>
<ItemGroup>
<ManagedAssemblyToLink Remove="@(_TrimmableManagedAssemblyToLink)" />
<ManagedAssemblyToLink Include="@(_TrimmableManagedAssemblyToLink)" IsTrimmable="true" />
</ItemGroup>

<!-- Root the main assembly, whether or not it has IsTrimmable set. -->
<ItemGroup>
<TrimmerRootAssembly Include="@(IntermediateAssembly)" />
</ItemGroup>

<!-- For .NET < 6, root and copy assemblies without IsTrimmable=true MSBuild metadata. -->
<ItemGroup Condition=" $([MSBuild]::VersionLessThan('$(TargetFrameworkVersion)', '6.0')) ">
<TrimmerRootAssembly Include="@(ManagedAssemblyToLink)" Condition=" '%(ManagedAssemblyToLink.IsTrimmable)' != 'true' " />
<ManagedAssemblyToLink Condition=" '%(ManagedAssemblyToLink.IsTrimmable)' != 'true' ">
<TrimMode>copy</TrimMode>
</ManagedAssemblyToLink>
</ItemGroup>

<!-- In .NET6+, set the action explicitly for any with IsTrimmable MSBuild metadata -->
<ItemGroup Condition=" $([MSBuild]::VersionGreaterThanOrEquals('$(TargetFrameworkVersion)', '6.0')) ">
<ManagedAssemblyToLink Condition=" '%(ManagedAssemblyToLink.IsTrimmable)' == 'true' And '%(ManagedAssemblyToLink.TrimMode)' == '' ">
<TrimMode>$(TrimMode)</TrimMode>
</ManagedAssemblyToLink>
<ManagedAssemblyToLink Condition=" '%(ManagedAssemblyToLink.IsTrimmable)' == 'false' And '%(ManagedAssemblyToLink.TrimMode)' == '' ">
<TrimMode>$(_TrimmerDefaultAction)</TrimMode>
</ManagedAssemblyToLink>
</ItemGroup>

<ItemGroup>
<_TrimmerFeatureSettings Include="@(RuntimeHostConfigurationOption)" Condition="'%(RuntimeHostConfigurationOption.Trim)' == 'true'" />
</ItemGroup>
Expand Down
Loading

0 comments on commit b8b3f10

Please sign in to comment.