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; 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 if (!(fields & BuildSetting.options)) { 93 foreach (t; s_options) 94 if (settings.options & t[0]) 95 settings.addDFlags(t[1]); 96 } 97 98 if (!(fields & BuildSetting.versions)) { 99 settings.addDFlags(settings.versions.map!(s => "-fversion="~s)().array()); 100 settings.versions = null; 101 } 102 103 if (!(fields & BuildSetting.debugVersions)) { 104 settings.addDFlags(settings.debugVersions.map!(s => "-fdebug="~s)().array()); 105 settings.debugVersions = null; 106 } 107 108 if (!(fields & BuildSetting.importPaths)) { 109 settings.addDFlags(settings.importPaths.map!(s => "-I"~s)().array()); 110 settings.importPaths = null; 111 } 112 113 if (!(fields & BuildSetting.stringImportPaths)) { 114 settings.addDFlags(settings.stringImportPaths.map!(s => "-J"~s)().array()); 115 settings.stringImportPaths = null; 116 } 117 118 if (!(fields & BuildSetting.sourceFiles)) { 119 settings.addDFlags(settings.sourceFiles); 120 settings.sourceFiles = null; 121 } 122 123 if (!(fields & BuildSetting.libs)) { 124 resolveLibs(settings, platform); 125 settings.addDFlags(settings.libs.map!(l => "-l"~l)().array()); 126 } 127 128 if (!(fields & BuildSetting.lflags)) { 129 settings.addDFlags(lflagsToDFlags(settings.lflags)); 130 settings.lflags = null; 131 } 132 133 if (settings.options & BuildOption.pic) 134 settings.addDFlags("-fPIC"); 135 136 assert(fields & BuildSetting.dflags); 137 assert(fields & BuildSetting.copyFiles); 138 } 139 140 void extractBuildOptions(ref BuildSettings settings) const 141 { 142 Appender!(string[]) newflags; 143 next_flag: foreach (f; settings.dflags) { 144 foreach (t; s_options) 145 if (t[1].canFind(f)) { 146 settings.options |= t[0]; 147 continue next_flag; 148 } 149 if (f.startsWith("-fversion=")) settings.addVersions(f[10 .. $]); 150 else if (f.startsWith("-fdebug=")) settings.addDebugVersions(f[8 .. $]); 151 else newflags ~= f; 152 } 153 settings.dflags = newflags.data; 154 } 155 156 string getTargetFileName(in BuildSettings settings, in BuildPlatform platform) 157 const { 158 assert(settings.targetName.length > 0, "No target name set."); 159 final switch (settings.targetType) { 160 case TargetType.autodetect: assert(false, "Configurations must have a concrete target type."); 161 case TargetType.none: return null; 162 case TargetType.sourceLibrary: return null; 163 case TargetType.executable: 164 if (platform.isWindows()) 165 return settings.targetName ~ ".exe"; 166 else return settings.targetName.idup; 167 case TargetType.library: 168 case TargetType.staticLibrary: 169 return "lib" ~ settings.targetName ~ ".a"; 170 case TargetType.dynamicLibrary: 171 if (platform.isWindows()) 172 return settings.targetName ~ ".dll"; 173 else if (platform.platform.canFind("darwin")) 174 return "lib" ~ settings.targetName ~ ".dylib"; 175 else return "lib" ~ settings.targetName ~ ".so"; 176 case TargetType.object: 177 if (platform.isWindows()) 178 return settings.targetName ~ ".obj"; 179 else return settings.targetName ~ ".o"; 180 } 181 } 182 183 void setTarget(ref BuildSettings settings, in BuildPlatform platform, string tpath = null) const 184 { 185 const targetFileName = getTargetFileName(settings, platform); 186 187 final switch (settings.targetType) { 188 case TargetType.autodetect: assert(false, "Invalid target type: autodetect"); 189 case TargetType.none: assert(false, "Invalid target type: none"); 190 case TargetType.sourceLibrary: assert(false, "Invalid target type: sourceLibrary"); 191 case TargetType.executable: break; 192 case TargetType.library: 193 case TargetType.staticLibrary: 194 case TargetType.object: 195 settings.addDFlags("-c"); 196 break; 197 case TargetType.dynamicLibrary: 198 settings.addDFlags("-shared", "-fPIC"); 199 addDynamicLibName(settings, platform, targetFileName); 200 break; 201 } 202 203 if (tpath is null) 204 tpath = (NativePath(settings.targetPath) ~ targetFileName).toNativeString(); 205 settings.addDFlags("-o", tpath); 206 } 207 208 void invoke(in BuildSettings settings, in BuildPlatform platform, void delegate(int, string) output_callback) 209 { 210 auto res_file = getTempFile("dub-build", ".rsp"); 211 writeFile(res_file, join(settings.dflags.map!(s => escape(s)), "\n")); 212 213 logDiagnostic("%s %s", platform.compilerBinary, join(cast(string[])settings.dflags, " ")); 214 string[string] env; 215 foreach (aa; [settings.environments, settings.buildEnvironments]) 216 foreach (k, v; aa) 217 env[k] = v; 218 invokeTool([platform.compilerBinary, "@"~res_file.toNativeString()], output_callback, env); 219 } 220 221 void invokeLinker(in BuildSettings settings, in BuildPlatform platform, string[] objects, void delegate(int, string) output_callback) 222 { 223 import std.string; 224 string[] args; 225 // As the user is supposed to call setTarget prior to invoke, -o target is already set. 226 if (settings.targetType == TargetType.staticLibrary || settings.targetType == TargetType.staticLibrary) { 227 auto tpath = extractTarget(settings.dflags); 228 assert(tpath !is null, "setTarget should be called before invoke"); 229 args = [ "ar", "rcs", tpath ] ~ objects; 230 } else { 231 args = platform.compilerBinary ~ objects ~ settings.sourceFiles ~ settings.lflags ~ settings.dflags.filter!(f => isLinkageFlag(f)).array; 232 if (platform.platform.canFind("linux")) 233 args ~= "-L--no-as-needed"; // avoids linker errors due to libraries being specified in the wrong order 234 } 235 logDiagnostic("%s", args.join(" ")); 236 string[string] env; 237 foreach (aa; [settings.environments, settings.buildEnvironments]) 238 foreach (k, v; aa) 239 env[k] = v; 240 invokeTool(args, output_callback, env); 241 } 242 243 string[] lflagsToDFlags(const string[] lflags) const 244 { 245 string[] dflags; 246 foreach( f; lflags ) 247 { 248 if ( f == "") { 249 continue; 250 } 251 dflags ~= "-Xlinker"; 252 dflags ~= f; 253 } 254 255 return dflags; 256 } 257 } 258 259 private string extractTarget(const string[] args) { auto i = args.countUntil("-o"); return i >= 0 ? args[i+1] : null; } 260 261 private bool isLinkageFlag(string flag) { 262 switch (flag) { 263 case "-c": 264 return false; 265 default: 266 return true; 267 } 268 } 269 270 private string escape(string str) 271 { 272 auto ret = appender!string(); 273 foreach (char ch; str) { 274 switch (ch) { 275 default: ret.put(ch); break; 276 case '\\': ret.put(`\\`); break; 277 case ' ': ret.put(`\ `); break; 278 } 279 } 280 return ret.data; 281 }