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