Skip to content
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

[Xamarin.Android.Build.Tasks] filter @(ReferencePath) for MonoAndroid assemblies #2934

Merged
merged 1 commit into from
Apr 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions src/Xamarin.Android.Build.Tasks/Tasks/FilterAssemblies.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using System.Collections.Generic;
using System.IO;
using System.Reflection.Metadata;
using System.Reflection.PortableExecutable;

namespace Xamarin.Android.Tasks
{
/// <summary>
/// Filters a set of assemblies based on a given TargetFrameworkIdentifier
/// </summary>
public class FilterAssemblies : Task
{
[Required]
public string TargetFrameworkIdentifier { get; set; }

[Required]
public bool DesignTimeBuild { get; set; }

public ITaskItem [] InputAssemblies { get; set; }

[Output]
public ITaskItem [] OutputAssemblies { get; set; }

public override bool Execute ()
{
if (InputAssemblies == null)
return true;

var output = new List<ITaskItem> (InputAssemblies.Length);
foreach (var assemblyItem in InputAssemblies) {
if (DesignTimeBuild && !File.Exists (assemblyItem.ItemSpec)) {
Log.LogDebugMessage ($"Skipping non-existent dependency '{assemblyItem.ItemSpec}' during a design-time build.");
continue;
}
using (var pe = new PEReader (File.OpenRead (assemblyItem.ItemSpec))) {
jonathanpeppers marked this conversation as resolved.
Show resolved Hide resolved
var reader = pe.GetMetadataReader ();
var assemblyDefinition = reader.GetAssemblyDefinition ();
var targetFrameworkIdentifier = assemblyDefinition.GetTargetFrameworkIdentifier (reader);
if (targetFrameworkIdentifier == TargetFrameworkIdentifier) {
output.Add (assemblyItem);
}
}
}
OutputAssemblies = output.ToArray ();

return !Log.HasLoggedErrors;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ void Extract (
.Where (a => a != null)
.Distinct ()) {
if (DesignTimeBuild && !File.Exists (assemblyPath)) {
Log.LogDebugMessage ("Skipping non existant dependancy '{0}' due to design time build.", assemblyPath);
Log.LogDebugMessage ($"Skipping non-existent dependency '{assemblyPath}' during a design-time build.");
continue;
}
string assemblyFileName = Path.GetFileNameWithoutExtension (assemblyPath);
Expand Down
29 changes: 28 additions & 1 deletion src/Xamarin.Android.Build.Tasks/Utilities/MetadataExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Reflection.Metadata;
using System;
using System.Reflection.Metadata;

namespace Xamarin.Android.Tasks
{
Expand All @@ -22,5 +23,31 @@ public static CustomAttributeValue<object> GetCustomAttributeArguments (this Cus
{
return attribute.DecodeValue (DummyCustomAttributeProvider.Instance);
}

/// <summary>
/// Returns the TargetFrameworkIdentifier of an assembly, or null if not found
/// </summary>
public static string GetTargetFrameworkIdentifier (this AssemblyDefinition assembly, MetadataReader reader)
{
foreach (var handle in assembly.GetCustomAttributes ()) {
var attribute = reader.GetCustomAttribute (handle);
var name = reader.GetCustomAttributeFullName (attribute);
if (name == "System.Runtime.Versioning.TargetFrameworkAttribute") {
var arguments = attribute.GetCustomAttributeArguments ();
foreach (var p in arguments.FixedArguments) {
// Of the form "MonoAndroid,Version=v8.1"
var value = p.Value?.ToString ();
if (!string.IsNullOrEmpty (value)) {
int commaIndex = value.IndexOf (",", StringComparison.Ordinal);
if (commaIndex != -1) {
return value.Substring (0, commaIndex);
}
}
}
return null;
}
}
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@
<Compile Include="Tasks\CheckForInvalidResourceFileNames.cs" />
<Compile Include="Tasks\CollectNonEmptyDirectories.cs" />
<Compile Include="Tasks\CollectPdbFiles.cs" />
<Compile Include="Tasks\FilterAssemblies.cs" />
<Compile Include="Tasks\GenerateLibraryResources.cs" />
<Compile Include="Tasks\Generator.cs" />
<Compile Include="Tasks\JarToXml.cs" />
Expand Down
15 changes: 11 additions & 4 deletions src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ Copyright (C) 2011-2012 Xamarin. All rights reserved.
<UsingTask TaskName="Xamarin.Android.Tasks.CreateAdditionalLibraryResourceCache" AssemblyFile="Xamarin.Android.Build.Tasks.dll" />
<UsingTask TaskName="Xamarin.Android.Tasks.CreateMsymManifest" AssemblyFile="Xamarin.Android.Build.Tasks.dll" />
<UsingTask TaskName="Xamarin.Android.Tasks.Crunch" AssemblyFile="Xamarin.Android.Build.Tasks.dll" />
<UsingTask TaskName="Xamarin.Android.Tasks.FilterAssemblies" AssemblyFile="Xamarin.Android.Build.Tasks.dll" />
<UsingTask TaskName="Xamarin.Android.Tasks.FindLayoutsToBind" AssemblyFile="Xamarin.Android.Build.Tasks.dll" />
<UsingTask TaskName="Xamarin.Android.Tasks.GenerateLayoutBindings" AssemblyFile="Xamarin.Android.Build.Tasks.dll" />
<UsingTask TaskName="Xamarin.Android.Tasks.GenerateLibraryResources" AssemblyFile="Xamarin.Android.Build.Tasks.dll" />
Expand Down Expand Up @@ -494,6 +495,12 @@ Copyright (C) 2011-2012 Xamarin. All rights reserved.
<_ReferenceDependencyPaths Include="@(ReferenceDependencyPaths)"
Condition="'$(AndroidApplication)' == '' Or !$(AndroidApplication)"/>
</ItemGroup>
<FilterAssemblies
DesignTimeBuild="$(DesignTimeBuild)"
TargetFrameworkIdentifier="MonoAndroid"
InputAssemblies="@(_ReferencePath);@(_ReferenceDependencyPaths)">
<Output TaskParameter="OutputAssemblies" ItemName="_MonoAndroidReferencePath" />
</FilterAssemblies>
</Target>

<PropertyGroup>
Expand All @@ -503,13 +510,13 @@ Copyright (C) 2011-2012 Xamarin. All rights reserved.
</PropertyGroup>

<Target Name="_BuildAdditionalResourcesCache"
Inputs="@(_ReferencePath);@(_ReferenceDependencyPaths);$(MSBuildProjectFullPath);$(NugetPackagesConfig);$(_AndroidBuildPropertiesCache)"
Inputs="@(_MonoAndroidReferencePath);$(MSBuildProjectFullPath);$(NugetPackagesConfig);$(_AndroidBuildPropertiesCache)"
Outputs="$(_AndroidStampDirectory)_BuildAdditionalResourcesCache.stamp"
DependsOnTargets="$(_BeforeBuildAdditionalResourcesCache)">
<GetAdditionalResourcesFromAssemblies
AndroidSdkDirectory="$(_AndroidSdkDirectory)"
AndroidNdkDirectory="$(_AndroidNdkDirectory)"
Assemblies="@(_ReferencePath);@(_ReferenceDependencyPaths)"
Assemblies="@(_MonoAndroidReferencePath)"
CacheFile="$(_AndroidResourcePathsCache)"
YieldDuringToolExecution="$(YieldDuringToolExecution)"
DesignTimeBuild="$(DesignTimeBuild)"
Expand Down Expand Up @@ -1389,13 +1396,13 @@ because xbuild doesn't support framework reference assemblies.
</Target>

<Target Name="_ResolveLibraryProjectImports"
Inputs="$(ProjectAssetsFile);$(MSBuildProjectFullPath);@(_ReferencePath);@(_ReferenceDependencyPaths);$(_AndroidBuildPropertiesCache)"
Inputs="$(ProjectAssetsFile);$(MSBuildProjectFullPath);@(_MonoAndroidReferencePath);$(_AndroidBuildPropertiesCache)"
Outputs="$(_AndroidStampDirectory)_ResolveLibraryProjectImports.stamp">
<ResolveLibraryProjectImports
ContinueOnError="$(DesignTimeBuild)"
CacheFile="$(_AndroidLibraryProjectImportsCache)"
DesignTimeBuild="$(DesignTimeBuild)"
Assemblies="@(_ReferencePath);@(_ReferenceDependencyPaths)"
Assemblies="@(_MonoAndroidReferencePath)"
AarLibraries="@(AndroidAarLibrary)"
ImportsDirectory="$(_LibraryProjectImportsDirectoryName)"
NativeImportsDirectory="$(_NativeLibraryImportsDirectoryName)"
Expand Down