-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathNativeBinaryBuilder.cs
345 lines (307 loc) · 14 KB
/
NativeBinaryBuilder.cs
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
////////////////////////////////////////////////////////////////////////////
//
// IL2C - A translator for ECMA-335 CIL/MSIL to C language.
// Copyright (c) Kouji Matsui (@kozy_kekyo, @kekyo@mastodon.cloud)
//
// Licensed under Apache-v2: https://opensource.org/licenses/Apache-2.0
//
////////////////////////////////////////////////////////////////////////////
#nullable enable
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IL2C.Internal;
using IL2C.Metadata;
namespace IL2C.Drivers
{
public sealed class ToolchainOptions
{
public readonly string NativeCompiler;
public readonly string NativeCompilerFlags;
public readonly string NativeLinkingFlags;
public readonly string NativeArchiver;
public readonly string[] AdditionalIncludeDirs;
public readonly string[] LibraryPaths;
public readonly string? MainTemplatePath;
public readonly bool EnableParallelism;
public ToolchainOptions(
string nativeCompiler,
string nativeCompilerFlags,
string nativeLinkingFlags,
string nativeArchiver,
string[] additionalIncludeDirs,
string[] libraryPaths,
string? mainTemplatePath,
bool enableParallelism)
{
this.NativeCompiler = nativeCompiler;
this.NativeCompilerFlags = nativeCompilerFlags;
this.NativeLinkingFlags = nativeLinkingFlags;
this.NativeArchiver = nativeArchiver;
this.AdditionalIncludeDirs = additionalIncludeDirs;
this.LibraryPaths = libraryPaths;
this.MainTemplatePath = mainTemplatePath;
this.EnableParallelism = enableParallelism;
}
}
public sealed class ArtifactPathOptions
{
public readonly string OutputNativeArchiveFileName;
public readonly string? OutputNativeExecutableFileName;
public ArtifactPathOptions(
string outputNativeArchiveFileName,
string? outputNativeExecutableFileName)
{
this.OutputNativeExecutableFileName = outputNativeExecutableFileName;
this.OutputNativeArchiveFileName = outputNativeArchiveFileName;
}
}
public static class NativeBinaryBuilder
{
private readonly struct CompilationResult
{
public readonly int ExitCode;
public readonly string Logs;
public readonly string OutputPath;
public CompilationResult(int exitCode, string logs, string outputPath)
{
this.ExitCode = exitCode;
this.Logs = logs;
this.OutputPath = outputPath;
}
public override string ToString() =>
$"{this.ExitCode}: {this.Logs}";
}
private static async Task<CompilationResult> ExecuteCompilerAsync(
string sourceCodePath,
string outputStagingDirPath,
string outputFileName,
string[] nativeToolchainBasePaths,
string nativeCompiler,
string nativeCompilerFlags,
string includeDir,
string sourceDir,
bool isCompileOnly,
string? libraryPath,
string[] additionalIncludeDirs,
string[] additionalLibraryPaths)
{
var sourceCodeFileName = Path.GetFileNameWithoutExtension(sourceCodePath);
var outputBasePath = Path.GetFullPath(Path.Combine(
outputStagingDirPath,
IOAccessor.GetDirectoryPath(sourceCodePath.Substring(sourceDir.Length + 1))));
var outputPath = Path.Combine(
outputBasePath,
outputFileName);
var buildScriptPath = Path.Combine(
outputBasePath,
$"build_{sourceCodeFileName}");
await IOAccessor.SafeCreateDirectoryAsync(
outputBasePath, false).
ConfigureAwait(false);
var result = await IOAccessor.ExecuteAsync(
outputBasePath,
buildScriptPath,
nativeToolchainBasePaths,
nativeCompiler,
new[]
{
nativeCompilerFlags,
isCompileOnly ? "-c" : "",
$"-I{IOAccessor.ToRelativePath(outputBasePath, includeDir)}",
$"-I{IOAccessor.ToRelativePath(outputBasePath, sourceDir)}",
}.
Concat(additionalIncludeDirs.Select(p => $"-I{IOAccessor.ToRelativePath(outputBasePath, p)}")).
Concat(new[]
{
"-o", IOAccessor.ToRelativePath(outputBasePath, outputPath),
IOAccessor.ToRelativePath(outputBasePath, sourceCodePath),
}).
Concat(libraryPath is { } lp ? new[] { IOAccessor.ToRelativePath(outputBasePath, lp) } : new string[0]).
Concat(additionalLibraryPaths.Select(p => IOAccessor.ToRelativePath(outputBasePath, p))).
ToArray()).
ConfigureAwait(false);
return new CompilationResult(result.ExitCode, result.Logs, outputPath);
}
private static ValueTask<ExecuteResult> ExecuteArchiverAsync(
string[] objectPaths,
string outputPath,
string[] nativeCompilerBasePaths,
string nativeArchiver)
{
var outputBasePath = Path.GetFullPath(IOAccessor.GetDirectoryPath(outputPath));
var buildScriptPath = Path.Combine(
outputBasePath,
$"build_{Path.GetFileNameWithoutExtension(outputPath)}");
return IOAccessor.ExecuteAsync(
outputBasePath,
buildScriptPath,
nativeCompilerBasePaths,
nativeArchiver,
new[]
{
"rcs",
IOAccessor.ToRelativePath(outputBasePath, outputPath),
}.
Concat(objectPaths.Select(p => IOAccessor.ToRelativePath(outputBasePath, p))).
ToArray());
}
public static async ValueTask CompileToNativeAsync(
ILogger logger,
ToolchainOptions toolchainOptions,
ArtifactPathOptions artifactPathOptions,
IMethodInformation? mainEntryPoint,
string sourceCodeDirPath,
string outputDirPath)
{
var sourceCodeDirFullPath = Path.GetFullPath(sourceCodeDirPath);
var outputDirFullPath = Path.GetFullPath(outputDirPath);
var outputNativeArchiveFullPath = Path.GetFullPath(
Path.Combine(
outputDirFullPath,
artifactPathOptions.OutputNativeArchiveFileName));
var nativeToolchainBasePath = IOAccessor.GetDirectoryPath(
toolchainOptions.NativeCompiler);
var nativeToolchainBasePaths =
string.IsNullOrWhiteSpace(nativeToolchainBasePath) ?
new string[0] :
new[] { Path.GetFullPath(nativeToolchainBasePath) };
logger.Information($"Preparing for compilation native binary: {sourceCodeDirFullPath}");
await IOAccessor.SafeCreateDirectoryAsync(
outputDirFullPath, outputDirFullPath != sourceCodeDirFullPath).
ConfigureAwait(false);
var outputStagingBaseDirPath = Path.Combine(outputDirFullPath, "obj");
await IOAccessor.SafeCreateDirectoryAsync(
outputStagingBaseDirPath, true).
ConfigureAwait(false);
var includeDir = Path.Combine(sourceCodeDirFullPath, "include");
var sourceDir = Path.Combine(sourceCodeDirFullPath, "src");
var sourceCodePaths =
Directory.EnumerateFiles(
sourceDir, "*.*", SearchOption.AllDirectories).
Where(p =>
Path.GetExtension(p) switch { ".c" => true, ".cpp" => true, _ => false, } &&
!Path.GetFileNameWithoutExtension(p).EndsWith("_bundle")). // Except bundler
Select(p => Path.Combine(sourceCodeDirFullPath, p)).
ToArray();
/////////////////////////////////////////////////////////////
// Compiling step:
// Compile in small pieces:
// Because LTO (LTCG) is not always enable, it is better to subdivide object files
// to reduce the amount of code when linking.
IList<CompilationResult> crs;
if (toolchainOptions.EnableParallelism)
{
logger.Information($"Building native library (In parallelism): {outputNativeArchiveFullPath}");
crs = await Task.WhenAll(
sourceCodePaths.
Select(sourceCodePath =>
{
logger.Trace($"Compiling source code: \"{sourceCodePath}\" ...");
return ExecuteCompilerAsync(
sourceCodePath,
outputStagingBaseDirPath,
Path.GetFileNameWithoutExtension(sourceCodePath) + ".o",
nativeToolchainBasePaths,
toolchainOptions.NativeCompiler,
toolchainOptions.NativeCompilerFlags,
includeDir,
sourceDir,
true,
null,
toolchainOptions.AdditionalIncludeDirs,
new string[0]);
})).
ConfigureAwait(false);
}
else
{
logger.Information($"Building native library: {outputNativeArchiveFullPath}");
crs = new List<CompilationResult>();
foreach (var sourceCodePath in sourceCodePaths)
{
logger.Trace($"Compiling source code: {sourceCodePath}");
var r = await ExecuteCompilerAsync(
sourceCodePath,
outputStagingBaseDirPath,
Path.GetFileNameWithoutExtension(sourceCodePath) + ".o",
nativeToolchainBasePaths,
toolchainOptions.NativeCompiler,
toolchainOptions.NativeCompilerFlags,
includeDir,
sourceDir,
true,
null,
toolchainOptions.AdditionalIncludeDirs,
new string[0]).
ConfigureAwait(false);
crs.Add(r);
}
}
var cr = crs.FirstOrDefault(r => r.ExitCode != 0);
if (cr.ExitCode != 0)
{
throw new Exception($"{Path.GetFileName(toolchainOptions.NativeCompiler)}: {cr}");
}
var ar = await ExecuteArchiverAsync(
crs.Select(cr => cr.OutputPath).ToArray(),
outputNativeArchiveFullPath,
nativeToolchainBasePaths,
toolchainOptions.NativeArchiver).
ConfigureAwait(false);
if (ar.ExitCode != 0)
{
throw new Exception($"{Path.GetFileName(toolchainOptions.NativeArchiver)}: {ar}");
}
/////////////////////////////////////////////////////////////
// Linking step:
if (artifactPathOptions.OutputNativeExecutableFileName is { } &&
mainEntryPoint is { } &&
toolchainOptions.MainTemplatePath is { })
{
var outputNativeExecutableFullPath = Path.GetFullPath(
Path.Combine(
outputDirFullPath,
artifactPathOptions.OutputNativeExecutableFileName));
logger.Information($"Linking native binary: {outputNativeExecutableFullPath}");
var mainSourceCodePath = Path.Combine(
outputDirFullPath,
Path.GetFileNameWithoutExtension(outputNativeExecutableFullPath) + ".c");
var mainBody = new StringBuilder(
await IOAccessor.ReadAllTextAsync(toolchainOptions.MainTemplatePath, new UTF8Encoding(false, true)).
ConfigureAwait(false));
mainBody.Replace("{headerName}", mainEntryPoint.DeclaringModule.DeclaringAssembly.Name + ".h");
mainBody.Replace("{mainIsVoid}", mainEntryPoint.ReturnType.IsVoidType ? "1" : "0");
mainBody.Replace("{mainSymbol}", mainEntryPoint.CLanguageFunctionFullName);
await IOAccessor.WriteAllTextAsync(mainSourceCodePath, mainBody.ToString(), new UTF8Encoding(false, true)).
ConfigureAwait(false);
var crf = await ExecuteCompilerAsync(
mainSourceCodePath,
outputDirFullPath,
outputNativeExecutableFullPath,
nativeToolchainBasePaths,
toolchainOptions.NativeCompiler,
toolchainOptions.NativeCompilerFlags + " " + toolchainOptions.NativeLinkingFlags,
includeDir,
sourceDir,
false,
outputNativeArchiveFullPath,
toolchainOptions.AdditionalIncludeDirs,
toolchainOptions.LibraryPaths).
ConfigureAwait(false);
if (crf.ExitCode != 0)
{
throw new Exception($"{Path.GetFileName(toolchainOptions.NativeCompiler)}: {crf}");
}
logger.Information($"Built native binary: {outputNativeExecutableFullPath}");
}
else
{
logger.Information($"Built native library: {outputNativeArchiveFullPath}");
}
}
}
}