Skip to content
Closed
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: 48 additions & 3 deletions tools/xabuild/XABuild.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
using Microsoft.Build.CommandLine;
using System;
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Xml;

namespace Xamarin.Android.Build
{
class XABuild
{
static Assembly Load (AssemblyName name, string path)
{
Console.WriteLine ($"[xabuild] custom assembly resolution '{name.FullName}' -> '{path}'");

//NOTE: may/may not be more correct?
//name.CodeBase = path;
//return Assembly.Load (name);

return Assembly.LoadFrom (path);
}

[MTAThread]
static int Main ()
{
Expand All @@ -19,6 +30,25 @@ static int Main ()
return 1;
}

AppDomain.CurrentDomain.AssemblyResolve += (sender, e) => {
var name = new AssemblyName (e.Name);
var path = Path.Combine (paths.MSBuildBin, name.Name + ".dll");
if (File.Exists (path)) {
return Load (name, path);
}
path = Path.Combine (paths.MSBuildBin, name.Name + ".exe");
if (File.Exists (path)) {
return Load (name, path);
}

if (e.RequestingAssembly != null) {
Console.WriteLine ($"[xabuild] assembly `{e.Name}` requested by `{e.RequestingAssembly.FullName}` not found at path `{paths.MSBuildBin}`, using default runtime behavior...");
} else {
Console.WriteLine ($"[xabuild] assembly `{e.Name}` not found at path `{paths.MSBuildBin}`, using default runtime behavior...");
}
return null; //Let the default runtime behavior occur
};

//Create a custom xabuild.exe.config
var xml = CreateConfig (paths);

Expand All @@ -45,7 +75,22 @@ static int Main ()
}
}

int exitCode = MSBuildApp.Main ();
//NOTE: Using Reflection to call MSBuildApp.Main allows us to wire up AppDomain.AssemblyResolve.
// Running on Mono, I could not even get a static ctor to work. It was loading MSBuild.dll up front.
var typeName = "Microsoft.Build.CommandLine.MSBuildApp, MSBuild";
var type = Type.GetType (typeName);
if (type == null) {
Console.WriteLine ($"Unable to load type `{typeName}`!");
return 1;
}
var method = type.GetMethod ("Main", BindingFlags.Static | BindingFlags.Public);
if (method == null) {
Console.WriteLine ($"Unable to find method `Main` on type `{type.FullName}`!");
return 1;
}

var main = (Func<int>)method.CreateDelegate (typeof (Func<int>));
int exitCode = main ();
if (exitCode != 0) {
Console.WriteLine ($"MSBuildApp.Main exited with {exitCode}, xabuild configuration is:");

Expand Down
7 changes: 0 additions & 7 deletions tools/xabuild/xabuild.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<Import Project="..\..\build-tools\scripts\MSBuildReferences.projitems" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
Expand Down Expand Up @@ -35,9 +34,6 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="MSBuild">
<HintPath>$(MSBuildReferencePath)\MSBuild.$(_MSBuildExtension)</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml" />
Expand All @@ -60,7 +56,4 @@
</Content>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="AfterBuild">
<Delete Files="$(OutputPath)MSBuild.$(_MSBuildExtension).config" />
</Target>
</Project>