-
Notifications
You must be signed in to change notification settings - Fork 566
Move StripEmbeddedLibraries to MSBuild task #10894
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
7 commits
Select commit
Hold shift + click to select a range
c5ecca9
[xabt] Move StripEmbeddedLibraries from ILLink custom step to standal…
sbomer d6c80a2
Fix missing using directive for LogDebugMessage extension method
sbomer fa2606c
Fix Cecil AssemblyResolutionException by adding DefaultAssemblyResolv…
sbomer f4155d8
Fix Windows file locking in StripEmbeddedLibraries by closing Cecil h…
sbomer 452cca8
Address PR review feedback: nullable, error handling, and target cond…
sbomer 803c779
Remove StripEmbeddedLibrariesStep from non-trimmed builds and drop MS…
sbomer 386de83
Simplify StripEmbeddedLibraries: use Cecil ReadWrite mode
jonathanpeppers 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
56 changes: 0 additions & 56 deletions
56
src/Microsoft.Android.Sdk.ILLink/StripEmbeddedLibraries.cs
This file was deleted.
Oops, something went wrong.
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
116 changes: 116 additions & 0 deletions
116
src/Xamarin.Android.Build.Tasks/Tasks/StripEmbeddedLibraries.cs
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,116 @@ | ||
| #nullable enable | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using Microsoft.Android.Build.Tasks; | ||
| using Microsoft.Build.Framework; | ||
| using Microsoft.Build.Utilities; | ||
| using Mono.Cecil; | ||
|
|
||
| namespace Xamarin.Android.Tasks; | ||
|
|
||
| /// <summary> | ||
| /// An MSBuild task that strips embedded Android resources (.jar, __AndroidNativeLibraries__.zip, | ||
| /// __AndroidLibraryProjects__.zip, __AndroidEnvironment__) from trimmed assemblies. | ||
| /// | ||
| /// This runs in the inner build after ILLink but before ReadyToRun/crossgen2 compilation, | ||
| /// so that R2R images are generated from the already-stripped assemblies. | ||
| /// </summary> | ||
| public class StripEmbeddedLibraries : AndroidTask | ||
| { | ||
| public override string TaskPrefix => "SEL"; | ||
|
|
||
| [Required] | ||
| public ITaskItem [] Assemblies { get; set; } = []; | ||
|
|
||
| public bool Deterministic { get; set; } | ||
|
|
||
| public override bool RunTask () | ||
| { | ||
| var resolver = new DefaultAssemblyResolver (); | ||
| var searchDirectories = new HashSet<string> (StringComparer.OrdinalIgnoreCase); | ||
|
|
||
| foreach (var assembly in Assemblies) { | ||
| var dir = Path.GetFullPath (Path.GetDirectoryName (assembly.ItemSpec) ?? ""); | ||
| if (searchDirectories.Add (dir)) { | ||
| resolver.AddSearchDirectory (dir); | ||
| } | ||
| } | ||
|
|
||
| try { | ||
| foreach (var assembly in Assemblies) { | ||
| if (MonoAndroidHelper.IsFrameworkAssembly (assembly)) { | ||
| continue; | ||
| } | ||
|
|
||
| StripAssembly (assembly.ItemSpec, resolver); | ||
| } | ||
| } finally { | ||
| resolver.Dispose (); | ||
| } | ||
|
|
||
| return !Log.HasLoggedErrors; | ||
| } | ||
|
|
||
| void StripAssembly (string assemblyPath, IAssemblyResolver resolver) | ||
| { | ||
| string pdbPath = Path.ChangeExtension (assemblyPath, ".pdb"); | ||
| bool havePdb = File.Exists (pdbPath); | ||
|
|
||
| var readerParams = new ReaderParameters { | ||
| ReadSymbols = havePdb, | ||
| ReadWrite = true, | ||
| AssemblyResolver = resolver, | ||
| }; | ||
|
|
||
| bool assembly_modified = false; | ||
|
|
||
| using (var assembly = AssemblyDefinition.ReadAssembly (assemblyPath, readerParams)) { | ||
| foreach (var module in assembly.Modules) { | ||
| foreach (var resource in module.Resources.ToArray ()) { | ||
| if (ShouldStripResource (resource)) { | ||
| Log.LogDebugMessage ($" Stripped {resource.Name} from {assembly.Name.Name}.dll"); | ||
| module.Resources.Remove (resource); | ||
| assembly_modified = true; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (!assembly_modified) { | ||
| return; | ||
| } | ||
|
|
||
| Log.LogDebugMessage ($" Writing stripped assembly: {assemblyPath}"); | ||
| assembly.Write (new WriterParameters { | ||
| WriteSymbols = havePdb, | ||
| DeterministicMvid = Deterministic, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Determines whether a resource should be stripped from the assembly. | ||
| /// Matches the same criteria as the old ILLink StripEmbeddedLibraries step. | ||
| /// </summary> | ||
| internal static bool ShouldStripResource (Resource resource) | ||
| { | ||
| if (!(resource is EmbeddedResource)) | ||
| return false; | ||
| // Embedded jars | ||
| if (resource.Name.EndsWith (".jar", StringComparison.InvariantCultureIgnoreCase)) | ||
| return true; | ||
| // Embedded AndroidNativeLibrary archive | ||
| if (resource.Name == "__AndroidNativeLibraries__.zip") | ||
| return true; | ||
| // Embedded AndroidResourceLibrary archive | ||
| if (resource.Name == "__AndroidLibraryProjects__.zip") | ||
| return true; | ||
| // Embedded AndroidEnvironment items | ||
| if (resource.Name.StartsWith ("__AndroidEnvironment__", StringComparison.Ordinal)) | ||
| return true; | ||
| return false; | ||
| } | ||
|
|
||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.