forked from dotnet/android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerator.cs
More file actions
292 lines (228 loc) · 9.03 KB
/
Generator.cs
File metadata and controls
292 lines (228 loc) · 9.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// Copyright (C) 2012 Xamarin, Inc. All rights reserved.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Microsoft.Android.Build.Tasks;
namespace Xamarin.Android.Tasks
{
public class BindingsGenerator : AndroidDotnetToolTask
{
public override string TaskPrefix => "BGN";
public bool OnlyRunXmlAdjuster { get; set; }
public string XmlAdjusterOutput { get; set; }
[Required]
public string OutputDirectory { get; set; }
public string EnumDirectory { get; set; }
public string EnumMetadataDirectory { get; set; }
[Required]
public string AndroidApiLevel { get; set; }
[Required]
public string ApiXmlInput { get; set; }
public string AssemblyName { get; set; }
public string CodegenTarget { get; set; }
public bool NoStdlib { get; set; }
public string TypeMappingReportFile { get; set; }
public bool UseShortFileNames { get; set; }
// apart from ReferencedManagedLibraries we need it to find mscorlib.dll.
[Required]
public string MonoAndroidFrameworkDirectories { get; set; }
public string LangVersion { get; set; }
public bool EmitLegacyInterfaceInvokers { get; set; }
public bool EnableBindingStaticAndDefaultInterfaceMethods { get; set; }
public bool EnableBindingNestedInterfaceTypes { get; set; }
public bool EnableBindingInterfaceConstants { get; set; }
public string EnableRestrictToAttributes { get; set; }
public bool EnableObsoleteOverrideInheritance { get; set; }
public string Nullable { get; set; }
public ITaskItem[] TransformFiles { get; set; }
public ITaskItem[] ReferencedManagedLibraries { get; set; }
public ITaskItem[] AnnotationsZipFiles { get; set; }
public ITaskItem[] NamespaceTransforms { get; set; }
public ITaskItem[] JavadocXml { get; set; }
public string JavadocVerbosity { get; set; }
public bool UseJavaLegacyResolver { get; set; }
private List<Tuple<string, string>> transform_files = new List<Tuple<string,string>> ();
public override bool RunTask ()
{
Directory.CreateDirectory (OutputDirectory);
// We need to do this validation in Execute rather than GenerateCommandLineCommands
// because we can't terminate the build nicely in GenerateCommandLineCommands.
if (TransformFiles != null)
foreach (var fixup in TransformFiles) {
try {
var doc = XDocument.Load (fixup.ItemSpec);
switch (doc.Root.Name.LocalName) {
case "metadata":
Log.LogDebugMessage ("Adding transform file {0} as metadata.", fixup.ItemSpec);
transform_files.Add (new Tuple<string, string> (fixup.ItemSpec, "fixup"));
break;
case "enum-field-mappings":
Log.LogDebugMessage ("Adding transform file {0} as enumfields.", fixup.ItemSpec);
transform_files.Add (new Tuple<string, string> (fixup.ItemSpec, "enumfields"));
break;
case "enum-method-mappings":
Log.LogDebugMessage ("Adding transform file {0} as enummethods.", fixup.ItemSpec);
transform_files.Add (new Tuple<string, string> (fixup.ItemSpec, "enummethods"));
break;
default:
Log.LogCodedError (
code: "XA4229",
file: fixup.ItemSpec,
lineNumber: 0,
message: Properties.Resources.XA4229,
messageArgs: new [] {
doc.Root.Name.LocalName
}
);
return false;
}
} catch (Exception ex) {
Log.LogCodedError (
code: "XA4230",
file: fixup.ItemSpec,
lineNumber: 0,
message: Properties.Resources.XA4230,
messageArgs: new [] {
ex
}
);
return false;
}
}
if (NamespaceTransforms?.Any () == true)
foreach (var nt in NamespaceTransforms) {
if (!nt.MetadataNames.OfType<string> ().Contains ("Replacement", StringComparer.OrdinalIgnoreCase))
Log.LogCodedError (
code: "XA4233",
message: Properties.Resources.XA4233,
messageArgs: new [] { nt.ItemSpec }
);
}
if (Log.HasLoggedErrors)
return false;
return base.RunTask ();
}
void WriteLine (StreamWriter sw, string line)
{
sw.WriteLine (line);
Log.LogDebugMessage (line);
}
protected override string GenerateCommandLineCommands ()
{
var cmd = GetCommandLineBuilder ();
if (NamespaceTransforms?.Any () == true) {
// ex: obj/Debug/generated/msbuild-metadata.xml
var transform_file = Path.Combine (OutputDirectory, "..", "msbuild-metadata.xml");
var root = new XElement ("metadata");
foreach (var nt in NamespaceTransforms)
root.Add (new XElement ("ns-replace", new XAttribute ("source", nt.ItemSpec), new XAttribute ("replacement", nt.GetMetadata ("replacement"))));
using (var xml_writer = XmlWriter.Create (transform_file, new XmlWriterSettings { Indent = true }))
root.WriteTo (xml_writer);
transform_files.Add (new Tuple<string, string> (transform_file, "fixup"));
}
string responseFile = Path.Combine (OutputDirectory, "generator.rsp");
Log.LogDebugMessage ("[Generator] response file: {0}", responseFile);
using (var sw = new StreamWriter (responseFile, append: false, encoding: Files.UTF8withoutBOM)) {
if (OnlyRunXmlAdjuster)
WriteLine (sw, "--only-xml-adjuster");
if (!string.IsNullOrEmpty (XmlAdjusterOutput))
WriteLine (sw, $"--xml-adjuster-output=\"{XmlAdjusterOutput}\"");
if (!string.IsNullOrEmpty (CodegenTarget))
WriteLine (sw, $"--codegen-target={CodegenTarget}");
if (!string.IsNullOrEmpty (OutputDirectory))
WriteLine (sw, $"--csdir=\"{OutputDirectory}\"");
if (!string.IsNullOrEmpty (EnumDirectory))
WriteLine (sw, $"--enumdir=\"{EnumDirectory}\"");
if (!string.IsNullOrEmpty (EnumMetadataDirectory))
WriteLine (sw, $"--enummetadata=\"{EnumMetadataDirectory}\"");
if (!string.IsNullOrEmpty (AssemblyName))
WriteLine (sw, $"--assembly={AssemblyName}");
if (!NoStdlib) {
string fxpath = MonoAndroidFrameworkDirectories.Split (';').First (p => new DirectoryInfo (p).GetFiles ("mscorlib.dll").Any ());
WriteLine (sw, $"--ref=\"{Path.Combine (Path.GetFullPath (fxpath), "mscorlib.dll")}\"");
}
if (ReferencedManagedLibraries != null)
foreach (var lib in ReferencedManagedLibraries)
WriteLine (sw, $"--ref=\"{Path.GetFullPath (lib.ItemSpec)}\"");
if (AnnotationsZipFiles != null)
foreach (var zip in AnnotationsZipFiles)
WriteLine (sw, $"--annotations=\"{Path.GetFullPath (zip.ItemSpec)}\"");
foreach (var tf in transform_files)
WriteLine (sw, $"\"--{tf.Item2}={tf.Item1}\"");
if (!string.IsNullOrEmpty (AndroidApiLevel))
WriteLine (sw, $"--api-level={AndroidApiLevel}");
if (!string.IsNullOrEmpty (TypeMappingReportFile))
WriteLine (sw, $"--type-map-report=\"{TypeMappingReportFile}\"");
WriteLine (sw, "--global");
WriteLine (sw, "--public");
if (UseShortFileNames)
WriteLine (sw, "--use-short-file-names");
if (SupportsCSharp8) {
var features = new List<string> ();
if (EmitLegacyInterfaceInvokers) {
features.Add ("emit-legacy-interface-invokers");
}
if (EnableBindingInterfaceConstants)
features.Add ("interface-constants");
if (EnableBindingNestedInterfaceTypes)
features.Add ("nested-interface-types");
if (EnableBindingStaticAndDefaultInterfaceMethods)
features.Add ("default-interface-methods");
if (!EnableObsoleteOverrideInheritance)
features.Add ("do-not-fix-obsolete-overrides");
if (string.Equals (EnableRestrictToAttributes, "obsolete", StringComparison.OrdinalIgnoreCase))
features.Add ("restrict-to-attributes");
if (string.Equals (Nullable, "enable", StringComparison.OrdinalIgnoreCase))
features.Add ("nullable-reference-types");
if (features.Any ())
WriteLine (sw, $"--lang-features={string.Join (",", features)}");
}
if (!string.IsNullOrEmpty (JavadocVerbosity))
WriteLine (sw, $"\"--doc-comment-verbosity={JavadocVerbosity}\"");
if (JavadocXml != null) {
foreach (var xml in JavadocXml) {
WriteLine (sw, $"\"--with-javadoc-xml={Path.GetFullPath (xml.ItemSpec)}\"");
}
}
if (UseJavaLegacyResolver)
WriteLine (sw, "--use-legacy-java-resolver=true");
}
cmd.AppendSwitch ($"\"{ApiXmlInput}\"");
cmd.AppendSwitch ($"\"@{responseFile}\"");
return cmd.ToString ();
}
protected override void LogEventsFromTextOutput (string singleLine, MessageImportance messageImportance)
{
base.LogEventsFromTextOutput (singleLine, messageImportance);
if (messageImportance != StandardErrorLoggingImportance)
return;
Log.LogFromStandardError ("BG0000", singleLine);
}
bool SupportsCSharp8 {
get {
// These are the values that pre-date C# 8. We assume any
// new value we encounter is something that supports it.
switch (LangVersion) {
case "7.3":
case "7.2":
case "7.1":
case "7":
case "6":
case "5":
case "4":
case "3":
case "ISO-2":
case "ISO-1":
return false;
}
return true;
}
}
}
}