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