Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

download/build d

By: a guest on Dec 14th, 2012  |  syntax: C++  |  size: 4.70 KB  |  views: 34  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /**
  2.    Responsible for grabbing latest source and building
  3. */
  4.  
  5. module build.dsystem;
  6.  
  7. import std.conv;
  8. import std.file;
  9. import std.path;
  10. import std.process;
  11. import std.stdio;
  12.  
  13. struct SrcGrabber {
  14.   const(string) PhobosUrl = "https://github.com/D-Programming-Language/phobos.git";
  15.  
  16.   const(string) DRuntimeUrl = "https://github.com/D-Programming-Language/druntime.git";
  17.  
  18.   const(string) DmdUrl = "https://github.com/D-Programming-Language/dmd.git";
  19.  
  20.   const(string) ToolsUrl = "https://github.com/D-Programming-Language/tools.git";
  21.  
  22.  
  23. // custom <dstruct src_grabber public_section>
  24.  
  25.   @disable this();
  26.  
  27.   this(string targetPath,
  28.        string phobosUrl = PhobosUrl,
  29.        string dRuntimeUrl = DRuntimeUrl,
  30.        string dmdUrl = DmdUrl,
  31.        string toolsUrl = ToolsUrl) {
  32.     _targetPath = targetPath;
  33.     _phobosUrl = phobosUrl;
  34.     _dRuntimeUrl = dRuntimeUrl;
  35.     _dmdUrl = dmdUrl;
  36.     _toolsUrl = toolsUrl;
  37.   }
  38.  
  39.   void grab() {
  40.     bool firstTime = !exists(_targetPath);
  41.     if(firstTime) {
  42.       mkdirRecurse(_targetPath);
  43.     }
  44.     cd(_targetPath);
  45.     string[] all = [_phobosUrl, _dRuntimeUrl, _dmdUrl, _toolsUrl];
  46.     foreach(repository; all) {
  47.       auto folder = baseName(stripExtension(repository));
  48.  
  49.       if(firstTime || !exists(folder)) {
  50.         runCommand("git clone "~repository);
  51.       } else {
  52.         cd(folder);
  53.         runCommand("git pull");
  54.         cd(_targetPath);
  55.       }
  56.     }
  57.   }
  58.  
  59. // end <dstruct src_grabber public_section>
  60.  
  61.   private {
  62.     /**
  63.        Where to download the files
  64.     */
  65.     string _targetPath;
  66.     string _phobosUrl;
  67.     string _dRuntimeUrl;
  68.     string _dmdUrl;
  69.     string _toolsUrl;
  70.   }
  71. }
  72.  
  73. struct SrcBuilder {
  74.   /**
  75.      Discriminate on what to build
  76.   */
  77.   enum BuildType {
  78.     DebugBuild,
  79.     ReleaseBuild
  80.   }
  81.  
  82.  
  83. // custom <dstruct src_builder public_section>
  84.  
  85.   @property bool isDebug() const {
  86.     return _buildType == BuildType.DebugBuild;
  87.   }
  88.  
  89.   this(string srcPath, BuildType buildType = BuildType.ReleaseBuild) {
  90.     _buildType = buildType;
  91.     _srcPath = buildPath(srcPath, (isDebug? "debug" : "release"));
  92.   }
  93.  
  94.   void build(bool grabFirst = true) {
  95.     if(grabFirst) {
  96.       auto grabber = SrcGrabber(_srcPath);
  97.       grabber.grab();
  98.     }
  99.     version(linux) {
  100.       auto makefile = "posix.mak";
  101.     }
  102.     version(Windows) {
  103.       auto makefile = "win32.mak";
  104.     }
  105.  
  106.     auto debugFlag = (isDebug? " DEBUG=1 " : "");
  107.     auto buildFlag = (isDebug? " BUILD=debug " : "");
  108.  
  109.     version(X86) {
  110.       const auto cpu = " TARGET_CPU=X86 ";
  111.     } else version(X86_64) {
  112.       const auto cpu = " TARGET_CPU=X86 ";
  113.     } else {
  114.       static assert(0, "Put your cpu type here");
  115.     }
  116.  
  117.     auto dryRun = "";
  118.  
  119.     cd(buildPath(_srcPath, "dmd", "src"));
  120.     runCommand(text("make ", dryRun, " -f ", makefile, cpu, " clean"));
  121.     runCommand(text("make ", dryRun, " MODEL=64 ", debugFlag, cpu, "-f ", makefile));
  122.  
  123.  
  124.     // TODO: druntime is not needed directly as phobos builds what is needed
  125.     // After building dmd, adjust path to be sure to use the just built dmd
  126.     // instead of current one
  127.    
  128.     cd(buildPath(_srcPath, "druntime"));
  129.     runCommand(text("make ", dryRun, " -f ", makefile, cpu, " clean"));
  130.     runCommand(text("make ", dryRun, " MODEL=64 ", debugFlag, cpu, " -f ", makefile));
  131.  
  132.     cd(buildPath(_srcPath, "phobos"));
  133.     runCommand(text("make ", dryRun, " -f ", makefile, cpu, " clean"));
  134.     runCommand(text("make ", dryRun, " MODEL=64 ", buildFlag, cpu, " -f ", makefile));
  135.  
  136.     static if(0) {
  137.       cd(buildPath(_srcPath, "tools"));
  138.       runCommand(text("make ", dryRun, " -f ", makefile, cpu, " clean"));
  139.       runCommand(text("make ", dryRun, " MODEL=64 ", debugFlag, cpu, " -f ", makefile));
  140.     }
  141.   }
  142.  
  143. // end <dstruct src_builder public_section>
  144.  
  145.   private {
  146.     BuildType _buildType;
  147.     /**
  148.        Where to find the source to build
  149.     */
  150.     string _srcPath;
  151.   }
  152. }
  153.  
  154. // custom <dmodule dsystem public_section>
  155.  
  156. void log(string msg) {
  157.   writeln(msg);
  158.   stdout.flush();
  159. }
  160.  
  161. void runCommand(string command) {
  162.   log(text("Running: ", command));
  163.   system(command);
  164. }
  165.  
  166. void cd(string path) {
  167.   log(text("cd to ", path));
  168.   chdir(path);
  169. }
  170.  
  171. // end <dmodule dsystem public_section>
  172.  
  173.  
  174. unittest {
  175.  
  176. // custom <dmodule dsystem unittest>
  177.  
  178.   auto home = environment.get("HOME");
  179.   auto targetPath = buildPath(home, "stage", "d_master");
  180.  
  181.   static if(1) {
  182.     auto releaseBuilder = SrcBuilder(targetPath, SrcBuilder.BuildType.ReleaseBuild);
  183.     releaseBuilder.build(false);
  184.   }
  185.  
  186.   static if(0) {
  187.     auto debugBuilder = SrcBuilder(targetPath, SrcBuilder.BuildType.DebugBuild);
  188.     debugBuilder.build(false);
  189.   }
  190.  
  191.  
  192. // end <dmodule dsystem unittest>
  193. }