-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdflat.cs
More file actions
482 lines (446 loc) · 16.2 KB
/
Copy pathdflat.cs
File metadata and controls
482 lines (446 loc) · 16.2 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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
using System;
using System.Linq;
using System.Collections.Generic;
using System.IO;
using System.Diagnostics;
using System.CommandLine;
using System.CommandLine.Help;
using System.CommandLine.Invocation;
#nullable enable
class Dflat
{
public static string version = "0.1.0";
public static string home = new FileInfo(Environment.ProcessPath).Directory.FullName; // where dflat lives
public static string cwd = Directory.GetCurrentDirectory(); // from where dflat is invoked
public static string csc = Path.Join(home, @"csc\csc.exe");
public static string ilc = Path.Join(home, @"ilc\ilc.exe");
public static string linker = Path.Join(home, @"linker\link.exe");
public static string aotsdk = Path.Join(home, @"libs\aotsdk");
public static string refs = Path.Join(home, @"libs\refs");
public static string runtime = Path.Join(home, @"libs\runtime");
public static string kits = Path.Join(home, @"libs\kits");
public static string msvc = Path.Join(home, @"libs\msvc");
static List<string> externalLibs = new();
static List<string> cscExtraArgs = new(), ilcExtraArgs = new(), linkerExraArgs = new();
static string cscExtraArgString = "", ilcExtraArgString = "", lldExtraArgString = "";
static string NORMAL = "\x1b[39m";
static Dictionary<string, string> COLORS = new()
{
{ "RED", "\x1b[91m" },
{ "GREEN", "\x1b[92m" },
};
static void Main(string[] args)
{
// check compilers
if (!File.Exists(csc)) { Print($"{csc} not found", "RED"); return; }
if (!File.Exists(ilc)) { Print($"{ilc} not found"); return; }
if (!File.Exists(linker)) { Print($"{linker} not found"); return; }
// check refs + runtime assemblies + aotsdk
if (!Directory.Exists(aotsdk)) { Print($"{aotsdk} not found"); return; }
if (!Directory.Exists(refs)) { Print($"{refs} not found"); return; }
if (!Directory.Exists(runtime)) { Print($"{runtime} not found"); return; }
if (!Directory.Exists(kits)) { Print($"{kits} not found"); return; }
if (!Directory.Exists(msvc)) { Print($"{msvc} not found"); return; }
Argument<List<FileInfo>> sourceFilesArg = new("SOURCE FILES") { Description = ".cs files to compile", };
Option<bool> justILFlag = new("/il") { Description = "Compile to IL", };
Option<string[]> externalLibsOption = new("/r") { Description = "Additional reference .dlls or folders containing them", };
Option<bool> verbosity = new("/verbose") { Description = "Set verbosity", };
Option<string> outputArg = new("/out") { Description = "Output file name", };
Option<string> entryPoint = new("/main") { Description = "Specify the class containing Main()", };
Option<string> langversion = new("/langversion") { Description = "Specify lang version, /langversion:? to list all available versions", };
Option<CSCTargets> targetsOption = new("/target") { Description = "Specify the target", };
Option<CSCPlatforms> platformOption = new("/platform") { Description = "Specify the platform", };
Option<bool> optimizeFlag = new("/optimize") { Description = "optimize", };
Option<string> cscArgStringOption = new("/csc") { Description = "extra csc flags [as a single string]", };
Option<string> ilcArgStringOption = new("/ilc") { Description = "extra ilc flags [as a single string]", };
Option<string> lldArgStringOption = new("/lld") { Description = "extra lld flags [as a single string]", };
RootCommand cmd = new($"dflat, a native aot compiler for c#\nAjaykrishnan R, 2025\nversion: {version}") {
sourceFilesArg,
outputArg,
entryPoint,
externalLibsOption,
justILFlag,
verbosity,
langversion,
targetsOption,
platformOption,
optimizeFlag,
cscArgStringOption,
ilcArgStringOption,
lldArgStringOption
};
// override defaults
HelpAction defaultHelpAction = null;
for (int i = 0; i < cmd.Options.Count; i++)
{
if (cmd.Options[i].GetType() == typeof(VersionOption))
{
VersionOption vo = new("/version", []);
vo.Action = new CustomVersionAction();
cmd.Options[i] = vo;
}
if (cmd.Options[i].GetType() == typeof(HelpOption))
{
defaultHelpAction = (HelpAction)cmd.Options[i].Action;
HelpOption ho = new("/h", ["/?", "/help"]);
ho.Action = defaultHelpAction;
cmd.Options[i] = ho;
}
}
cmd.SetAction(result =>
{
// handle options that are also flags first
if (result.GetValue(langversion) != null)
{
string ver = result.GetValue(langversion);
if (ver == "latest" || ver == "latestmajor" || ver == "default" || ver == "preview")
cscExtraArgs.Add($"/langversion:{ver}");
else if (ver == "?" || ver == "h")
{
CallCompiler(csc, "/langversion:?");
return;
}
else
{
Console.WriteLine("Language version not recognized\nlatest\nlatestmajor\ndefault\npreview");
return;
}
}
List<FileInfo> sourceFiles = result.GetValue(sourceFilesArg);
if (sourceFiles.Count == 0)
{
Print($"No source files supplied", "RED");
defaultHelpAction.Invoke(result);
return;
}
foreach (FileInfo sourceFile in sourceFiles)
{
if (!sourceFile.Exists)
{
Console.Error.WriteLine($"file {sourceFile.Name} does not exist");
return;
}
if (!sourceFile.Name.EndsWith(".cs"))
{
Console.Error.WriteLine($"please input a .cs file");
return;
}
}
if (result.GetValue(verbosity)) { verbose = true; }
foreach (string path in result.GetValue(externalLibsOption))
{
if (File.GetAttributes(path).HasFlag(FileAttributes.Directory))
{
foreach (string dll in Directory.GetFiles(path).Where(file => file.EndsWith(".dll")))
{
externalLibs.Add(new FileInfo(dll).FullName);
}
continue;
}
else if (!File.Exists(path))
{
Console.Error.WriteLine($"{path} does not exist");
return;
}
externalLibs.Add(new FileInfo(path).FullName);
}
outputType = result.GetValue(targetsOption);
if (result.GetValue(platformOption) != null) cscExtraArgs.Add($"/platform:{result.GetValue(platformOption).ToString()}");
if (result.GetValue(optimizeFlag)) { cscExtraArgs.Add("/O"); ilcExtraArgs.Add("--optimize"); }
if (result.GetValue(entryPoint) != null) { cscExtraArgs.Add($"/main:{result.GetValue(entryPoint)}"); }
if (result.GetValue(justILFlag)) { justIL = true; }
if (result.GetValue(cscArgStringOption) != null) { cscExtraArgString = result.GetValue(cscArgStringOption); }
if (result.GetValue(ilcArgStringOption) != null) { ilcExtraArgString = result.GetValue(ilcArgStringOption); }
if (result.GetValue(lldArgStringOption) != null) { lldExtraArgString = result.GetValue(lldArgStringOption); }
Compile(sourceFiles, result.GetValue(outputArg), cscExtraArgs, ilcExtraArgs);
});
cmd.Parse(args).Invoke();
}
static string tmpDir = Path.Join(cwd, ".dflat.tmp");
static string outName; // name emitted output entity
static string ilOut;
static string objOut;
static string outDir;
static string outPath; // output path
// export definitions created by ILC for linker
static string def;
static Stopwatch sw = new();
static bool justIL = false;
static CSCTargets outputType = CSCTargets.EXE;
static void Compile(List<FileInfo> sourceFiles, string? exeOut, List<string> cscExtraArgs, List<string> ilcExtraArgs)
{
// set paths
outName = sourceFiles.First().Name.Replace(".cs", "");
outDir = cwd;
if (exeOut != null)
{
// remove trailing "\"s if any
while (exeOut.EndsWith(@"\"))
{
exeOut = exeOut.Remove(exeOut.Length - 1);
}
if (exeOut.Contains("/"))
{
Print("Only windows style paths supported", "RED");
return;
}
if (exeOut.Contains(@"\") || Directory.Exists(Path.Join(cwd, exeOut)))
{ // is path
string[] parts = exeOut.Split(@"\");
string parent = exeOut.Replace(parts.Last(), ""); // exeOut with the last part removed
if (Directory.Exists(exeOut))
{
outDir = exeOut;
}
else if (Directory.Exists(parent))
{
outName = parts.Last().Replace(".exe", "");
outDir = parent;
}
}
else
{
outName = exeOut.Replace(".exe", "");
}
}
if (!justIL)
{
Directory.CreateDirectory(tmpDir);
ilOut = Path.Join(tmpDir, $"{outName}.il.out");
objOut = Path.Join(tmpDir, $"{outName}.obj");
outPath = outputType switch
{
CSCTargets.EXE => Path.Join(outDir, $"{outName}.exe"),
CSCTargets.WINEXE => Path.Join(outDir, $"{outName}.exe"),
CSCTargets.LIBRARY => Path.Join(outDir, $"{outName}.dll"),
};
}
else
{
ilOut = Path.Join(outDir, $"{outName}.il.out");
}
if (outputType == CSCTargets.LIBRARY)
{
def = Path.Join(tmpDir, $"{outName}.def");
cscExtraArgs.Add($"/target:library");
ilcExtraArgs.AddRange(["--nativelib", "--export-unmanaged-entrypoints", $"--exportsfile:{def}"]);
linkerExraArgs.AddRange(["/dll", $"/def:{def}", "/noimplib"]);
}
else if (outputType == CSCTargets.WINEXE) { cscExtraArgs.Add($"/target:winexe"); }
sw.Start();
if (!HandleError(CscCompile(sourceFiles, cscExtraArgs))) return;
if (justIL) { Finish(); return; }
if (!HandleError(ILCompile(ilcExtraArgs))) return;
if (!HandleError(Link(linkerExraArgs))) return;
Finish();
}
static void Print(string message, string? color = null, int[]? rgb = null)
{
string code;
if (color != null)
{
COLORS.TryGetValue(color.ToUpper(), out string colorCode);
code = colorCode == null ? NORMAL : colorCode;
Console.Error.WriteLine($"{code}{message}{NORMAL}");
return;
}
if (rgb.Length != 3) return;
string rgbCode = $"\x1b[38;2;{rgb[0]};{rgb[1]};{rgb[2]}m";
Console.Error.WriteLine($"{rgbCode}{message}{NORMAL}");
}
static bool HandleError(bool result)
{
if (!result)
{
sw.Stop();
if (Directory.Exists(tmpDir)) Directory.Delete(tmpDir, recursive: true);
Print($"Compilation failed", "RED");
}
return result;
}
static void Finish()
{
sw.Stop();
Print($"Compilation finished in {(double)sw.ElapsedMilliseconds / 1000}s, output written to {outPath}", "GREEN");
if (Directory.Exists(tmpDir)) Directory.Delete(tmpDir, recursive: true);
}
static bool verbose = false;
static void Log(string text)
{
if (verbose) Print(text, rgb: [100, 100, 100]);
}
public static void CallCompiler(string compiler, string argString)
{
ProcessStartInfo psi = new()
{
FileName = compiler,
Arguments = argString,
};
Process process = new()
{
StartInfo = psi,
};
process.Start();
process.WaitForExit();
}
static bool CscCompile(List<FileInfo> sourceFiles, List<string> args)
{
Log("CSCCompile...");
string argString = $"/noconfig /out:{ilOut} /nologo /nostdlib /nosdkpath /unsafe";
foreach (FileInfo sourceFile in sourceFiles)
{
argString += $" \"{sourceFile.FullName}\"";
}
foreach (string dll in Directory.GetFiles(refs).Where(file => file.EndsWith(".dll")))
{
argString += $" /r:\"{new FileInfo(dll).FullName}\"";
}
foreach (string dll in externalLibs)
{
argString += $" /r:\"{dll}\"";
}
foreach (string arg in args)
{
argString += $" {arg}";
}
argString += $" {cscExtraArgString}";
Log(argString);
CallCompiler(csc, argString);
var exists = File.Exists(ilOut);
return exists;
}
static bool ILCompile(List<string> args)
{
Log("ILCompile...");
string argString = $"{ilOut} --out:{objOut}";
argString += $" -r:\"{Path.Join(aotsdk, "*.dll")}\"";
argString += $" -r:\"{Path.Join(runtime, "*.dll")}\"";
argString += $" --generateunmanagedentrypoints:System.Private.CoreLib,HIDDEN";
argString += $" --initassembly:System.Private.CoreLib";
argString += $" --initassembly:System.Private.StackTraceMetadata";
argString += $" --initassembly:System.Private.TypeLoader";
argString += $" --initassembly:System.Private.Reflection.Execution";
argString += $" --directpinvokelist:\"{Path.Join(home, @"libs\WindowsAPIs.txt")}\"";
argString += $" --directpinvoke:System.Globalization.Native";
argString += $" --directpinvoke:System.IO.Compression.Native";
argString += $" -O"; // optimize
argString += $" -g";
argString += $" --dehydrate";
argString += $" --stacktracedata frames";
argString += $" --scanreflection";
// furthur optimizations
argString += $" --feature:System.StartupHookProvider.IsSupported=false";
argString += $" --feature:System.Diagnostics.Tracing.EventSource.IsSupported=false";
argString += $" --feature:System.Resources.ResourceManager.AllowCustomResourceTypes=false";
argString += $" --feature:System.Linq.Expressions.CanEmitObjectArrayDelegate=false";
argString += $" --feature:System.Globalization.Invariant=true";
argString += $" --feature:System.Diagnostics.Debugger.IsSupported=false";
foreach (string dll in externalLibs)
{
argString += $" -r:\"{dll}\"";
}
foreach (string arg in args)
{
argString += $" {arg}";
}
argString += $" {ilcExtraArgString}";
Log(argString);
CallCompiler(ilc, argString);
return File.Exists(objOut);
}
static bool Link(List<string> args)
{
Log("Linking...");
string argString = $"{objOut} /out:{outPath} /nodefaultlib /nologo";
argString += outputType switch
{
CSCTargets.EXE => $" \"{Path.Join(aotsdk, "bootstrapper.obj")}\"",
CSCTargets.WINEXE => $" \"{Path.Join(aotsdk, "bootstrapper.obj")}\"",
CSCTargets.LIBRARY => $" \"{Path.Join(aotsdk, "bootstrapperdll.obj")}\"",
};
argString += outputType switch
{
CSCTargets.EXE => $" /subsystem:console",
CSCTargets.WINEXE => $" /subsystem:windows /entry:wmainCRTStartup",
_ => ""
};
argString += $" \"{Path.Join(aotsdk, "dllmain.obj")}\"";
argString += $" \"{Path.Join(aotsdk, "Runtime.ServerGC.lib")}\"";
argString += $" \"{Path.Join(aotsdk, "standalonegc-disabled.lib")}\"";
argString += $" \"{Path.Join(aotsdk, "aotminipal.lib")}\"";
argString += $" \"{Path.Join(aotsdk, "brotlicommon.lib")}\"";
argString += $" \"{Path.Join(aotsdk, "eventpipe-enabled.lib")}\"";
argString += $" \"{Path.Join(aotsdk, "Runtime.WorkstationGC.lib")}\"";
argString += $" \"{Path.Join(aotsdk, "brotlidec.lib")}\"";
argString += $" \"{Path.Join(aotsdk, "brotlienc.lib")}\"";
argString += $" \"{Path.Join(aotsdk, "Runtime.VxsortEnabled.lib")}\"";
argString += $" \"{Path.Join(aotsdk, "System.Globalization.Native.Aot.lib")}\"";
argString += $" \"{Path.Join(aotsdk, "System.IO.Compression.Native.Aot.lib")}\"";
argString += $" \"{Path.Join(aotsdk, "zlibstatic.lib")}\"";
argString += $" \"{Path.Join(kits, "advapi32.lib")}\"";
argString += $" \"{Path.Join(kits, "bcrypt.lib")}\"";
argString += $" \"{Path.Join(kits, "crypt32.lib")}\"";
argString += $" \"{Path.Join(kits, "iphlpapi.lib")}\"";
argString += $" \"{Path.Join(kits, "kernel32.lib")}\"";
argString += $" \"{Path.Join(kits, "mswsock.lib")}\"";
argString += $" \"{Path.Join(kits, "ncrypt.lib")}\"";
argString += $" \"{Path.Join(kits, "ntdll.lib")}\"";
argString += $" \"{Path.Join(kits, "ole32.lib")}\"";
argString += $" \"{Path.Join(kits, "oleaut32.lib")}\"";
argString += $" \"{Path.Join(kits, "secur32.lib")}\"";
argString += $" \"{Path.Join(kits, "user32.lib")}\"";
argString += $" \"{Path.Join(kits, "uuid.lib")}\"";
argString += $" \"{Path.Join(kits, "version.lib")}\"";
argString += $" \"{Path.Join(kits, "ws2_32.lib")}\"";
argString += $" \"{Path.Join(kits, "synchronization.lib")}\"";
///<summary>
///https://learn.microsoft.com/en-us/cpp/c-runtime-library/crt-library-features?view=msvc-170
///</summary>
// actual crt
// use ucrt instead of statically linking libucrt since ucrt is now part of windows
argString += $" \"{Path.Join(kits, "ucrt.lib")}\"";
// crt initializer (crt startup)
argString += $" \"{Path.Join(msvc, "libcmt.lib")}\"";
// C++ multithreaded runtime
argString += $" \"{Path.Join(msvc, "msvcprt.lib")}\"";
argString += $" \"{Path.Join(msvc, "vcruntime.lib")}\"";
argString += $" \"{Path.Join(msvc, "oldnames.lib")}\"";
foreach (string arg in args)
{
argString += $" {arg}";
}
argString += $" {lldExtraArgString}";
Log(argString);
CallCompiler(linker, argString);
return File.Exists(outPath);
}
}
class CustomVersionAction : SynchronousCommandLineAction
{
public override int Invoke(ParseResult ps)
{
Console.WriteLine($"v{Dflat.version}");
Console.Write($"CSC: ");
Dflat.CallCompiler(Dflat.csc, "/version");
Console.Write($"ILC: ");
Dflat.CallCompiler(Dflat.ilc, "--version");
Console.WriteLine($"Runtime: {FileVersionInfo.GetVersionInfo(Path.Join(Dflat.runtime, "System.dll")).ProductVersion}");
return 0;
}
}
enum CSCTargets
{
EXE,
WINEXE,
LIBRARY,
}
enum CSCPlatforms
{
x86,
Itamium,
x64,
arm,
arm64,
anycpu32bitpreferred,
anycpu
}