Newer
Older
dub_jkp / source / dub / generators / rdmd.d
  1. /**
  2. Generator for direct RDMD builds.
  3. Copyright: © 2013 rejectedsoftware e.K.
  4. License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file.
  5. Authors: Sönke Ludwig
  6. */
  7. module dub.generators.rdmd;
  8.  
  9. import dub.compilers.compiler;
  10. import dub.generators.generator;
  11. import dub.package_;
  12. import dub.packagemanager;
  13. import dub.project;
  14.  
  15. import std.array;
  16. import std.conv;
  17. import std.exception;
  18. import std.file;
  19. import std.string;
  20. import stdx.process;
  21.  
  22. import vibe.core.file;
  23. import vibe.core.log;
  24. import vibe.inet.path;
  25.  
  26.  
  27. class RdmdGenerator : ProjectGenerator {
  28. private {
  29. Project m_project;
  30. PackageManager m_pkgMgr;
  31. }
  32. this(Project app, PackageManager mgr)
  33. {
  34. m_project = app;
  35. m_pkgMgr = mgr;
  36. }
  37. void generateProject(GeneratorSettings settings)
  38. {
  39.  
  40. //Added check for existance of [AppNameInPackagejson].d
  41. //If exists, use that as the starting file.
  42. auto outfile = getBinName(m_project);
  43. auto mainsrc = getMainSourceFile(m_project);
  44.  
  45. logDebug("Application output name is '%s'", outfile);
  46.  
  47. // Create start script, which will be used by the calling bash/cmd script.
  48. // build "rdmd --force %DFLAGS% -I%~dp0..\source -Jviews -Isource @deps.txt %LIBS% source\app.d" ~ application arguments
  49. // or with "/" instead of "\"
  50. string[] flags = ["--force", "--build-only", "--compiler="~settings.compilerBinary];
  51. Path run_exe_file;
  52. if( !settings.run ){
  53. flags ~= "-of"~(m_project.binaryPath~outfile).toNativeString();
  54. } else {
  55. import std.random;
  56. auto rnd = to!string(uniform(uint.min, uint.max)) ~ "-";
  57. auto tmp = environment.get("TEMP");
  58. if( !tmp.length ) tmp = environment.get("TMP");
  59. if( !tmp.length ){
  60. version(Posix) tmp = "/tmp";
  61. else tmp = ".";
  62. }
  63. run_exe_file = Path(tmp~"/.rdmd/source/"~rnd~outfile);
  64. flags ~= "-of"~run_exe_file.toNativeString();
  65. }
  66.  
  67. auto buildsettings = settings.buildSettings;
  68. m_project.addBuildSettings(buildsettings, settings.platform, settings.config);
  69. buildsettings.addDFlags(["-w"/*, "-property"*/]);
  70. string dflags = environment.get("DFLAGS");
  71. if( dflags ){
  72. settings.buildType = "$DFLAGS";
  73. buildsettings.addDFlags(dflags.split());
  74. } else {
  75. addBuildTypeFlags(buildsettings, settings.buildType);
  76. }
  77.  
  78. settings.compiler.prepareBuildSettings(buildsettings, BuildSetting.commandLine);
  79. flags ~= buildsettings.dflags;
  80. flags ~= (mainsrc).toNativeString();
  81.  
  82. if( settings.config.length ) logInfo("Building configuration "~settings.config~", build type "~settings.buildType);
  83. else logInfo("Building default configuration, build type "~settings.buildType);
  84.  
  85. logInfo("Running %s", "rdmd " ~ join(flags, " "));
  86. auto rdmd_pid = spawnProcess("rdmd", flags);
  87. auto result = rdmd_pid.wait();
  88. enforce(result == 0, "Build command failed with exit code "~to!string(result));
  89.  
  90. // TODO: move to a common place - this is not generator specific
  91. if( buildsettings.copyFiles.length ){
  92. logInfo("Copying files...");
  93. foreach( f; buildsettings.copyFiles ){
  94. auto src = Path(f);
  95. auto dst = (run_exe_file.empty ? m_project.binaryPath : run_exe_file.parentPath) ~ Path(f).head;
  96. logDebug(" %s to %s", src.toNativeString(), dst.toNativeString());
  97. try copyFile(src, dst, true);
  98. catch logWarn("Failed to copy to %s", dst.toNativeString());
  99. }
  100. }
  101.  
  102. if( settings.run ){
  103. auto prg_pid = spawnProcess(run_exe_file.toNativeString(), settings.runArgs);
  104. result = prg_pid.wait();
  105. remove(run_exe_file.toNativeString());
  106. foreach( f; buildsettings.copyFiles )
  107. remove((run_exe_file.parentPath ~ Path(f).head).toNativeString());
  108. enforce(result == 0, "Program exited with code "~to!string(result));
  109. }
  110. }
  111. }
  112.  
  113. private string getBinName(in Project prj)
  114. {
  115. // take the project name as the base or fall back to "app"
  116. string ret = prj.name;
  117. if( ret.length == 0 ) ret ="app";
  118. version(Windows) { ret ~= ".exe"; }
  119. return ret;
  120. }
  121.  
  122. private Path getMainSourceFile(in Project prj)
  123. {
  124. auto p = Path("source") ~ (prj.name ~ ".d");
  125. return existsFile(p) ? p : Path("source/app.d");
  126. }
  127.