-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Use linker extensibility to enable better trimming #23512
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,10 @@ | ||
| // Copyright (c) .NET Foundation. All rights reserved. | ||
| // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using System.Xml; | ||
| using System.Xml.Linq; | ||
| using Microsoft.Build.Framework; | ||
|
|
@@ -21,16 +23,27 @@ public class CreateBlazorTrimmerRootDescriptorFile : Task | |
|
|
||
| public override bool Execute() | ||
| { | ||
| using var fileStream = File.Create(TrimmerFile.ItemSpec); | ||
| var rootDescriptor = CreateRootDescriptorContents(); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding the extra package file uncovered an issue with publish incrementalism. We were always writing a new trimmer descriptor which would cause the linker to be non-incremental. The task was updated to use the same pattern as |
||
| if (File.Exists(TrimmerFile.ItemSpec)) | ||
| { | ||
| var existing = File.ReadAllText(TrimmerFile.ItemSpec); | ||
|
|
||
| if (string.Equals(rootDescriptor, existing, StringComparison.Ordinal)) | ||
| { | ||
| Log.LogMessage(MessageImportance.Low, "Skipping write to file {0} because contents would not change.", TrimmerFile.ItemSpec); | ||
| // Avoid writing if the file contents are identical. This is required for build incrementalism. | ||
| return !Log.HasLoggedErrors; | ||
| } | ||
| } | ||
|
|
||
| WriteRootDescriptor(fileStream); | ||
| return true; | ||
| File.WriteAllText(TrimmerFile.ItemSpec, rootDescriptor); | ||
| return !Log.HasLoggedErrors; | ||
| } | ||
|
|
||
| internal void WriteRootDescriptor(Stream stream) | ||
| internal string CreateRootDescriptorContents() | ||
| { | ||
| var roots = new XElement("linker"); | ||
| foreach (var assembly in Assemblies) | ||
| foreach (var assembly in Assemblies.OrderBy(a => a.ItemSpec)) | ||
| { | ||
| // NOTE: Descriptor files don't include the file extension | ||
| // in the assemblyName. | ||
|
|
@@ -60,10 +73,7 @@ internal void WriteRootDescriptor(Stream stream) | |
| OmitXmlDeclaration = true | ||
| }; | ||
|
|
||
| using var writer = XmlWriter.Create(stream, xmlWriterSettings); | ||
| var xDocument = new XDocument(roots); | ||
|
|
||
| xDocument.Save(writer); | ||
| return new XDocument(roots).Root.ToString(); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,10 +18,13 @@ Copyright (c) .NET Foundation. All rights reserved. | |
| <UsingTask TaskName="Microsoft.AspNetCore.Razor.Tasks.CreateBlazorTrimmerRootDescriptorFile" AssemblyFile="$(RazorSdkBuildTasksAssembly)" /> | ||
|
|
||
| <PropertyGroup> | ||
| <PublishTrimmed Condition="'$(PublishTrimmed)' == ''">true</PublishTrimmed> | ||
| <SelfContained>true</SelfContained> | ||
| <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies> | ||
|
|
||
| <!-- Trimmer defaults --> | ||
| <PublishTrimmed Condition="'$(PublishTrimmed)' == ''">true</PublishTrimmed> | ||
| <TrimMode Condition="'$(TrimMode)' == ''">link</TrimMode> | ||
|
|
||
| <StaticWebAssetBasePath Condition="'$(StaticWebAssetBasePath)' == ''">/</StaticWebAssetBasePath> | ||
| <BlazorCacheBootResources Condition="'$(BlazorCacheBootResources)' == ''">true</BlazorCacheBootResources> | ||
|
|
||
|
|
@@ -46,14 +49,6 @@ Copyright (c) .NET Foundation. All rights reserved. | |
| <KnownFrameworkReference Remove="Microsoft.AspNetCore.App" /> | ||
| </ItemGroup> | ||
|
|
||
| <!-- Temporary workaround until ILLink.targets are updated --> | ||
| <PropertyGroup Condition=" '$(PublishTrimmed)' == 'true' "> | ||
| <IntermediateLinkDir Condition=" '$(IntermediateLinkDir)' == '' ">$(IntermediateOutputPath)linked\</IntermediateLinkDir> | ||
| <IntermediateLinkDir Condition=" !HasTrailingSlash('$(IntermediateLinkDir)') ">$(IntermediateLinkDir)\</IntermediateLinkDir> | ||
| <!-- Used to enable incremental build for the linker target. --> | ||
| <_LinkSemaphore>$(IntermediateOutputPath)Link.semaphore</_LinkSemaphore> | ||
| </PropertyGroup> | ||
|
|
||
| <Import Project="Microsoft.NET.Sdk.Razor.Components.ServiceWorkerAssetsManifest.targets" Condition="'$(ServiceWorkerAssetsManifest)' != ''" /> | ||
|
|
||
| <Target Name="_ScrambleDotnetJsFileName" AfterTargets="ResolveRuntimePackAssets"> | ||
|
|
@@ -305,18 +300,22 @@ Copyright (c) .NET Foundation. All rights reserved. | |
| </Copy> | ||
| </Target> | ||
|
|
||
| <Target Name="_CreateBlazorTrimmerRootDescriptorFiles" BeforeTargets="ILLink" AfterTargets="ComputeResolvedFilesToPublishList"> | ||
| <Target Name="_BlazorWasmPrepareForLink" BeforeTargets="PrepareForILLink"> | ||
pranavkm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| <PropertyGroup> | ||
| <_BlazorTypeGranularTrimmerDescriptorFile>$(IntermediateOutputPath)typegranularity.trimmerdescriptor.xml</_BlazorTypeGranularTrimmerDescriptorFile> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <_BlazorTypeGranularAssembly | ||
| Include="@(ResolvedFileToPublish)" | ||
| Include="@(ManagedAssemblyToLink)" | ||
| Condition="'%(Extension)' == '.dll' AND ($([System.String]::Copy('%(Filename)').StartsWith('Microsoft.AspNetCore.')) or $([System.String]::Copy('%(Filename)').StartsWith('Microsoft.Extensions.')))"> | ||
| <Required>false</Required> | ||
| <Preserve>all</Preserve> | ||
| </_BlazorTypeGranularAssembly> | ||
|
|
||
| <ManagedAssemblyToLink | ||
| IsTrimmable="true" | ||
| Condition="'%(Extension)' == '.dll' AND ($([System.String]::Copy('%(Filename)').StartsWith('Microsoft.AspNetCore.')) or $([System.String]::Copy('%(Filename)').StartsWith('Microsoft.Extensions.')))" /> | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (minor) I think there is a way to not duplicate this condition, and instead just use
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is that by removing and adding it? ItemGroup |
||
| </ItemGroup> | ||
|
|
||
| <CreateBlazorTrimmerRootDescriptorFile | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍