-
Notifications
You must be signed in to change notification settings - Fork 569
Migrate WarnOnPreserveAttribute from ILLink step to MSBuild task #10693
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
3 commits
Select commit
Hold shift + click to select a range
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
38 changes: 0 additions & 38 deletions
38
src/Microsoft.Android.Sdk.ILLink/WarnOnPreserveAttribute.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
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
62 changes: 62 additions & 0 deletions
62
src/Xamarin.Android.Build.Tasks/Tasks/CheckForObsoletePreserveAttribute.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,62 @@ | ||
| #nullable enable | ||
|
|
||
| using System; | ||
| using System.IO; | ||
| using System.Reflection.Metadata; | ||
| using System.Reflection.PortableExecutable; | ||
| using Microsoft.Build.Framework; | ||
| using Microsoft.Android.Build.Tasks; | ||
|
|
||
| namespace Xamarin.Android.Tasks | ||
| { | ||
| /// <summary> | ||
| /// This task provides warnings when an assembly references the obsolete PreserveAttribute. | ||
| /// The PreserveAttribute used to indicate to the linker that a type or member should not be trimmed. | ||
| /// It had similar functionality to the newer DynamicDependencyAttribute, but was Android-specific and is now obsolete. | ||
| /// </summary> | ||
| public class CheckForObsoletePreserveAttribute : AndroidTask | ||
| { | ||
| public override string TaskPrefix => "COPA"; | ||
|
|
||
| public ITaskItem[] Assemblies { get; set; } = []; | ||
|
|
||
| public override bool RunTask () | ||
| { | ||
| foreach (var assembly in Assemblies) { | ||
| if (HasObsoletePreserveAttribute (assembly.ItemSpec, out string? assemblyName)) { | ||
| Log.LogCodedWarning ("IL6001", $"Assembly '{assemblyName}' contains reference to obsolete attribute 'Android.Runtime.PreserveAttribute'. Members with this attribute may be trimmed. Please use System.Diagnostics.CodeAnalysis.DynamicDependencyAttribute instead"); | ||
| } | ||
| } | ||
| return true; // Warnings don't fail the build | ||
| } | ||
|
|
||
| bool HasObsoletePreserveAttribute (string assemblyPath, out string? assemblyName) | ||
| { | ||
| assemblyName = null; | ||
|
|
||
| try { | ||
| using var stream = File.OpenRead (assemblyPath); | ||
| using var pe = new PEReader (stream); | ||
|
|
||
| if (!pe.HasMetadata) | ||
| return false; | ||
|
|
||
| var reader = pe.GetMetadataReader (); | ||
| assemblyName = reader.GetString (reader.GetAssemblyDefinition ().Name); | ||
|
|
||
| foreach (var handle in reader.TypeReferences) { | ||
| var typeRef = reader.GetTypeReference (handle); | ||
| var ns = reader.GetString (typeRef.Namespace); | ||
| var name = reader.GetString (typeRef.Name); | ||
|
|
||
| if (ns == "Android.Runtime" && name == "PreserveAttribute") | ||
| return true; | ||
| } | ||
| return false; | ||
| } catch (BadImageFormatException ex) { | ||
| Log.LogDebugMessage ($"Could not read assembly '{assemblyPath}': {ex.Message}"); | ||
| 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.