-
Notifications
You must be signed in to change notification settings - Fork 2
/
regenerate.d
355 lines (294 loc) · 9.13 KB
/
regenerate.d
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
module regenerate;
import std.string;
import std.file;
import std.path;
import std.range;
import std.algorithm;
import std.process;
import std.getopt;
import std.experimental.logger;
import std.stdio : writeln;
struct Options
{
@("Path to temporary directory, `temp` by default.")
string tempDirectory = "temp";
@("Path to d++ executable. Only needed when doing bindings. Found in path if not provided.")
string dppExecutablePath;
@("Path to bindbc generator. Only needed when doing bindings.")
string generatorRepoPath;
@("Path to `cimgui.h`.")
string cimguiHeaderPath;
@("Whether to get cimgui from github and compile into a dll and lib.")
bool compileImgui;
enum CimguiBranch
{
docking, master
}
@("Controls which branch of cimgui to build. (Can be `docking` or `master`.)")
CimguiBranch cimguiBranch = CimguiBranch.docking;
@("Whether to regenerate bindbc-cimgui bindings using the generator.")
bool generateBindings;
@("Whether to get dpp from github and compile it.")
bool compileDpp;
}
__gshared Options op;
__gshared bool hasErrors;
void _error(string message)
{
hasErrors = true;
error(message);
}
int main(string[] args)
{
string getoptMixin()
{
auto ret = "auto helpInformation = getopt(args";
static foreach (field; Options.tupleof)
{
import std.format;
ret ~= `, "%s", "%s", &op.%1$s`.format(__traits(identifier, field), __traits(getAttributes, field)[0]);
}
ret ~= ");";
return ret;
}
mixin(getoptMixin());
if (helpInformation.helpWanted || op == Options.init)
{
defaultGetoptPrinter("Help message", helpInformation.options);
return 0;
}
void ensurePathExistsIfProvided(string name, string path)
{
if (path && !exists(path))
{
_error(name ~ " not found by the specified path " ~ path);
}
}
ensurePathExistsIfProvided("Dpp", op.dppExecutablePath);
ensurePathExistsIfProvided("Generator", op.generatorRepoPath);
ensurePathExistsIfProvided("cimgui header", op.cimguiHeaderPath);
if (op.compileDpp && op.dppExecutablePath)
{
_error("Incompatible arguments: `compileDpp` and `dppExecutablePath`");
}
if (op.compileImgui && op.cimguiHeaderPath)
{
_error("Incompatible arguments: `compileImgui` and `cimguiHeaderPath`");
}
op.tempDirectory = absolutePath(op.tempDirectory);
mkdirRecurse(op.tempDirectory);
if (hasErrors)
return 1;
if (op.compileDpp)
{
op.dppExecutablePath = dppCompilationWorkflow();
if (!op.dppExecutablePath)
return 1;
log("Dpp has been written to " ~ op.dppExecutablePath);
}
if (op.compileImgui)
{
string outputDirectory = imguiCompilationWorkflow();
if (!outputDirectory)
return 1;
log("Imgui binaries have been written to " ~ outputDirectory);
}
if (op.generateBindings)
{
generateBindingsWorkflow();
}
return hasErrors ? 1 : 0;
}
string takeAfterLast(string str, string pattern)
{
auto index = lastIndexOf(str, pattern);
return str[index + 1..$];
}
// Returns the path to the cloned repo
string gitClone(string repoUrl, bool recursive = true, string branch = null)
{
string repoName = repoUrl.takeAfterLast("/");
string repoPath = buildPath(op.tempDirectory, repoName);
if (exists(repoPath))
{
log("Found " ~ repoName ~ " clone at " ~ repoPath ~ ", skipping cloning.");
return repoPath;
}
string[] args = ["git", "clone", repoUrl];
if (branch)
{
args ~= "--branch=" ~ branch;
}
if (recursive)
{
args ~= "--recursive";
}
auto result = _exec(args, op.tempDirectory);
if (result.status == 0)
return repoPath;
_error("Failed to clone " ~ repoName ~ ": " ~ result.output);
return null;
}
string dppCompilationWorkflow()
{
const dppRepoUrl = "https://github.com/atilaneves/dpp";
log("You will need to install LLVM if it's not installed already, see the dpp repo: " ~ dppRepoUrl);
if (!checkCommandsExist(["git", "dub"]))
return null;
const dppClonedRepoPath = gitClone(dppRepoUrl);
if (!dppClonedRepoPath)
return null;
const result = _exec(["dub", "build"], dppClonedRepoPath);
if (result.status != 0)
{
_error("Failed to build dpp: " ~ result.output);
return null;
}
string dppExecutablePath;
version (Windows)
{
dppExecutablePath = buildPath(dppClonedRepoPath, "bin/d++.exe");
if (!exists(dppExecutablePath))
{
_error("Expected d++ at " ~ dppExecutablePath);
}
}
return dppExecutablePath;
}
string gitCloneImgui()
{
const repoUrl = "https://www.github.com/cimgui/cimgui";
string commitHash, branch;
if (op.cimguiBranch == Options.CimguiBranch.docking)
{
commitHash = "873c03c3673033bf8e8dd22901d4a3934b7407b2";
branch = "docking_inter";
}
else
{
commitHash = "17ffa736d353591b9545d1cedaa6373482f7f1a3";
branch = "master";
}
const recursive = false;
const clonedRepoPath = gitClone(repoUrl, recursive, branch);
if (!clonedRepoPath)
return null;
if (commitHash)
{
auto result = _exec(["git", "checkout", commitHash], clonedRepoPath);
if (result.status != 0) return null;
}
// result = _exec(["git", "submodule", "init"], clonedRepoPath);
// if (result.status != 0) return null;
auto result = _exec(["git", "submodule", "update", "--init", "--recursive"], clonedRepoPath);
if (result.status != 0) return null;
return clonedRepoPath;
}
string imguiCompilationWorkflow()
{
if (!checkCommandsExist(["git", "cmake"]))
return null;
const imguiClonedRepoPath = gitCloneImgui();
if (!imguiClonedRepoPath)
return null;
const imguiBuildDirectory = "imgui_build";
const configuration = "RelWithDebInfo";
auto result = _exec(
["cmake", "-Hcimgui", "-B" ~ imguiBuildDirectory, "-DCMAKE_BUILD_TYPE=" ~ configuration], op.tempDirectory);
if (result.status != 0)
{
_error("Failed to run cmake: " ~ result.output);
return null;
}
result = _exec(["cmake", "--build", imguiBuildDirectory, "--config", configuration], op.tempDirectory);
if (result.status != 0)
{
_error("Failed to run cmake build: " ~ result.output);
return null;
}
// The output should be in RelWithDebInfo
const outputDirectory = buildPath(op.tempDirectory, imguiBuildDirectory, configuration);
return outputDirectory;
}
void generateBindingsWorkflow()
{
if (!checkCommandsExist(["git", "dub"]))
return;
string generatorRepoPath;
if (op.generatorRepoPath)
{
generatorRepoPath = op.generatorRepoPath;
}
else
{
generatorRepoPath = gitClone("https://github.com/MrcSnm/bindbc-generator");
if (!generatorRepoPath)
return;
}
string imguiPath;
string cimguiHeaderPath;
if (op.cimguiHeaderPath)
{
imguiPath = dirName(op.cimguiHeaderPath);
cimguiHeaderPath = op.cimguiHeaderPath;
}
else
{
imguiPath = gitCloneImgui();
if (!imguiPath)
return;
cimguiHeaderPath = buildPath(imguiPath, "cimgui.h");
}
string cimguiPluginGenPath = buildPath(generatorRepoPath, "bindbc/cimgui");
auto generatorArgs = [
"dub", "--",
"--recompile", "--load-all",
"--file", cimguiHeaderPath,
"--temp-path", op.tempDirectory,
"--presets", "cimgui"
];
if (op.dppExecutablePath)
{
generatorArgs ~= "--dpp-path";
generatorArgs ~= op.dppExecutablePath;
}
string argumentsString = escapeShellCommand(generatorArgs);
// We need to append the plugin arguments as strings, since otherwise they get escaped
// and the default parser in the generator cannot deal with that.
argumentsString ~= ` --plugin-args cimgui-overloads="[%s, %s, %s]"`.format(imguiPath, cimguiPluginGenPath, "d-conv");
log(argumentsString);
auto result = executeShell(argumentsString, null, Config.none, size_t.max, generatorRepoPath);
if (result.status != 0)
{
_error("Failed to apply the generator: ");
writeln(result.output);
return;
}
writeln(result.output);
log("Find the output files somewhere in " ~ generatorRepoPath ~ " and in here " ~ cimguiPluginGenPath);
}
auto _exec(string[] args, string workingDirectory = null)
{
string cmd = escapeShellCommand(args);
log(cmd);
return execute(args, null, Config.none, size_t.max, workingDirectory);
}
bool existsCommand(string command)
{
version (Windows)
{
return execute(["where", command]).status == 0;
}
assert(0, "Linux and the alike are not implemented");
}
bool checkCommandsExist(string[] commands)
{
foreach (command; commands)
{
if (!existsCommand(command))
{
_error("`" ~ command ~ "` not found in path");
}
}
return !hasErrors;
}