1 /** 2 GDC 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.gdc; 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.process : execute; 21 import std.typecons; 22 23 24 class GDCCompiler : Compiler { 25 private static immutable s_options = [ 26 tuple(BuildOption.debugMode, ["-fdebug"]), 27 tuple(BuildOption.releaseMode, ["-frelease"]), 28 tuple(BuildOption.coverage, ["-fprofile-arcs", "-ftest-coverage"]), 29 tuple(BuildOption.debugInfo, ["-g"]), 30 tuple(BuildOption.debugInfoC, ["-g"]), 31 //tuple(BuildOption.alwaysStackFrame, ["-X"]), 32 //tuple(BuildOption.stackStomping, ["-X"]), 33 tuple(BuildOption.inline, ["-finline-functions"]), 34 tuple(BuildOption.noBoundsCheck, ["-fno-bounds-check"]), 35 tuple(BuildOption.optimize, ["-O2"]), 36 tuple(BuildOption.profile, ["-pg"]), 37 tuple(BuildOption.unittests, ["-funittest"]), 38 tuple(BuildOption.verbose, ["-v"]), 39 tuple(BuildOption.ignoreUnknownPragmas, ["-fignore-unknown-pragmas"]), 40 tuple(BuildOption.syntaxOnly, ["-fsyntax-only"]), 41 tuple(BuildOption.warnings, ["-Wall"]), 42 tuple(BuildOption.warningsAsErrors, ["-Werror", "-Wall"]), 43 tuple(BuildOption.ignoreDeprecations, ["-Wno-deprecated"]), 44 tuple(BuildOption.deprecationWarnings, ["-Wdeprecated"]), 45 tuple(BuildOption.deprecationErrors, ["-Werror", "-Wdeprecated"]), 46 tuple(BuildOption.property, ["-fproperty"]), 47 //tuple(BuildOption.profileGC, ["-?"]), 48 tuple(BuildOption.betterC, ["-fno-druntime"]), 49 tuple(BuildOption.color, ["-fdiagnostics-color=always"]), 50 51 tuple(BuildOption._docs, ["-fdoc-dir=docs"]), 52 tuple(BuildOption._ddox, ["-Xfdocs.json", "-fdoc-file=__dummy.html"]), 53 ]; 54 55 @property string name() const { return "gdc"; } 56 57 string determineVersion(string compiler_binary, string verboseOutput) 58 { 59 const result = execute([ 60 compiler_binary, 61 "-dumpfullversion", 62 "-dumpversion" 63 ]); 64 65 return result.status == 0 ? result.output : null; 66 } 67 68 BuildPlatform determinePlatform(ref BuildSettings settings, string compiler_binary, string arch_override) 69 { 70 string[] arch_flags; 71 switch (arch_override) { 72 default: throw new UnsupportedArchitectureException(arch_override); 73 case "": break; 74 case "arm": arch_flags = ["-marm"]; break; 75 case "arm_thumb": arch_flags = ["-mthumb"]; break; 76 case "x86": arch_flags = ["-m32"]; break; 77 case "x86_64": arch_flags = ["-m64"]; break; 78 } 79 settings.addDFlags(arch_flags); 80 81 return probePlatform( 82 compiler_binary, 83 arch_flags ~ ["-fsyntax-only", "-v"], 84 arch_override 85 ); 86 } 87 88 void prepareBuildSettings(ref BuildSettings settings, const scope ref BuildPlatform platform, BuildSetting fields = BuildSetting.all) const 89 { 90 enforceBuildRequirements(settings); 91 92 // Keep the current dflags at the end of the array so that they will overwrite other flags. 93 // This allows user $DFLAGS to modify flags added by us. 94 const dflagsTail = settings.dflags; 95 settings.dflags = []; 96 97 if (!(fields & BuildSetting.options)) { 98 foreach (t; s_options) 99 if (settings.options & t[0]) 100 settings.addDFlags(t[1]); 101 } 102 103 if (!(fields & BuildSetting.versions)) { 104 settings.addDFlags(settings.versions.map!(s => "-fversion="~s)().array()); 105 settings.versions = null; 106 } 107 108 if (!(fields & BuildSetting.debugVersions)) { 109 settings.addDFlags(settings.debugVersions.map!(s => "-fdebug="~s)().array()); 110 settings.debugVersions = null; 111 } 112 113 if (!(fields & BuildSetting.importPaths)) { 114 settings.addDFlags(settings.importPaths.map!(s => "-I"~s)().array()); 115 settings.importPaths = null; 116 } 117 118 if (!(fields & BuildSetting.cImportPaths)) { 119 settings.addDFlags(settings.cImportPaths.map!(s => "-I"~s)().array()); 120 settings.cImportPaths = null; 121 } 122 123 if (!(fields & BuildSetting.stringImportPaths)) { 124 settings.addDFlags(settings.stringImportPaths.map!(s => "-J"~s)().array()); 125 settings.stringImportPaths = null; 126 } 127 128 if (!(fields & BuildSetting.sourceFiles)) { 129 settings.addDFlags(settings.sourceFiles); 130 settings.sourceFiles = null; 131 } 132 133 if (!(fields & BuildSetting.libs)) { 134 resolveLibs(settings, platform); 135 settings.addDFlags(settings.libs.map!(l => "-l"~l)().array()); 136 } 137 138 if (!(fields & BuildSetting.lflags)) { 139 settings.addDFlags(lflagsToDFlags(settings.lflags)); 140 settings.lflags = null; 141 } 142 143 if (settings.options & BuildOption.pic) 144 settings.addDFlags("-fPIC"); 145 146 settings.addDFlags(dflagsTail); 147 148 assert(fields & BuildSetting.dflags); 149 assert(fields & BuildSetting.copyFiles); 150 } 151 152 void extractBuildOptions(ref BuildSettings settings) const 153 { 154 Appender!(string[]) newflags; 155 next_flag: foreach (f; settings.dflags) { 156 foreach (t; s_options) 157 if (t[1].canFind(f)) { 158 settings.options |= t[0]; 159 continue next_flag; 160 } 161 if (f.startsWith("-fversion=")) settings.addVersions(f[10 .. $]); 162 else if (f.startsWith("-fdebug=")) settings.addDebugVersions(f[8 .. $]); 163 else newflags ~= f; 164 } 165 settings.dflags = newflags.data; 166 } 167 168 string getTargetFileName(in BuildSettings settings, in BuildPlatform platform) 169 const { 170 assert(settings.targetName.length > 0, "No target name set."); 171 final switch (settings.targetType) { 172 case TargetType.autodetect: assert(false, "Configurations must have a concrete target type."); 173 case TargetType.none: return null; 174 case TargetType.sourceLibrary: return null; 175 case TargetType.executable: 176 if (platform.isWindows()) 177 return settings.targetName ~ ".exe"; 178 else return settings.targetName.idup; 179 case TargetType.library: 180 case TargetType.staticLibrary: 181 return "lib" ~ settings.targetName ~ ".a"; 182 case TargetType.dynamicLibrary: 183 if (platform.isWindows()) 184 return settings.targetName ~ ".dll"; 185 else if (platform.platform.canFind("darwin")) 186 return "lib" ~ settings.targetName ~ ".dylib"; 187 else return "lib" ~ settings.targetName ~ ".so"; 188 case TargetType.object: 189 if (platform.isWindows()) 190 return settings.targetName ~ ".obj"; 191 else return settings.targetName ~ ".o"; 192 } 193 } 194 195 void setTarget(ref BuildSettings settings, in BuildPlatform platform, string tpath = null) const 196 { 197 const targetFileName = getTargetFileName(settings, platform); 198 199 final switch (settings.targetType) { 200 case TargetType.autodetect: assert(false, "Invalid target type: autodetect"); 201 case TargetType.none: assert(false, "Invalid target type: none"); 202 case TargetType.sourceLibrary: assert(false, "Invalid target type: sourceLibrary"); 203 case TargetType.executable: break; 204 case TargetType.library: 205 case TargetType.staticLibrary: 206 case TargetType.object: 207 settings.addDFlags("-c"); 208 break; 209 case TargetType.dynamicLibrary: 210 settings.addDFlags("-shared", "-fPIC"); 211 addDynamicLibName(settings, platform, targetFileName); 212 break; 213 } 214 215 if (tpath is null) 216 tpath = (NativePath(settings.targetPath) ~ targetFileName).toNativeString(); 217 settings.addDFlags("-o", tpath); 218 } 219 220 void invoke(in BuildSettings settings, in BuildPlatform platform, void delegate(int, string) output_callback, NativePath cwd) 221 { 222 auto res_file = getTempFile("dub-build", ".rsp"); 223 writeFile(res_file, join(settings.dflags.map!(s => escape(s)), "\n")); 224 225 logDiagnostic("%s %s", platform.compilerBinary, join(cast(string[])settings.dflags, " ")); 226 string[string] env; 227 foreach (aa; [settings.environments, settings.buildEnvironments]) 228 foreach (k, v; aa) 229 env[k] = v; 230 invokeTool([platform.compilerBinary, "@"~res_file.toNativeString()], output_callback, cwd, env); 231 } 232 233 void invokeLinker(in BuildSettings settings, in BuildPlatform platform, string[] objects, void delegate(int, string) output_callback, NativePath cwd) 234 { 235 import std.string; 236 string[] args; 237 // As the user is supposed to call setTarget prior to invoke, -o target is already set. 238 if (settings.targetType == TargetType.staticLibrary || settings.targetType == TargetType.staticLibrary) { 239 auto tpath = extractTarget(settings.dflags); 240 assert(tpath !is null, "setTarget should be called before invoke"); 241 args = [ "ar", "rcs", tpath ] ~ objects; 242 } else { 243 args = platform.compilerBinary ~ objects ~ settings.sourceFiles ~ settings.lflags ~ settings.dflags.filter!(f => isLinkageFlag(f)).array; 244 if (platform.platform.canFind("linux")) 245 args ~= "-L--no-as-needed"; // avoids linker errors due to libraries being specified in the wrong order 246 } 247 logDiagnostic("%s", args.join(" ")); 248 string[string] env; 249 foreach (aa; [settings.environments, settings.buildEnvironments]) 250 foreach (k, v; aa) 251 env[k] = v; 252 invokeTool(args, output_callback, cwd, env); 253 } 254 255 string[] lflagsToDFlags(const string[] lflags) const 256 { 257 string[] dflags; 258 foreach( f; lflags ) 259 { 260 if ( f == "") { 261 continue; 262 } 263 dflags ~= "-Xlinker"; 264 dflags ~= f; 265 } 266 267 return dflags; 268 } 269 } 270 271 private string extractTarget(const string[] args) { auto i = args.countUntil("-o"); return i >= 0 ? args[i+1] : null; } 272 273 private bool isLinkageFlag(string flag) { 274 switch (flag) { 275 case "-c": 276 return false; 277 default: 278 return true; 279 } 280 } 281 282 private string escape(string str) 283 { 284 auto ret = appender!string(); 285 foreach (char ch; str) { 286 switch (ch) { 287 default: ret.put(ch); break; 288 case '\\': ret.put(`\\`); break; 289 case ' ': ret.put(`\ `); break; 290 } 291 } 292 return ret.data; 293 }