-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.d
executable file
·185 lines (153 loc) · 4.26 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
#!/usr/bin/env rdmd
import std.range;
import std.algorithm;
import std.stdio;
import std.file;
import std.path;
import std.process;
import std.parallelism;
enum sourcePath = "source";
int run(string[] args) {
return wait(spawnProcess(args));
}
/**
* Given a filename and a new root directory, replace the
* root directory in the filename with the given root directory.
*/
@safe pure nothrow
string replaceRoot(string inFilename, string newRoot) {
return buildPath(chain(
only(newRoot),
pathSplitter(inFilename).dropOne()
));
}
@safe pure nothrow
string objectFilenameForSource(string sourceFilename) {
return sourceFilename
.replaceRoot("obj")
.stripExtension
~ ".o";
}
string compileSource(string sourceFilename, string objectFilename) {
if (exists(objectFilename)) {
// Don't generate this again if we've already got it.
return objectFilename;
}
mkdirRecurse(dirName(objectFilename));
string[] commandLine;
if (sourceFilename.endsWith(".cpp")) {
commandLine = [
"g++",
"-c", sourceFilename,
"-o", objectFilename,
// TODO: Determine what the path for this is and configure it.
"-I/usr/include/qt4/"
];
} else {
commandLine = [
"dmd",
"-c", sourceFilename,
"-of" ~ objectFilename,
// TODO: Turn this off after clearing warnings.
"-d",
"-Isource",
"-I../dsmoke/source",
"-I../dstruct/source",
];
}
writeln(commandLine.join(" "));
run(commandLine);
return objectFilename;
}
void linkObjects(immutable(string[]) objectList, string outputFilename) {
string[] linkCommandline = cast(string[]) chain(
[
"dmd",
"-lib",
"-of" ~ outputFilename,
],
objectList
).array;
if (run(linkCommandline)) {
throw new Exception("Linking failed!");
};
}
int main(string[] argv) {
import std.getopt;
bool cleanBuild = false;
bool showHelp = false;
try {
getopt(
argv,
"clean", &cleanBuild,
"help", &showHelp,
);
} catch (Exception ex) {
stderr.writeln(ex.msg);
return 1;
}
if (showHelp || argv.length > 1) {
enum string[] usage = [
"rdmd build.d [--clean]",
"",
"Build the DQt library.",
"",
" --clean Clean up build files first.",
" --help Print this help message.",
];
foreach(line; usage) {
stderr.writeln(line);
}
return 1;
}
// Build the code generator first.
chdir("generator");
if (run(["dub", "build", "-q"])) {
stderr.writeln("Building the code generator failed!");
return 1;
}
chdir("..");
if (!exists(sourcePath)) {
mkdir(sourcePath);
}
// Run that code generator now.
if (run([buildPath("generator", "bin", "dqt_generator")])) {
stderr.writeln("Running the code generator failed!");
return 1;
}
// Find all the source files now we've generated them.
immutable sourceList = cast(immutable)
dirEntries(sourcePath, SpanMode.breadth)
.filter!(x => isFile(x))
.map!(x => x.name)
.filter!(x => x.endsWith(".d") || x.endsWith(".cpp"))
.array;
if (!exists("obj")) {
mkdir("obj");
}
// Get all of the destination filenames for the objects.
immutable objectList = cast(immutable)
sourceList
.map!objectFilenameForSource
.array;
if (cleanBuild) {
// Remove all objects first in a clean build.
foreach(filename; objectList.filter!exists) {
remove(filename);
}
}
// Build all of the sources.
foreach(i, args; parallel(zip(sourceList, objectList))) {
auto sourceFilename = args[0];
auto objectFilename = args[1];
compileSource(sourceFilename, objectFilename);
}
// Link the object code into the final library.
try {
linkObjects(objectList, buildPath("lib", "libdqt.a"));
} catch (Exception ex) {
stderr.writeln(ex.msg);
return 1;
}
return 0;
}