forked from DlangScience/scid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.d
executable file
·241 lines (195 loc) · 6.04 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
#!/usr/bin/env dmd -run
/** Build system for SciD
This is an experimental build system for SciD, using a regular D
script instead of a makefile.
Usage:
To build the library and generate header (.di) files, run
---
rdmd build
---
To build only the library file, run
---
rdmd build lib
---
To only generate header files, run
---
rdmd build headers
---
To make the documentation, run
---
rdmd build html
---
Any command line arguments beyond the first one will be passed
on to the compiler. For example, to enable optimisations and
inlining when building the library, run
---
rdmd build lib -O -inline
---
*/
import std.algorithm, std.array, std.exception, std.file, std.getopt, std.path,
std.process, std.stdio, std.string, std.zip;
/** Various build directories.
NOTE:
Running "build clean" will recursively and mercilessly
delete these directories. Make sure you don't use the current
directory, the root directory or any such thing. Preferably,
just leave them the way they are.
*/
version (Posix)
{
immutable libDir = "generated"; // Location of lib file
immutable headerDir = "generated/headers"; // Location of .di files
immutable htmlDir = "generated/html"; // Location of .html files
}
version (Windows)
{
immutable libDir = r"generated";
immutable headerDir = r"generated\headers";
immutable htmlDir = r"generated\html";
}
/** The name of the library. */
immutable libName = "scid";
/** The top-level directory of the source files. */
immutable srcDir = "scid";
int main(string[] args)
in { assert (args.length > 0); }
body
{
bool gdc;
string compiler = "dmd";
getopt(args, std.getopt.config.passThrough, "gdc", &gdc);
if(gdc)
compiler = "gdmd";
try
{
if (args.length <= 1)
{
buildLib(compiler, null);
buildHeaders(compiler, null);
}
else if (args[1][0] == '-')
{
buildLib(compiler, args[1 .. $]);
buildHeaders(compiler, args[1 .. $]);
}
else if (args[1] == "lib") buildLib(compiler, args[2 .. $]);
else if (args[1] == "headers") buildHeaders(compiler, args[2 .. $]);
else if (args[1] == "html") buildHTML(compiler, args[2 .. $]);
else if (args[1] == "clean") buildClean();
else enforce(false, "Unknown command: " ~ args[1]);
return 0;
}
catch (Exception e)
{
stderr.writeln(e.msg);
return 1;
}
}
/** Build the library file. */
void buildLib(string compiler, string[] extraOptions)
{
ensureDir(libDir);
auto sources = getSources();
version (Posix) immutable libFile = "lib"~libName~".a";
version (Windows) immutable libFile = libName~".lib";
immutable buildCmd = compiler ~ " "
~std.string.join(sources, " ")
~" -property -w -lib -od"~libDir~" -of"~libFile
~" "~std.string.join(extraOptions, " ");
writeln(buildCmd);
enforce(system(buildCmd) == 0, "Error building library");
}
/** Generate header files. */
void buildHeaders(string compiler, string[] extraOptions)
{
ensureDir(headerDir);
auto sources = getSources();
foreach (s; sources)
{
immutable diPath = buildPath(headerDir, s).setExtension(".di");
ensureDir(dirName(diPath));
immutable cmd = compiler~" "~s~" -c -o- -H -Hf"~diPath
~" "~std.string.join(extraOptions, " ");
writeln(cmd);
enforce(system(cmd) == 0, "Error making header file: "~baseName(diPath));
}
}
/** Build documentation. */
void buildHTML(string compiler, string[] extraOptions)
{
auto sources = getSources();
sort(sources);
ensureDir(htmlDir);
unzip("candydoc.zip", htmlDir);
string[] htmlFiles;
string moduleList = "MODULES =\n";
foreach (s; sources)
{
version (Posix) auto slash = "/";
version (Windows) auto slash = "\\";
htmlFiles ~= baseName(replace(s, slash, "_"),".d") ~ ".html";
// Do not list the scid.internal.* modules in the
// doc browser tree.
if (std.string.indexOf(s, "internal") == -1)
moduleList ~=
"\t$(MODULE "
~baseName(replace(s, slash, "."), ".d")
~")\n";
}
immutable modulesDdoc = buildPath(htmlDir, "candydoc", "modules.ddoc");
writeln("Writing "~modulesDdoc);
std.file.write(modulesDdoc, moduleList);
immutable candyDdoc = buildPath(htmlDir, "candydoc", "candy.ddoc");
foreach (i; 0 .. sources.length)
{
immutable cmd =
compiler~" "~sources[i]~" "~candyDdoc~" "~modulesDdoc
~" -c -o- -D -Dd"~htmlDir~" -Df"~htmlFiles[i]
~" "~std.string.join(extraOptions, " ");
writeln(cmd);
enforce(system(cmd) == 0, "Error making HTML file: "~htmlFiles[i]);
}
}
/** Remove build directories. */
void buildClean()
{
void rm(string path)
{
if (!exists(path)) return;
writeln("Removing: ", path);
if (isDir(path)) rmdirRecurse(path);
else std.file.remove(path);
}
rm(libDir);
rm(headerDir);
rm(htmlDir);
rm(__FILE__~".deps"); // Clean up after rdmd as well
}
// Various utility functions
string[] getSources()
{
static string[] sources;
if (sources == null)
{
foreach (string f; dirEntries(srcDir, SpanMode.depth))
if (isFile(f) && extension(f) == ".d") sources ~= f;
}
return sources;
}
void ensureDir(string dir)
{
if (exists(dir)) enforce(isDir(dir), "Not a directory: "~dir);
else mkdirRecurse(dir);
}
void unzip(string zipFile, string toDir)
{
writeln("Unzipping "~zipFile~" to "~toDir);
auto zip = new ZipArchive(std.file.read(zipFile));
foreach (member; zip.directory)
{
if (member.name[$-1] == '/') continue; // Skip directory names
immutable f = buildPath(toDir, member.name);
ensureDir(dirName(f));
std.file.write(f, zip.expand(member));
}
}