Skip to content

Commit

Permalink
[Xamarin.Android.Build.Tasks] Import from monodroid/301e7238
Browse files Browse the repository at this point in the history
Added Xamarin.Android.Build.Tasks and its supporting Libraries.
Code has also been updated to make uses of the new libraries in
the Java.Interop repository

Note : Xamarin.Android.Build.Tasks has a compile time code
generation step which generates Profile.g.cs. This file contains
the list of SharedRuntimeAssemblies. If building manually you will
need to make sure that Xamarin.Android.Tools.BootstrapTasks is
built first.
  • Loading branch information
dellis1972 committed Apr 25, 2016
1 parent a549be3 commit 59ec488
Show file tree
Hide file tree
Showing 194 changed files with 43,528 additions and 59 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ bin
Configuration.Override.props
obj
packages
.DS_Store
162 changes: 103 additions & 59 deletions Xamarin.Android.sln

Large diffs are not rendered by default.

58 changes: 58 additions & 0 deletions src/Xamarin.Android.Build.Tasks/Generator/Generator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mono.Cecil;
using Microsoft.Build.Utilities;

using Java.Interop.Tools.Diagnostics;
using Java.Interop.Tools.JavaCallableWrappers;

namespace Xamarin.Android.Tasks
{
class Generator
{
public static bool CreateJavaSources (TaskLoggingHelper log, IEnumerable<TypeDefinition> javaTypes, string outputPath, bool useSharedRuntime, bool generateOnCreateOverrides, bool hasExportReference)
{
bool ok = true;
foreach (var t in javaTypes) {
try {
GenerateJavaSource (log, t, outputPath, useSharedRuntime, generateOnCreateOverrides, hasExportReference);
} catch (XamarinAndroidException xae) {
ok = false;
log.LogError (
subcategory: "",
errorCode: "XA" + xae.Code,
helpKeyword: string.Empty,
file: xae.SourceFile,
lineNumber: xae.SourceLine,
columnNumber: 0,
endLineNumber: 0,
endColumnNumber: 0,
message: xae.MessageWithoutCode,
messageArgs: new object [0]
);
}
}
return ok;
}

static void GenerateJavaSource (TaskLoggingHelper log, TypeDefinition t, string outputPath, bool useSharedRuntime, bool generateOnCreateOverrides, bool hasExportReference)
{
try {
var jti = new JavaCallableWrapperGenerator (t, log.LogWarning) {
UseSharedRuntime = useSharedRuntime,
GenerateOnCreateOverrides = generateOnCreateOverrides,
};

jti.Generate (outputPath);
if (jti.HasExport && !hasExportReference)
Diagnostic.Error (4210, "You need to add a reference to Mono.Android.Export.dll when you use ExportAttribute or ExportFieldAttribute.");
} catch (Exception ex) {
if (ex is XamarinAndroidException)
throw;
Diagnostic.Error (4209, "Failed to create JavaTypeInfo for class: {0} due to {1}", t.FullName, ex);
}
}
}
}
18,132 changes: 18,132 additions & 0 deletions src/Xamarin.Android.Build.Tasks/Ionic.Zip.xml

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions src/Xamarin.Android.Build.Tasks/Linker/LinkModes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace Xamarin.Android.Tasks
{
public enum LinkModes
{
None,
SdkOnly,
Full
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using System;
using System.Collections.Generic;

using Mono.Cecil;

using Mono.Tuner;

namespace Mobile.Tuner {

public abstract class MobileProfile : Profile {

static readonly HashSet<string> Sdk = new HashSet<string> {
"mscorlib",
"System",
"System.ComponentModel.Composition",
"System.ComponentModel.DataAnnotations",
"System.Core",
"System.Data",
"System.Data.Services.Client",
"System.IO.Compression.FileSystem",
"System.IO.Compression",
"System.Json",
"System.Net",
"System.Net.Http",
"System.Numerics",
"System.Runtime.Serialization",
"System.ServiceModel",
"System.ServiceModel.Web",
"System.Transactions",
"System.Web.Services",
"System.Windows",
"System.Xml",
"System.Xml.Linq",
"System.Xml.Serialization",
"Microsoft.CSharp",
"Microsoft.VisualBasic",
"Mono.CSharp",
"Mono.Cairo",
"Mono.CompilerServices.SymbolWriter",
"Mono.Data.Tds",
"Mono.Data.Sqlite",
"Mono.Security",
"OpenTK",
"OpenTK-1.0",
// Facades assemblies (PCL)
"System.Collections.Concurrent",
"System.Collections",
"System.ComponentModel.Annotations",
"System.ComponentModel.EventBasedAsync",
"System.ComponentModel",
"System.Diagnostics.Contracts",
"System.Diagnostics.Debug",
"System.Diagnostics.Tools",
"System.Dynamic.Runtime",
"System.EnterpriseServices",
"System.Globalization",
"System.IO",
"System.Linq.Expressions",
"System.Linq.Parallel",
"System.Linq.Queryable",
"System.Linq",
"System.Net.NetworkInformation",
"System.Net.Primitives",
"System.Net.Requests",
"System.ObjectModel",
"System.Reflection.Extensions",
"System.Reflection.Primitives",
"System.Reflection",
"System.Resources.ResourceManager",
"System.Runtime.Extensions",
"System.Runtime.InteropServices",
"System.Runtime.InteropServices.WindowsRuntime",
"System.Runtime.Numerics",
"System.Runtime.Serialization.Json",
"System.Runtime.Serialization.Primitives",
"System.Runtime.Serialization.Xml",
"System.Runtime",
"System.Security.Principal",
"System.ServiceModel.Http",
"System.ServiceModel.Primitives",
"System.Text.Encoding.Extensions",
"System.Text.Encoding",
"System.Text.RegularExpressions",
"System.Threading.Tasks.Parallel",
"System.Threading.Tasks",
"System.Threading",
"System.Xml.ReaderWriter",
"System.Xml.XDocument",
"System.Xml.XmlSerializer",
};

protected override bool IsSdk (string assemblyName)
{
return Sdk.Contains (assemblyName);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// Copyright 2011, 2015 Xamarin Inc. All rights reserved.

using System;

using Mono.Linker;
using Mono.Tuner;

using Mono.Cecil;

namespace MonoDroid.Tuner {

public class ApplyPreserveAttribute : ApplyPreserveAttributeBase {

// System.ServiceModeldll is an SDK assembly but it does contain types with [DataMember] attributes
public override bool IsActiveFor (AssemblyDefinition assembly)
{
if (Profile.IsSdkAssembly (assembly)) {
if (assembly.Name.Name != "System.ServiceModel")
return false;
}
return Annotations.GetAction (assembly) == AssemblyAction.Link;
}

protected override bool IsPreservedAttribute (ICustomAttributeProvider provider, CustomAttribute attribute, out bool removeAttribute)
{
removeAttribute = false;
TypeReference type = attribute.Constructor.DeclaringType;

switch (type.Namespace) {
case "Android.Runtime":
// there's no need to keep the [Preserve] attribute in the assembly once it was processed
if (type.Name == "PreserveAttribute") {
removeAttribute = true;
return true;
}
break;
case "System.Runtime.Serialization":
bool srs = false;
// http://bugzilla.xamarin.com/show_bug.cgi?id=1415
// http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datamemberattribute.aspx
if (provider is PropertyDefinition || provider is FieldDefinition || provider is EventDefinition)
srs = (type.Name == "DataMemberAttribute");
else if (provider is TypeDefinition)
srs = (type.Name == "DataContractAttribute");

if (srs) {
MarkDefautConstructor (provider);
return true;
}
break;
case "System.Xml.Serialization":
// http://msdn.microsoft.com/en-us/library/83y7df3e.aspx
string name = type.Name;
if ((name.StartsWith ("Xml", StringComparison.Ordinal) && name.EndsWith ("Attribute", StringComparison.Ordinal))) {
// but we do not have to keep things that XML serialization will ignore anyway!
if (name != "XmlIgnoreAttribute") {
// the default constructor of the type *being used* is needed
MarkDefautConstructor (provider);
return true;
}
}
break;
default:
if (type.Name == "PreserveAttribute") {
// there's no need to keep the [Preserve] attribute in the assembly once it was processed
removeAttribute = true;
return true;
}
break;
}
// keep them (provider and attribute)
return false;
}

// xml serialization requires the default .ctor to be present
void MarkDefautConstructor (ICustomAttributeProvider provider)
{
TypeDefinition td = (provider as TypeDefinition);
if (td == null) {
PropertyDefinition pd = (provider as PropertyDefinition);
if (pd != null) {
MarkDefautConstructor (pd.DeclaringType);
MarkGenericType (pd.PropertyType as GenericInstanceType);
td = pd.PropertyType.Resolve ();
} else {
FieldDefinition fd = (provider as FieldDefinition);
if (fd != null) {
MarkDefautConstructor (fd.DeclaringType);
MarkGenericType (fd.FieldType as GenericInstanceType);
td = (fd.FieldType as TypeReference).Resolve ();
}
}
}

// e.g. <T> property (see bug #5543) or field (see linkall unit tests)
if (td != null)
MarkDefautConstructor (td);
}

void MarkGenericType (GenericInstanceType git)
{
if (git == null || !git.HasGenericArguments)
return;

foreach (TypeReference tr in git.GenericArguments)
MarkDefautConstructor (tr.Resolve ());
}

void MarkDefautConstructor (TypeDefinition type)
{
if ((type == null) || !type.HasMethods)
return;

foreach (MethodDefinition ctor in type.Methods) {
if (!ctor.IsConstructor || ctor.IsStatic || ctor.HasParameters)
continue;

Annotations.AddPreservedMethod (type, ctor);
}
}
}
}
101 changes: 101 additions & 0 deletions src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
using System;
using System.Collections.Generic;

using Mono.Cecil;

using Mono.Linker;

using Mono.Tuner;

namespace MonoDroid.Tuner {

static class Extensions {

const string JavaObject = "Java.Lang.Object";
const string IJavaObject = "Android.Runtime.IJavaObject";
const string JavaThrowable = "Java.Lang.Throwable";

public static bool IsJavaObject (this TypeDefinition type)
{
return type.Inherits (JavaObject);
}

public static bool IsJavaException (this TypeDefinition type)
{
return type.Inherits (JavaThrowable);
}

public static bool ImplementsIJavaObject (this TypeDefinition type)
{
return type.Implements (IJavaObject);
}

public static object GetSettableValue (this CustomAttributeArgument arg)
{
TypeReference tr = arg.Value as TypeReference;
TypeDefinition td = tr != null ? tr.Resolve () : null;
return td != null ? td.FullName + "," + td.Module.Assembly.FullName : arg.Value;
}

public static bool Implements (this TypeReference self, string interfaceName)
{
if (interfaceName == null)
throw new ArgumentNullException ("interfaceName");
if (self == null)
return false;

TypeDefinition type = self.Resolve ();
if (type == null)
return false; // not enough information available

// special case, check if we implement ourselves
if (type.IsInterface && (type.FullName == interfaceName))
return true;

return Implements (type, interfaceName, (interfaceName.IndexOf ('`') >= 0));
}

public static bool Implements (TypeDefinition type, string interfaceName, bool generic)
{
while (type != null) {
// does the type implements it itself
if (type.HasInterfaces) {
foreach (TypeReference iface in type.Interfaces) {
string fullname = (generic) ? iface.GetElementType ().FullName : iface.FullName;
if (fullname == interfaceName)
return true;
//if not, then maybe one of its parent interfaces does
if (Implements (iface.Resolve (), interfaceName, generic))
return true;
}
}

type = type.BaseType != null ? type.BaseType.Resolve () : null;
}
return false;
}

public static bool Inherits (this TypeReference self, string className)
{
if (className == null)
throw new ArgumentNullException ("className");
if (self == null)
return false;

TypeReference current = self.Resolve ();
while (current != null) {
string fullname = current.FullName;
if (fullname == className)
return true;
if (fullname == "System.Object")
return false;

TypeDefinition td = current.Resolve ();
if (td == null)
return false; // could not resolve type
current = td.BaseType;
}
return false;
}
}
}
Loading

0 comments on commit 59ec488

Please sign in to comment.