forked from IronLanguages/ironpython3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfig.cs
More file actions
357 lines (312 loc) · 13.4 KB
/
Config.cs
File metadata and controls
357 lines (312 loc) · 13.4 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using IKVM.Reflection;
using IKVM.Reflection.Emit;
using System.Resources;
using System.Reflection;
using Microsoft.Scripting.Runtime;
namespace IronPythonCompiler {
public class Config {
public Config() {
Embed = false;
Files = new List<string>();
Platform = IKVM.Reflection.PortableExecutableKinds.ILOnly;
Machine = IKVM.Reflection.ImageFileMachine.AMD64;
Standalone = false;
Target = PEFileKinds.Dll;
UseMta = false;
UseSta = false;
MainName = Main = string.Empty;
Output = string.Empty;
OutputPath = string.Empty;
Win32Icon = string.Empty;
FileVersion = string.Empty;
ProductName = string.Empty;
Copyright = string.Empty;
ProductVersion = string.Empty;
ErrorMessageFormat = "Error occurred: {0}";
PythonOptions = new Dictionary<string, object>();
DLLs = new List<string>();
}
public string ErrorMessageFormat {
get;
private set;
}
public string FileVersion {
get;
private set;
}
public string Win32Icon {
get;
private set;
}
public string ProductName {
get;
private set;
}
public string Copyright {
get;
private set;
}
public string ProductVersion {
get;
private set;
}
public string Output {
get;
private set;
}
public string OutputPath {
get;
private set;
}
public string Main {
get;
private set;
}
public string MainName {
get;
private set;
}
public PEFileKinds Target {
get;
private set;
}
public bool UseMta {
get;
private set;
}
public bool UseSta {
get;
private set;
}
public bool Standalone {
get;
private set;
}
public List<string> Files {
get;
private set;
}
public List<string> DLLs {
get;
private set;
}
public IDictionary<string, object> PythonOptions {
get;
private set;
}
public bool Embed {
get;
internal set;
}
public IKVM.Reflection.ImageFileMachine Machine {
get;
private set;
}
public IKVM.Reflection.PortableExecutableKinds Platform {
get;
private set;
}
public void ParseArgs(IEnumerable<string> args, List<string> respFiles = null) {
var helpStrings = new string[] { "/?", "-?", "/h", "-h" };
foreach (var a in args) {
var arg = a.Trim();
if (arg.StartsWith("#", StringComparison.Ordinal)) {
continue;
}
if (arg.StartsWith("/main:", StringComparison.Ordinal)) {
MainName = Main = arg.Substring(6).Trim('"');
// only override the target kind if its currently a DLL
if (Target == PEFileKinds.Dll) {
Target = PEFileKinds.ConsoleApplication;
}
} else if (arg.StartsWith("/out:", StringComparison.Ordinal)) {
Output = arg.Substring(5).Trim('"');
} else if (arg.StartsWith("/target:", StringComparison.Ordinal)) {
string tgt = arg.Substring(8).Trim('"');
switch (tgt) {
case "exe":
Target = PEFileKinds.ConsoleApplication;
break;
case "winexe":
Target = PEFileKinds.WindowApplication;
break;
default:
Target = PEFileKinds.Dll;
break;
}
} else if (arg.StartsWith("/platform:", StringComparison.Ordinal)) {
string plat = arg.Substring(10).Trim('"');
switch (plat) {
case "x86":
Platform = IKVM.Reflection.PortableExecutableKinds.ILOnly | IKVM.Reflection.PortableExecutableKinds.Required32Bit;
Machine = IKVM.Reflection.ImageFileMachine.I386;
break;
case "x64":
Platform = IKVM.Reflection.PortableExecutableKinds.ILOnly | IKVM.Reflection.PortableExecutableKinds.PE32Plus;
Machine = IKVM.Reflection.ImageFileMachine.AMD64;
break;
default:
Platform = IKVM.Reflection.PortableExecutableKinds.ILOnly;
Machine = IKVM.Reflection.ImageFileMachine.AMD64;
break;
}
} else if (arg.StartsWith("/win32icon:", StringComparison.Ordinal)) {
Win32Icon = arg.Substring(11).Trim('"');
} else if (arg.StartsWith("/fileversion:", StringComparison.Ordinal)) {
FileVersion = arg.Substring(13).Trim('"');
} else if (arg.StartsWith("/productversion:", StringComparison.Ordinal)) {
ProductVersion = arg.Substring(16).Trim('"');
} else if (arg.StartsWith("/productname:", StringComparison.Ordinal)) {
ProductName = arg.Substring(13).Trim('"');
} else if (arg.StartsWith("/copyright:", StringComparison.Ordinal)) {
Copyright = arg.Substring(11).Trim('"');
} else if (arg.StartsWith("/errfmt:", StringComparison.Ordinal)) {
ErrorMessageFormat = arg.Substring(8);
} else if (arg.Equals("/embed", StringComparison.Ordinal)) {
Embed = true;
} else if (arg.Equals("/standalone", StringComparison.Ordinal)) {
Standalone = true;
} else if (arg.Equals("/mta", StringComparison.Ordinal)) {
UseMta = true;
} else if (arg.Equals("/sta", StringComparison.Ordinal)) {
UseSta = true;
} else if (arg.StartsWith("/recurse:", StringComparison.Ordinal)) {
string pattern = arg.Substring(9);
if (string.IsNullOrWhiteSpace(pattern)) {
ConsoleOps.Error(true, "Missing pattern for /recurse option");
}
foreach (var f in Directory.EnumerateFiles(Environment.CurrentDirectory, pattern)) {
Files.Add(Path.GetFullPath(f));
}
} else if (Array.IndexOf(helpStrings, arg) >= 0) {
ConsoleOps.Usage(true);
} else if (arg.StartsWith("/py:", StringComparison.Ordinal)) {
// if you add a parameter that takes a different type then
// ScriptingRuntimeHelpers.True/False or int
// you need ot also modify Program.cs for standalone generation.
string[] pyargs = arg.Substring(4).Trim('"').Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
switch (pyargs[0]) {
case "-X:Frames":
PythonOptions["Frames"] = ScriptingRuntimeHelpers.True;
break;
case "-X:FullFrames":
PythonOptions["Frames"] = PythonOptions["FullFrames"] = ScriptingRuntimeHelpers.True;
break;
case "-X:Tracing":
PythonOptions["Tracing"] = ScriptingRuntimeHelpers.True;
break;
case "-X:GCStress":
int gcStress;
if (!int.TryParse(pyargs[1], out gcStress) || (gcStress < 0 || gcStress > GC.MaxGeneration)) {
ConsoleOps.Error(true, $"The argument for the {pyargs[1]} option must be between 0 and {GC.MaxGeneration}.");
}
PythonOptions["GCStress"] = gcStress;
break;
case "-X:MaxRecursion":
// we need about 6 frames for starting up, so 10 is a nice round number.
int limit;
if (!int.TryParse(pyargs[1], out limit) || limit < 10) {
ConsoleOps.Error(true, $"The argument for the {pyargs[1]} option must be an integer >= 10.");
}
PythonOptions["RecursionLimit"] = limit;
break;
case "-X:EnableProfiler":
PythonOptions["EnableProfiler"] = ScriptingRuntimeHelpers.True;
break;
case "-X:LightweightScopes":
PythonOptions["LightweightScopes"] = ScriptingRuntimeHelpers.True;
break;
case "-X:Debug":
PythonOptions["Debug"] = ScriptingRuntimeHelpers.True;
break;
}
} else {
if (arg.StartsWith("@", StringComparison.Ordinal)) {
var respFile = Path.GetFullPath(arg.Substring(1));
if (respFiles == null) {
respFiles = new List<string>();
}
if (!respFiles.Contains(respFile)) {
respFiles.Add(respFile);
ParseArgs(File.ReadAllLines(respFile), respFiles);
} else {
ConsoleOps.Warning($"Already parsed response file '{arg.Substring(1)}'");
}
} else {
if (arg.EndsWith(".dll", StringComparison.OrdinalIgnoreCase)) {
DLLs.Add(arg);
} else {
Files.Add(arg);
}
}
}
}
}
public bool Validate() {
if (Files.Count == 1 && string.IsNullOrWhiteSpace(MainName)) {
MainName = Files[0];
}
if (Files.Count == 0 && !string.IsNullOrWhiteSpace(MainName)) {
Files.Add(MainName);
}
if (Files == null || Files.Count == 0 || string.IsNullOrEmpty(MainName)) {
ConsoleOps.Error("No files or main defined");
return false;
}
if (Target != PEFileKinds.Dll && string.IsNullOrEmpty(MainName)) {
ConsoleOps.Error("EXEs require /main:<filename> to be specified");
return false;
}
if (DLLs.Count > 0 && !Standalone) {
ConsoleOps.Error("DLLs can only be used in standalone mode");
return false;
}
if (string.IsNullOrWhiteSpace(Output) && !string.IsNullOrWhiteSpace(MainName)) {
Output = Path.GetFileNameWithoutExtension(MainName);
OutputPath = Path.GetDirectoryName(MainName);
} else if (string.IsNullOrWhiteSpace(Output) && Files != null && Files.Count > 0) {
Output = Path.GetFileNameWithoutExtension(Files[0]);
OutputPath = Path.GetDirectoryName(Files[0]);
}
if (!string.IsNullOrWhiteSpace(Win32Icon) && Target == PEFileKinds.Dll) {
ConsoleOps.Error("DLLs may not have a win32icon");
return false;
} else if (!string.IsNullOrWhiteSpace(Win32Icon) && !File.Exists(Win32Icon)) {
ConsoleOps.Error($"win32icon '{Win32Icon}' does not exist");
return false;
}
return true;
}
public override string ToString() {
StringBuilder res = new StringBuilder("Input Files:\n");
foreach (var file in Files) {
res.AppendLine($"\t{file}");
}
res.AppendLine($"Output:\n\t{Output}");
res.AppendLine($"OutputPath:\n\t{OutputPath}");
res.AppendLine($"Target:\n\t{Target}");
res.AppendLine($"Platform:\n\t{Machine}");
if (Target == PEFileKinds.WindowApplication) {
res.AppendLine("Threading:");
if (UseMta) {
res.AppendLine("\tMTA");
} else {
res.AppendLine("\tSTA");
}
}
if (PythonOptions.Count > 0) {
res.AppendLine("\nIronPython Context Options:");
foreach (var option in PythonOptions) {
res.AppendLine($"\t{option.Key} = {option.Value}");
}
}
return res.ToString();
}
}
}