Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
253 lines (228 loc) · 9.49 KB
/
Copy pathProgram.cs
File metadata and controls
253 lines (228 loc) · 9.49 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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Newtonsoft.Json;
using Silk.NET.BuildTools.Common;
namespace Silk.NET.BuildTools
{
internal class Program
{
private static int Main(string[] args)
{
Console.WriteLine("Silk.NET Build Tools");
Console.WriteLine("Copyright (C) .NET Foundation and Contributors");
Console.WriteLine();
if (args.Length == 1 && args[0] == "jsonex")
{
// get a template json file
Console.WriteLine
(
JsonConvert.SerializeObject
(
ExampleJsonFile, Formatting.Indented
)
);
return 0;
}
if (args.Length == 0)
{
Console.WriteLine
(
"This is an internal tool not meant for general consumption. If you are working on " +
"Silk.NET (or a Silk-based project), please let us know in our Discord server and we'll do our " +
"best to help you navigate BuildTools."
);
return 1;
}
var sw = Stopwatch.StartNew();
var extraCtrls = new List<string>();
var failedJobs = 0;
Console.SetOut(ConsoleWriter.GetOrCreate(Console.Out));
foreach (var arg in args)
{
if (arg.StartsWith("--"))
{
Console.WriteLine($"Control descriptor \"{arg}\" will be applied to every job herein.");
extraCtrls.Add(arg[2..]);
continue;
}
var jobSw = Stopwatch.StartNew();
var abs = Path.GetFullPath(arg);
Environment.CurrentDirectory = Path.GetDirectoryName
(abs) ?? throw new NullReferenceException("Dir path null.");
Generator.Run(AddDescriptors(JsonConvert.DeserializeObject<Config>(File.ReadAllText(abs)), extraCtrls));
jobSw.Stop();
Thread.Sleep(3000); // cooldown to ensure all the threads have reported their results.
ConsoleWriter.Instance.BeginPlainRegion();
Console.WriteLine();
Console.WriteLine("Job Summary");
Console.WriteLine("===========");
Console.WriteLine();
foreach (var kvp in ConsoleWriter.Instance.Timings.Values)
{
if (kvp.Value.Success)
{
Console.WriteLine
($"Task \"{kvp.Key}\" took {kvp.Value.Time.TotalSeconds} second(s) to complete.");
}
else
{
Console.WriteLine
($"Task \"{kvp.Key}\" failed after {kvp.Value.Time.TotalSeconds} second(s).");
failedJobs++;
}
}
Console.WriteLine();
Console.WriteLine($"In total, this particular job took {jobSw.Elapsed.TotalSeconds} second(s) to complete.");
Console.WriteLine();
ConsoleWriter.Instance.EndPlainRegion();
ConsoleWriter.Instance.Reset();
}
sw.Stop();
ConsoleWriter.Instance.Wait();
Console.SetOut(ConsoleWriter.Instance.Base);
Console.WriteLine($"Complete bind took {sw.Elapsed.TotalSeconds} second(s).");
static Config AddDescriptors(Config config, IReadOnlyList<string> descriptors)
{
for (var i = 0; i < config.Tasks.Length; i++)
{
var task = config.Tasks[i];
config.Tasks[i].Controls = task.Controls.Concat(descriptors).ToArray();
}
return config;
}
if (failedJobs > 0)
{
return -2000000000 - failedJobs;
}
return 0;
}
internal class ConsoleWriter : TextWriter
{
internal static ConsoleWriter Instance { get; private set; }
public ThreadLocal<string> CurrentName { get; private set; } = new ThreadLocal<string>();
public ThreadLocal<KeyValuePair<string, (TimeSpan Time, bool Success)>> Timings { get; private set; } =
new ThreadLocal<KeyValuePair<string, (TimeSpan Time, bool Success)>>(true);
private readonly ConcurrentQueue<string> _logs = new();
private int _exitOnEmpty = 0; // int32 writes are atomic
private int _plain = 0;
private Task _logLoop;
public readonly TextWriter Base;
private ConsoleWriter(TextWriter @base)
{
Base = @base;
Encoding = Base.Encoding;
Instance = this;
_logLoop = Task.Run(LogLoop);
}
public override Encoding Encoding { get; }
public static ConsoleWriter GetOrCreate(TextWriter @base) => Instance ??= new ConsoleWriter(@base);
public void Reset()
{
CurrentName = new ThreadLocal<string>();
Timings = new ThreadLocal<KeyValuePair<string, (TimeSpan Time, bool Success)>>(true);
}
public override void WriteLine(string? value)
{
_logs.Enqueue(_plain switch
{
1 => value,
_ => $"[{DateTime.Now:T}] [{CurrentName.Value}] {Task.CurrentId}> " + value
});
}
public override void WriteLine() => _logs.Enqueue("");
public void BeginPlainRegion() => _plain = 1;
public void EndPlainRegion() => _plain = 0;
public void Wait()
{
_exitOnEmpty = 1;
SpinWait.SpinUntil(() => _exitOnEmpty == 0);
}
private void LogLoop()
{
while (true)
{
if (_logs.IsEmpty && _exitOnEmpty == 1)
{
_exitOnEmpty = 0;
break;
}
if (_logs.TryDequeue(out var line))
{
Base.WriteLine(line);
}
}
}
}
///////////////////////////////////////////////////////////////////////////////////////
// The meaningful part of the file ends here, from here it's just an example structure.
///////////////////////////////////////////////////////////////////////////////////////
private static Config ExampleJsonFile = new Config
{
Tasks = new[]
{
new BindTask
{
BakeryOpts = new BakeryOptions
{
Include = new[] {"glcore"}
},
CacheFolder = "/path/to/cacheFolder",
CacheKey = "glcoreCacheKey",
ClangOpts = new ClangTaskOptions
{
ClangArgs = new[] {"--clang-arguments"},
ClassMappings = new Dictionary<string, string>
{
{"sourceHeader.h", "DestinationClass"}
}
},
Controls = new[]
{
"control-variables-to-define-how-gernation-runs",
"convert-windows-only",
"convert-macos-only",
"convert-linux-only"
},
ConverterOpts = new ConverterOptions
{
Reader = "gl",
Constructor = "gl"
},
FunctionPrefix = "gl",
ExtensionsNamespace = "MyNamespace.ForExtensions",
Namespace = "MyNamespace",
Mode = ConverterMode.ConvertConstruct,
Name = "Profile Name",
Sources = new[] {"/path/to/sourceFile.xml", "/path/to/header.h"},
NameContainer = new NameContainer
{
ClassName = "MyNameContainer",
Android = "libapi.so",
IOS = "libapi.dylib",
Linux = "libapi.so",
MacOS = "libapi.dylib",
Windows64 = "api.dll",
Windows86 = "api.dll"
},
OutputOpts = new OutputOptions
{
Folder = "/path/to/outputFolder", License = "/path/to/licenseFile",
Props = "/path/to/customMSBuild.props"
},
TypeMaps = new List<Dictionary<string, string>>
{new Dictionary<string, string> {{"HWND", "nint"}}}
}
}
};
}
}