1 |
// Written in The D Programming Language |
---|
2 |
/// Script to build DDMD |
---|
3 |
import std.file; |
---|
4 |
import std.getopt; |
---|
5 |
import std.path; |
---|
6 |
import std.process; |
---|
7 |
import std.stdio; |
---|
8 |
import std.string: replace, format; |
---|
9 |
import std.zip; |
---|
10 |
|
---|
11 |
enum dmdVersionDefault = "2.039"; |
---|
12 |
enum setupDoneFile = ".setup-done"; |
---|
13 |
enum dmdArchiveBaseURL = "http://ftp.digitalmars.com/"; |
---|
14 |
version(Windows) |
---|
15 |
{ |
---|
16 |
enum scriptName = "build.bat"; |
---|
17 |
enum osSubDir = "windows\\"; |
---|
18 |
enum configFile = "sc.ini"; |
---|
19 |
enum execExt = ".exe"; |
---|
20 |
} |
---|
21 |
else |
---|
22 |
{ |
---|
23 |
enum scriptName = "./build.sh"; |
---|
24 |
enum osSubDir = "linux/"; |
---|
25 |
enum configFile = "dmd.conf"; |
---|
26 |
enum execExt = ""; |
---|
27 |
} |
---|
28 |
|
---|
29 |
string dmdVersion; |
---|
30 |
string dmdPackage; |
---|
31 |
string dmdArchive; |
---|
32 |
string dmBase=""; |
---|
33 |
bool shouldDownload; |
---|
34 |
|
---|
35 |
void doCopy(string from, string to) |
---|
36 |
{ |
---|
37 |
from = normFilePath(from); |
---|
38 |
to = normFilePath(to); |
---|
39 |
|
---|
40 |
writefln(`copy "%s" "%s"`, from, to); |
---|
41 |
copy(from, to); |
---|
42 |
} |
---|
43 |
|
---|
44 |
void doChDir(string dir) |
---|
45 |
{ |
---|
46 |
dir = normDirPath(dir); |
---|
47 |
|
---|
48 |
writefln(`chdir "%s"`, dir); |
---|
49 |
chdir(dir); |
---|
50 |
} |
---|
51 |
|
---|
52 |
int doSystem(string cmd) |
---|
53 |
{ |
---|
54 |
writeln(cmd); |
---|
55 |
return system(cmd); |
---|
56 |
} |
---|
57 |
|
---|
58 |
void copyRecurse(string from, string to) |
---|
59 |
{ |
---|
60 |
from = normDirPath(from)[0..$-1]; |
---|
61 |
to = normDirPath(to)[0..$-1]; |
---|
62 |
version(Windows) |
---|
63 |
doSystem(`xcopy "%s" "%s" /E /I /Y /Q`.format(from, to)); |
---|
64 |
else |
---|
65 |
doSystem(`cp '%s' '%s' -r`.format(from, to)); |
---|
66 |
} |
---|
67 |
|
---|
68 |
void copyAndPatch(string from, string to, void delegate(ref string) patcher) |
---|
69 |
{ |
---|
70 |
auto data = cast(string)read(from); |
---|
71 |
patcher(data); |
---|
72 |
std.file.write(to, data); |
---|
73 |
} |
---|
74 |
|
---|
75 |
/// makePath("C:\foo\bar\dir") will create "C:\foo\bar\dir" if it doesn't already exist. |
---|
76 |
/// makePath("C:\foo\bar\dir\") will create "C:\foo\bar\dir" if it doesn't already exist. |
---|
77 |
void makePath(string dir) |
---|
78 |
{ |
---|
79 |
dir = normDirPath(dir)[0..$-1]; |
---|
80 |
if(!exists(dir)) |
---|
81 |
mkdirRecurse(dir); |
---|
82 |
} |
---|
83 |
|
---|
84 |
/// makePathTo("C:\foo\bar\file.txt") will create "C:\foo\bar\" if it doesn't already exist. |
---|
85 |
/// makePathTo("C:\foo\bar\dir\") will create "C:\foo\bar\" if it doesn't already exist. |
---|
86 |
void makePathTo(string file) |
---|
87 |
{ |
---|
88 |
file = normFilePath(file); |
---|
89 |
auto dir = dirname(file); |
---|
90 |
makePath(dir); |
---|
91 |
} |
---|
92 |
|
---|
93 |
/// Ensure trailing slash and OS-correct path separators |
---|
94 |
string normDirPath(string str) |
---|
95 |
{ |
---|
96 |
str = normPathSep(str); |
---|
97 |
if(str.length > 0 && str[$-1] != sep[0]) |
---|
98 |
str ~= sep; |
---|
99 |
|
---|
100 |
return str; |
---|
101 |
} |
---|
102 |
|
---|
103 |
/// Ensure no trailing slash and OS-correct path separators |
---|
104 |
string normFilePath(string str) |
---|
105 |
{ |
---|
106 |
str = normPathSep(str); |
---|
107 |
if(str.length > 0 && str[$-1] == sep[0]) |
---|
108 |
str = str[0..$-1]; |
---|
109 |
|
---|
110 |
return str; |
---|
111 |
} |
---|
112 |
|
---|
113 |
/// Ensure OS-correct path separators |
---|
114 |
string normPathSep(string str) |
---|
115 |
{ |
---|
116 |
version(Windows) |
---|
117 |
str = str.replace("/", "\\"); |
---|
118 |
else |
---|
119 |
str = str.replace("\\", "/"); |
---|
120 |
|
---|
121 |
return str; |
---|
122 |
} |
---|
123 |
|
---|
124 |
unittest |
---|
125 |
{ |
---|
126 |
version(Windows) |
---|
127 |
{ |
---|
128 |
assert(normDirPath ("C:\\a/b\\c/d" ) == "C:\\a\\b\\c\\d\\"); |
---|
129 |
assert(normDirPath ("C:\\a/b\\c/d\\") == "C:\\a\\b\\c\\d\\"); |
---|
130 |
assert(normDirPath ("C:\\a/b\\c/d/" ) == "C:\\a\\b\\c\\d\\"); |
---|
131 |
assert(normFilePath("C:\\a/b\\c/d" ) == "C:\\a\\b\\c\\d" ); |
---|
132 |
assert(normFilePath("C:\\a/b\\c/d\\") == "C:\\a\\b\\c\\d" ); |
---|
133 |
} |
---|
134 |
else |
---|
135 |
{ |
---|
136 |
assert(normDirPath ("\\a/b\\c/d" ) == "/a/b/c/d/"); |
---|
137 |
assert(normDirPath ("\\a/b\\c/d\\") == "/a/b/c/d/"); |
---|
138 |
assert(normDirPath ("\\a/b\\c/d/" ) == "/a/b/c/d/"); |
---|
139 |
assert(normFilePath("\\a/b\\c/d" ) == "/a/b/c/d" ); |
---|
140 |
assert(normFilePath("\\a/b\\c/d\\") == "/a/b/c/d" ); |
---|
141 |
} |
---|
142 |
} |
---|
143 |
|
---|
144 |
bool initialSetup() |
---|
145 |
{ |
---|
146 |
writeln("Running initial setup..."); |
---|
147 |
|
---|
148 |
// Download dmd zip |
---|
149 |
if(!exists(dmdArchive) && shouldDownload) |
---|
150 |
doSystem("wget "~dmdArchiveBaseURL~dmdArchive); |
---|
151 |
|
---|
152 |
// Extract dmd zip |
---|
153 |
writeln("Extracting dmd archive..."); |
---|
154 |
if(exists("dmd2")) |
---|
155 |
rmdirRecurse("dmd2"); |
---|
156 |
auto zip = new ZipArchive(std.file.read(dmdArchive)); |
---|
157 |
foreach(member; zip.directory) |
---|
158 |
{ |
---|
159 |
makePathTo(member.name); |
---|
160 |
std.file.write(member.name, zip.expand(member)); |
---|
161 |
} |
---|
162 |
|
---|
163 |
// Make mars2.c with 'main' hidden |
---|
164 |
doChDir("dmd2/src/dmd"); |
---|
165 |
copyAndPatch("mars.c", "mars2.c", (ref string data) { |
---|
166 |
data = data.replace("int main(int argc, char *argv[])", "int HIDE_main(int argc, char *argv[])"); |
---|
167 |
}); |
---|
168 |
|
---|
169 |
// Apply patch |
---|
170 |
doChDir("../../.."); |
---|
171 |
doChDir("dmd2"); |
---|
172 |
doSystem("patch -p1 --binary < " ~ normFilePath("../dmdpatch.patch")); |
---|
173 |
|
---|
174 |
// Setup makefile for dmd.lib |
---|
175 |
doChDir(".."); |
---|
176 |
doChDir("dmd2/src/dmd"); |
---|
177 |
version(Windows) |
---|
178 |
enum makefile = "win32.mak"; |
---|
179 |
else |
---|
180 |
{ |
---|
181 |
enum makefile = "linux_lib.mak"; |
---|
182 |
doCopy("../../../"~makefile, makefile); |
---|
183 |
} |
---|
184 |
|
---|
185 |
version(Windows) |
---|
186 |
copyAndPatch(makefile, makefile, (ref string data) { |
---|
187 |
if(dmBase == "") |
---|
188 |
{ |
---|
189 |
data = data.replace("\nCC=$(SCROOT)\\bin\\dmc", "\nCC=dmc"); |
---|
190 |
data = data.replace("\nLIB=$(SCROOT)\\bin\\lib", "\nLIB=lib"); |
---|
191 |
} |
---|
192 |
else |
---|
193 |
data = data.replace("\nD=", "\nD="~dmBase); |
---|
194 |
}); |
---|
195 |
|
---|
196 |
|
---|
197 |
// Build dmd.lib |
---|
198 |
version(Windows) |
---|
199 |
doSystem("make deblib -f"~makefile); |
---|
200 |
else |
---|
201 |
doSystem("make -f"~makefile); |
---|
202 |
doCopy("dmd.lib", "../../../dmd.lib"); |
---|
203 |
|
---|
204 |
// Copy and patch config file |
---|
205 |
doChDir("../../.."); |
---|
206 |
copyAndPatch( |
---|
207 |
normFilePath("dmd2/"~osSubDir~"bin/"~configFile), |
---|
208 |
normFilePath("bin/"~configFile), |
---|
209 |
(ref string data) { |
---|
210 |
data = data.replace(normDirPath("../.."), normDirPath("..")); |
---|
211 |
} |
---|
212 |
); |
---|
213 |
|
---|
214 |
// Copy std lib |
---|
215 |
if(exists("src")) |
---|
216 |
rmdirRecurse("src"); |
---|
217 |
mkdir("src"); |
---|
218 |
copyRecurse("dmd2/src/druntime", "src/druntime"); |
---|
219 |
copyRecurse("dmd2/src/phobos", "src/phobos" ); |
---|
220 |
copyRecurse("dmd2/"~osSubDir~"lib", "lib" ); |
---|
221 |
|
---|
222 |
// Copy linker |
---|
223 |
doCopy("dmd2/"~osSubDir~"bin/link"~execExt, "bin/link"~execExt); |
---|
224 |
|
---|
225 |
// Make a note that setup is done |
---|
226 |
std.file.write(setupDoneFile, ""); |
---|
227 |
|
---|
228 |
return true; |
---|
229 |
} |
---|
230 |
|
---|
231 |
int main(string[] args) |
---|
232 |
{ |
---|
233 |
endOfOptions = ""; |
---|
234 |
bool help; |
---|
235 |
bool debugOnly; |
---|
236 |
bool releaseOnly; |
---|
237 |
dmdVersion = dmdVersionDefault; |
---|
238 |
getopt( |
---|
239 |
args, |
---|
240 |
std.getopt.config.caseSensitive, |
---|
241 |
"setup", { if(exists(setupDoneFile)) std.file.remove(setupDoneFile); }, |
---|
242 |
"debug|d", &debugOnly, |
---|
243 |
"release|r", &releaseOnly, |
---|
244 |
"ver", &dmdVersion, |
---|
245 |
"download", &shouldDownload, |
---|
246 |
"dmbase", &dmBase, |
---|
247 |
"help|h|H|?", &help |
---|
248 |
); |
---|
249 |
|
---|
250 |
dmdPackage = "dmd."~dmdVersion; |
---|
251 |
dmdArchive = dmdPackage~".zip"; |
---|
252 |
|
---|
253 |
auto helpMsg = |
---|
254 |
`This script will compile DDMD |
---|
255 |
|
---|
256 |
To run this script, you must download a copy of `~dmdArchive~` to this |
---|
257 |
directory. It can be obtained from: |
---|
258 |
`~dmdArchiveBaseURL~dmdArchive~` |
---|
259 |
Or just use --download if you have wget installed. |
---|
260 |
|
---|
261 |
Note that this script must be run from the main DDMD directory. |
---|
262 |
|
---|
263 |
Also, make sure you have GNU patch installed and current versions |
---|
264 |
of DMC and DMD (D2) on the PATH. |
---|
265 |
|
---|
266 |
Usage: |
---|
267 |
`~scriptName~` [options...] |
---|
268 |
|
---|
269 |
--help,-h,-H,-? Display this help message |
---|
270 |
--debug,-d Only build debug version |
---|
271 |
--release,-r Only build release version |
---|
272 |
--ver={ver} Base DDMD off specific DMD version (default: `~dmdVersionDefault~`) |
---|
273 |
--setup Re-run initial setup |
---|
274 |
--download If running initial setup and the dmd zip doesn't exist, |
---|
275 |
use wget to download it |
---|
276 |
--dmbase={path} Path to directory containing 'dm' for building dmd.lib |
---|
277 |
(Optional if --setup is used, otherwise ignored) |
---|
278 |
`; |
---|
279 |
|
---|
280 |
// Assume the user meant they wanted both. |
---|
281 |
if(debugOnly && releaseOnly) |
---|
282 |
{ |
---|
283 |
debugOnly = releaseOnly = false; |
---|
284 |
} |
---|
285 |
|
---|
286 |
if( |
---|
287 |
help || |
---|
288 |
( !exists(dmdArchive) && !shouldDownload ) || |
---|
289 |
( exists(dmdArchive) && !isfile(dmdArchive) ) |
---|
290 |
) |
---|
291 |
{ |
---|
292 |
write(helpMsg); |
---|
293 |
return 1; |
---|
294 |
} |
---|
295 |
|
---|
296 |
if(!exists(setupDoneFile)) |
---|
297 |
{ |
---|
298 |
if(!initialSetup()) |
---|
299 |
return 1; |
---|
300 |
} |
---|
301 |
|
---|
302 |
int ret=0; |
---|
303 |
version(Windows) |
---|
304 |
{ |
---|
305 |
system("cls"); |
---|
306 |
if(ret == 0) ret = doSystem(r"dmc.exe bridge\bridge.cpp -c"); |
---|
307 |
if(!releaseOnly) if(ret == 0) ret = doSystem(r"dmd -debug -g @commands.txt"); |
---|
308 |
if(!debugOnly) if(ret == 0) ret = doSystem(r"dmd -release -O -inline @commands.txt"); |
---|
309 |
} |
---|
310 |
else |
---|
311 |
{ |
---|
312 |
if(ret == 0) ret = doSystem("g++ -c bridge/bridge.cpp -obridge.o"); |
---|
313 |
if(!releaseOnly) if(ret == 0) ret = doSystem("dmd -debug -gc @commands.linux.txt"); |
---|
314 |
if(!debugOnly) if(ret == 0) ret = doSystem("dmd -release -O -inline @commands.linux.txt"); |
---|
315 |
} |
---|
316 |
|
---|
317 |
return ret; |
---|
318 |
} |
---|