-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.d
544 lines (477 loc) · 16.5 KB
/
Main.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
/+
+ Copyright Andrej Mitrovic 2011.
+ Copyright Tomasz Stachowiak 2009 - 2011.
+ Distributed under the Boost Software License, Version 1.0.
+ (See accompanying file LICENSE_1_0.txt or copy at
+ http://www.boost.org/LICENSE_1_0.txt)
+/
module xfbuild.Main;
import xfbuild.MT;
import xfbuild.BuildTask;
import xfbuild.Misc;
import xfbuild.Compiler : CompilerError;
import xfbuild.GlobalParams;
import xfbuild.Exception;
import xfbuild.Process;
import dcollections.HashMap;
import std.algorithm;
import std.array;
import std.conv;
import std.datetime;
import std.exception;
import std.stdio;
import std.file;
import std.path;
import std.string;
import std.c.process;
import std.cpuid : threadsPerCPU;
void printHelpAndQuit(int status)
{
writeln(
`xfBuild 0.5.2
http://github.com/AndrejMitrovic/xfbuild
Usage:
xfbuild [+help]
xfbuild [ROOT | OPTION | COMPILER OPTION]...
Track dependencies and their changes of one or more modules, compile them
with COMPILER OPTION(s) and link all objects into OUTPUT [see OPTION(s)].
ROOT:
String ended with either ".d" or "/" indicating a module
or a directory of modules to be compiled, respectively.
OPTION(s) are prefixed by "+".
COMPILER OPTION(s) are anything that is not OPTION(s) or ROOT(s).
Recognized OPTION(s):
+x=PACKAGE Don't compile any modules within the package
+xpath=PATH Don't compile any modules within the path
+full Perform a full build
+clean Perform clean, remove object files
+redep Remove the dependency file afterwards
+v Print the compilation commands
+h Manage headers for faster compilation
+mod-limit=NUM Compile max NUM modules at a time
+D=DEPS Put the resulting dependencies into DEPS [default: .deps]
+O=OBJS Put compiled objects into OBJS [default: .objs]
+q Use -oq when compiling (only supported by ldc)
+noop Don't use -op when compiling
+nolink Don't link
+o=OUTPUT Link objects into the resulting binary OUTPUT
+c=COMPILER Use the D Compiler COMPILER [default: dmd]
+C=EXT Extension of the compiler-generated object files
[default: .obj on Windows, .o otherwise]
+rmo Reverse Module Order
(when compiling - might uncrash OPTLINK)
+mbm Module By Module, compiles one module at a time
(useful to debug some compiler bugs)
+R Recursively scan directories for modules
+nodeps Don't use dependencies' file
+keeprsp Don't remove .rsp files upon errors`);
version (MultiThreaded)
{
writeln(
`
Multithreading options:
+threads=NUM Number of theads to use [default: CPU core count]
+no-affinity Do NOT manage process affinity (New feature which
should prevent DMD hanging on multi-core systems)
+linker-affinity=MASK Process affinity mask for the linker
(hexadecimal) [default: {:x} (OS-dependent)]`);
}
writeln(
`
Environment Variables:
XFBUILDFLAGS You can put any option from above into that variable
Note: Keep in mind that command line options override
those
D_COMPILER The D Compiler to use [default: dmd]
Note: XFBUILDFLAGS and command line options override
this`
);
debug
writefln("\nBuilt with %s v%s and Phobos at %s %s",
__VENDOR__, __VERSION__, __DATE__, __TIME__);
exit(status);
}
struct ArgParser
{
void error(string arg)
{
throw new ParseException(format("Unknown argument: +%s", arg));
}
struct Reg
{
string arg;
void delegate() a;
void delegate(string) b;
}
Reg[] regs;
void bind(string arg, void delegate() a)
{
regs ~= Reg(arg, a, null);
}
void bind(string arg, void delegate(string) b)
{
regs ~= Reg(arg, null, b);
}
void parse(string[] args)
{
argIter:
foreach (arg; args)
{
if (arg.length == 0)
continue;
if (arg[0] != '+')
{
error(arg);
continue;
}
arg = arg[1..$];
foreach (reg; regs)
{
if (reg.arg.length <= arg.length && reg.arg == arg[0..reg.arg.length])
{
try
{
if (reg.a !is null)
{
reg.a();
}
else
{
reg.b(arg[reg.arg.length..$]);
}
}
catch (Exception e)
{
enforceEx!ParseException(0, format("Failed to parse option %s.\n\nError: %s", reg.arg, e.toString));
}
continue argIter;
}
}
error(arg);
}
}
}
void determineSystemSpecificOptions()
{
version (Windows)
{
/* Walter has admitted to OPTLINK having issues with threading */
globalParams.linkerAffinityMask = getNthAffinityMaskBit(0);
}
}
int main(string[] allArgs)
{
version(Profile)
{
auto argsWatch = StopWatch(AutoStart.yes);
//~ argsWatch.stop();
//~ writefln("--Profiler-- Argument parsing done in %s msecs.", argsWatch.peek.msecs);
}
mutex = new Foo;
determineSystemSpecificOptions();
string[] envArgs;
if (std.process.environment.get("XFBUILDFLAGS"))
{
foreach (flag; split(std.process.environment.get("XFBUILDFLAGS"), " "))
{
if (0 != flag.length)
{
envArgs ~= flag;
}
}
}
globalParams.compilerName = std.process.environment.get("D_COMPILER", "dmd");
if (0 == envArgs.length && 1 == allArgs.length)
{
// wrong invocation, return failure
printHelpAndQuit(1);
}
if (2 == allArgs.length && "+help" == allArgs[1])
{
// standard help screen
printHelpAndQuit(0);
}
bool profiling = false;
string[] args;
string[] mainFiles;
try
{
string[] dirsAndModules;
foreach (arg; allArgs[1..$])
{
if (0 == arg.length)
continue;
if ('-' == arg[0])
{
globalParams.compilerOptions ~= arg;
}
else if ('+' == arg[0])
{
args ~= arg;
}
else
{
if ((arg == "." || arg == "./" || arg == "/" || arg.length > 2)
&& (arg[$ - 2..$] == ".d" || arg[$ - 1] == '/'))
{
dirsAndModules ~= arg;
}
else
{
globalParams.compilerOptions ~= arg;
}
}
}
ArgParser parser;
auto threadsToUse = max(threadsPerCPU, 1);
globalParams.threadsToUse = threadsToUse;
bool quit = false;
bool removeObjs = false;
bool removeDeps = false;
// support for argument stle without assignment (+oa.exe == +o=a.exe)
string oldStyleArg(ref string arg)
{
arg.munch("=");
return arg;
}
parser.bind("full",
{
removeObjs = true;
}
);
parser.bind("clean",
{
removeObjs = true;
quit = true;
}
);
parser.bind("c", (string arg)
{
globalParams.compilerName = oldStyleArg(arg);
}
);
parser.bind("C", (string arg)
{
globalParams.objExt = oldStyleArg(arg);
}
); // HACK: should use profiles/configs instead
parser.bind("O", (string arg)
{
string objPath = oldStyleArg(arg);
globalParams.objPath = buildNormalizedPath(objPath);
}
);
parser.bind("D", (string arg)
{
string depsPath = oldStyleArg(arg);
verifyMakeFilePath(depsPath, "+D");
globalParams.depsPath = depsPath;
}
);
parser.bind("o", (string arg)
{
// todo: have to remove exe if we're regenerating
string outputFile = oldStyleArg(arg);
verifyMakeFilePath(outputFile, "+o");
globalParams.outputFile = buildNormalizedPath(outputFile);
}
);
// major todo: longer arguments with same name must be first in array, otherwise
// they end up being passed as shorter arguments. We have to implement something
// better.
parser.bind("xpath", (string arg)
{
string outPath = oldStyleArg(arg);
globalParams.ignorePaths ~= buildNormalizedPath(outPath);
}
);
parser.bind("x", (string arg)
{
globalParams.ignore ~= oldStyleArg(arg);
}
);
parser.bind("modLimit", (string arg)
{
globalParams.maxModulesToCompile = to!int(oldStyleArg(arg));
}
);
parser.bind("mod-limit=", (string arg)
{
globalParams.maxModulesToCompile = to!int(arg);
}
);
parser.bind("redep", { removeDeps = true;
}
);
parser.bind("v",
{
globalParams.verbose = globalParams.printCommands = true;
}
);
//parser.bind("profile", { profiling = true; });
parser.bind("h",
{
globalParams.manageHeaders = true;
}
);
parser.bind("threads", (string arg)
{
globalParams.threadsToUse = to!int(oldStyleArg(arg));
}
);
parser.bind("no-affinity",
{
globalParams.manageAffinity = false;
}
);
parser.bind("linker-affinity=", (string arg)
{
string x = oldStyleArg(arg);
globalParams.linkerAffinityMask = parse!int(x, 16);
}
);
parser.bind("q",
{
globalParams.useOQ = true;
}
);
parser.bind("noop",
{
globalParams.useOP = false;
}
);
parser.bind("nolink",
{
globalParams.nolink = true;
}
);
parser.bind("rmo",
{
globalParams.reverseModuleOrder = true;
}
);
parser.bind("mbm",
{
globalParams.moduleByModule = true;
}
);
parser.bind("R",
{
globalParams.recursiveModuleScan = true;
}
);
parser.bind("nodeps",
{
globalParams.useDeps = false;
}
);
parser.bind("keeprsp",
{
globalParams.removeRspOnFail = false;
}
);
// remember to parse the XFBUILDFLAGS _before_ args passed in main()
parser.parse(envArgs);
parser.parse(args);
version(Profile)
{
argsWatch.stop();
writefln("--Profiler-- Argument parsing done in %s msecs.", argsWatch.peek.msecs);
}
//------------------------------------------------------------
void _ScanForModules(string[] paths, ref string[] modules, bool recursive = false, bool justCheckAFolder = false)
{
foreach (child; paths)
{
if (child.exists())
{
if (child.isFile)
{
if (child.extension == ".d")
{
// major todo: this used to be .toString
modules ~= child;
}
}
}
else
{
throw new Exception("File not found: " ~ child);
}
}
}
//-----------------------------------------------------------
version(Profile)
{
auto scanWatch = StopWatch(AutoStart.yes);
//~ scanWatch.stop();
//~ writefln("--Profiler-- Argument parsing done in %s msecs.", scanWatch.peek.msecs);
}
_ScanForModules(dirsAndModules, mainFiles, globalParams.recursiveModuleScan);
version(Profile)
{
scanWatch.stop();
writefln("--Profiler-- Module scanning done in %s msecs.", scanWatch.peek.msecs);
}
if ("increBuild" == globalParams.compilerName)
{
globalParams.useOP = true;
globalParams.nolink = true;
}
/+{
if (std.file.exists(globalParams.projectFile) && Path.isFile(globalParams.projectFile)) {
scope json = new Json!(char);
auto jobj = json.parse('{' ~ cast(string)File.get(globalParams.projectFile) ~ '}').toObject.hashmap();
if (auto noHeaders = "noHeaders" in jobj) {
auto arr = (*noHeaders).toArray();
foreach (nh; arr) {
auto modName = nh.toString();
globalParams.noHeaders ~= modName;
}
}
}
}+/
{
bool doWriteDeps = !removeDeps;
auto buildTask = BuildTask(doWriteDeps, mainFiles);
if (!globalParams.objPath.exists)
mkdir(globalParams.objPath);
if (removeObjs)
buildTask.removeObjFiles();
if (removeDeps)
{
try
{
if (globalParams.depsPath.exists)
{
std.file.remove(globalParams.depsPath);
}
}
catch (Exception exc)
{
enforceEx!BuildException(0, format("Couldn't remove deps file %s.", globalParams.depsPath));
}
}
if (quit)
return 0;
enforceEx!ParseException(mainFiles !is null, "At least one Module needs to be specified, see +help.");
buildTask.execute();
}
//});
/+if (profiling) {
scope formatter = new ProfilingDataFormatter;
foreach (row, col, node; formatter) {
char[256] spaces = ' ';
int numSpaces = node.bottleneck ? col-1 : col;
if (numSpaces < 0) numSpaces = 0;
writefln("%s%s%s", node.bottleneck ? "*" : "", spaces[0..numSpaces], node.text);
}
}+/
return 0;
}
catch (BuildException e)
{
writefln("Build failed: %s", e.errorMsg);
return 1;
}
}