-
-
Notifications
You must be signed in to change notification settings - Fork 227
/
ldc.d
331 lines (290 loc) · 11 KB
/
ldc.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
/**
LDC compiler support.
Copyright: © 2013-2013 rejectedsoftware e.K.
License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file.
Authors: Sönke Ludwig
*/
module dub.compilers.ldc;
import dub.compilers.compiler;
import dub.compilers.utils;
import dub.internal.utils;
import dub.internal.vibecompat.core.file;
import dub.internal.vibecompat.inet.path;
import dub.internal.logging;
import std.algorithm;
import std.array;
import std.exception;
import std.typecons;
class LDCCompiler : Compiler {
private static immutable s_options = [
tuple(BuildOption.debugMode, ["-d-debug"]),
tuple(BuildOption.releaseMode, ["-release"]),
tuple(BuildOption.coverage, ["-cov"]),
tuple(BuildOption.coverageCTFE, ["-cov=ctfe"]),
tuple(BuildOption.debugInfo, ["-g"]),
tuple(BuildOption.debugInfoC, ["-gc"]),
tuple(BuildOption.alwaysStackFrame, ["-disable-fp-elim"]),
//tuple(BuildOption.stackStomping, ["-?"]),
tuple(BuildOption.inline, ["-enable-inlining", "-Hkeep-all-bodies"]),
tuple(BuildOption.noBoundsCheck, ["-boundscheck=off"]),
tuple(BuildOption.optimize, ["-O3"]),
tuple(BuildOption.profile, ["-fdmd-trace-functions"]),
tuple(BuildOption.unittests, ["-unittest"]),
tuple(BuildOption.verbose, ["-v"]),
tuple(BuildOption.ignoreUnknownPragmas, ["-ignore"]),
tuple(BuildOption.syntaxOnly, ["-o-"]),
tuple(BuildOption.warnings, ["-wi"]),
tuple(BuildOption.warningsAsErrors, ["-w"]),
tuple(BuildOption.ignoreDeprecations, ["-d"]),
tuple(BuildOption.deprecationWarnings, ["-dw"]),
tuple(BuildOption.deprecationErrors, ["-de"]),
tuple(BuildOption.property, ["-property"]),
//tuple(BuildOption.profileGC, ["-?"]),
tuple(BuildOption.betterC, ["-betterC"]),
tuple(BuildOption.lowmem, ["-lowmem"]),
tuple(BuildOption.color, ["-enable-color"]),
tuple(BuildOption._docs, ["-Dd=docs"]),
tuple(BuildOption._ddox, ["-Xf=docs.json", "-Dd=__dummy_docs"]),
];
@property string name() const { return "ldc"; }
enum ldcVersionRe = `^version\s+v?(\d+\.\d+\.\d+[A-Za-z0-9.+-]*)`;
unittest {
import std.regex : matchFirst, regex;
auto probe = `
binary /usr/bin/ldc2
version 1.11.0 (DMD v2.081.2, LLVM 6.0.1)
config /etc/ldc2.conf (x86_64-pc-linux-gnu)
`;
auto re = regex(ldcVersionRe, "m");
auto c = matchFirst(probe, re);
assert(c && c.length > 1 && c[1] == "1.11.0");
}
string determineVersion(string compiler_binary, string verboseOutput)
{
import std.regex : matchFirst, regex;
auto ver = matchFirst(verboseOutput, regex(ldcVersionRe, "m"));
return ver && ver.length > 1 ? ver[1] : null;
}
BuildPlatform determinePlatform(ref BuildSettings settings, string compiler_binary, string arch_override)
{
string[] arch_flags;
switch (arch_override) {
case "": break;
case "x86": arch_flags = ["-march=x86"]; break;
case "x86_mscoff": arch_flags = ["-march=x86"]; break;
case "x86_64": arch_flags = ["-march=x86-64"]; break;
case "aarch64": arch_flags = ["-march=aarch64"]; break;
case "powerpc64": arch_flags = ["-march=powerpc64"]; break;
default:
if (arch_override.canFind('-'))
arch_flags = ["-mtriple="~arch_override];
else
throw new UnsupportedArchitectureException(arch_override);
break;
}
settings.addDFlags(arch_flags);
return probePlatform(
compiler_binary,
arch_flags ~ ["-c", "-o-", "-v"],
arch_override
);
}
void prepareBuildSettings(ref BuildSettings settings, const scope ref BuildPlatform platform, BuildSetting fields = BuildSetting.all) const
{
import std.format : format;
enforceBuildRequirements(settings);
if (!(fields & BuildSetting.options)) {
foreach (t; s_options)
if (settings.options & t[0])
settings.addDFlags(t[1]);
}
// since LDC always outputs multiple object files, avoid conflicts by default
settings.addDFlags("--oq", format("-od=%s/obj", settings.targetPath));
if (!(fields & BuildSetting.versions)) {
settings.addDFlags(settings.versions.map!(s => "-d-version="~s)().array());
settings.versions = null;
}
if (!(fields & BuildSetting.debugVersions)) {
settings.addDFlags(settings.debugVersions.map!(s => "-d-debug="~s)().array());
settings.debugVersions = null;
}
if (!(fields & BuildSetting.importPaths)) {
settings.addDFlags(settings.importPaths.map!(s => "-I"~s)().array());
settings.importPaths = null;
}
if (!(fields & BuildSetting.cImportPaths)) {
settings.addDFlags(settings.cImportPaths.map!(s => "-I"~s)().array());
settings.cImportPaths = null;
}
if (!(fields & BuildSetting.stringImportPaths)) {
settings.addDFlags(settings.stringImportPaths.map!(s => "-J"~s)().array());
settings.stringImportPaths = null;
}
if (!(fields & BuildSetting.sourceFiles)) {
settings.addDFlags(settings.sourceFiles);
settings.sourceFiles = null;
}
if (!(fields & BuildSetting.libs)) {
resolveLibs(settings, platform);
settings.addLFlags(settings.libs.map!(l => "-l"~l)().array());
}
if (!(fields & BuildSetting.lflags)) {
settings.addDFlags(lflagsToDFlags(settings.lflags));
settings.lflags = null;
}
if (settings.options & BuildOption.pic) {
if (platform.isWindows()) {
/* This has nothing to do with PIC, but as the PIC option is exclusively
* set internally for code that ends up in a dynamic library, explicitly
* specify what `-shared` defaults to (`-shared` can't be used when
* compiling only, without linking).
* *Pre*pending the flags enables the user to override them.
*/
settings.prependDFlags("-fvisibility=public", "-dllimport=all");
} else {
settings.addDFlags("-relocation-model=pic");
}
}
assert(fields & BuildSetting.dflags);
assert(fields & BuildSetting.copyFiles);
}
void extractBuildOptions(ref BuildSettings settings) const
{
Appender!(string[]) newflags;
next_flag: foreach (f; settings.dflags) {
foreach (t; s_options)
if (t[1].canFind(f)) {
settings.options |= t[0];
continue next_flag;
}
if (f.startsWith("-d-version=")) settings.addVersions(f[11 .. $]);
else if (f.startsWith("-d-debug=")) settings.addDebugVersions(f[9 .. $]);
else newflags ~= f;
}
settings.dflags = newflags.data;
}
string getTargetFileName(in BuildSettings settings, in BuildPlatform platform)
const {
assert(settings.targetName.length > 0, "No target name set.");
const p = platform.platform;
final switch (settings.targetType) {
case TargetType.autodetect: assert(false, "Configurations must have a concrete target type.");
case TargetType.none: return null;
case TargetType.sourceLibrary: return null;
case TargetType.executable:
if (p.canFind("windows"))
return settings.targetName ~ ".exe";
else if (p.canFind("wasm"))
return settings.targetName ~ ".wasm";
else return settings.targetName.idup;
case TargetType.library:
case TargetType.staticLibrary:
if (p.canFind("windows") && !p.canFind("mingw"))
return settings.targetName ~ ".lib";
else return "lib" ~ settings.targetName ~ ".a";
case TargetType.dynamicLibrary:
if (p.canFind("windows"))
return settings.targetName ~ ".dll";
else if (p.canFind("darwin"))
return "lib" ~ settings.targetName ~ ".dylib";
else return "lib" ~ settings.targetName ~ ".so";
case TargetType.object:
if (p.canFind("windows"))
return settings.targetName ~ ".obj";
else return settings.targetName ~ ".o";
}
}
void setTarget(ref BuildSettings settings, in BuildPlatform platform, string tpath = null) const
{
const targetFileName = getTargetFileName(settings, platform);
final switch (settings.targetType) {
case TargetType.autodetect: assert(false, "Invalid target type: autodetect");
case TargetType.none: assert(false, "Invalid target type: none");
case TargetType.sourceLibrary: assert(false, "Invalid target type: sourceLibrary");
case TargetType.executable: break;
case TargetType.library:
case TargetType.staticLibrary:
settings.addDFlags("-lib");
break;
case TargetType.dynamicLibrary:
settings.addDFlags("-shared");
addDynamicLibName(settings, platform, targetFileName);
break;
case TargetType.object:
settings.addDFlags("-c");
break;
}
if (tpath is null)
tpath = (NativePath(settings.targetPath) ~ targetFileName).toNativeString();
settings.addDFlags("-of"~tpath);
}
void invoke(in BuildSettings settings, in BuildPlatform platform, void delegate(int, string) output_callback, NativePath cwd)
{
auto res_file = getTempFile("dub-build", ".rsp");
const(string)[] args = settings.dflags;
if (platform.frontendVersion >= 2066) args ~= "-vcolumns";
writeFile(res_file, escapeArgs(args).join("\n"));
logDiagnostic("%s %s", platform.compilerBinary, escapeArgs(args).join(" "));
string[string] env;
foreach (aa; [settings.environments, settings.buildEnvironments])
foreach (k, v; aa)
env[k] = v;
invokeTool([platform.compilerBinary, "@"~res_file.toNativeString()], output_callback, cwd, env);
}
void invokeLinker(in BuildSettings settings, in BuildPlatform platform, string[] objects, void delegate(int, string) output_callback, NativePath cwd)
{
import std.string;
auto tpath = NativePath(settings.targetPath) ~ getTargetFileName(settings, platform);
auto args = ["-of"~tpath.toNativeString()];
args ~= objects;
args ~= settings.sourceFiles;
if (platform.platform.canFind("linux"))
args ~= "-L--no-as-needed"; // avoids linker errors due to libraries being specified in the wrong order
args ~= lflagsToDFlags(settings.lflags);
args ~= settings.dflags.filter!(f => isLinkerDFlag(f)).array;
auto res_file = getTempFile("dub-build", ".lnk");
writeFile(res_file, escapeArgs(args).join("\n"));
logDiagnostic("%s %s", platform.compilerBinary, escapeArgs(args).join(" "));
string[string] env;
foreach (aa; [settings.environments, settings.buildEnvironments])
foreach (k, v; aa)
env[k] = v;
invokeTool([platform.compilerBinary, "@"~res_file.toNativeString()], output_callback, cwd, env);
}
string[] lflagsToDFlags(const string[] lflags) const
{
return map!(f => "-L"~f)(lflags.filter!(f => f != "")()).array();
}
private auto escapeArgs(in string[] args)
{
return args.map!(s => s.canFind(' ') ? "\""~s~"\"" : s);
}
static bool isLinkerDFlag(string arg)
{
if (arg.length > 2 && arg.startsWith("--"))
arg = arg[1 .. $]; // normalize to 1 leading hyphen
switch (arg) {
case "-g", "-gc", "-m32", "-m64", "-shared", "-lib",
"-betterC", "-disable-linker-strip-dead", "-static":
return true;
default:
return arg.startsWith("-L")
|| arg.startsWith("-Xcc=")
|| arg.startsWith("-defaultlib=")
|| arg.startsWith("-platformlib=")
|| arg.startsWith("-flto")
|| arg.startsWith("-fsanitize=")
|| arg.startsWith("-gcc=")
|| arg.startsWith("-link-")
|| arg.startsWith("-linker=")
|| arg.startsWith("-march=")
|| arg.startsWith("-mscrtlib=")
|| arg.startsWith("-mtriple=");
}
}
bool isWindowsCOFF(in BuildPlatform platform)
{
// What will happen on ARM Windows? Who knows. Once LDC ships for ARM, lets find out!
return platform.isWindows();
}
}