Skip to content

Commit 31f5b9e

Browse files
dustin-wojciechowskiGitHub Actions Autoformatter
andauthored
[Generator] Add BindingTouchConfig to BindingTouch (#19485)
This takes the variables instantiated at the beginning of the Main3 method and places them into their own class that will be used as a parameter object to extract methods out of the rest of what's in the Main3 method. The CreateOptionSet() method is an example of what I have envisioned. Also I renamed some of the values to be more helpful based on the descriptions in the option set. --------- Co-authored-by: GitHub Actions Autoformatter <github-actions-autoformatter@xamarin.com>
1 parent 47c8b64 commit 31f5b9e

File tree

3 files changed

+92
-80
lines changed

3 files changed

+92
-80
lines changed

src/bgen/BindingTouch.cs

Lines changed: 65 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -205,41 +205,18 @@ static int Main2 (string [] args)
205205
return touch.Main3 (args);
206206
}
207207

208-
int Main3 (string [] args)
208+
public OptionSet CreateOptionSet (BindingTouchConfig config)
209209
{
210-
bool show_help = false;
211-
bool zero_copy = false;
212-
string? basedir = null;
213-
string? tmpdir = null;
214-
string? ns = null;
215-
bool delete_temp = true, debug = false;
216-
bool unsafef = true;
217-
bool external = false;
218-
bool public_mode = true;
219-
bool nostdlib = false;
220-
bool? inline_selectors = null;
221-
List<string> sources;
222-
var resources = new List<string> ();
223-
var linkwith = new List<string> ();
224-
var api_sources = new List<string> ();
225-
var core_sources = new List<string> ();
226-
var extra_sources = new List<string> ();
227-
var defines = new List<string> ();
228-
string? generate_file_list = null;
229-
bool process_enums = false;
230-
231-
ErrorHelper.ClearWarningLevels ();
232-
233-
var os = new OptionSet () {
234-
{ "h|?|help", "Displays the help", v => show_help = true },
210+
return new OptionSet () {
211+
{ "h|?|help", "Displays the help", v => config.ShowHelp = true },
235212
{ "a", "Include alpha bindings (Obsolete).", v => {}, true },
236-
{ "outdir=", "Sets the output directory for the temporary binding files", v => { basedir = v; }},
213+
{ "outdir=", "Sets the output directory for the temporary binding files", v => { config.BindingFilesOutputDirectory = v; }},
237214
{ "o|out=", "Sets the name of the output library", v => outfile = v },
238-
{ "tmpdir=", "Sets the working directory for temp files", v => { tmpdir = v; delete_temp = false; }},
239-
{ "debug", "Generates a debugging build of the binding", v => debug = true },
240-
{ "sourceonly=", "Only generates the source", v => generate_file_list = v },
241-
{ "ns=", "Sets the namespace for storing helper classes", v => ns = v },
242-
{ "unsafe", "Sets the unsafe flag for the build", v=> unsafef = true },
215+
{ "tmpdir=", "Sets the working directory for temp files", v => { config.TemporaryFileDirectory = v; config.DeleteTemporaryFiles = false; }},
216+
{ "debug", "Generates a debugging build of the binding", v => config.IsDebug = true },
217+
{ "sourceonly=", "Only generates the source", v => config.GeneratedFileList = v },
218+
{ "ns=", "Sets the namespace for storing helper classes", v => config.HelperClassNamespace = v },
219+
{ "unsafe", "Sets the unsafe flag for the build", v=> config.IsUnsafe = true },
243220
{ "core", "Use this to build product assemblies", v => BindThirdPartyLibrary = false },
244221
{ "r|reference=", "Adds a reference", v => references.Add (v) },
245222
{ "lib=", "Adds the directory to the search path for the compiler", v => libs.Add (v) },
@@ -252,24 +229,24 @@ int Main3 (string [] args)
252229
},
253230
{ "sdk=", "Sets the .NET SDK to use (Obsolete)", v => {}, true },
254231
{ "new-style", "Build for Unified (Obsolete).", v => { Console.WriteLine ("The --new-style option is obsolete and ignored."); }, true},
255-
{ "d=", "Defines a symbol", v => defines.Add (v) },
256-
{ "api=", "Adds a API definition source file", v => api_sources.Add (v) },
257-
{ "s=", "Adds a source file required to build the API", v => core_sources.Add (v) },
232+
{ "d=", "Defines a symbol", v => config.Defines.Add (v) },
233+
{ "api=", "Adds a API definition source file", v => config.ApiSources.Add (v) },
234+
{ "s=", "Adds a source file required to build the API", v => config.CoreSources.Add (v) },
258235
{ "q", "Quiet", v => ErrorHelper.Verbosity-- },
259236
{ "v", "Sets verbose mode", v => ErrorHelper.Verbosity++ },
260-
{ "x=", "Adds the specified file to the build, used after the core files are compiled", v => extra_sources.Add (v) },
261-
{ "e", "Generates smaller classes that can not be subclassed (previously called 'external mode')", v => external = true },
262-
{ "p", "Sets private mode", v => public_mode = false },
237+
{ "x=", "Adds the specified file to the build, used after the core files are compiled", v => config.ExtraSources.Add (v) },
238+
{ "e", "Generates smaller classes that can not be subclassed (previously called 'external mode')", v => config.IsExternal = true },
239+
{ "p", "Sets private mode", v => config.IsPublicMode = false },
263240
{ "baselib=", "Sets the base library", v => baselibdll = v },
264241
{ "attributelib=", "Sets the attribute library", v => attributedll = v },
265-
{ "use-zero-copy", v=> zero_copy = true },
266-
{ "nostdlib", "Does not reference mscorlib.dll library", l => nostdlib = true },
242+
{ "use-zero-copy", v=> config.UseZeroCopy = true },
243+
{ "nostdlib", "Does not reference mscorlib.dll library", l => config.OmitStandardLibrary = true },
267244
{ "no-mono-path", "Launches compiler with empty MONO_PATH", l => { }, true },
268245
{ "native-exception-marshalling", "Enable the marshalling support for Objective-C exceptions", (v) => { /* no-op */} },
269246
{ "inline-selectors:", "If Selector.GetHandle is inlined and does not need to be cached (enabled by default in Xamarin.iOS, disabled in Xamarin.Mac)",
270-
v => inline_selectors = string.Equals ("true", v, StringComparison.OrdinalIgnoreCase) || string.IsNullOrEmpty (v)
247+
v => config.InlineSelectors = string.Equals ("true", v, StringComparison.OrdinalIgnoreCase) || string.IsNullOrEmpty (v)
271248
},
272-
{ "process-enums", "Process enums as bindings, not external, types.", v => process_enums = true },
249+
{ "process-enums", "Process enums as bindings, not external, types.", v => config.ProcessEnums = true },
273250
{ "link-with=,", "Link with a native library {0:FILE} to the binding, embedded as a resource named {1:ID}",
274251
(path, id) => {
275252
if (path is null || path.Length == 0)
@@ -278,11 +255,11 @@ int Main3 (string [] args)
278255
if (id is null || id.Length == 0)
279256
id = Path.GetFileName (path);
280257

281-
if (linkwith.Contains (id))
258+
if (config.LinkWith.Contains (id))
282259
throw new Exception ("-link-with=FILE,ID cannot assign the same resource id to multiple libraries.");
283260

284-
resources.Add (string.Format ("-res:{0},{1}", path, id));
285-
linkwith.Add (id);
261+
config.Resources.Add (string.Format ("-res:{0},{1}", path, id));
262+
config.LinkWith.Add (id);
286263
}
287264
},
288265
{ "unified-full-profile", "Launches compiler pointing to XM Full Profile", l => { /* no-op*/ }, true },
@@ -321,16 +298,24 @@ int Main3 (string [] args)
321298
{ "compiled-api-definition-assembly=", "An assembly with the compiled api definitions.", (v) => compiled_api_definition_assembly = v },
322299
new Mono.Options.ResponseFileSource (),
323300
};
301+
}
302+
303+
int Main3 (string [] args)
304+
{
305+
ErrorHelper.ClearWarningLevels ();
306+
307+
BindingTouchConfig config = new ();
308+
OptionSet os = CreateOptionSet (config);
324309

325310
try {
326-
sources = os.Parse (args);
311+
config.Sources = os.Parse (args);
327312
} catch (Exception e) {
328313
Console.Error.WriteLine ("{0}: {1}", ToolName, e.Message);
329314
Console.Error.WriteLine ("see {0} --help for more information", ToolName);
330315
return 1;
331316
}
332317

333-
if (show_help) {
318+
if (config.ShowHelp) {
334319
ShowHelp (os);
335320
return 0;
336321
}
@@ -341,7 +326,7 @@ int Main3 (string [] args)
341326
switch (target_framework.Value.Platform) {
342327
case ApplePlatform.iOS:
343328
CurrentPlatform = PlatformName.iOS;
344-
nostdlib = true;
329+
config.OmitStandardLibrary = true;
345330
if (string.IsNullOrEmpty (baselibdll))
346331
baselibdll = CurrentPlatform.GetPath ("lib/mono/Xamarin.iOS/Xamarin.iOS.dll");
347332
if (!IsDotNet) {
@@ -351,7 +336,7 @@ int Main3 (string [] args)
351336
break;
352337
case ApplePlatform.TVOS:
353338
CurrentPlatform = PlatformName.TvOS;
354-
nostdlib = true;
339+
config.OmitStandardLibrary = true;
355340
if (string.IsNullOrEmpty (baselibdll))
356341
baselibdll = CurrentPlatform.GetPath ("lib/mono/Xamarin.TVOS/Xamarin.TVOS.dll");
357342
if (!IsDotNet) {
@@ -361,7 +346,7 @@ int Main3 (string [] args)
361346
break;
362347
case ApplePlatform.WatchOS:
363348
CurrentPlatform = PlatformName.WatchOS;
364-
nostdlib = true;
349+
config.OmitStandardLibrary = true;
365350
if (string.IsNullOrEmpty (baselibdll))
366351
baselibdll = CurrentPlatform.GetPath ("lib/mono/Xamarin.WatchOS/Xamarin.WatchOS.dll");
367352
if (!IsDotNet) {
@@ -371,7 +356,7 @@ int Main3 (string [] args)
371356
break;
372357
case ApplePlatform.MacCatalyst:
373358
CurrentPlatform = PlatformName.MacCatalyst;
374-
nostdlib = true;
359+
config.OmitStandardLibrary = true;
375360
if (string.IsNullOrEmpty (baselibdll))
376361
baselibdll = CurrentPlatform.GetPath ("lib/mono/Xamarin.MacCatalyst/Xamarin.MacCatalyst.dll");
377362
if (!IsDotNet) {
@@ -381,7 +366,7 @@ int Main3 (string [] args)
381366
break;
382367
case ApplePlatform.MacOSX:
383368
CurrentPlatform = PlatformName.MacOSX;
384-
nostdlib = true;
369+
config.OmitStandardLibrary = true;
385370
if (string.IsNullOrEmpty (baselibdll)) {
386371
if (target_framework == TargetFramework.Xamarin_Mac_2_0_Mobile)
387372
baselibdll = CurrentPlatform.GetPath ("lib", "reference", "mobile", "Xamarin.Mac.dll");
@@ -414,22 +399,22 @@ int Main3 (string [] args)
414399
throw ErrorHelper.CreateError (1053, target_framework);
415400
}
416401

417-
if (sources.Count > 0) {
418-
api_sources.Insert (0, sources [0]);
419-
for (int i = 1; i < sources.Count; i++)
420-
core_sources.Insert (i - 1, sources [i]);
402+
if (config.Sources.Count > 0) {
403+
config.ApiSources.Insert (0, config.Sources [0]);
404+
for (int i = 1; i < config.Sources.Count; i++)
405+
config.CoreSources.Insert (i - 1, config.Sources [i]);
421406
}
422407

423-
if (api_sources.Count == 0) {
408+
if (config.ApiSources.Count == 0) {
424409
Console.WriteLine ("Error: no api file provided");
425410
ShowHelp (os);
426411
return 1;
427412
}
428413

429-
if (tmpdir is null)
430-
tmpdir = GetWorkDir ();
414+
if (config.TemporaryFileDirectory is null)
415+
config.TemporaryFileDirectory = GetWorkDir ();
431416

432-
string firstApiDefinitionName = Path.GetFileNameWithoutExtension (api_sources [0]);
417+
string firstApiDefinitionName = Path.GetFileNameWithoutExtension (config.ApiSources [0]);
433418
firstApiDefinitionName = firstApiDefinitionName.Replace ('-', '_'); // This is not exhaustive, but common.
434419
if (outfile is null)
435420
outfile = firstApiDefinitionName + ".dll";
@@ -438,7 +423,7 @@ int Main3 (string [] args)
438423
var paths = libs.Select ((v) => "-lib:" + v);
439424

440425
try {
441-
var tmpass = GetCompiledApiBindingsAssembly (tmpdir, refs, nostdlib, api_sources, core_sources, defines, paths);
426+
var tmpass = GetCompiledApiBindingsAssembly (config.TemporaryFileDirectory, refs, config.OmitStandardLibrary, config.ApiSources, config.CoreSources, config.Defines, paths);
442427
universe = new MetadataLoadContext (
443428
new SearchPathsAssemblyResolver (
444429
GetLibraryDirectories ().ToArray (),
@@ -466,7 +451,7 @@ int Main3 (string [] args)
466451
#endif
467452
continue;
468453

469-
if (!linkwith.Contains (linkWith.LibraryName)) {
454+
if (!config.LinkWith.Contains (linkWith.LibraryName)) {
470455
Console.Error.WriteLine ("Missing native library {0}, please use `--link-with' to specify the path to this library.", linkWith.LibraryName);
471456
return 1;
472457
}
@@ -497,7 +482,7 @@ int Main3 (string [] args)
497482
var types = new List<Type> ();
498483
var strong_dictionaries = new List<Type> ();
499484
foreach (var t in api.GetTypes ()) {
500-
if ((process_enums && t.IsEnum) ||
485+
if ((config.ProcessEnums && t.IsEnum) ||
501486
AttributeManager.HasAttribute<BaseTypeAttribute> (t) ||
502487
AttributeManager.HasAttribute<ProtocolAttribute> (t) ||
503488
AttributeManager.HasAttribute<StaticAttribute> (t) ||
@@ -509,55 +494,55 @@ int Main3 (string [] args)
509494

510495
namespaceCache ??= new NamespaceCache (
511496
CurrentPlatform,
512-
ns ?? firstApiDefinitionName,
497+
config.HelperClassNamespace ?? firstApiDefinitionName,
513498
skipSystemDrawing
514499
);
515500

516-
var g = new Generator (this, public_mode, external, debug, types.ToArray (), strong_dictionaries.ToArray ()) {
517-
BaseDir = basedir ?? tmpdir,
518-
ZeroCopyStrings = zero_copy,
519-
InlineSelectors = inline_selectors ?? (CurrentPlatform != PlatformName.MacOSX),
501+
var g = new Generator (this, config.IsPublicMode, config.IsExternal, config.IsDebug, types.ToArray (), strong_dictionaries.ToArray ()) {
502+
BaseDir = config.BindingFilesOutputDirectory ?? config.TemporaryFileDirectory,
503+
ZeroCopyStrings = config.UseZeroCopy,
504+
InlineSelectors = config.InlineSelectors ?? (CurrentPlatform != PlatformName.MacOSX),
520505
};
521506

522507
g.Go ();
523508

524-
if (generate_file_list is not null) {
525-
using (var f = File.CreateText (generate_file_list)) {
509+
if (config.GeneratedFileList is not null) {
510+
using (var f = File.CreateText (config.GeneratedFileList)) {
526511
foreach (var x in g.GeneratedFiles.OrderBy ((v) => v))
527512
f.WriteLine (x);
528513
}
529514
return 0;
530515
}
531516

532517
var cargs = new List<string> ();
533-
if (unsafef)
518+
if (config.IsUnsafe)
534519
cargs.Add ("-unsafe");
535520
cargs.Add ("-target:library");
536521
cargs.Add ("-out:" + outfile);
537-
foreach (var def in defines)
522+
foreach (var def in config.Defines)
538523
cargs.Add ("-define:" + def);
539524
#if NET
540525
cargs.Add ("-define:NET");
541526
#endif
542527
cargs.AddRange (g.GeneratedFiles);
543-
cargs.AddRange (core_sources);
544-
cargs.AddRange (extra_sources);
528+
cargs.AddRange (config.CoreSources);
529+
cargs.AddRange (config.ExtraSources);
545530
cargs.AddRange (refs);
546531
cargs.Add ("-r:" + baselibdll);
547-
cargs.AddRange (resources);
548-
if (nostdlib) {
532+
cargs.AddRange (config.Resources);
533+
if (config.OmitStandardLibrary) {
549534
cargs.Add ("-nostdlib");
550535
cargs.Add ("-noconfig");
551536
}
552537
if (!string.IsNullOrEmpty (Path.GetDirectoryName (baselibdll)))
553538
cargs.Add ("-lib:" + Path.GetDirectoryName (baselibdll));
554539

555-
AddNFloatUsing (cargs, tmpdir);
540+
AddNFloatUsing (cargs, config.TemporaryFileDirectory);
556541

557-
Compile (cargs, 1000, tmpdir);
542+
Compile (cargs, 1000, config.TemporaryFileDirectory);
558543
} finally {
559-
if (delete_temp)
560-
Directory.Delete (tmpdir, true);
544+
if (config.DeleteTemporaryFiles)
545+
Directory.Delete (config.TemporaryFileDirectory, true);
561546
}
562547
return 0;
563548
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System.Collections.Generic;
2+
#nullable enable
3+
4+
public class BindingTouchConfig {
5+
public bool ShowHelp = false;
6+
public bool UseZeroCopy = false;
7+
public string? BindingFilesOutputDirectory = null;
8+
public string? TemporaryFileDirectory = null;
9+
public string? HelperClassNamespace = null;
10+
public bool DeleteTemporaryFiles = true;
11+
public bool IsDebug = false;
12+
public bool IsUnsafe = true;
13+
public bool IsExternal = false;
14+
public bool IsPublicMode = true;
15+
public bool OmitStandardLibrary = false;
16+
public bool? InlineSelectors = null;
17+
public List<string> Sources = new ();
18+
public List<string> Resources = new ();
19+
public List<string> LinkWith = new ();
20+
public List<string> ApiSources = new ();
21+
public List<string> CoreSources = new ();
22+
public List<string> ExtraSources = new ();
23+
public List<string> Defines = new ();
24+
public string? GeneratedFileList = null;
25+
public bool ProcessEnums = false;
26+
}

src/generator.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@
157157
<Compile Include="bgen\Extensions\StringExtensions.cs" />
158158
<Compile Include="bgen\Extensions\TypeExtensions.cs" />
159159
<Compile Include="bgen\Models\AsyncMethodInfo.cs" />
160+
<Compile Include="bgen\Models\BindingTouchConfig.cs" />
160161
<Compile Include="bgen\Models\MarshalInfo.cs" />
161162
<Compile Include="bgen\Models\MarshalType.cs" />
162163
<Compile Include="bgen\Models\MarshalTypeList.cs" />

0 commit comments

Comments
 (0)