IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
ImportCommand.cs
Go to the documentation of this file.
1#nullable enable
2
3using System;
4using System.Collections.Generic;
5using System.CommandLine;
6using System.CommandLine.Parsing;
7using System.IO;
8
10
11namespace IKVM.Tools.Importer
12{
13
14 class ImportCommand : RootCommand
15 {
16
17 readonly static char[] LIST_SEPARATOR = [';', ','];
18
23 public ImportCommand(bool root = true)
24 {
25 Add(InputsArgument = new Argument<FileInfo[]>(
26 name: "classOrJar",
27 description: "Path or name of class or JAR file to import.")
28 .LegalFilePathsOnly());
29
30 Add(OutputOption = new Option<FileInfo?>(
31 aliases: ["-out", "-o"],
32 description: "Specify the output filename.")
33 .LegalFilePathsOnly());
34
35 Add(AssemblyNameOption = new Option<string?>(
36 name: "-assembly",
37 description: "Specify assembly name."));
38
39 Add(TargetOption = new Option<string?>(
40 name: "-target",
41 description: "Specifies the format of the output file.",
42 getDefaultValue: () => root ? "library" : null)
43 .FromAmong("exe", "winexe", "library", "module"));
44
45 Add(PlatformOption = new Option<string?>(
46 name: "-platform",
47 description: "Limit which platforms this code can run on: x86, x64, arm, anycpu32bitpreferred, or anycpu.\nThe default is anycpu.",
48 getDefaultValue: () => root ? "anycpu" : null)
49 .FromAmong("x86", "x64", "arm", "arm64", "anycpu32bitpreferred", "anycpu"));
50
51 Add(ApartmentOption = new Option<string?>(
52 name: "-apartment",
53 description: "Apply STAThreadAttribute to main.",
54 getDefaultValue: () => root ? "none" : null)
55 .FromAmong("sta", "mta", "none"));
56
57 Add(NoGlobbingOption = new Option<bool>(
58 name: "-noglobbing",
59 description: "Don't glob the arguments passed to main."));
60
61 Add(PropertiesOption = new Option<string[]>(
62 name: "-D",
63 description: "Set system property (at runtime)."));
64
65 Add(EnableAssertionsOption = new Option<string?>(
66 aliases: ["-enableassertions", "-ea"],
67 parseArgument: ParseOptionalString,
68 isDefault: true,
69 description: "Set system property to enable assertions.")
70 {
71 Arity = ArgumentArity.ZeroOrMore,
72 IsRequired = false
73 });
74
75 Add(DisableAssertionsOption = new Option<string?>(
76 aliases: ["-disableassertions", "-da"],
77 parseArgument: ParseOptionalString,
78 isDefault: true,
79 description: "Set system property to disable assertions.")
80 {
81 Arity = ArgumentArity.ZeroOrMore,
82 IsRequired = false
83 });
84
85 Add(RemoveAssertionsOption = new Option<bool>(
86 name: "-removeassertions",
87 description: "Remove all assert statements."));
88
89 Add(MainClassOption = new Option<string>(
90 name: "-main",
91 description: "Specify the class containing the main method."));
92
93 Add(ReferenceOption = new Option<string[]>(
94 aliases: ["-reference", "-r"],
95 description: "Reference an assembly."));
96
97 Add(RecurseOption = new Option<string[]>(
98 name: "-recurse",
99 description: "Recurse directory and include matching file."));
100
101 Add(ResourceOption = new Option<string[]>(
102 name: "-resource",
103 description: "Include file as Java resource.")
104 {
105 Arity = ArgumentArity.OneOrMore
106 });
107
108 Add(ExternalResourceOption = new Option<string[]>(
109 aliases: ["-externalresource"],
110 description: "Reference file as Java resource.")
111 {
112 Arity = ArgumentArity.OneOrMore
113 });
114
115 Add(NoJNIOption = new Option<bool>(
116 name: "-nojni",
117 description: "Do not generate JNI stub for native methods."));
118
119 Add(ExcludeOption = new Option<FileInfo?>(
120 name: "-exclude",
121 description: "A file containing a list of classes to exclude.")
122 .LegalFilePathsOnly());
123
124 Add(VersionOption = new Option<string?>(
125 name: "-version",
126 description: "Specify assembly version."));
127
128 Add(FileVersionOption = new Option<string?>(
129 name: "-fileversion",
130 description: "File version."));
131
132 Add(Win32IconOption = new Option<FileInfo?>(
133 name: "-win32icon",
134 description: "Embed specified icon in output.")
135 .LegalFilePathsOnly());
136
137 Add(Win32ManifestOption = new Option<FileInfo?>(
138 name: "-win32manifest",
139 description: "Specify a Win32 manifest file (.xml).")
140 .LegalFilePathsOnly());
141
142 Add(KeyFileOption = new Option<FileInfo?>(
143 aliases: ["-keyfile"],
144 description: "Use keyfile to sign the assembly.")
145 .LegalFilePathsOnly());
146
147 Add(KeyOption = new Option<string?>(
148 aliases: ["-key"],
149 description: "Use keycontainer to sign the assembly."));
150
151 Add(DelaySignOption = new Option<bool>(
152 aliases: ["-delaysign"],
153 description: "Delay-sign the assembly."));
154
155 Add(DebugOption = new Option<string?>(
156 aliases: ["-debug"],
157 description: "Specify debugging type ('portable' is default)\n'portable' is a cross-platform format,\n'embedded' is a cross-platform format embedded into the target dll or exe.",
158 getDefaultValue: () => root ? "portable" : null)
159 .FromAmong("none", "portable", "embedded"));
160
161 Add(DeterministicOption = new Option<bool>(
162 aliases: ["-deterministic"],
163 description: "Produce a deterministic assembly (including module version GUID and timestamp)."));
164
165 Add(OptimizeOption = new Option<bool>(
166 aliases: ["-optimize"],
167 description: ""));
168
169 Add(SourcePathOption = new Option<DirectoryInfo?>(
170 aliases: ["-srcpath"],
171 description: "Prepend path and package name to source file.")
172 .LegalFilePathsOnly());
173
174 Add(RemapOption = new Option<FileInfo?>(
175 aliases: ["-remap"],
176 description: "Specify a XML map file that rewrites entities.")
177 .LegalFilePathsOnly());
178
179 Add(NoStackTraceInfoOption = new Option<bool>(
180 aliases: ["-nostacktraceinfo"],
181 description: "Don't create metadata to emit rich stack traces."));
182
183 Add(RemoveUnusedPrivateFieldsOption = new Option<bool>(
184 aliases: ["-opt:fields"],
185 description: "Remove unused private fields."));
186
187 Add(CompressResourcesOption = new Option<bool>(
188 aliases: ["-compressresources"],
189 description: "Compress resources."));
190
191 Add(StrictFinalFieldSemanticsOption = new Option<bool>(
192 aliases: ["-strictfinalfieldsemantics"],
193 description: " Don't allow final fields to be modified outside of initializer methods."));
194
195 Add(PrivatePackageOption = new Option<string[]>(
196 aliases: ["-privatepackage"],
197 description: "Mark all classes with a package name starting with <prefix> as internal to the assembly."));
198
199 Add(PublicPackageOption = new Option<string[]>(
200 aliases: ["-publicpackage"],
201 description: "Mark all classes with a package name starting with <prefix> as public to the assembly."));
202
203 Add(NoWarnOption = new Option<Diagnostic[]>(
204 aliases: ["-nowarn"],
205 description: "Disable specific warning messages.",
206 parseArgument: ParseDiagnosticArray));
207
208 Add(WarnAsErrorOption = new Option<Diagnostic[]>(
209 aliases: ["-warnaserror"],
210 description: "Report all warnings as errors.",
211 parseArgument: ParseDiagnosticArray));
212
213 if (root)
214 Add(RuntimeOption = new Option<FileInfo?>(
215 aliases: ["-runtime"],
216 description: "Path to the IKVM.Runtime assembly.")
217 .LegalFilePathsOnly());
218
219 Add(ClassLoaderOption = new Option<string?>(
220 aliases: ["-classloader"],
221 description: "Set custom class loader class for assembly."));
222
223 if (root)
224 Add(SharedClassLoaderOption = new Option<bool>(
225 aliases: ["-sharedclassloader"],
226 description: "All targets below this level share a common class loader."));
227
228 Add(BaseAddressOption = new Option<string?>(
229 aliases: ["-baseaddress"],
230 description: "Base address for the library to be built."));
231
232 Add(FileAlignOption = new Option<string?>(
233 aliases: ["-filealign"],
234 description: "Specify the alignment used for output file."));
235
236 Add(NoPeerCrossReferenceOption = new Option<bool>(
237 aliases: ["-nopeercrossreference"],
238 description: "Do not automatically cross reference all peers."));
239
240 Add(NoStdLibOption = new Option<bool>(
241 aliases: ["-nostdlib"],
242 description: "Do not reference standard libraries"));
243
244 Add(LibraryOption = new Option<DirectoryInfo[]>(
245 aliases: ["-lib"],
246 description: "Additional directories to search for references."));
247
248 Add(NoAutoSerializationOption = new Option<bool>(
249 aliases: ["-noautoserialization"],
250 description: "Disable automatic .NET serialization support."));
251
252 Add(HighEntropyVAOption = new Option<bool>(
253 aliases: ["-highentropyva"],
254 description: "Enable high entropy ASLR."));
255
256 Add(ProxyOption = new Option<string[]>(
257 aliases: ["-proxy"],
258 description: "Generate proxies for specified class."));
259
260 Add(AllowNonVirtualCallsOption = new Option<bool>(
261 aliases: ["-XX:+AllowNonVirtualCalls"],
262 description: "Allow non-virtual calls."));
263
264 Add(StaticOption = new Option<bool>(
265 aliases: ["-static"],
266 description: "Disable dynamic binding."));
267
268 Add(NoJarStubsOption = new Option<bool>(
269 aliases: ["-nojarstubs"],
270 description: "undocumented temporary option to mitigate risk."));
271
272 Add(AssemblyAttributesOption = new Option<FileInfo[]>(
273 aliases: ["-assemblyattributes"],
274 description: "Read assembly custom attributes from specified class file.")
275 .LegalFilePathsOnly());
276
277 Add(WarningLevel4Option = new Option<bool>(
278 aliases: ["-w4"],
279 description: "Undocumented option to always warn if a class isn't found."));
280
281 Add(NoParameterReflectionOption = new Option<bool>(
282 aliases: ["-noparameterreflection"],
283 description: "Undocumented option to compile core class libraries with, to disable MethodParameter attribute."));
284
285 if (root)
286 Add(BootstrapOption = new Option<bool>(
287 aliases: ["-bootstrap"],
288 description: "Undocumented option to compile core class libraries with, to allow creation of base class libraries."));
289
290 if (root)
291 Add(LogOption = new Option<string>(
292 aliases: ["-log"],
293 getDefaultValue: () => "text",
294 description: "Logging options."));
295
296 ResourceOption.AddValidator(ValidateResource);
297 }
298
304 Diagnostic[] ParseDiagnosticArray(ArgumentResult result)
305 {
306 var l = new List<Diagnostic>();
307
308 foreach (var i in result.Tokens)
309 {
310 foreach (var j in i.Value.Split(LIST_SEPARATOR, StringSplitOptions.RemoveEmptyEntries))
311 {
312 if (int.TryParse(j, out var id) && Diagnostic.GetById(id) is { } diagnostic)
313 l.Add(diagnostic);
314
315 if (j.StartsWith("IKVM", StringComparison.OrdinalIgnoreCase) && int.TryParse(j["IKVM".Length..], out var id2) && Diagnostic.GetById(id2) is { } diagnostic2)
316 l.Add(diagnostic2);
317 }
318 }
319
320 return l.ToArray();
321 }
322
329 string? ParseOptionalString(ArgumentResult result)
330 {
331 if (result.Tokens.Count == 0)
332 {
333 // no argument specified
334 if (result.Parent is OptionResult { IsImplicit: true })
335 return null;
336
337 // argument specified but no items
338 return "";
339 }
340
341 // items
342 var l = new string[result.Tokens.Count];
343 for (int i = 0; i < result.Tokens.Count; i++)
344 l[i] = result.Tokens[i].Value;
345
346 return string.Join(";", l);
347 }
348
353 void ValidateResource(OptionResult symbolResult)
354 {
355
356 }
357
358 public Argument<FileInfo[]> InputsArgument { get; set; }
359
360 public Option<FileInfo?> OutputOption { get; set; }
361
362 public Option<string?> AssemblyNameOption { get; set; }
363
364 public Option<string?> TargetOption { get; set; }
365
366 public Option<string?> PlatformOption { get; set; }
367
368 public Option<string?> ApartmentOption { get; set; }
369
370 public Option<bool> NoGlobbingOption { get; set; }
371
372 public Option<string[]> PropertiesOption { get; set; }
373
374 public Option<string?> EnableAssertionsOption { get; set; }
375
376 public Option<string?> DisableAssertionsOption { get; set; }
377
378 public Option<bool> RemoveAssertionsOption { get; set; }
379
380 public Option<string> MainClassOption { get; set; }
381
382 public Option<string[]> ReferenceOption { get; set; }
383
384 public Option<string[]> RecurseOption { get; set; }
385
386 public Option<string[]> ResourceOption { get; set; }
387
388 public Option<string[]> ExternalResourceOption { get; set; }
389
390 public Option<bool> NoJNIOption { get; set; }
391
392 public Option<FileInfo?> ExcludeOption { get; set; }
393
394 public Option<string?> VersionOption { get; set; }
395
396 public Option<string?> FileVersionOption { get; set; }
397
398 public Option<FileInfo?> Win32IconOption { get; set; }
399
400 public Option<FileInfo?> Win32ManifestOption { get; set; }
401
402 public Option<FileInfo?> KeyFileOption { get; set; }
403
404 public Option<string?> KeyOption { get; set; }
405
406 public Option<bool> DelaySignOption { get; set; }
407
408 public Option<string?> DebugOption { get; set; }
409
410 public Option<bool> DeterministicOption { get; set; }
411
412 public Option<bool> OptimizeOption { get; set; }
413
414 public Option<DirectoryInfo?> SourcePathOption { get; set; }
415
416 public Option<FileInfo?> RemapOption { get; set; }
417
418 public Option<bool> NoStackTraceInfoOption { get; set; }
419
420 public Option<bool> RemoveUnusedPrivateFieldsOption { get; set; }
421
422 public Option<bool> CompressResourcesOption { get; set; }
423
424 public Option<bool> StrictFinalFieldSemanticsOption { get; set; }
425
426 public Option<string[]> PrivatePackageOption { get; set; }
427
428 public Option<string[]> PublicPackageOption { get; set; }
429
430 public Option<Diagnostic[]> NoWarnOption { get; set; }
431
432 public Option<Diagnostic[]> WarnAsErrorOption { get; set; }
433
434 public Option<FileInfo?>? RuntimeOption { get; set; }
435
436 public Option<string?> ClassLoaderOption { get; set; }
437
438 public Option<bool>? SharedClassLoaderOption { get; set; }
439
440 public Option<string?> BaseAddressOption { get; set; }
441
442 public Option<string?> FileAlignOption { get; set; }
443
444 public Option<bool> NoPeerCrossReferenceOption { get; set; }
445
446 public Option<bool> NoStdLibOption { get; set; }
447
448 public Option<DirectoryInfo[]> LibraryOption { get; set; }
449
450 public Option<bool> NoAutoSerializationOption { get; set; }
451
452 public Option<bool> HighEntropyVAOption { get; set; }
453
454 public Option<string[]> ProxyOption { get; set; }
455
456 public Option<bool> AllowNonVirtualCallsOption { get; set; }
457
458 public Option<bool> StaticOption { get; set; }
459
460 public Option<bool> NoJarStubsOption { get; set; }
461
462 public Option<FileInfo[]> AssemblyAttributesOption { get; set; }
463
464 public Option<bool> WarningLevel4Option { get; set; }
465
466 public Option<bool> NoParameterReflectionOption { get; set; }
467
468 public Option<bool>? BootstrapOption { get; set; }
469
470 public Option<string>? LogOption { get; set; }
471
472 }
473
474}
static ? Diagnostic GetById(int id)
Option< string[]> ExternalResourceOption
Option< DirectoryInfo[]> LibraryOption
Option< bool > RemoveUnusedPrivateFieldsOption
Option< FileInfo?> Win32ManifestOption
Option< Diagnostic[]> WarnAsErrorOption
ImportCommand(bool root=true)
Initializes a new instance.
Option< DirectoryInfo?> SourcePathOption
Argument< FileInfo[]> InputsArgument
Option< FileInfo[]> AssemblyAttributesOption
Option< Diagnostic[]> NoWarnOption
Option< bool > StrictFinalFieldSemanticsOption