-
Notifications
You must be signed in to change notification settings - Fork 11
/
Install.d
374 lines (285 loc) · 9.52 KB
/
Install.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
/**
* Copyright: Copyright (c) 2010 Jacob Carlborg. All rights reserved.
* Authors: Jacob Carlborg
* Version: Initial created: Nov 8, 2010
* License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0)
*/
module dvm.commands.Install;
import tango.core.Exception;
import tango.io.Stdout;
import tango.io.device.File;
import tango.net.http.HttpGet;
import tango.sys.Common;
import tango.sys.Environment;
import tango.sys.Process;
import tango.sys.win32.Types;
import tango.text.convert.Format : format = Format;
import tango.text.Util;
import tango.util.compress.Zip : extractArchive;
import dvm.commands.Command;
import dvm.commands.DvmInstall;
import dvm.commands.Fetch;
import dvm.commands.Use;
import dvm.core._;
import dvm.dvm.Wrapper;
import dvm.dvm._;
import Path = dvm.io.Path;
import dvm.util.Util;
import dvm.util.Version;
class Install : Fetch
{
private
{
string archivePath;
string tmpCompilerPath;
string installPath_;
Wrapper wrapper;
static if (darwin)
const platform = "osx";
else static if (freebsd)
const platform = "freebsd";
else static if (linux)
const platform = "linux";
else static if (Windows)
const platform = "windows";
}
this ()
{
super("install", "Install one or many D versions.");
}
void execute ()
{
if (args.first == "dvm") // special case for the installation of dvm itself
{
(new DvmInstall).invoke(args);
return;
}
install;
}
private:
void install ()
{
auto filename = buildFilename;
auto url = buildUrl(filename);
archivePath = Path.join(options.path.archives, filename);
fetch(url, archivePath);
println("Installing: dmd-", args.first);
unpack;
moveFiles;
installWrapper;
setPermissions;
installEnvironment(createEnvironment);
patchDmdConf;
if (options.tango)
installTango;
}
void unpack ()
{
tmpCompilerPath = Path.join(options.path.tmp, "dmd-" ~ args.first);
verbose("Unpacking:");
verbose(options.indentation, "source: ", archivePath);
verbose(options.indentation, "destination: ", tmpCompilerPath, '\n');
extractArchive(archivePath, tmpCompilerPath);
}
void moveFiles ()
{
auto dmd = args.first.length > 0 && args.first[0] == '2' ? "dmd2" : "dmd";
auto root = Path.join(tmpCompilerPath, dmd);
auto platformRoot = Path.join(root, platform);
if (!Path.exists(platformRoot))
throw new DvmException(format(`The platform "{}" is not compatible with the compiler dmd {}`, platform, args.first), __FILE__, __LINE__);
auto binSource = getBinSource(platformRoot);
auto binDest = Path.join(installPath, options.path.bin);
auto libSource = getLibSource(platformRoot);
auto libDest = Path.join(installPath, options.path.lib);
auto srcSource = Path.join(root, options.path.src);
auto srcDest = Path.join(installPath, options.path.src);
verbose("Moving:");
move(binSource, binDest);
move(libSource, libDest);
move(srcSource, srcDest);
}
void installTango ()
{
verbose("Installing Tango");
fetchTango;
unpackTango;
setupTangoEnvironment;
buildTango;
moveTangoFiles;
patchDmdConfForTango;
}
void installWrapper ()
{
wrapper.target = Path.join(installPath, options.path.bin, "dmd");
wrapper.path = Path.join(options.path.dvm, options.path.bin, "dmd-") ~ args.first;
verbose("Installing wrapper: " ~ wrapper.path);
wrapper.write;
}
void setPermissions ()
{
verbose("Setting permissions:");
permission(Path.join(installPath, options.path.bin, "dmd"), "+x");
permission(Path.join(installPath, options.path.bin, "dumpobj"), "+x");
permission(Path.join(installPath, options.path.bin, "obj2asm"), "+x");
auto rdmdPath = Path.join(installPath, options.path.bin, "rdmd");
if (Path.exists(rdmdPath))
permission(rdmdPath, "+x");
permission(wrapper.path, "+x");
}
void installEnvironment (ShellScript sh)
{
sh.path = options.path.env;
Path.createPath(sh.path);
sh.path = Path.join(sh.path, "dmd-" ~ args.first);
verbose("Installing environment: ", sh.path);
sh.write;
}
ShellScript createEnvironment ()
{
auto sh = new ShellScript;
sh.echoOff;
auto envPath = Path.join(installPath, options.path.bin);
auto binPath = Path.join(options.path.dvm, options.path.bin);
sh.exportPath("PATH", envPath, binPath, Sh.variable("PATH", false));
return sh;
}
void patchDmdConf (bool tango = false)
{
auto dmdConfPath = Path.join(installPath, options.path.conf);
verbose("Patching: ", dmdConfPath);
auto src = tango ? "-I%@P%/../import -defaultlib=tango -debuglib=tango -version=Tango" : "-I%@P%/../src/phobos";
auto content = cast(string) File.get(dmdConfPath);
content = content.substitute("-I%@P%/../../src/phobos", src);
content = content.substitute("-I%@P%/../../src/druntime/import", "-I%@P%/../src/druntime/import");
content = content.substitute("-L-L%@P%/../lib32", "-L-L%@P%/../lib");
File.set(dmdConfPath, content);
}
void fetchTango ()
{
const tangoUrl = "http://dsource.org/projects/tango/changeset/head/trunk?old_path=%2F&format=zip";
fetch(tangoUrl, options.path.tangoZip);
}
void unpackTango ()
{
verbose("Unpacking:");
verbose(options.indentation, "source: ", options.path.tangoZip);
verbose(options.indentation, "destination: ", options.path.tangoTmp, '\n');
extractArchive(options.path.tangoZip, options.path.tangoUnarchived);
}
void setupTangoEnvironment ()
{
verbose(format(`Installing "{}" as the temporary D compiler`, args.first));
auto path = Environment.get("PATH");
path = Path.join(installPath, options.path.bin) ~ options.path.pathSeparator ~ path;
Environment.set("PATH", path);
}
void buildTango ()
{
verbose("Setting permission:");
permission(options.path.tangoBob, "+x");
verbose("Building Tango...");
auto tangoBuildOptions = options.is64bit ? "-m=64" : "-m=32";
auto process = new Process(true, options.path.tangoBob, "-r=dmd", "-c=dmd", "-u", "-q", tangoBuildOptions, "-l=" ~ options.path.tangoLibName, ".");
process.workDir = options.path.tangoTmp;
process.execute;
auto result = process.wait;
if (options.verbose || result.reason != Process.Result.Exit)
{
println("Output of the Tango build:", "\n");
Stdout.copy(process.stdout).flush;
println();
println("Process ", process.programName, '(', process.pid, ')', " exited with:");
println(options.indentation, "reason: ", result);
println(options.indentation, "status: ", result.status, "\n");
}
}
void moveTangoFiles ()
{
verbose("Moving:");
auto importDest = Path.join(installPath, options.path.import_);
auto tangoSource = options.path.tangoSrc;
auto tangoDest = Path.join(importDest, "tango");
auto objectSrc = options.path.tangoObject;
auto objectDest = Path.join(importDest, options.path.object_di);
auto vendorSrc = options.path.tangoVendor;
auto vendorDest = Path.join(importDest, options.path.std);
move(options.path.tangoLib, Path.join(installPath, options.path.lib, options.path.tangoLibName ~ options.path.libExtension));
move(vendorSrc, vendorDest);
move(tangoSource, tangoDest);
move(objectSrc, objectDest);
}
void patchDmdConfForTango ()
{
auto dmdConfPath = Path.join(installPath, options.path.conf);
verbose("Patching: ", dmdConfPath);
auto src = "-I%@P%/../import -defaultlib=tango -debuglib=tango -version=Tango";
auto content = cast(string) File.get(dmdConfPath);
content = content.substitute("-I%@P%/../src/phobos", src);
content = content.substitute("-I%@P%/../../src/druntime/import", "");
File.set(dmdConfPath, content);
}
void move (string source, string destination)
{
verbose(options.indentation, "source: ", source);
verbose(options.indentation, "destination: ", destination, '\n');
if (Path.exists(destination))
Path.remove(destination, true);
if (Path.isFile(source))
Path.createPath(Path.parse(destination).path);
else
Path.createPath(destination);
Path.rename(source, destination);
}
string installPath ()
{
if (installPath_.length > 0)
return installPath_;
return installPath_ = Path.join(options.path.compilers, "dmd-" ~ args.first);
}
void permission (string path, string mode)
{
version (Posix)
{
verbose(options.indentation, "mode: " ~ mode);
verbose(options.indentation, "file: " ~ path, '\n');
Path.permission(path, mode);
}
}
string getLibSource (string platformRoot)
{
string libPath = Path.join(platformRoot, options.path.lib);
if (Path.exists(libPath))
return libPath;
if (options.is64bit)
{
libPath = Path.join(platformRoot, options.path.lib64);
if (Path.exists(libPath))
return libPath;
else
throw new DvmException("There is no 64bit compiler available on this platform", __FILE__, __LINE__);
}
libPath = Path.join(platformRoot, options.path.lib32);
if (Path.exists(libPath))
return libPath;
throw new DvmException("Could not find the library path: " ~ libPath, __FILE__, __LINE__);
}
string getBinSource (string platformRoot)
{
string binPath = Path.join(platformRoot, options.path.bin);
if (Path.exists(binPath))
return binPath;
if (options.is64bit)
{
binPath = Path.join(platformRoot, options.path.bin64);
if (Path.exists(binPath))
return binPath;
else
throw new DvmException("There is no 64bit compiler available on this platform", __FILE__, __LINE__);
}
binPath = Path.join(platformRoot, options.path.bin32);
if (Path.exists(binPath))
return binPath;
throw new DvmException("Could not find the binrary path: " ~ binPath, __FILE__, __LINE__);
}
}