This repository has been archived by the owner on Apr 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
session.d
executable file
·782 lines (682 loc) · 24.7 KB
/
session.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
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
/*
Copyright (c) 2013-2014 Timur Gafarov
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
module session;
import std.stdio;
import std.path;
import std.file;
import std.string;
import std.datetime;
import std.conv;
import std.digest.crc;
import std.process: executeShell;
import std.algorithm;
static import std.process;
import core.stdc.stdlib;
import cmdopt;
import conf;
import lexer;
import dmodule;
import project;
import exdep;
class BuildSession
{
CmdOptions ops;
Config config;
string[] versionIds;
string[] debugIds;
string[] externals;
bool[string] scanned;
// Quit at any time without throwing an exception
void quit(int code, string message = "")
{
if (message.length > 0)
writefln("Cook session failed: %s", message);
if (ops._debug_)
printConfig();
version(Windows)
executeShell("pause");
core.stdc.stdlib.exit(code);
}
void printConfig()
{
writeln("Configuration:");
string[] confContents;
foreach(i, v; config.data)
{
string f = formatPattern(v, config, '%');
confContents ~= std.string.format(" %s: %s", i, f);
}
confContents.sort;
foreach(i, v; confContents)
writeln(v);
}
this(CmdOptions cmdops, Config conf)
{
ops = cmdops;
config = conf;
prepare();
}
void prepare()
{
// Set default configuration keys
string tempTarget = "main";
config.set("profile", "default", false);
config.set("prephase", "", false);
config.set("postphase", "", false);
config.set("source.language", "d", false);
config.set("source.ext", ".d", false);
config.set("compiler", "dmd", false);
config.set("linker", "dmd", false);
version(Windows) config.set("librarian", "lib", false);
version(linux) config.set("librarian", "dmd", false);
config.set("cflags", "", false);
config.set("lflags", "", false);
//config.set("modules", ""); // TODO?
config.set("obj.path", "", false);
config.set("obj.path.use", "true", false);
config.set("obj.ext", "", false);
config.set("modules.main", "main", false);
config.set("modules.forced", "", false);
config.set("target", "", false);
config.set("rc", "", false);
config.set("modules.cache", "main.cache", false);
version(Windows)
{
config.set("obj.path", "o_windows/");
config.set("obj.ext", ".obj");
}
version(linux)
{
config.set("obj.path", "o_linux/");
config.set("obj.ext", ".o");
config.append("lflags", "-L-rpath -L\".\" -L-ldl ");
}
config.set("modules.maindir", ".");
if (ops.targets.length)
{
// FIXME: currently we use only first given target
string mainModule = ops.targets[0];
config.set("modules.main", mainModule);
// strip extension, if any
if (extension(config.get("modules.main")) == ".d")
config.set("modules.main", config.get("modules.main")[0..$-2]);
string maindir = dirName(config.get("modules.main"));
config.set("modules.maindir", maindir);
}
// Set cache filename
string cacheFilePostfix = "-" ~ config.get("profile");
version(Windows)
{
cacheFilePostfix ~= "-windows.cache";
}
version(linux)
{
cacheFilePostfix ~= "-linux.cache";
}
tempTarget = baseName(config.get("modules.main"));
config.set("modules.cache", config.get("modules.main") ~ cacheFilePostfix);
// Set responce file name
string rspFilePostfix = "-" ~ config.get("profile");
version(Windows)
{
rspFilePostfix ~= "-windows.rsp";
}
version(linux)
{
rspFilePostfix ~= "-linux.rsp";
}
config.set("modules.rsp", config.get("modules.main") ~ rspFilePostfix);
config.set("project.compile", "%compiler% %cflags% -c %source% -of%object%", false);
if (ops.rsp)
config.set("project.link", "%linker% %lflags% -of%target% %packages% @" ~ config.get("modules.rsp"), false);
else
config.set("project.link", "%linker% %lflags% -of%target% %objects% %packages%", false);
if (ops.rsp)
{
version(Windows) config.set("project.linklib", "%librarian% %lflags% -c -p32 -of%target% @" ~ config.get("modules.rsp"), false);
version(linux) config.set("project.linklib", "%librarian% %lflags% -lib -of%target% @" ~ config.get("modules.rsp"), false);
version(Windows) config.set("project.linkpkg", "%librarian% %lflags% -c -p32 -of%package% @" ~ config.get("modules.rsp"), false);
version(linux) config.set("project.linkpkg", "%librarian% %lflags% -lib -of%package% @" ~ config.get("modules.rsp"), false);
}
else
{
version(Windows) config.set("project.linklib", "%librarian% %lflags% -c -p32 -of%target% %objects%", false);
version(linux) config.set("project.linklib", "%librarian% %lflags% -lib -of%target% %objects%", false);
version(Windows) config.set("project.linkpkg", "%librarian% %lflags% -c -p32 -of%package% %objects%", false);
version(linux) config.set("project.linkpkg", "%librarian% %lflags% -lib -of%package% %objects%", false);
}
version(linux) config.set("project.run", "./%target%", false);
version(Windows) config.set("project.run", "%target%", false);
config.set("project.packages", "", false);
if (ops.output.length)
{
tempTarget = ops.output;
}
if (ops.clean)
{
if (exists(config.get("modules.cache")))
std.file.remove(config.get("modules.cache"));
if (exists(config.get("modules.rsp")))
std.file.remove(config.get("modules.rsp"));
// FIXME: use config for these
if (exists("o_linux")) rmdirRecurse("o_linux");
if (exists("o_windows")) rmdirRecurse("o_windows");
quit(0);
}
if (ops.cache.length)
config.set("modules.cache", ops.cache);
if (ops.rc.length)
config.set("rc", ops.rc);
if (ops.cflags.length)
config.append("cflags", ops.cflags ~ " ");
if (ops.lflags.length)
config.append("lflags", ops.lflags ~ " ");
if ("./" ~ tempTarget != ops.program)
config.set("target", tempTarget);
else
quit(1, "illegal target name: \"" ~ tempTarget ~ "\" (conflicts with Cook executable)");
version(Windows)
{
if (ops.noconsole)
config.append("lflags", "-L/exet:nt/su:windows ");
}
// Read user-wide default configuration
string userConfigFilename = home(".cook/default.conf", ops);
if (exists(userConfigFilename))
{
readConfiguration(config, userConfigFilename);
}
// Read default configuration
string defaultConfigFilename = "default.conf";
if (exists(defaultConfigFilename))
{
readConfiguration(config, defaultConfigFilename);
}
// Read project configuration
if (ops.conf.length)
{
if (exists(ops.conf))
readConfiguration(config, ops.conf);
}
else
{
string configFilename = "./" ~ config.get("target") ~ ".conf";
if (exists(configFilename))
readConfiguration(config, configFilename);
}
config.append("cflags", " -I%modules.maindir% ");
if (ops.release)
config.append("cflags", " -release -O -inline -noboundscheck ");
}
void build(Project proj)
{
string externalsStr = config.get("project.external");
if (externalsStr.length)
externals = split(externalsStr);
setVersionIds(proj);
if (ops.rebuild)
{
writeln("Do you really want to rebuild the project? (Y/N)");
bool rebuild = false;
bool confirmed = false;
while(!confirmed)
{
string input = readln();
if (input.length)
{
if (input[0] == 'Y' || input[0] == 'y')
{
rebuild = true;
confirmed = true;
}
else if (input[0] == 'N' || input[0] == 'n')
{
confirmed = true;
}
}
if (!confirmed)
writeln("Please, write Y or N");
}
if (!rebuild)
ops.rebuild = false;
}
if (!ops.rebuild)
readCache(proj);
scanProjectHierarchy(proj);
traceBackwardDependencies(proj);
addForcedModules(proj);
writeCache(proj);
if (ops.dump.length)
{
if (ops.dump in proj.modules)
{
auto m = proj.modules[ops.dump];
writefln("Filename: %s", m.filename);
writefln("Package name: %s", m.packageName);
writefln("Last modified: %s", m.lastModified);
writefln("Imports: %s", m.importedFiles);
writefln("Version ids: %s", m.versionIds);
writefln("Debug ids: %s", m.debugIds);
}
}
doPrephase();
compileAndLink(proj);
strip();
run();
if (ops._debug_)
printConfig();
}
void setVersionIds(Project proj)
{
versionIds = split(config.get("version"));
debugIds = split(config.get("debug"));
foreach(v; versionIds)
{
proj.versionIds[v] = 1;
if (v == "Wine")
proj.versionIds["Windows"] = 1;
}
foreach(v; debugIds)
proj.debugIds[v] = 1;
string ver;
foreach(i, v; versionIds)
{
ver ~= " -version=" ~ v;
}
if (ver.length)
config.append("cflags", ver);
string deb;
foreach(i, v; debugIds)
{
deb ~= " -debug=" ~ v;
}
if (deb.length)
config.append("cflags", deb);
}
void readCache(Project proj)
{
string cacheFile = config.get("modules.cache");
if (exists(cacheFile) && !ops.nocache)
{
string cache = readText(cacheFile);
foreach (line; splitLines(cache))
{
auto tokens = split(line);
auto m = proj.addModule(tokens[0]);
m.lastModified = SysTime.fromISOExtString(tokens[1]);
m.globalFile = cast(bool)to!uint(tokens[2]);
foreach (i; tokens[3..$])
m.addImportFile(i);
}
}
}
void scanProjectHierarchy(Project proj)
{
proj.mainModuleFilename = config.get("modules.main") ~ config.get("source.ext");
if (exists(proj.mainModuleFilename))
{
scanDependencies(proj, proj.mainModuleFilename);
}
else
quit(1, "no main module found");
if (proj.modules.length == 0)
quit(1, "no source files found");
}
void scanDependencies(Project proj, string fileName, bool globalFile = false)
{
scanned[fileName] = true;
DModule m;
if (!exists(fileName))
return;
// if it is a new module
if (!(fileName in proj.modules))
{
if (!ops.quiet) writefln("Analyzing \"%s\"...", fileName);
m = proj.addModule(fileName);
m.globalFile = globalFile;
m.lastModified = timeLastModified(fileName);
if (!m.buildDependencyList())
quit(1, "cannot build proper dependency list");
m.packageName = pathToModule(dirName(fileName));
scanModule(proj, m);
}
else // if we already have it
{
m = proj.modules[fileName];
//TODO: cache this
m.packageName = pathToModule(dirName(fileName));
immutable auto lm = timeLastModified(fileName);
if (lm > m.lastModified)
{
if (!ops.quiet) writefln("Analyzing \"%s\"...", fileName);
m.lastModified = lm;
if (!m.buildDependencyList())
quit(1, "cannot build proper dependency list");
m.forceRebuild = true;
scanModule(proj, m);
}
}
foreach (mName; m.importedFiles)
if (!(mName in scanned))
{
scanDependencies(proj, mName);
}
}
bool existsInExternals(
string filename,
ref string externalFilename)
{
foreach(e; externals)
{
string f = e ~ "/" ~ filename;
if (exists(f))
{
externalFilename = f;
return true;
}
}
return false;
}
void scanModule(Project proj, DModule m)
{
foreach(importedFile; m.importedFiles)
{
string subprojectFile = importedFile;
if (config.get("modules.maindir") != ".")
subprojectFile =
config.get("modules.maindir") ~ "/" ~ importedFile;
string externalFile;
if (exists(importedFile))
scanDependencies(proj, importedFile);
else if (exists(subprojectFile))
scanDependencies(proj, subprojectFile);
else if (existsInExternals(subprojectFile, externalFile))
scanDependencies(proj, externalFile, true);
else
{
// Treat it as package import (<importedFile>/package.d)
string pkgFile =
stripExtension(importedFile) ~ "/"
~ moduleToPath("package", config.get("source.ext"));
string subprojectPkgFile =
config.get("modules.maindir") ~ "/"
~ pkgFile;
if (exists(pkgFile))
scanDependencies(proj, pkgFile);
else if (exists(subprojectPkgFile))
scanDependencies(proj, subprojectPkgFile);
else if (existsInExternals(pkgFile, externalFile))
scanDependencies(proj, externalFile, true);
}
}
}
void traceBackwardDependencies(Project proj)
{
if (!ops.nobacktrace)
{
foreach(modulei, modulev; proj.modules)
{
foreach (mName; modulev.importedFiles)
{
if (mName in proj.modules)
{
auto imModule = proj.modules[mName];
imModule.backdeps[modulei] = modulev;
}
}
}
foreach(mName, m; proj.modules)
{
foreach(i, v; m.backdeps)
{
v.forceRebuild = v.forceRebuild || m.forceRebuild;
}
}
}
}
void addForcedModules(Project proj)
{
foreach(fileName; split(config.get("modules.forced")))
{
if (exists(fileName))
{
DModule m = proj.addModule(fileName);
m.lastModified = timeLastModified(fileName);
if (!m.buildDependencyList())
quit(1, "cannot build proper dependency list");
foreach(importedFile; m.importedFiles)
{
if (exists(importedFile))
scanDependencies(proj, importedFile);
}
}
}
}
void doPrephase()
{
uint retcode;
string prephase = formatPattern(config.get("prephase"), config, '%');
if (prephase != "")
{
if (!ops.quiet)
writeln(prephase);
if (!ops.emulate)
{
retcode = executeShell(prephase).status;
if (retcode)
quit(1, "Prephase error");
}
}
}
void writeCache(Project proj)
{
string cache;
foreach (i, m; proj.modules)
{
//TODO: cache module's package also
cache ~= i ~ " " ~ m.toString() ~ "\n";
}
if (!ops.emulate)
if (!ops.nocache)
std.file.write(config.get("modules.cache"), cache);
}
void writeResponceFile(string rsp)
{
if (!ops.emulate)
if (ops.rsp)
std.file.write(config.get("modules.rsp"), rsp);
}
void compileAndLink(Project proj)
{
string[] pkgList = split(config.get("project.packages"));
string linkList;
// Compile modules
if (config.get("obj.path") != "")
if (!exists(config.get("obj.path")))
mkdir(config.get("obj.path"));
bool terminate = false;
foreach (i, v; proj.modules)
{
if (!terminate && exists(i))
{
string targetObjectName = i;
string tobjext = extension(targetObjectName);
targetObjectName = targetObjectName[0..$-tobjext.length] ~ config.get("obj.ext");
string targetObject;
if (v.globalFile)
{
string hash = crc32Of(targetObjectName).crcHexString;
targetObject = home(format(".cook/obj/%s/%s%s",
config.get("profile"),
hash,
config.get("obj.ext")), ops);
}
else
targetObject = config.get("obj.path") ~ "/" ~ targetObjectName;
if ((timeLastModified(i) > timeLastModified(targetObject, SysTime.min))
|| v.forceRebuild
|| ops.rebuild)
{
if (config.get("obj.path.use") == "false")
targetObject = targetObjectName;
config.set("source", i);
config.set("object", targetObject);
string command = formatPattern(config.get("project.compile"), config, '%');
if (!ops.quiet)
writeln(command);
if (!ops.emulate)
{
immutable auto retcode = executeShell(command).status;
if (retcode)
terminate = true;
}
}
if (config.get("obj.path.use") == "false")
targetObject = targetObjectName;
if (!matches(v.packageName, pkgList))
linkList ~= targetObject ~ " ";
}
}
// If compilation error occured
if (terminate)
{
quit(1, "compilation error");
}
// Compile resource file, if any
version(Windows)
{
if (config.get("rc").length > 0)
{
string res = stripExtension(config.get("rc")) ~ ".res ";
string command = "windres -i " ~ config.get("rc") ~ " -o " ~ res ~ "-O res";
if (!ops.quiet)
writeln(command);
if (!ops.emulate)
{
immutable auto retcode = executeShell(command).status;
if (retcode)
quit(1);
}
config.append("lflags", res);
}
}
// Link packages, if any
// WARNING: alpha stage, needs work!
// TODO: do not relink a package, if it is unchanged
// TODO: do not create a package, if it is empty
string pkgLibList;
foreach(pkg; pkgList)
{
string pkgLinkList;
foreach(i, m; proj.modules)
{
if (m.packageName == pkg)
{
string targetObjectName = i;
string tobjext = extension(targetObjectName);
targetObjectName = targetObjectName[0..$-tobjext.length] ~ config.get("obj.ext");
string targetObject = config.get("obj.path") ~ targetObjectName;
pkgLinkList ~= targetObject ~ " ";
}
}
//TODO: pkgext should be a configuration key
string pkgExt;
version(Windows) pkgExt = ".lib";
version(linux) pkgExt = ".a";
config.set("objects", pkgLinkList);
config.set("package", pkg ~ pkgExt);
string command = formatPattern(config.get("project.linkpkg"), config, '%');
writeResponceFile(pkgLinkList);
if (!ops.quiet)
writeln(command);
if (!ops.emulate)
{
immutable auto retcode = executeShell(command).status;
if (retcode)
quit(1, "package linking error");
}
pkgLibList ~= config.get("package") ~ " ";
}
// Link
config.set("objects", linkList);
config.set("packages", pkgLibList);
if (ops.lib)
{
version(Windows) config.append("target", ".lib");
version(linux) config.append("target", ".a");
writeResponceFile(linkList);
string command = formatPattern(config.get("project.linklib"), config, '%');
if (!ops.quiet)
writeln(command);
if (!ops.emulate)
{
immutable auto retcode = executeShell(command).status;
if (retcode)
quit(1, "linking error");
}
}
else
{
version(Windows) config.append("target", ".exe");
writeResponceFile(linkList);
string command = formatPattern(config.get("project.link"), config, '%');
if (!ops.quiet)
writeln(command);
if (!ops.emulate)
{
immutable auto retcode = executeShell(command).status;
if (retcode)
quit(1, "linking error");
}
}
}
void strip()
{
if (ops.strip)
{
version(linux)
{
string command = "strip " ~ config.get("target");
if (!ops.quiet)
writeln(command);
if (!ops.emulate)
std.process.system(command);
}
}
}
void run()
{
if (ops.run && !ops.lib)
{
if (!ops.emulate)
{
string command = formatPattern(config.get("project.run"), config, '%');
if (!ops.quiet)
writeln(command);
if (!ops.emulate)
executeShell(command);
}
}
}
}