-
Notifications
You must be signed in to change notification settings - Fork 129
Support attribute trimming opt-in #1839
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d1a6b3d
Support attribute opt-in
sbomer 6011ad3
Update docs
sbomer 73b2c4a
Rename test attributes to match
sbomer 6a72f8d
Don't pass native SDK assemblies to tests
sbomer 7126322
Update LinkTask
sbomer 308fe09
PR feedback
sbomer 0348c04
PR feedback: rename options
sbomer 7e793f4
Fix ILLink.Tasks test
sbomer f4fab05
PR feedback
sbomer bbb7b68
PR feedback
sbomer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Collections.Generic; | ||
using System.IO; | ||
|
||
namespace Mono.Linker.Steps | ||
{ | ||
public class ProcessReferencesStep : BaseStep | ||
{ | ||
protected override void Process () | ||
{ | ||
// Walk over all -reference inputs and resolve any that may need to be rooted. | ||
|
||
// For example: | ||
// -reference dir/Unreferenced.dll --action copy --trim-mode copyused | ||
// In this case we need to check whether Unreferenced has the | ||
// IsTrimmable attribute, and root it if not. | ||
// -reference dir/Unreferenced.dll --action copy --trim-mode copyused --action link Unreferenced | ||
// The per-assembly action wins over the default --action or --trim-mode, | ||
// so we don't need to load the assembly to check for IsTrimmable attribute. | ||
// -reference dir/Unreferenced.dll --action link --trim-mode link | ||
// In this case, we don't need to load the assembly up-front, because it will | ||
// not get the copy/save action, regardless of the IsTrimmable attribute. | ||
|
||
// Note that we don't do the same for assemblies which may be resolved from input directories - such | ||
// assemblies will only be rooted if something loads them. | ||
foreach (var assemblyPath in GetInputAssemblyPaths ()) { | ||
var assemblyName = Path.GetFileNameWithoutExtension (assemblyPath); | ||
|
||
// If there's no way that this reference could have the copy/save action, | ||
// we don't need to load it up-front. | ||
if (!MaybeIsFullyPreservedAssembly (assemblyName)) | ||
continue; | ||
|
||
// For the remaining references, we need to resolve them (which looks for IsTrimmable attribute) | ||
// to determine the action. | ||
var assembly = Context.TryResolve (assemblyName); | ||
if (assembly == null) { | ||
Context.LogError ($"Reference assembly '{assemblyPath}' could not be loaded", 1039); | ||
continue; | ||
} | ||
|
||
// If the assigned action (now taking into account the IsTrimmable attribute) requires us | ||
// to root the assembly, do so. | ||
if (IsFullyPreservedAction (Annotations.GetAction (assembly))) | ||
Annotations.Mark (assembly.MainModule, new DependencyInfo (DependencyKind.AssemblyAction, assembly)); | ||
} | ||
} | ||
|
||
IEnumerable<string> GetInputAssemblyPaths () | ||
{ | ||
var assemblies = new HashSet<string> (); | ||
foreach (var referencePath in Context.Resolver.GetReferencePaths ()) { | ||
var assemblyName = Path.GetFileNameWithoutExtension (referencePath); | ||
if (assemblies.Add (assemblyName)) | ||
yield return referencePath; | ||
} | ||
} | ||
|
||
public static bool IsFullyPreservedAction (AssemblyAction action) | ||
{ | ||
return action == AssemblyAction.Copy || action == AssemblyAction.Save; | ||
} | ||
|
||
bool MaybeIsFullyPreservedAssembly (string assemblyName) | ||
{ | ||
if (Context.Actions.TryGetValue (assemblyName, out AssemblyAction action)) | ||
return IsFullyPreservedAction (action); | ||
|
||
return IsFullyPreservedAction (Context.DefaultAction) || IsFullyPreservedAction (Context.TrimAction); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
nit: should be private
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.
This one is used also from MarkStep