1 /**
2 	LDC compiler support.
3 
4 	Copyright: © 2013-2013 rejectedsoftware e.K.
5 	License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file.
6 	Authors: Sönke Ludwig
7 */
8 module dub.compilers.ldc;
9 
10 import dub.compilers.compiler;
11 import dub.compilers.utils;
12 import dub.internal.utils;
13 import dub.internal.vibecompat.core.file;
14 import dub.internal.vibecompat.inet.path;
15 import dub.internal.logging;
16 
17 import std.algorithm;
18 import std.array;
19 import std.exception;
20 import std.typecons;
21 
22 
23 class LDCCompiler : Compiler {
24 	private static immutable s_options = [
25 		tuple(BuildOption.debugMode, ["-d-debug"]),
26 		tuple(BuildOption.releaseMode, ["-release"]),
27 		tuple(BuildOption.coverage, ["-cov"]),
28 		tuple(BuildOption.coverageCTFE, ["-cov=ctfe"]),
29 		tuple(BuildOption.debugInfo, ["-g"]),
30 		tuple(BuildOption.debugInfoC, ["-gc"]),
31 		tuple(BuildOption.alwaysStackFrame, ["-disable-fp-elim"]),
32 		//tuple(BuildOption.stackStomping, ["-?"]),
33 		tuple(BuildOption.inline, ["-enable-inlining", "-Hkeep-all-bodies"]),
34 		tuple(BuildOption.noBoundsCheck, ["-boundscheck=off"]),
35 		tuple(BuildOption.optimize, ["-O3"]),
36 		tuple(BuildOption.profile, ["-fdmd-trace-functions"]),
37 		tuple(BuildOption.unittests, ["-unittest"]),
38 		tuple(BuildOption.verbose, ["-v"]),
39 		tuple(BuildOption.ignoreUnknownPragmas, ["-ignore"]),
40 		tuple(BuildOption.syntaxOnly, ["-o-"]),
41 		tuple(BuildOption.warnings, ["-wi"]),
42 		tuple(BuildOption.warningsAsErrors, ["-w"]),
43 		tuple(BuildOption.ignoreDeprecations, ["-d"]),
44 		tuple(BuildOption.deprecationWarnings, ["-dw"]),
45 		tuple(BuildOption.deprecationErrors, ["-de"]),
46 		tuple(BuildOption.property, ["-property"]),
47 		//tuple(BuildOption.profileGC, ["-?"]),
48 		tuple(BuildOption.betterC, ["-betterC"]),
49 		tuple(BuildOption.lowmem, ["-lowmem"]),
50 		tuple(BuildOption.color, ["-enable-color"]),
51 
52 		tuple(BuildOption._docs, ["-Dd=docs"]),
53 		tuple(BuildOption._ddox, ["-Xf=docs.json", "-Dd=__dummy_docs", "-oq"]),
54 	];
55 
56 	@property string name() const { return "ldc"; }
57 
58 	enum ldcVersionRe = `^version\s+v?(\d+\.\d+\.\d+[A-Za-z0-9.+-]*)`;
59 
60 	unittest {
61 		import std.regex : matchFirst, regex;
62 		auto probe = `
63 binary    /usr/bin/ldc2
64 version   1.11.0 (DMD v2.081.2, LLVM 6.0.1)
65 config    /etc/ldc2.conf (x86_64-pc-linux-gnu)
66 `;
67 		auto re = regex(ldcVersionRe, "m");
68 		auto c = matchFirst(probe, re);
69 		assert(c && c.length > 1 && c[1] == "1.11.0");
70 	}
71 
72 	string determineVersion(in BuildPlatform platform, string verboseOutput)
73 	{
74 		import std.regex : matchFirst, regex;
75 		auto ver = matchFirst(verboseOutput, regex(ldcVersionRe, "m"));
76 		return ver && ver.length > 1 ? ver[1] : null;
77 	}
78 
79 	BuildPlatform determinePlatform(ref BuildSettings settings, string compiler_binary, string arch_override)
80 	{
81 		string[] arch_flags;
82 		bool arch_override_is_triple = false;
83 		switch (arch_override) {
84 			case "": break;
85 			case "x86": arch_flags = ["-march=x86"]; break;
86 			case "x86_mscoff": arch_flags = ["-march=x86"]; break;
87 			case "x86_64": arch_flags = ["-march=x86-64"]; break;
88 			case "aarch64": arch_flags = ["-march=aarch64"]; break;
89 			case "powerpc64": arch_flags = ["-march=powerpc64"]; break;
90 			default:
91 				if (arch_override.canFind('-')) {
92 					arch_override_is_triple = true;
93 					arch_flags = ["-mtriple="~arch_override];
94 				} else
95 					throw new UnsupportedArchitectureException(arch_override);
96 				break;
97 		}
98 
99 		auto bp = probePlatform(compiler_binary, arch_flags);
100 
101 		bool keep_arch = arch_override_is_triple;
102 		if (!keep_arch && arch_flags.length)
103 			keep_arch = bp.architecture != probePlatform(compiler_binary, []).architecture;
104 		settings.maybeAddArchFlags(keep_arch, arch_flags, arch_override);
105 
106 		return bp;
107 	}
108 
109 	void prepareBuildSettings(ref BuildSettings settings, const scope ref BuildPlatform platform, BuildSetting fields = BuildSetting.all) const
110 	{
111 		enforceBuildRequirements(settings);
112 
113 		// Keep the current dflags at the end of the array so that they will overwrite other flags.
114 		// This allows user $DFLAGS to modify flags added by us.
115 		const dflagsTail = settings.dflags;
116 		settings.dflags = [];
117 
118 		if (!(fields & BuildSetting.options)) {
119 			foreach (t; s_options)
120 				if (settings.options & t[0])
121 					settings.addDFlags(t[1]);
122 		}
123 
124 		if (!(fields & BuildSetting.versions)) {
125 			settings.addDFlags(settings.versions.map!(s => "-d-version="~s)().array());
126 			settings.versions = null;
127 		}
128 
129 		if (!(fields & BuildSetting.debugVersions)) {
130 			settings.addDFlags(settings.debugVersions.map!(s => "-d-debug="~s)().array());
131 			settings.debugVersions = null;
132 		}
133 
134 		if (!(fields & BuildSetting.importPaths)) {
135 			settings.addDFlags(settings.importPaths.map!(s => "-I"~s)().array());
136 			settings.importPaths = null;
137 		}
138 
139 		if (!(fields & BuildSetting.cImportPaths)) {
140 			settings.addDFlags(settings.cImportPaths.map!(s => "-P-I"~s)().array());
141 			settings.cImportPaths = null;
142 		}
143 
144 		if (!(fields & BuildSetting.stringImportPaths)) {
145 			settings.addDFlags(settings.stringImportPaths.map!(s => "-J"~s)().array());
146 			settings.stringImportPaths = null;
147 		}
148 
149 		if (!(fields & BuildSetting.sourceFiles)) {
150 			settings.addDFlags(settings.sourceFiles);
151 			settings.sourceFiles = null;
152 		}
153 
154 		if (!(fields & BuildSetting.libs)) {
155 			resolveLibs(settings, platform);
156 			settings.addLFlags(settings.libs.map!(l => "-l"~l)().array());
157 		}
158 
159 		if (!(fields & BuildSetting.frameworks)) {
160 			if (platform.isDarwin())
161 				settings.addLFlags(settings.frameworks.map!(l => ["-framework", l])().joiner.array());
162 			else
163 				logDiagnostic("Not a darwin-derived platform, skipping frameworks...");
164 		}
165 
166 		if (!(fields & BuildSetting.lflags)) {
167 			settings.addDFlags(lflagsToDFlags(settings.lflags));
168 			settings.lflags = null;
169 		}
170 
171 		if (settings.options & BuildOption.pic) {
172 			if (platform.isWindows()) {
173 				/* This has nothing to do with PIC, but as the PIC option is exclusively
174 				 * set internally for code that ends up in a dynamic library, explicitly
175 				 * specify what `-shared` defaults to (`-shared` can't be used when
176 				 * compiling only, without linking).
177 				 * *Pre*pending the flags enables the user to override them.
178 				 */
179 				settings.prependDFlags("-fvisibility=public", "-dllimport=all");
180 			} else {
181 				settings.addDFlags("-relocation-model=pic");
182 			}
183 		}
184 
185 		settings.addDFlags(dflagsTail);
186 
187 		assert(fields & BuildSetting.dflags);
188 		assert(fields & BuildSetting.copyFiles);
189 	}
190 
191 	void extractBuildOptions(ref BuildSettings settings) const
192 	{
193 		Appender!(string[]) newflags;
194 		next_flag: foreach (f; settings.dflags) {
195 			foreach (t; s_options)
196 				if (t[1].canFind(f)) {
197 					settings.options |= t[0];
198 					continue next_flag;
199 				}
200 			if (f.startsWith("-d-version=")) settings.addVersions(f[11 .. $]);
201 			else if (f.startsWith("-d-debug=")) settings.addDebugVersions(f[9 .. $]);
202 			else newflags ~= f;
203 		}
204 		settings.dflags = newflags.data;
205 	}
206 
207 	string getTargetFileName(in BuildSettings settings, in BuildPlatform platform)
208 	const {
209 		assert(settings.targetName.length > 0, "No target name set.");
210 
211 		const p = platform.platform;
212 		final switch (settings.targetType) {
213 			case TargetType.autodetect: assert(false, "Configurations must have a concrete target type.");
214 			case TargetType.none: return null;
215 			case TargetType.sourceLibrary: return null;
216 			case TargetType.executable:
217 				if (p.canFind("windows"))
218 					return settings.targetName ~ ".exe";
219 				else if (p.canFind("wasm"))
220 					return settings.targetName ~ ".wasm";
221 				else return settings.targetName.idup;
222 			case TargetType.library:
223 			case TargetType.staticLibrary:
224 				if (p.canFind("windows") && !p.canFind("mingw"))
225 					return settings.targetName ~ ".lib";
226 				else return "lib" ~ settings.targetName ~ ".a";
227 			case TargetType.dynamicLibrary:
228 				if (p.canFind("windows"))
229 					return settings.targetName ~ ".dll";
230 				else if (p.canFind("darwin"))
231 					return "lib" ~ settings.targetName ~ ".dylib";
232 				else return "lib" ~ settings.targetName ~ ".so";
233 			case TargetType.object:
234 				if (p.canFind("windows"))
235 					return settings.targetName ~ ".obj";
236 				else return settings.targetName ~ ".o";
237 		}
238 	}
239 
240 	void setTarget(ref BuildSettings settings, in BuildPlatform platform, string tpath = null) const
241 	{
242 		const targetFileName = getTargetFileName(settings, platform);
243 
244 		const p = platform.platform;
245 		final switch (settings.targetType) {
246 			case TargetType.autodetect: assert(false, "Invalid target type: autodetect");
247 			case TargetType.none: assert(false, "Invalid target type: none");
248 			case TargetType.sourceLibrary: assert(false, "Invalid target type: sourceLibrary");
249 			case TargetType.executable: break;
250 			case TargetType.library:
251 			case TargetType.staticLibrary:
252 				// -oq: name object files uniquely (so the files don't collide)
253 				settings.addDFlags("-lib", "-oq");
254 				// -cleanup-obj (supported since LDC v1.1): remove object files after archiving to static lib
255 				if (platform.frontendVersion >= 2071) {
256 					settings.addDFlags("-cleanup-obj");
257 				}
258 				if (platform.frontendVersion < 2095) {
259 					// Since LDC v1.25, -cleanup-obj defaults to a unique temp -od directory
260 					// We need to resort to a unique-ish -od directory before that
261 					settings.addDFlags("-od=" ~ settings.targetPath ~ "/obj");
262 				}
263 				break;
264 			case TargetType.dynamicLibrary:
265 				settings.addDFlags("-shared");
266 				addDynamicLibName(settings, platform, targetFileName);
267 				break;
268 			case TargetType.object:
269 				settings.addDFlags("-c");
270 
271 				// When using wasm-ld on output objects, we need to explicitly
272 				// not strip dead symbols, otherwise we'll get a linker error.
273 				// as wasm-ld only works on relocatable objects.
274 				if (p.canFind("wasm")) {
275 					settings.addDFlags("--disable-linker-strip-dead");
276 					settings.addLFlags("-r");
277 				}
278 				break;
279 		}
280 
281 		if (tpath is null)
282 			tpath = (NativePath(settings.targetPath) ~ targetFileName).toNativeString();
283 		settings.addDFlags("-of"~tpath);
284 	}
285 
286 	void invoke(in BuildSettings settings, in BuildPlatform platform, void delegate(int, string) output_callback, NativePath cwd)
287 	{
288 		auto res_file = getTempFile("dub-build", ".rsp");
289 		const(string)[] args = settings.dflags;
290 		if (platform.frontendVersion >= 2066) args ~= "-vcolumns";
291 		writeFile(res_file, escapeArgs(args).join("\n"));
292 
293 		logDiagnostic("%s %s", platform.compilerBinary, escapeArgs(args).join(" "));
294 		string[string] env;
295 		foreach (aa; [settings.environments, settings.buildEnvironments])
296 			foreach (k, v; aa)
297 				env[k] = v;
298 		invokeTool([platform.compilerBinary, "@"~res_file.toNativeString()], output_callback, cwd, env);
299 	}
300 
301 	void invokeLinker(in BuildSettings settings, in BuildPlatform platform, string[] objects, void delegate(int, string) output_callback, NativePath cwd)
302 	{
303 		import std.string;
304 		auto tpath = NativePath(settings.targetPath) ~ getTargetFileName(settings, platform);
305 		auto args = ["-of"~tpath.toNativeString()];
306 		const p = platform.platform;
307 
308 		args ~= objects;
309 		args ~= settings.sourceFiles;
310 
311 		// Avoids linker errors due to libraries being specified in the wrong order.
312 		// However, the wasm-ld linker does not have --no-as-needed and emscripten is
313 		// implicitly treated as a "linux" platform.
314 		if (p.canFind("linux") && !p.canFind("emscripten"))
315 			args ~= "-L--no-as-needed";
316 
317 		args ~= lflagsToDFlags(settings.lflags);
318 		args ~= settings.dflags.filter!(f => isLinkerDFlag(f)).array;
319 
320 		auto res_file = getTempFile("dub-build", ".lnk");
321 		writeFile(res_file, escapeArgs(args).join("\n"));
322 
323 		logDiagnostic("%s %s", platform.compilerBinary, escapeArgs(args).join(" "));
324 		string[string] env;
325 		foreach (aa; [settings.environments, settings.buildEnvironments])
326 			foreach (k, v; aa)
327 				env[k] = v;
328 		invokeTool([platform.compilerBinary, "@"~res_file.toNativeString()], output_callback, cwd, env);
329 	}
330 
331 	string[] lflagsToDFlags(const string[] lflags) const
332 	{
333         return map!(f => "-L"~f)(lflags.filter!(f => f != "")()).array();
334 	}
335 
336 	private auto escapeArgs(in string[] args)
337 	{
338 		return args.map!(s => s.canFind(' ') ? "\""~s~"\"" : s);
339 	}
340 
341 	static bool isLinkerDFlag(string arg)
342 	{
343 		if (arg.length > 2 && arg.startsWith("--"))
344 			arg = arg[1 .. $]; // normalize to 1 leading hyphen
345 
346 		switch (arg) {
347 			case "-g", "-gc", "-m32", "-m64", "-mwasm64", "-shared", "-lib",
348 			     "-betterC", "-disable-linker-strip-dead", "-static", "-r":
349 				return true;
350 			default:
351 				return arg.startsWith("-L")
352 				    || arg.startsWith("-Xcc=")
353 				    || arg.startsWith("-defaultlib=")
354 				    || arg.startsWith("-platformlib=")
355 				    || arg.startsWith("-flto")
356 				    || arg.startsWith("-fsanitize=")
357 				    || arg.startsWith("-gcc=")
358 				    || arg.startsWith("-link-")
359 				    || arg.startsWith("-linker=")
360 				    || arg.startsWith("-march=")
361 				    || arg.startsWith("-mscrtlib=")
362 				    || arg.startsWith("-mtriple=");
363 		}
364 	}
365 
366 	protected string[] defaultProbeArgs () const {
367 		return ["-c", "-o-", "-v"];
368 	}
369 }