Newer
Older
dub_jkp / source / dub / compilers / gdc.d
@Geod24 Geod24 on 29 Aug 2014 7 KB Various small fixes
  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.internal.utils;
  12. import dub.internal.vibecompat.core.log;
  13. import dub.internal.vibecompat.inet.path;
  14. import dub.platform;
  15.  
  16. import std.algorithm;
  17. import std.array;
  18. import std.conv;
  19. import std.exception;
  20. import std.file;
  21. import std.process;
  22. import std.random;
  23. import std.typecons;
  24.  
  25.  
  26. class GdcCompiler : Compiler {
  27. private static immutable s_options = [
  28. tuple(BuildOptions.debugMode, ["-fdebug"]),
  29. tuple(BuildOptions.releaseMode, ["-frelease"]),
  30. tuple(BuildOptions.coverage, ["-fprofile-arcs", "-ftest-coverage"]),
  31. tuple(BuildOptions.debugInfo, ["-g"]),
  32. tuple(BuildOptions.debugInfoC, ["-g", "-fdebug-c"]),
  33. //tuple(BuildOptions.alwaysStackFrame, ["-X"]),
  34. //tuple(BuildOptions.stackStomping, ["-X"]),
  35. tuple(BuildOptions.inline, ["-finline-functions"]),
  36. tuple(BuildOptions.noBoundsCheck, ["-fno-bounds-check"]),
  37. tuple(BuildOptions.optimize, ["-O3"]),
  38. tuple(BuildOptions.profile, ["-pg"]),
  39. tuple(BuildOptions.unittests, ["-funittest"]),
  40. tuple(BuildOptions.verbose, ["-fd-verbose"]),
  41. tuple(BuildOptions.ignoreUnknownPragmas, ["-fignore-unknown-pragmas"]),
  42. tuple(BuildOptions.syntaxOnly, ["-fsyntax-only"]),
  43. tuple(BuildOptions.warnings, ["-Wall"]),
  44. tuple(BuildOptions.warningsAsErrors, ["-Werror", "-Wall"]),
  45. tuple(BuildOptions.ignoreDeprecations, ["-Wno-deprecated"]),
  46. tuple(BuildOptions.deprecationWarnings, ["-Wdeprecated"]),
  47. tuple(BuildOptions.deprecationErrors, ["-Werror", "-Wdeprecated"]),
  48. tuple(BuildOptions.property, ["-fproperty"]),
  49. ];
  50.  
  51. @property string name() const { return "gdc"; }
  52.  
  53. BuildPlatform determinePlatform(ref BuildSettings settings, string compiler_binary, string arch_override)
  54. {
  55. import std.process;
  56. import std.string;
  57.  
  58. auto fil = generatePlatformProbeFile();
  59.  
  60. string[] arch_flags;
  61.  
  62. switch (arch_override) {
  63. default: throw new Exception("Unsupported architecture: "~arch_override);
  64. case "": break;
  65. case "x86": arch_flags = ["-m32"]; break;
  66. case "x86_64": arch_flags = ["-m64"]; break;
  67. }
  68. settings.addDFlags(arch_flags);
  69.  
  70. auto result = executeShell(escapeShellCommand(compiler_binary ~ arch_flags ~ ["-c", "-o", (getTempDir()~"dub_platform_probe").toNativeString(), fil.toNativeString()]));
  71. enforce(result.status == 0, format("Failed to invoke the compiler %s to determine the build platform: %s",
  72. compiler_binary, result.output));
  73.  
  74. auto build_platform = readPlatformProbe(result.output);
  75. build_platform.compilerBinary = compiler_binary;
  76.  
  77. if (build_platform.compiler != this.name) {
  78. logWarn(`The determined compiler type "%s" doesn't match the expected type "%s". This will probably result in build errors.`,
  79. build_platform.compiler, this.name);
  80. }
  81.  
  82. if (arch_override.length && !build_platform.architecture.canFind(arch_override)) {
  83. logWarn(`Failed to apply the selected architecture %s. Got %s.`,
  84. arch_override, build_platform.architecture);
  85. }
  86.  
  87. return build_platform;
  88. }
  89.  
  90. void prepareBuildSettings(ref BuildSettings settings, BuildSetting fields = BuildSetting.all) const
  91. {
  92. enforceBuildRequirements(settings);
  93.  
  94. if (!(fields & BuildSetting.options)) {
  95. foreach (t; s_options)
  96. if (settings.options & t[0])
  97. settings.addDFlags(t[1]);
  98. }
  99.  
  100. if (!(fields & BuildSetting.versions)) {
  101. settings.addDFlags(settings.versions.map!(s => "-fversion="~s)().array());
  102. settings.versions = null;
  103. }
  104.  
  105. if (!(fields & BuildSetting.debugVersions)) {
  106. settings.addDFlags(settings.debugVersions.map!(s => "-fdebug="~s)().array());
  107. settings.debugVersions = null;
  108. }
  109.  
  110. if (!(fields & BuildSetting.importPaths)) {
  111. settings.addDFlags(settings.importPaths.map!(s => "-I"~s)().array());
  112. settings.importPaths = null;
  113. }
  114.  
  115. if (!(fields & BuildSetting.stringImportPaths)) {
  116. settings.addDFlags(settings.stringImportPaths.map!(s => "-J"~s)().array());
  117. settings.stringImportPaths = null;
  118. }
  119.  
  120. if (!(fields & BuildSetting.sourceFiles)) {
  121. settings.addDFlags(settings.sourceFiles);
  122. settings.sourceFiles = null;
  123. }
  124.  
  125. if (!(fields & BuildSetting.libs)) {
  126. resolveLibs(settings);
  127. settings.addDFlags(settings.libs.map!(l => "-l"~l)().array());
  128. }
  129.  
  130. if (!(fields & BuildSetting.lflags)) {
  131. foreach( f; settings.lflags )
  132. settings.addDFlags(["-Xlinker", f]);
  133. settings.lflags = null;
  134. }
  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. void setTarget(ref BuildSettings settings, in BuildPlatform platform, string tpath = null) const
  157. {
  158. final switch (settings.targetType) {
  159. case TargetType.autodetect: assert(false, "Invalid target type: autodetect");
  160. case TargetType.none: assert(false, "Invalid target type: none");
  161. case TargetType.sourceLibrary: assert(false, "Invalid target type: sourceLibrary");
  162. case TargetType.executable: break;
  163. case TargetType.library:
  164. case TargetType.staticLibrary:
  165. settings.addDFlags("-c");
  166. break;
  167. case TargetType.dynamicLibrary:
  168. settings.addDFlags("-shared", "-fPIC");
  169. break;
  170. }
  171.  
  172. if (tpath is null)
  173. tpath = (Path(settings.targetPath) ~ getTargetFileName(settings, platform)).toNativeString();
  174. settings.addDFlags("-o", tpath);
  175. }
  176.  
  177. void invoke(in BuildSettings settings, in BuildPlatform platform, void delegate(int, string) output_callback)
  178. {
  179. auto res_file = getTempDir() ~ ("dub-build-"~uniform(0, uint.max).to!string~"-.rsp");
  180. std.file.write(res_file.toNativeString(), join(settings.dflags.map!(s => escape(s)), "\n"));
  181. scope (exit) remove(res_file.toNativeString());
  182.  
  183. logDiagnostic("%s %s", platform.compilerBinary, join(cast(string[])settings.dflags, " "));
  184. invokeTool([platform.compilerBinary, "@"~res_file.toNativeString()], output_callback);
  185. }
  186.  
  187. void invokeLinker(in BuildSettings settings, in BuildPlatform platform, string[] objects, void delegate(int, string) output_callback)
  188. {
  189. import std.string;
  190. string[] args;
  191. // As the user is supposed to call setTarget prior to invoke, -o target is already set.
  192. if (settings.targetType == TargetType.staticLibrary || settings.targetType == TargetType.staticLibrary) {
  193. auto tpath = extractTarget(settings.dflags);
  194. assert(tpath !is null, "setTarget should be called before invoke");
  195. args = [ "ar", "rcs", tpath ] ~ objects;
  196. } else {
  197. args = platform.compilerBinary ~ objects ~ settings.sourceFiles ~ settings.lflags ~ settings.dflags.filter!(f => isLinkageFlag(f)).array;
  198. version(linux) args ~= "-L--no-as-needed"; // avoids linker errors due to libraries being speficied in the wrong order by DMD
  199. }
  200. logDiagnostic("%s", args.join(" "));
  201. invokeTool(args, output_callback);
  202. }
  203. }
  204.  
  205. private string extractTarget(const string[] args) { auto i = args.countUntil("-o"); return i >= 0 ? args[i+1] : null; }
  206.  
  207. private bool isLinkageFlag(string flag) {
  208. switch (flag) {
  209. case "-c":
  210. return false;
  211. default:
  212. return true;
  213. }
  214. }
  215.  
  216. private string escape(string str)
  217. {
  218. auto ret = appender!string();
  219. foreach (char ch; str) {
  220. switch (ch) {
  221. default: ret.put(ch); break;
  222. case '\\': ret.put(`\\`); break;
  223. case ' ': ret.put(`\ `); break;
  224. }
  225. }
  226. return ret.data;
  227. }