Skip to content

Commit

Permalink
[Xamarin.Android.Build.Tasks] Better support for netstandard libraries.
Browse files Browse the repository at this point in the history
Fixes dotnet#1154, dotnet#1162

Netstandard packages sometimes ship with both reference and
implementation assemblies. The Nuget build task `ResolveNuGetPackageAssets`
only resolves the `ref` version of the assemblies. There does
not seem to be away way to FORCE Nuget to resolve the lib one.
How .net Core manages to do this is still a mistery. That said
the Nuget `ResolveNuGetPackageAssets` does give us a hint as to
how to use the `project.assets.json` file to figure out what
`lib` version of the package we should be including.

This commit reworks `ResolveAssemblies` to attempt to map the
`ref` to a `lib` if we find a Referenece Assembly. Historically
we just issue a warning (which will probably be ignored), but
now we will use the `project.assets.json` file to find the
implementation version of the `ref` assembly.

We need to be using `NewtonSoft.Json` since it provides a decent
API for querying json. We make use of the Nuget build properties
`$(ProjectLockFile)` for the location of the `project.assets.json`
, `$(NuGetPackageRoot)` for the root folder of the Nuget packages
and `$(NuGetTargetMoniker)` and `$(_NuGetTargetFallbackMoniker)`
for resolving which `TargetFrameworks` we are looking for. All of these
properties should be set by Nuget. If they are not we should fallback
to the default behaviour and just issue the warning.

	{
  		"version": 3,
  		"targets": {
    			"MonoAndroid,Version=v8.1": {
				"System.IO.Packaging/4.4.0": {
					"type": "package",
					"compile": {
						"ref/netstandard1.3/System.IO.Packaging.dll": {}
					},
					"runtime": {
						"lib/netstandard1.3/System.IO.Packaging.dll": {}
					}
				},
			}
		}
	}

The code above is a cut down sample of the `project.assets.json`. So our
code will first resolve the `targets`. We use `$(NuGetTargetMoniker)` to
do this. For an android project this should have a value of
`MonoAndroid,Version=v8.1`. Note we do NOT need to worry about the version
here. When Nuget restores the packages it creates the file so it will
use the correct version.
Next we try to find the `System.IO.Packaging/4.4.0`. My first throught
was to use the version of the reference assembly we just loaded. But
in this package even though the nuget version was `4.4.0` the assembly
versioin was `4.0.2` .. so not helpful. So we need to just search for the
items starting with `System.IO.Packaging`. Next is to look for the `runtime`
item and then finally the value of that. Once we have resolved the path
we need to then combine that with the `$(NuGetPackageRoot)` to get the
full path to the new library. If at any point during all of this code
we don't get what we expect (i.e a null) we should abort and just issue
the warning.

The only real concern with this is if the format of the `project.assets.json`
file changes. It is not ment to be edited by a human so there is the
possibiltity that the Nuget team will decide to either change the schema or
even migrate to a new file format.
  • Loading branch information
dellis1972 committed Mar 6, 2018
1 parent 351ca70 commit 0a4b493
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 2 deletions.
59 changes: 57 additions & 2 deletions src/Xamarin.Android.Build.Tasks/Tasks/ResolveAssemblies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
using MonoDroid.Tuner;
using System.IO;
using Xamarin.Android.Tools;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

using Java.Interop.Tools.Cecil;

Expand All @@ -24,6 +26,15 @@ public class ResolveAssemblies : Task
[Required]
public string ReferenceAssembliesDirectory { get; set; }

[Required]
public string ProjectLockFile { get; set; }

[Required]
public ITaskItem[] TargetMonikers { get; set; }

[Required]
public string NuGetPackageRoot { get; set; }

public string I18nAssemblies { get; set; }
public string LinkMode { get; set; }

Expand Down Expand Up @@ -65,6 +76,12 @@ bool Execute (DirectoryAssemblyResolver resolver)

var topAssemblyReferences = new List<AssemblyDefinition> ();

JObject lockFile;
using (var streamReader = new StreamReader(ProjectLockFile))
{
lockFile = JObject.Load(new JsonTextReader(streamReader));
}

try {
foreach (var assembly in Assemblies) {
var assembly_path = Path.GetDirectoryName (assembly.ItemSpec);
Expand All @@ -77,8 +94,12 @@ bool Execute (DirectoryAssemblyResolver resolver)
if (assemblyDef == null)
throw new InvalidOperationException ("Failed to load assembly " + assembly.ItemSpec);
if (MonoAndroidHelper.IsReferenceAssembly (assemblyDef)) {
Log.LogWarning ($"Ignoring {assembly_path} as it is a Reference Assembly");
continue;
// Resolve "runtime" library
assemblyDef = ResolveRuntimeAssemblyForReferenceAssembly (lockFile["targets"], resolver, assemblyDef.Name);
if (assemblyDef == null) {
Log.LogWarning ($"Ignoring {assembly_path} as it is a Reference Assembly");
continue;
}
}
topAssemblyReferences.Add (assemblyDef);
assemblies.Add (Path.GetFullPath (assemblyDef.MainModule.FullyQualifiedName));
Expand Down Expand Up @@ -120,6 +141,40 @@ bool Execute (DirectoryAssemblyResolver resolver)
readonly List<string> do_not_package_atts = new List<string> ();
int indent = 2;

JToken GetTargetFrameworkMonikerForNuget (JToken targets)
{
foreach (var targetMoniker in TargetMonikers) {
var targetMonikerWithoutRuntimeIdentifier = targetMoniker.ItemSpec;
if (targets [targetMonikerWithoutRuntimeIdentifier] != null)
return targets [targetMonikerWithoutRuntimeIdentifier];
}
var enumerableTargets = targets.Cast<KeyValuePair<string, JToken>> ();
return enumerableTargets.FirstOrDefault ().Value ?? null;
}

AssemblyDefinition ResolveRuntimeAssemblyForReferenceAssembly (JToken targets, DirectoryAssemblyResolver resolver, AssemblyNameDefinition assemblyNameDefinition)
{
var targetFramework = GetTargetFrameworkMonikerForNuget (targets);
if (targetFramework == null)
return null;
var packageRoot = targetFramework.Cast<JProperty>().FirstOrDefault (x => x.Name.StartsWith (assemblyNameDefinition.Name));
if (packageRoot == null)
return null;
var packageName = packageRoot.Name;
var package = packageRoot.FirstOrDefault ();
if (package == null)
return null;
var runtime = package ["runtime"];
if (runtime == null)
return null;
var property = runtime.FirstOrDefault () as JProperty;
if (property == null)
return null;
var path = Path.Combine (NuGetPackageRoot, packageName, property.Name);
Log.LogDebugMessage ($"Attempting to load {path}");
return resolver.Load (path, forceLoad: true);
}

void AddAssemblyReferences (DirectoryAssemblyResolver resolver, ICollection<string> assemblies, AssemblyDefinition assembly, bool topLevel)
{
var fqname = assembly.MainModule.FullyQualifiedName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ protected override void OnResume()
"System.dll",
"System.Runtime.Serialization.dll",
"System.IO.Packaging.dll",
"System.IO.Compression.dll",
"Mono.Android.Export.dll",
"App1.dll",
"FormsViewGroup.dll",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@
<Reference Include="FSharp.Compiler.CodeDom">
<HintPath>..\..\packages\FSharp.Compiler.CodeDom.1.0.0.1\lib\net40\FSharp.Compiler.CodeDom.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json">
<HintPath>..\..\packages\Newtonsoft.Json.11.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="$(IntermediateOutputPath)Profile.g.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1581,6 +1581,9 @@ because xbuild doesn't support framework reference assemblies.
Assemblies="@(FilteredAssemblies)"
I18nAssemblies="$(MandroidI18n)"
LinkMode="$(AndroidLinkMode)"
ProjectLockFile="$(ProjectLockFile)"
NuGetPackageRoot="$(NuGetPackageRoot)"
TargetMonikers="$(NuGetTargetMoniker);$(_NuGetTargetFallbackMoniker)"
ReferenceAssembliesDirectory="$(TargetFrameworkDirectory)">
<Output TaskParameter="ResolvedAssemblies" ItemName="ResolvedAssemblies" />
<Output TaskParameter="ResolvedUserAssemblies" ItemName="ResolvedUserAssemblies" />
Expand Down
1 change: 1 addition & 0 deletions src/Xamarin.Android.Build.Tasks/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
<packages>
<package id="FSharp.Compiler.CodeDom" version="1.0.0.1" targetFramework="net45" />
<package id="Irony" version="0.9.1" targetFramework="net45" />
<package id="Newtonsoft.Json" version="11.0.1" targetFramework="net451" />
</packages>

0 comments on commit 0a4b493

Please sign in to comment.