-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathbuild.d
857 lines (718 loc) · 22 KB
/
build.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
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
/*
* DSFML - The Simple and Fast Multimedia Library for D
*
* Copyright (c) 2013 - 2017 Jeremy DeHaan ([email protected])
*
* This software is provided 'as-is', without any express or implied warranty.
* In no event will the authors be held liable for any damages arising from the
* use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not claim
* that you wrote the original software. If you use this software in a product,
* an acknowledgment in the product documentation would be appreciated but is
* not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source distribution
*/
module build;
import std.stdio;
import std.file;
import std.process;
import std.algorithm;
import std.array;
import std.getopt;
static if (__VERSION__ < 2068L)
{
static assert(0, "Please upgrade your compiler to v2.068 or later");
}
version(DigitalMars)
{
string compiler = "dmd ";
}
else version(GNU)
{
string compiler = "gdc ";
}
else version(LDC)
{
string compiler = "ldc2 ";
}
else
{
static assert(false, "Unknown or unsupported compiler.");
}
//build settings
string prefix;
string postfix;
string extension;
string linkerInclude;
string libSwitches;
string docSwitches;
string interfaceSwitches;
string unittestSwitches;
//switch settings
bool buildingLibs;
bool buildingInterfaceFiles;
bool buildingDoc;
bool buildingWebsiteDocs;
bool debugLibs;
bool force32Build;
bool force64Build;
bool buildingUnittests;
string unittestLibraryLocation;
bool buildingAll;
bool hasUnrecognizedSwitch;
string unrecognizedSwitch;
string makefileType;
string makefileProgram;
string objExt;
string singleFileSwitches;
string archSwitch;
//Possibly use this to completely automate the process for dmd/ldc users on windows
//environment.get("VCINSTALLDIR");
//will need to find an alternative for gdc users on windows in regards to mingw
version(Windows)
{
}
else version(linux)
{
}
else version(OSX)
{
}
//FreeBSD Support coming soon!
else
{
static assert(false, "DSFML is only supported on OSX, Windows, and Linux.");
}
string[5] modules = ["system", "audio", "network", "window", "graphics"];
string selectedModule;
//lists of d files and c++ object files
string[][string] fileList;
string[string] objectList;
/**
* Checks for any inconsistencies with the passed switchs.
*
* Returns: true if no errors were found, false otherwise.
*/
bool checkSwitchErrors()
{
//can't force both
if(force32Build && force64Build)
{
writeln("Can't use -m32 and -m64 together");
return false;
}
//if other Switchs are used with -all
if(buildingAll)
{
if(buildingLibs || buildingDoc ||
buildingInterfaceFiles || buildingUnittests)
{
writeln("Can't use -all with any other build switches ",
"(-lib, -doc, -import, -unittest)");
return false;
}
}
return true;
}
//initialize all build settings
void initialize()
{
archSwitch = "";
postfix = "";
if(force32Build)
{
archSwitch = " -m32";
}
if(force64Build)
{
archSwitch = " -m64";
}
version (Windows)
{
write("Building for Windows ");
prefix = "";
extension = ".lib";
objExt = ".obj";
//Default to 64 bit on windows because why wouldn't we?
if(!force64Build || !force32Build)
{
archSwitch = " -m64";
}
if(force32Build)
{
archSwitch = " -m32mscoff";
}
makefileProgram = "nmake";
makefileType = `"NMake Makefiles"`;
linkerInclude = "";
}
else
{
version(linux)
write("Building for Linux ");
else
write("Building for OSX ");
prefix = "lib";
extension = ".a";
objExt = ".o";
makefileProgram = "make";
makefileType = `"Unix Makefiles"`;
}
version(DigitalMars)
{
writeln("with dmd");
initializeDMD();
}
else version(GNU)
{
writeln("with gdc");
initializeGDC();
}
else version(LDC)
{
writeln("with ldc");
initializeLDC();
}
// add links to the SFML files for building the unit tests
unittestSwitches ~= lib("sfml-graphics")~lib("sfml-window")~
lib("sfml-audio")~lib("sfml-network")~
lib("sfml-system");
//need to link to c++ standard library on these systems
version(Posix)
{
unittestSwitches ~= lib("stdc++");
}
//Does OSX need to include rpath for unittests?
//populate file lists
fileList["system"] = ["clock", "config", "err", "inputstream", "lock",
"mutex", "package", "sleep", "string", "thread",
"time", "vector2", "vector3"];
fileList["audio"] = ["listener", "music", "package", "sound",
"soundbuffer", "soundbufferrecorder",
"inputsoundfile", "outputsoundfile",
"soundrecorder", "soundsource", "soundstream"];
fileList["network"] = ["ftp", "http", "ipaddress", "package",
"packet", "socket", "socketselector",
"tcplistener", "tcpsocket", "udpsocket"];
fileList["window"] = ["context", "contextsettings", "event", "joystick",
"keyboard", "mouse", "sensor", "touch", "package",
"videomode", "window", "windowhandle"];
fileList["graphics"] = ["blendmode", "circleshape", "color", "convexshape",
"drawable", "font", "glsl", "glyph", "image",
"package", "primitivetype", "rect",
"rectangleshape", "renderstates", "rendertarget",
"rendertexture", "renderwindow", "shader", "shape",
"sprite", "text", "texture", "transform",
"transformable", "vertex", "vertexarray", "view"];
//populate C++ object list
string dir = "src/DSFMLC/System/CMakeFiles/dsfmlc-system.dir/";
objectList["system"] = dir~"Err.cpp"~objExt~" "~
dir~"ErrStream.cpp"~objExt~" "~
dir~"String.cpp"~objExt~" ";
dir = "src/DSFMLC/Audio/CMakeFiles/dsfmlc-audio.dir/";
objectList["audio"] = dir~"InputSoundFile.cpp"~objExt~" "~
dir~"Listener.cpp"~objExt~" "~
dir~"OutputSoundFile.cpp"~objExt~" "~
dir~"Sound.cpp"~objExt~" "~
dir~"SoundBuffer.cpp"~objExt~" "~
dir~"SoundRecorder.cpp"~objExt~" "~
dir~"SoundStream.cpp"~objExt~" ";
dir = "src/DSFMLC/Network/CMakeFiles/dsfmlc-network.dir/";
objectList["network"] = dir~"Ftp.cpp"~objExt~" "~
dir~"Http.cpp"~objExt~" "~
dir~"IpAddress.cpp"~objExt~" "~
dir~"Packet.cpp"~objExt~" "~
dir~"SocketSelector.cpp"~objExt~" "~
dir~"TcpListener.cpp"~objExt~" "~
dir~"TcpSocket.cpp"~objExt~" "~
dir~"UdpSocket.cpp"~objExt~" ";
dir = "src/DSFMLC/Window/CMakeFiles/dsfmlc-window.dir/";
objectList["window"] = dir~"Context.cpp"~objExt~" "~
dir~"Joystick.cpp"~objExt~" "~
dir~"Keyboard.cpp"~objExt~" "~
dir~"Mouse.cpp"~objExt~" "~
dir~"Sensor.cpp"~objExt~" "~
dir~"Touch.cpp"~objExt~" "~
dir~"VideoMode.cpp"~objExt~" "~
dir~"Window.cpp"~objExt~" ";
dir = "src/DSFMLC/Graphics/CMakeFiles/dsfmlc-graphics.dir/";
objectList["graphics"] = dir~"Font.cpp"~objExt~" "~
dir~"Image.cpp"~objExt~" "~
dir~"RenderTexture.cpp"~objExt~" "~
dir~"RenderWindow.cpp"~objExt~" "~
dir~"Shader.cpp"~objExt~" "~
dir~"Texture.cpp"~objExt~" "~
dir~"Transform.cpp"~objExt~" ";
if(debugLibs)
{
singleFileSwitches = " -g " ~ singleFileSwitches;
postfix = "-d";
}
writeln();
}
/// Initialize the DMD compiler
void initializeDMD()
{
singleFileSwitches = archSwitch ~ " -c -O -release -inline -Isrc -of";
libSwitches = archSwitch ~ " -lib -oflib/"~prefix~"dsfml-";
docSwitches = " -c -o- -op -D -Dd../../doc -I../../src";
interfaceSwitches = " -c -o- -op -H -Hd../import -I../src";
unittestSwitches =
archSwitch ~ " -main -unittest -version=DSFML_Unittest_System " ~
"-version=DSFML_Unittest_Window -version=DSFML_Unittest_Graphics " ~
"-version=DSFML_Unittest_Audio -version=DSFML_Unittest_Network " ~
"-ofunittest/unittest";
version (Windows)
{
unittestSwitches ~= ".exe -L/LIBPATH:lib -L/LIBPATH:SFML\\lib ";
}
else
{
linkerInclude = "-L-l";
unittestSwitches ~= " -L-LSFML/lib ";
}
}
/// Initialize the GDC compiler
void initializeGDC()
{
//need to set up for windows and macOS later
singleFileSwitches = archSwitch ~ " -c -O3 -frelease -Isrc -o";
libSwitches = "ar rcs ./lib/libdsfml-";
docSwitches = " -c -fdoc -I../../src";
interfaceSwitches = " -c -fintfc -I../src -o obj.o";
unittestSwitches =
archSwitch ~ " -funittest -fversion=DSFML_Unittest_System " ~
"-fversion=DSFML_Unittest_Window -fversion=DSFML_Unittest_Graphics " ~
"-fversion=DSFML_Unittest_Audio -fversion=DSFML_Unittest_Network " ~
"-ounittest/unittest";
version(linux)
{
linkerInclude = "-l";
unittestSwitches ~= " -LSFML/lib ";
}
}
/// Initialize the LDC compiler
void initializeLDC()
{
string linkToSFMLLibs = "";
singleFileSwitches = archSwitch ~ " -c -O -release -oq -I=src -of=";
libSwitches = archSwitch ~ " -lib -of=lib/"~prefix~"dsfml-";
docSwitches = " -c -o- -op -D -Dd=../../doc -I=../../src";
interfaceSwitches = " -c -o- -op -H -Hd=../import -I=../src";
unittestSwitches =
archSwitch ~ " -main -unittest -d-version=DSFML_Unittest_System " ~
"-d-version=DSFML_Unittest_Window -d-version=DSFML_Unittest_Graphics " ~
"-d-version=DSFML_Unittest_Audio -d-version=DSFML_Unittest_Network " ~
"-of=unittest/unittest";
version(Windows)
{
unittestSwitches ~= ".exe -L=/LIBPATH:lib -L=/LIBPATH:SFML\\lib ";
}
else
{
linkerInclude = "-L=-l";
unittestSwitches ~= " -L=-LSFML/lib ";
}
}
/**
* Build the DSFMLC source files.
*
* Returns: true on successful build, false otherwise.
*/
bool buildDSFMLC()
{
chdir("src/DSFMLC/");
// generate the cmake files on the first run
if(!exists("CMakeCache.txt"))
{
auto pid = spawnShell("cmake -G"~makefileType~" .");
if(wait(pid) != 0)
{
return false;
}
}
//always try to rebuild c++ files. They will be skipped if nothing to do.
auto pid = spawnProcess([makefileProgram]);
if(wait(pid) != 0)
{
return false;
}
writeln();
chdir("../..");
return true;
}
/**
* Build the static libraries.
*
* Returns: true on successful build, false otherwise.
*/
bool buildLibs()
{
import std.ascii: toUpper;
if(!exists("lib/"))
{
mkdir("lib/");
}
if(!buildDSFMLC())
return false;
foreach(theModule;modules)
{
//+1 to include the library file in the count
size_t numberOfFiles = fileList[theModule].length + 1;
string files = "";
string objLocation = "obj/"~(debugLibs?"debug":"release")~
"/dsfml/" ~theModule~"/";
if(!exists(objLocation))
{
mkdirRecurse(objLocation);
}
foreach (fileNumber, name; fileList[theModule])
{
string objectFile = objLocation~name~objExt;
string dFile = "src/dsfml/" ~theModule~"/"~name~".d";
string buildCommand = compiler~dFile~singleFileSwitches~objectFile;
if(needToBuild(objectFile, dFile))
{
progressOutput(fileNumber, numberOfFiles, dFile);
auto status = executeShell(buildCommand);
if(status.status !=0)
{
writeln(status.output);
return false;
}
}
files~= objectFile ~ " ";
}
files ~= objectList[theModule];
string buildCommand = compiler ~ files;
version(GNU)
{
//We're using ar here, so we'll completely reset the build command
buildCommand = libSwitches~theModule~postfix~extension~" "~files;
}
else
{
buildCommand ~= libSwitches~theModule~postfix~extension;
}
//always rebuilds the lib in case the cpp files were re-built
auto status = executeShell(buildCommand);
if(status.status !=0)
{
writeln("Something happened!");
writeln(status.output);
return false;
}
writeln("[100%] Built "~prefix~"dsfml-" ~ theModule~extension);
}
return true;
}
/**
* Build DSFML unit tests.
*
* Returns: true if unit tests could be built, false if not.
*/
bool buildUnittests()
{
import std.ascii: toUpper;
if(!findSFML())
return false;
if(!buildDSFMLC())
return false;
string files = "";
foreach(theModule;modules)
{
foreach(string name; fileList[theModule])
{
files~= "src/dsfml/" ~theModule~"/"~name~".d ";
}
files ~= objectList[theModule];
}
string buildCommand = compiler;
version(GNU)
{
std.file.write("main.d", "void main(){}");
buildCommand ~= "main.d ";
}
buildCommand ~= files~unittestSwitches;
write("Building unittest/unitest");
version(Windows)
writeln(".exe");
else
writeln();
auto status = executeShell(buildCommand);
version(GNU)
{
std.file.remove("main.d");
}
if(status.status !=0)
{
writeln(status.output);
return false;
}
return true;
}
/**
* Build DSFML documentation.
*
* Returns: true if documentation could be built, false if not.
*/
bool buildDocumentation()
{
if(!exists("doc/"))
{
mkdir("doc/");
}
chdir("src/dsfml/");
string docExtension;
string ddoc;
if(buildingWebsiteDocs)
{
docExtension = ".php";
version(GNU)
ddoc = "../../doc/website_documentation.ddoc";
else
ddoc = " ../../doc/website_documentation.ddoc";
}
else
{
docExtension = ".html";
version(GNU)
ddoc = "../../doc/default_ddoc_theme.ddoc";
else
ddoc = " ../../doc/local_documentation.ddoc";
}
foreach(theModule;modules)
{
if(selectedModule != "" && theModule != selectedModule)
{
continue;
}
// skipping package.d
size_t numberOfFiles = fileList[theModule].length -1 ;
foreach (fileNumber, name; fileList[theModule])
{
if (name == "package")
continue;
string docFile = "../../doc/"~theModule~"/"~name~docExtension;
string outputFile = name~docExtension;
string dFile = theModule~"/"~name~".d";
string buildCommand;
version(GNU)
{
buildCommand = compiler~dFile~" -fdoc-inc="~ddoc~
" -fdoc-dir=../../doc/dsfml/"~theModule~
docSwitches~" -o obj.o";
}
else
buildCommand = compiler~dFile~ddoc~docSwitches;
if(needToBuild(docFile, dFile))
{
progressOutput(fileNumber, numberOfFiles, outputFile);
auto status = executeShell(buildCommand);
if(status.status !=0)
{
writeln(status.output);
return false;
}
if(docExtension!=".html")
rename("../../doc/"~theModule~"/"~name~".html",
"../../doc/"~theModule~"/"~name~docExtension);
}
}
}
version(GNU)
core.stdc.stdio.remove("obj.o");
chdir("../..");
return true;
}
/**
* Build DSFML interface files.
*
* Returns: true if documentation could be built, false if not.
*/
bool buildInterfaceFiles()
{
if(!exists("import/"))
{
mkdir("import/");
}
chdir("src/");
foreach(theModule;modules)
{
size_t numberOfFiles = fileList[theModule].length;
foreach (fileNumber, name; fileList[theModule])
{
string dFile = "dsfml/"~theModule~"/"~name~".d";
string outputFile = "../import/dsfml/"~theModule~"/"~name~".di";
if(name == "package")
outputFile.length = outputFile.length - 1;
version(GNU)
{
interfaceSwitches ~= " -fintfc-dir=../import/dsfml/"~theModule;
}
string buildCommand = compiler~dFile~interfaceSwitches;
if(needToBuild(outputFile, dFile))
{
progressOutput(fileNumber, numberOfFiles, outputFile[3 .. $]);
auto status = executeShell(buildCommand);
if(status.status !=0)
{
writeln(status.output);
return false;
}
}
}
if(exists("../import/dsfml/"~theModule~"/package.di"))
{
rename("../import/dsfml/"~theModule~"/package.di",
"../import/dsfml/"~theModule~"/package.d");
}
}
version(GNU)
core.stdc.stdio.remove("obj.o");
chdir("../..");
return true;
}
/**
* Display the progress as a percentage given the current file is next.
*
* Display example:
* [ 20%] Building dsfml/src/system/clock.d
*/
void progressOutput(size_t currentFile, size_t totalFiles, string fileName)
{
size_t percentage = ((currentFile+1)*100)/totalFiles;
writefln("[%3u%%] Building %s", percentage, fileName);
}
/**
* Checks the timestamps on the object file and its associated source file.
*
* If the source file has been more recently updated than the object file was
* built, or if the object file doesn't yet exist, this will return true.
*/
bool needToBuild(string objLocation, string srcLocation)
{
import std.file: exists, timeStamp = timeLastModified;
return exists(objLocation)?timeStamp(objLocation) < timeStamp(srcLocation):
true;
}
/**
* Search for SFML shared libraries in the submodule directory.
*
* Returns: true if SFML shared libraries can be found, false otherwise.
*/
bool findSFML()
{
version(Windows)
{
//technically, we also need .lib files because Windows is stupid, but
//the build script will only look for the .dll's.
string dynamicExtension = "-2.dll";
}
else version(linux)
{
string dynamicExtension = ".so";
}
else
{
string dynamicExtension = ".dylib";
}
//check to make sure ALL SFML libs were built
foreach(theModule; modules)
{
if(!exists("SFML/lib/"~prefix~"sfml-"~theModule~dynamicExtension))
{
writeln("SFML/lib/"~prefix~"sfml-"~theModule~dynamicExtension,
" not found.");
writeln("Building unit tests requires SFML libs in ",
"dsfml/SFML/lib/ directory.");
return false;
}
}
return true;
}
/**
* Builds a string consisting of the linker flags, library name, and extention.
*
* This is to simplify building the list of libraries needed when building unit
* tests.
*
*/
string lib(string library)
{
version(Windows)
return linkerInclude~library~extension~" ";
else
return linkerInclude~library~" ";
}
int main(string[] args)
{
GetoptResult optInfo;
try
{
optInfo = getopt(args,
"lib", "Build static libraries.", &buildingLibs,
"m32", "Force 32 bit building.", &force32Build,
"m64", "Force 64 bit building.", &force64Build,
"unittest", "Build DSFML unit test executable.", &buildingUnittests,
"doc", "Build DSFML documentation.", &buildingDoc,
"webdoc", "Build the DSFML website documentation.", &buildingWebsiteDocs,
"import", "Generate D interface files.", &buildingInterfaceFiles,
"debug", "Build debug libraries (ignored when not building libraries).", &debugLibs
);
}
catch(GetOptException e)
{
writeln(e.msg);
return -1;
}
if(optInfo.helpWanted)
{
defaultGetoptPrinter("Switch Information\n"
~"Default (no switches passed) will be to build static libraries with the compiler that built this.",
optInfo.options);
return 0;
}
//default to building libs
if(!buildingLibs && !buildingDoc && !buildingInterfaceFiles &&
!buildingWebsiteDocs && !buildingUnittests && !buildingAll)
{
buildingLibs = true;
}
if(!checkSwitchErrors())
{
return -1;
}
writeln();
initialize();
if(buildingLibs)
{
if(!buildLibs())
return -1;
}
if(buildingUnittests)
{
if(!buildUnittests())
return -1;
}
if(buildingDoc || buildingWebsiteDocs)
{
if(!buildDocumentation())
return -1;
}
if(buildingInterfaceFiles)
{
if(!buildInterfaceFiles())
return -1;
}
return 0;
}