1 /** 2 DMD 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.dmd; 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 // Determines whether the specified process is running under WOW64 or an Intel64 of x64 processor. 23 version (Windows) 24 private Nullable!bool isWow64() { 25 // See also: https://docs.microsoft.com/de-de/windows/desktop/api/sysinfoapi/nf-sysinfoapi-getnativesysteminfo 26 import core.sys.windows.windows : GetNativeSystemInfo, SYSTEM_INFO, PROCESSOR_ARCHITECTURE_AMD64; 27 28 static Nullable!bool result; 29 30 // A process's architecture won't change over while the process is in memory 31 // Return the cached result 32 if (!result.isNull) 33 return result; 34 35 SYSTEM_INFO systemInfo; 36 GetNativeSystemInfo(&systemInfo); 37 result = systemInfo.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64; 38 return result; 39 } 40 41 class DMDCompiler : Compiler { 42 private static immutable s_options = [ 43 tuple(BuildOption.debugMode, ["-debug"]), 44 tuple(BuildOption.releaseMode, ["-release"]), 45 tuple(BuildOption.coverage, ["-cov"]), 46 tuple(BuildOption.coverageCTFE, ["-cov=ctfe"]), 47 tuple(BuildOption.debugInfo, ["-g"]), 48 tuple(BuildOption.debugInfoC, ["-g"]), 49 tuple(BuildOption.alwaysStackFrame, ["-gs"]), 50 tuple(BuildOption.stackStomping, ["-gx"]), 51 tuple(BuildOption.inline, ["-inline"]), 52 tuple(BuildOption.noBoundsCheck, ["-noboundscheck"]), 53 tuple(BuildOption.optimize, ["-O"]), 54 tuple(BuildOption.profile, ["-profile"]), 55 tuple(BuildOption.unittests, ["-unittest"]), 56 tuple(BuildOption.verbose, ["-v"]), 57 tuple(BuildOption.ignoreUnknownPragmas, ["-ignore"]), 58 tuple(BuildOption.syntaxOnly, ["-o-"]), 59 tuple(BuildOption.warnings, ["-wi"]), 60 tuple(BuildOption.warningsAsErrors, ["-w"]), 61 tuple(BuildOption.ignoreDeprecations, ["-d"]), 62 tuple(BuildOption.deprecationWarnings, ["-dw"]), 63 tuple(BuildOption.deprecationErrors, ["-de"]), 64 tuple(BuildOption.property, ["-property"]), 65 tuple(BuildOption.profileGC, ["-profile=gc"]), 66 tuple(BuildOption.betterC, ["-betterC"]), 67 tuple(BuildOption.lowmem, ["-lowmem"]), 68 tuple(BuildOption.color, ["-color"]), 69 70 tuple(BuildOption._docs, ["-Dddocs"]), 71 tuple(BuildOption._ddox, ["-Xfdocs.json", "-Df__dummy.html"]), 72 ]; 73 74 @property string name() const { return "dmd"; } 75 76 enum dmdVersionRe = `^version\s+v?(\d+\.\d+\.\d+[A-Za-z0-9.+-]*)`; 77 78 unittest { 79 import std.regex : matchFirst, regex; 80 auto probe = ` 81 binary dmd 82 version v2.082.0 83 config /etc/dmd.conf 84 `; 85 auto re = regex(dmdVersionRe, "m"); 86 auto c = matchFirst(probe, re); 87 assert(c && c.length > 1 && c[1] == "2.082.0"); 88 } 89 90 unittest { 91 import std.regex : matchFirst, regex; 92 auto probe = ` 93 binary dmd 94 version v2.084.0-beta.1 95 config /etc/dmd.conf 96 `; 97 auto re = regex(dmdVersionRe, "m"); 98 auto c = matchFirst(probe, re); 99 assert(c && c.length > 1 && c[1] == "2.084.0-beta.1"); 100 } 101 102 string determineVersion(string compiler_binary, string verboseOutput) 103 { 104 import std.regex : matchFirst, regex; 105 auto ver = matchFirst(verboseOutput, regex(dmdVersionRe, "m")); 106 return ver && ver.length > 1 ? ver[1] : null; 107 } 108 109 BuildPlatform determinePlatform(ref BuildSettings settings, string compiler_binary, string arch_override) 110 { 111 // Set basic arch flags for the probe - might be revised based on the exact value + compiler version 112 string[] arch_flags; 113 if (arch_override.length) 114 arch_flags = [ arch_override != "x86_64" ? "-m32" : "-m64" ]; 115 else 116 { 117 // Don't use Optlink by default on Windows 118 version (Windows) { 119 const is64bit = isWow64(); 120 if (!is64bit.isNull) 121 arch_flags = [ is64bit.get ? "-m64" : "-m32" ]; 122 } 123 } 124 125 BuildPlatform bp = probePlatform( 126 compiler_binary, 127 arch_flags ~ ["-quiet", "-c", "-o-", "-v"], 128 arch_override 129 ); 130 131 switch (arch_override) { 132 default: throw new UnsupportedArchitectureException(arch_override); 133 case "": break; 134 // DMD 2.099 made MsCOFF the default, and DMD v2.109 removed OMF 135 // support. Default everything to MsCOFF, people wanting to use OMF 136 // should use an older DMD / dub. 137 case "x86", "x86_omf", "x86_mscoff": arch_flags = ["-m32"]; break; 138 case "x86_64": arch_flags = ["-m64"]; break; 139 } 140 settings.addDFlags(arch_flags); 141 142 return bp; 143 } 144 145 version (Windows) version (DigitalMars) unittest 146 { 147 BuildSettings settings; 148 auto compiler = new DMDCompiler; 149 auto bp = compiler.determinePlatform(settings, "dmd", "x86"); 150 assert(bp.isWindows()); 151 assert(bp.architecture.canFind("x86")); 152 settings = BuildSettings.init; 153 bp = compiler.determinePlatform(settings, "dmd", "x86_omf"); 154 assert(bp.isWindows()); 155 assert(bp.architecture.canFind("x86")); 156 settings = BuildSettings.init; 157 bp = compiler.determinePlatform(settings, "dmd", "x86_mscoff"); 158 assert(bp.isWindows()); 159 assert(bp.architecture.canFind("x86")); 160 settings = BuildSettings.init; 161 bp = compiler.determinePlatform(settings, "dmd", "x86_64"); 162 assert(bp.isWindows()); 163 assert(bp.architecture.canFind("x86_64")); 164 assert(!bp.architecture.canFind("x86")); 165 settings = BuildSettings.init; 166 bp = compiler.determinePlatform(settings, "dmd", ""); 167 if (!isWow64.isNull && !isWow64.get) assert(bp.architecture.canFind("x86")); 168 if (!isWow64.isNull && isWow64.get) assert(bp.architecture.canFind("x86_64")); 169 } 170 171 version (LDC) unittest { 172 import std.conv : to; 173 174 BuildSettings settings; 175 auto compiler = new DMDCompiler; 176 auto bp = compiler.determinePlatform(settings, "ldmd2", "x86"); 177 assert(bp.architecture.canFind("x86"), bp.architecture.to!string); 178 bp = compiler.determinePlatform(settings, "ldmd2", ""); 179 version (X86) assert(bp.architecture.canFind("x86"), bp.architecture.to!string); 180 version (X86_64) assert(bp.architecture.canFind("x86_64"), bp.architecture.to!string); 181 } 182 183 void prepareBuildSettings(ref BuildSettings settings, const scope ref BuildPlatform platform, 184 BuildSetting fields = BuildSetting.all) const 185 { 186 enforceBuildRequirements(settings); 187 188 // Keep the current dflags at the end of the array so that they will overwrite other flags. 189 // This allows user $DFLAGS to modify flags added by us. 190 const dflagsTail = settings.dflags; 191 settings.dflags = []; 192 193 if (!(fields & BuildSetting.options)) { 194 foreach (t; s_options) 195 if (settings.options & t[0]) 196 settings.addDFlags(t[1]); 197 } 198 199 if (!(fields & BuildSetting.versions)) { 200 settings.addDFlags(settings.versions.map!(s => "-version="~s)().array()); 201 settings.versions = null; 202 } 203 204 if (!(fields & BuildSetting.debugVersions)) { 205 settings.addDFlags(settings.debugVersions.map!(s => "-debug="~s)().array()); 206 settings.debugVersions = null; 207 } 208 209 if (!(fields & BuildSetting.importPaths)) { 210 settings.addDFlags(settings.importPaths.map!(s => "-I"~s)().array()); 211 settings.importPaths = null; 212 } 213 214 if (!(fields & BuildSetting.cImportPaths)) { 215 settings.addDFlags(settings.cImportPaths.map!(s => "-I"~s)().array()); 216 settings.cImportPaths = null; 217 } 218 219 if (!(fields & BuildSetting.stringImportPaths)) { 220 settings.addDFlags(settings.stringImportPaths.map!(s => "-J"~s)().array()); 221 settings.stringImportPaths = null; 222 } 223 224 if (!(fields & BuildSetting.libs)) { 225 resolveLibs(settings, platform); 226 if (platform.isWindows()) 227 settings.addSourceFiles(settings.libs.map!(l => l~".lib")().array()); 228 else 229 settings.addLFlags(settings.libs.map!(l => "-l"~l)().array()); 230 } 231 232 if (!(fields & BuildSetting.sourceFiles)) { 233 settings.addDFlags(settings.sourceFiles); 234 settings.sourceFiles = null; 235 } 236 237 if (!(fields & BuildSetting.lflags)) { 238 settings.addDFlags(lflagsToDFlags(settings.lflags)); 239 settings.lflags = null; 240 } 241 242 if (platform.platform.canFind("posix") && (settings.options & BuildOption.pic)) 243 settings.addDFlags("-fPIC"); 244 245 settings.addDFlags(dflagsTail); 246 247 assert(fields & BuildSetting.dflags); 248 assert(fields & BuildSetting.copyFiles); 249 } 250 251 void extractBuildOptions(ref BuildSettings settings) const 252 { 253 Appender!(string[]) newflags; 254 next_flag: foreach (f; settings.dflags) { 255 foreach (t; s_options) 256 if (t[1].canFind(f)) { 257 settings.options |= t[0]; 258 continue next_flag; 259 } 260 if (f.startsWith("-version=")) settings.addVersions(f[9 .. $]); 261 else if (f.startsWith("-debug=")) settings.addDebugVersions(f[7 .. $]); 262 else newflags ~= f; 263 } 264 settings.dflags = newflags.data; 265 } 266 267 string getTargetFileName(in BuildSettings settings, in BuildPlatform platform) 268 const { 269 import std.conv: text; 270 assert(settings.targetName.length > 0, "No target name set."); 271 final switch (settings.targetType) { 272 case TargetType.autodetect: 273 assert(false, 274 text("Configurations must have a concrete target type, ", settings.targetName, 275 " has ", settings.targetType)); 276 case TargetType.none: return null; 277 case TargetType.sourceLibrary: return null; 278 case TargetType.executable: 279 if (platform.isWindows()) 280 return settings.targetName ~ ".exe"; 281 else return settings.targetName.idup; 282 case TargetType.library: 283 case TargetType.staticLibrary: 284 if (platform.isWindows()) 285 return settings.targetName ~ ".lib"; 286 else return "lib" ~ settings.targetName ~ ".a"; 287 case TargetType.dynamicLibrary: 288 if (platform.isWindows()) 289 return settings.targetName ~ ".dll"; 290 else if (platform.platform.canFind("darwin")) 291 return "lib" ~ settings.targetName ~ ".dylib"; 292 else return "lib" ~ settings.targetName ~ ".so"; 293 case TargetType.object: 294 if (platform.isWindows()) 295 return settings.targetName ~ ".obj"; 296 else return settings.targetName ~ ".o"; 297 } 298 } 299 300 void setTarget(ref BuildSettings settings, in BuildPlatform platform, string tpath = null) const 301 { 302 const targetFileName = getTargetFileName(settings, platform); 303 304 final switch (settings.targetType) { 305 case TargetType.autodetect: assert(false, "Invalid target type: autodetect"); 306 case TargetType.none: assert(false, "Invalid target type: none"); 307 case TargetType.sourceLibrary: assert(false, "Invalid target type: sourceLibrary"); 308 case TargetType.executable: break; 309 case TargetType.library: 310 case TargetType.staticLibrary: 311 settings.addDFlags("-lib"); 312 break; 313 case TargetType.dynamicLibrary: 314 if (platform.compiler != "dmd" || platform.isWindows() || platform.platform.canFind("osx")) 315 settings.addDFlags("-shared"); 316 else 317 settings.prependDFlags("-shared", "-defaultlib=libphobos2.so"); 318 addDynamicLibName(settings, platform, targetFileName); 319 break; 320 case TargetType.object: 321 settings.addDFlags("-c"); 322 break; 323 } 324 325 if (tpath is null) 326 tpath = (NativePath(settings.targetPath) ~ targetFileName).toNativeString(); 327 settings.addDFlags("-of"~tpath); 328 } 329 330 void invoke(in BuildSettings settings, in BuildPlatform platform, void delegate(int, string) output_callback, NativePath cwd) 331 { 332 auto res_file = getTempFile("dub-build", ".rsp"); 333 // clean-up early to avoid build-up of temporaries when invoke is called 334 // many times in one DUB session. (e.g. when using DUB as a library) 335 scope (exit) 336 removeFile(res_file); 337 const(string)[] args = settings.dflags; 338 if (platform.frontendVersion >= 2066) args ~= "-vcolumns"; 339 writeFile(res_file, escapeArgs(args).join("\n")); 340 341 logDiagnostic("[cwd=%s] %s %s", cwd, platform.compilerBinary, escapeArgs(args).join(" ")); 342 string[string] env; 343 foreach (aa; [settings.environments, settings.buildEnvironments]) 344 foreach (k, v; aa) 345 env[k] = v; 346 invokeTool([platform.compilerBinary, "@"~res_file.toNativeString()], output_callback, cwd, env); 347 } 348 349 void invokeLinker(in BuildSettings settings, in BuildPlatform platform, string[] objects, void delegate(int, string) output_callback, NativePath cwd) 350 { 351 import std.string; 352 auto tpath = NativePath(settings.targetPath) ~ getTargetFileName(settings, platform); 353 auto args = ["-of"~tpath.toNativeString()]; 354 args ~= objects; 355 args ~= settings.sourceFiles; 356 if (platform.platform.canFind("linux")) 357 args ~= "-L--no-as-needed"; // avoids linker errors due to libraries being specified in the wrong order by DMD 358 args ~= lflagsToDFlags(settings.lflags); 359 if (platform.compiler == "ldc") { 360 // ldmd2: support the full LDC-specific list + extra "-m32mscoff", a superset of the DMD list 361 import dub.compilers.ldc : LDCCompiler; 362 args ~= settings.dflags.filter!(f => f == "-m32mscoff" || LDCCompiler.isLinkerDFlag(f)).array; 363 } else { 364 args ~= settings.dflags.filter!(f => isLinkerDFlag(f)).array; 365 } 366 367 auto res_file = getTempFile("dub-build", ".lnk"); 368 writeFile(res_file, escapeArgs(args).join("\n")); 369 370 logDiagnostic("[cwd=%s] %s %s", cwd, platform.compilerBinary, escapeArgs(args).join(" ")); 371 string[string] env; 372 foreach (aa; [settings.environments, settings.buildEnvironments]) 373 foreach (k, v; aa) 374 env[k] = v; 375 invokeTool([platform.compilerBinary, "@"~res_file.toNativeString()], output_callback, cwd, env); 376 } 377 378 string[] lflagsToDFlags(const string[] lflags) const 379 { 380 return map!(f => "-L"~f)(lflags.filter!(f => f != "")()).array(); 381 } 382 383 private auto escapeArgs(in string[] args) 384 { 385 return args.map!(s => s.canFind(' ') ? "\""~s~"\"" : s); 386 } 387 388 static bool isLinkerDFlag(string arg) 389 { 390 switch (arg) { 391 case "-g", "-gc", "-m32", "-m64", "-shared", "-lib", "-betterC": 392 return true; 393 default: 394 return arg.startsWith("-L") 395 || arg.startsWith("-Xcc=") 396 || arg.startsWith("-defaultlib="); 397 } 398 } 399 }