1 module Build;
2 
3 import std.algorithm;
4 import std.array;
5 import std.file;
6 import std.getopt;
7 import std.path;
8 import std.process;
9 import std.stdio;
10 import std.string;
11 
12 string dcflags;
13 string ldflags;
14 
15 int main(string[] args)
16 {
17 	version(Posix)
18 	{
19 		writeln("This build script is currently Windows only.");
20 		return(1);
21 	}
22 
23 	getopt(args, "dcflags", &dcflags, "ldflags", &ldflags);
24 	args.popFront();
25 	
26 	foreach ( arg; args )
27 	{
28 		if ( !["gtkd", "gtkdgl", "sv", "gstreamer", "vte", "peas", "all"].canFind(arg) )
29 		{
30 			writefln("Unknown option: %s", arg);
31 			return 1;
32 		}
33 	}
34 	
35 	if ( args.length == 0 )
36 		args = ["gtkd", "gtkdgl", "sv"];
37 		
38 	if ( args.canFind("all") )
39 		args = ["gtkd", "gtkdgl", "sv", "gstreamer", "vte"];
40 	
41 	foreach ( arg; args )
42 	{
43 		switch ( arg )
44 		{
45 			case "gtkd":
46 				build("generated\\gtkd", "gtkd");
47 				break;
48 			case "gtkdgl":
49 				build("generated\\gtkdgl", "gtkdgl");
50 				break;
51 			case "sv":
52 				build("generated\\sourceview", "gtkdsv");
53 				break;
54 			case "gstreamer":
55 				build("generated\\gstreamer", "gstreamerd");
56 				break;
57 			case "vte":
58 				build("generated\\vte", "vted");
59 				break;
60 			case "peas":
61 				build("generated\\peas", "peasd");
62 				break;
63 			default:
64 				assert(false);
65 				break;
66 		}
67 	}
68 	
69 	return(0);
70 }
71 
72 void build(string dir, string lib)
73 {
74 	version(Win64)
75 	{
76 		std.file.write("build.rf", format("-m64 -c -lib %s %s -Igenerated/gtkd -of%s.lib %s", dcflags, ldflags, lib, dFiles(dir)));
77 		executeShell("dmd @build.rf");
78 	}
79 	else
80 	{
81 		if (lib == "gtkd")
82 		{
83 			string files = dFiles(dir);
84 			
85 			ptrdiff_t pivot = indexOf(files, ' ', files.length / 2);
86 
87 			string files2 = files[pivot .. $];
88 			files = files [0 .. pivot];
89 
90 			std.file.write("build.rf", format("-c %s -Igenerated/gtkd -ofgtkd1.obj %s", dcflags, files));
91 			executeShell("dmd @build.rf");
92 			std.file.write("build.rf", format("-c %s -Igenerated/gtkd -ofgtkd2.obj %s", dcflags, files2));
93 			executeShell("dmd @build.rf");
94 
95 			executeShell(format("dmd -lib %s -of%s.lib gtkd1.obj gtkd2.obj", ldflags, lib));
96 
97 			std.file.remove("gtkd1.obj");
98 			std.file.remove("gtkd2.obj");
99 		}
100 		else
101 		{
102 			std.file.write("build.rf", format("-c %s -Igenerated/gtkd -of%s.obj %s", dcflags, lib, dFiles(dir)));
103 			executeShell("dmd @build.rf");
104 			executeShell(format("dmd -lib %s -of%s.lib %s.obj", ldflags, lib, lib));
105 			std.file.remove(lib ~".obj");
106 		}
107 	}
108 	
109 	std.file.remove("build.rf");
110 }
111 
112 string dFiles(string sourceDir)
113 {
114 	string files;
115 	auto entries = dirEntries(sourceDir, SpanMode.breadth);
116 	
117 	foreach ( DirEntry entry; entries )
118 	{
119 		if ( entry.isDir == false && entry.name.extension == ".d" )
120 		{
121 			files ~= entry.name ~ " ";
122 		}
123 	}
124 	
125 	return files;
126 }