-
Notifications
You must be signed in to change notification settings - Fork 5
/
build_brala.d
614 lines (509 loc) · 18.1 KB
/
build_brala.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
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
#!rdmd
private {
import std.algorithm : max, map, filter, endsWith, canFind;
import std.path : buildPath, extension, setExtension, dirName, baseName, getcwd;
import std.array : join, split, appender, array;
import std.process : shell, system, environment;
import std.file : dirEntries, SpanMode, mkdirRecurse, rmdirRecurse,
FileException, exists, copy, remove, readText, write, chdir;
import std.stdio : writeln, writefln, File;
import std.string : format, stripRight, toUpper;
import std.parallelism : TaskPool;
import std.exception : enforce, collectException;
import std.getopt;
import std.digest.md;
import std.digest.digest;
import core.time : dur;
}
version(Windows) {
enum OBJ = ".obj";
enum SO = ".dll";
enum SO_LINK = ".lib";
} else version(linux) {
enum OBJ = ".o";
enum SO = ".so";
enum SO_LINK = "";
} else version(OSX) {
enum OBJ = ".o";
enum SO = ".dylib";
enum SO_LINK = "";
}
enum LF = "-L";
version(Windows) {
enum LLF = "";
} else {
enum LLF = "-L-l";
}
alias filter!("a.length > 0") filter0;
bool is32bit() {
return is(uint == size_t);
}
unittest {
if(is32bit()) {
assert(!is(ulong == size_t));
} else {
assert(is(ulong == size_t));
}
}
abstract class Compiler {
string build_prefix;
string[] import_paths;
string[] additional_flags;
@property string import_flags() {
return filter0(import_paths).map!(x => "-I" ~ x).join(" ");
}
@property string compiling_ext() { throw new Exception("Not Implemented"); }
void compile(string prefix, string file) { throw new Exception("Not Implemented"); }
string version_(string ver) { throw new Exception("Not Implemented"); }
@property string debug_() { throw new Exception("Not Implemented"); }
@property string debug_info() { throw new Exception("Not Implemented"); }
@property string compiler() { throw new Exception("Not Implemented"); }
bool is_available() {
return system("%s -v".format(compiler)) == 0;
}
void print(string cmd, string src, string dest) {
writefln(" %s %s", compiler.toUpper, src);
}
}
class DCompiler : Compiler {
override @property string compiling_ext() {
return ".d";
}
}
class CCompiler : Compiler {
override @property string compiling_ext() {
return ".c";
}
}
interface Linker {
void link(string, string[], string[], string[]);
}
class DMD : DCompiler, Linker {
override string version_(string ver) {
return "-version=" ~ ver;
}
override @property string debug_() { return "-debug"; }
override @property string debug_info() { return "-g -gc"; }
override @property string compiler() { return "dmd"; }
override void compile(string src, string dest) {
string cmd = "dmd %s %s -c %s -of%s".format(import_flags, filter0(additional_flags).join(" "), src, dest);
print(cmd, src, dest);
shell(cmd);
}
void link(string out_path, string[] object_files, string[] libraries, string[] options) {
string cmd = "dmd %s %s %s -of%s".format(
object_files.join(" "),
filter0(libraries).map!(x => LLF ~ x).join(" "),
filter0(options).map!(x => LF ~ x).join(" "),
out_path);
writefln("%s", cmd);
print(cmd, out_path, "");
shell(cmd);
}
}
// GDC: "-fdebug -g";
// LDC: "-debug -g -gc";
class GDC : DCompiler, Linker {
override string version_(string ver) {
return "-fversion=" ~ ver;
}
override @property string debug_() { return "-fdebug"; }
override @property string debug_info() { return "-g"; }
override @property string compiler() { return "gdc"; }
override void compile(string src, string dest) {
string cmd = "gdc %s %s -c %s -o %s".format(import_flags, filter0(additional_flags).join(" "), src, dest);
print(cmd, src, dest);
shell(cmd);
}
void link(string out_path, string[] object_files, string[] libraries, string[] options) {
string cmd = "gdc %s %s %s -o %s".format(
object_files.join(" "),
filter0(libraries).map!(x => LLF ~ x).join(" "),
filter0(options).map!(x => LF ~ x).join(" "),
out_path);
print(cmd, out_path, "");
shell(cmd);
}
}
class LDC : DCompiler, Linker {
override string version_(string ver) {
return "-d-version=" ~ ver;
}
override @property string debug_() { return "-d-debug"; }
override @property string debug_info() { return "-g -gc"; }
override @property string compiler() { return "ldc"; }
override void compile(string src, string dest) {
string cmd = "ldc2 %s %s -c %s -of=%s".format(import_flags, filter0(additional_flags).join(" "), src, dest);
print(cmd, src, dest);
shell(cmd);
}
void link(string out_path, string[] object_files, string[] libraries, string[] options) {
string cmd = "ldc2 %s %s %s -of=%s".format(
object_files.join(" "),
filter0(libraries).map!(x => LLF ~ x).join(" "),
filter0(options).map!(x => LF ~ x).join(" "),
out_path);
print(cmd, out_path, "");
shell(cmd);
}
}
class DMC : CCompiler {
override @property string compiler() { return "dmc"; }
override void compile(string src, string dest) {
string cmd = "dmc %s %s -c %s -o%s".format(import_flags, filter0(additional_flags).join(" "), src, dest);
print(cmd, src, dest);
shell(cmd);
}
}
class GCC : CCompiler {
override @property string compiler() { return "gcc"; }
override void compile(string src, string dest) {
string cmd = "gcc %s %s -c %s -o %s".format(import_flags, filter0(additional_flags).join(" "), src, dest);
print(cmd, src, dest);
shell(cmd);
}
}
interface Cache {
bool is_cached(string src, string dest);
void add_file_to_cache(string file);
}
class NoCache : Cache {
bool is_cached(string src, string dest) { return false; }
void add_file_to_cache(string file) {}
}
class MD5Cache : Cache {
string[string] cache;
this()() {}
this(T)(T c) if(is(T : string) || is(T : File) || is(T : string[])) {
load(c);
}
void load(string cache_file) {
if(cache_file.exists()) {
File f = File(cache_file, "r");
scope(exit) f.close();
load(f);
}
}
void load(File cache_file) {
_add_to_cache(cache_file.byLine());
}
void load(string[] cache_file) {
_add_to_cache(cache_file);
}
private void _add_to_cache(T)(T iter) {
foreach(raw_line; iter) {
if(!raw_line.length) {
continue;
}
static if(!is(typeof(line) : string)) {
string line = raw_line.idup;
} else {
alias raw_line line;
}
string hash = line.split()[$-1];
string file = line[0..$-hash.length].stripRight();
cache[file] = hash;
}
}
void add_file_to_cache(string file) {
cache[file] = MD5Cache.hexdigest_from_file(file);
}
bool is_cached(string src, string dest) {
if(src in cache && dest.exists()) {
string hexdigest = MD5Cache.hexdigest_from_file(src);
return cache[src] == hexdigest;
}
return false;
}
void save(string file) {
File f = File(file, "w");
scope(exit) f.close();
save(f);
}
void save(File file) {
foreach(k, v; cache) {
file.writefln("%s %s", k, v);
}
}
static string hexdigest_from_file(Hash = MD5)(string file) {
File f = File(file, "r");
scope(exit) f.close();
return hexdigest_from_file!(Hash)(f);
}
static string hexdigest_from_file(Hash = MD5)(File file) {
auto b = file.byChunk(4096 * 1024);
return hexDigest!Hash(b).idup;
}
}
struct ScanPath {
string path;
alias path this;
SpanMode mode;
}
class Builder {
ScanPath[] scan_paths;
protected string[] _object_files;
@property string[] object_files() { return _object_files; }
protected Compiler[string] compiler;
Linker linker;
Cache cache;
string out_path;
string out_file;
@property string[] library_paths() {
version(Windows) {
return [buildPath("lib", "win"), buildPath("lib", "win%s".format(is32bit() ? "32" : "64"))];
} else version(linux) {
return [buildPath("lib", "linux"), buildPath("lib", "linux%s".format(is32bit() ? "32" : "64"))];
} else version(OSX) {
return [buildPath("lib", "osx"), buildPath("lib", "osx%s".format(is32bit() ? "32" : "64"))];
}
}
string[] libraries_win;
string[] libraries_win32;
string[] libraries_win64;
string[] libraries_linux;
string[] libraries_linux32;
string[] libraries_linux64;
string[] libraries_osx;
string[] libraries_osx32;
string[] libraries_osx64;
@property string[] libraries() {
version(Windows) {
static if(is32bit()) {
return libraries_win ~ libraries_win32;
} else {
return libraries_win ~ libraries_win64;
}
} else version(linux) {
static if(is32bit()) {
return libraries_linux ~ libraries_linux32;
} else {
return libraries_linux ~ libraries_linux64;
}
} else version(OSX) {
static if(is32bit()) {
return libraries_osx ~ libraries_osx32;
} else {
return libraries_osx ~ libraries_osx64;
}
}
}
string[] linker_options_win;
string[] linker_options_win32;
string[] linker_options_win64;
string[] linker_options_linux;
string[] linker_options_linux32;
string[] linker_options_linux64;
string[] linker_options_osx;
string[] linker_options_osx32;
string[] linker_options_osx64;
@property string[] linker_options() {
version(Windows) {
static if(is32bit()) {
return linker_options_win ~ linker_options_win32;
} else {
return linker_options_win ~ linker_options_win64;
}
} else version(linux) {
static if(is32bit()) {
return linker_options_linux ~ linker_options_linux32;
} else {
return linker_options_linux ~ linker_options_linux64;
}
} else version(OSX) {
static if(is32bit()) {
return linker_options_osx ~ linker_options_osx32;
} else {
return linker_options_osx ~ linker_options_osx64;
}
}
}
string build_prefix;
this() {
// TODO: implement find_compiler
version(Windows) {
auto dc = new DMD();
auto cc = new DMC();
this(new NoCache(), dc, dc, cc);
} else {
auto dc = new DMD();
auto cc = new GCC();
this(new NoCache(), dc, dc, cc);
}
}
this(Cache cache, Linker linker, Compiler[] compiler...) {
this.cache = cache;
this.linker = linker;
foreach(c; compiler) {
this.compiler[c.compiling_ext] = c;
}
}
void add_scan_path(string path, SpanMode mode = SpanMode.breadth) {
scan_paths ~= ScanPath(path, mode);
}
void add_scan_path(ScanPath scan_path) {
scan_paths ~= scan_path;
}
void compile(TaskPool task_pool=null) {
foreach(path; scan_paths) {
auto files = map!(x => x.name)(filter!(e => compiler.keys.canFind(e.name.extension))(dirEntries(path, path.mode))).array();
enum foreach_body = "collectException(mkdirRecurse(buildPath(build_prefix, file.dirName())));
Compiler compiler = this.compiler[file.extension];
string dest = buildPath(build_prefix, file).setExtension(OBJ);
if(!cache.is_cached(file, dest)) {
compiler.compile(file, dest);
cache.add_file_to_cache(file);
}
_object_files ~= dest;";
if(task_pool is null) {
foreach(file; files) {
mixin(foreach_body);
}
} else {
size_t work_units = max(files.length / task_pool.size, 1);
foreach(file; task_pool.parallel(files, work_units)) {
mixin(foreach_body);
}
}
}
}
void link() {
linker.link(buildPath(out_path, out_file), _object_files, libraries, linker_options);
}
}
string[] glfw_libraries() {
version(Windows) {
return [];
} else {
string pkg_cfg_path = environment.get("PKG_CONFIG_PATH", "");
environment["PKG_CONFIG_PATH"] = buildPath("build", "glfw", "src");
string[] result;
try {
result = shell(`pkg-config --static --libs glfw3`).split();
} finally {
environment["PKG_CONFIG_PATH"] = pkg_cfg_path;
}
return result;
}
}
void build_glfw() {
version(Windows) { return; }
collectException(mkdirRecurse(["build", "glfw", "src"].buildPath));
string old = getcwd();
chdir(["build", "glfw"].buildPath);
scope(success) chdir(old);
writeln(" CMAKE src/c/glfw/*");
shell(`
cmake -DBUILD_SHARED_LIBS=OFF -DGLFW_BUILD_EXAMPLES=OFF -DGLFW_BUILD_TESTS=OFF ../../src/c/glfw
`);
shell(`
make --silent -j3
`);
}
int main(string[] args) {
size_t jobs = 1;
string cache_file = ".build_cache";
bool no_cache = false;
bool override_cache = false;
getopt(args, "jobs|j", &jobs,
"cache|c", &cache_file,
"no-cache", &no_cache,
"override-cache|o", &override_cache);
enforce(jobs >= 1, "Jobs can't be 0 or negative");
TaskPool task_pool;
if(jobs > 1) {
task_pool = new TaskPool(jobs);
}
scope(exit) { if(task_pool !is null) task_pool.finish(); }
MD5Cache md5_cache = new MD5Cache();
if(!override_cache) {
md5_cache.load(cache_file);
}
Cache cache;
if(no_cache) {
cache = new NoCache();
} else {
cache = md5_cache;
}
version(Windows) {
auto cc = new DMC();
} else {
auto cc = new GCC();
}
version(DigitalMars) {
auto dc = new DMD();
} else version(GNU) {
auto dc = new GDC();
} else version(LDC) {
auto dc = new LDC();
} else version(SDC) {
static assert(false, "This compiler is too awesome to compile BraLa at the moment");
} else {
static assert(false, "Unsupported compiler");
}
dc.additional_flags = [dc.version_("glad"), dc.version_("gl3n"), dc.version_("stb"),
dc.version_("glamour"), dc.debug_, dc.debug_info];
version(Windows) {
dc.additional_flags ~= "-m32";
}
dc.import_paths = [buildPath("brala"),
buildPath("src", "d"),
buildPath("src", "d", "gl3n"),
buildPath("src", "d", "glad"),
buildPath("src", "d", "glamour"),
buildPath("src", "d", "glfw"),
buildPath("src", "d", "glwtf"),
buildPath("src", "d", "minilib"),
buildPath("src", "d", "nbd"),
buildPath("src", "d", "openssl"),
buildPath("src", "d", "std")];
auto builder = new Builder(cache, dc, dc, cc);
builder.out_path = buildPath("bin");
builder.out_file = "bralad";
builder.build_prefix = buildPath("build");
if(args.canFind("clean")) {
collectException(builder.build_prefix.rmdirRecurse());
collectException(".build_cache".remove());
return 0;
}
builder.add_scan_path(buildPath("brala"));
builder.add_scan_path(buildPath("src", "d", "arsd"));
builder.add_scan_path(buildPath("src", "d", "etc"));
builder.add_scan_path(buildPath("src", "d", "gl3n", "gl3n"));
builder.add_scan_path(buildPath("src", "d", "glad"));
builder.add_scan_path(buildPath("src", "d", "glamour", "glamour"));
builder.add_scan_path(buildPath("src", "d", "glwtf", "glwtf"));
builder.add_scan_path(buildPath("src", "d", "minilib"));
builder.add_scan_path(buildPath("src", "d", "nbd"), SpanMode.shallow);
// builder.add_scan_path(buildPath("src", "d", "openssl"));
builder.add_scan_path(buildPath("src", "d", "std"));
builder.add_scan_path(buildPath("src", "c"), SpanMode.shallow);
builder.libraries_win = [buildPath("lib", "win", "libssl32.lib"),
buildPath("lib", "win", "libeay32.lib"),
buildPath("lib", "win", "glfw3.lib")];
builder.linker_options_win = [];
builder.libraries_linux = ["ssl", "crypto", "dl"];
builder.linker_options_linux = ["-Lbuild/glfw/src"];
builder.linker_options_linux ~= glfw_libraries();
builder.linker_options_linux32 ~= ["-Llib/linux32"];
builder.linker_options_linux64 ~= ["-Llib/linux64"];
builder.libraries_osx = builder.libraries_linux;
builder.linker_options_osx = builder.linker_options_linux;
builder.linker_options_osx ~= ["-Llib/osx"];
collectException(rmdirRecurse(builder.out_file));
build_glfw();
builder.compile(task_pool);
builder.link();
md5_cache.save(cache_file);
// executable is linked, now copy the .dll/.so over to the executable
// using -rpath on linux/osx, no need for copying
version(Windows) {
foreach(path; builder.library_paths) {
if(!path.exists) continue;
foreach(file; filter!(x => x.extension == SO)(dirEntries(path, SpanMode.depth))) {
copy(file, buildPath("bin", file.baseName));
}
}
}
return 0;
}