|
| 1 | +#!/usr/bin/env rdmd |
| 2 | +/** |
| 3 | +Phobos V3 Build Script |
| 4 | +
|
| 5 | +Usage: |
| 6 | + ./build_v3.d [debug,release,unittest] |
| 7 | +*/ |
| 8 | + |
| 9 | +import std.conv; |
| 10 | +import std.datetime; |
| 11 | +import std.file; |
| 12 | +import std.path; |
| 13 | +import std.process; |
| 14 | +import std.stdio; |
| 15 | +import std.string; |
| 16 | + |
| 17 | +int main(string[] args) |
| 18 | +{ |
| 19 | + int result = 0; |
| 20 | + |
| 21 | + bool buildUnittest = false; |
| 22 | + bool buildRelease = false; |
| 23 | + if (args.length > 1) |
| 24 | + { |
| 25 | + buildUnittest = args[1] == "unittest"; |
| 26 | + buildRelease = args[1] == "release"; |
| 27 | + } |
| 28 | + |
| 29 | + string argFilePath = buildNormalizedPath(getcwd(), "phobosbuildargs.txt"); |
| 30 | + auto dFiles = dirEntries(buildNormalizedPath(getcwd(), "phobos"), "*.d", SpanMode.breadth); |
| 31 | + auto argFile = File(argFilePath, "w"); |
| 32 | + |
| 33 | + version(Windows) |
| 34 | + { |
| 35 | + string unittestExecutable = buildNormalizedPath(getcwd(), "unittest.exe"); |
| 36 | + } |
| 37 | + else |
| 38 | + { |
| 39 | + string unittestExecutable = buildNormalizedPath(getcwd(), "unittest"); |
| 40 | + } |
| 41 | + |
| 42 | + scope(exit) |
| 43 | + { |
| 44 | + argFile.close(); |
| 45 | + remove(argFilePath); |
| 46 | + |
| 47 | + if (exists(unittestExecutable)) remove(unittestExecutable); |
| 48 | + } |
| 49 | + |
| 50 | + result = runCommand("dmd --version", getcwd()); |
| 51 | + if (result != 0) |
| 52 | + { |
| 53 | + writeln("Compiler Failure."); |
| 54 | + return result; |
| 55 | + } |
| 56 | + |
| 57 | + writeln("Source files:"); |
| 58 | + //Add source file list to args file. |
| 59 | + foreach(dFile; dFiles) |
| 60 | + { |
| 61 | + if (dFile.isDir()) continue; |
| 62 | + argFile.writeln(dFile.name); |
| 63 | + writeln(dFile.name); |
| 64 | + } |
| 65 | + |
| 66 | + //Add appropriate DMD arguments to the args file. |
| 67 | + argFile.writeln("-od=./lib"); |
| 68 | + if (buildUnittest) |
| 69 | + { |
| 70 | + argFile.writeln("-main"); |
| 71 | + argFile.writeln("-unittest"); |
| 72 | + argFile.writeln("-debug"); |
| 73 | + |
| 74 | + version(Windows) |
| 75 | + { |
| 76 | + argFile.writeln("-of=unittest.exe"); |
| 77 | + } |
| 78 | + else |
| 79 | + { |
| 80 | + argFile.writeln("-of=unittest"); |
| 81 | + } |
| 82 | + } |
| 83 | + else if (buildRelease) |
| 84 | + { |
| 85 | + argFile.writeln("-release -O"); |
| 86 | + argFile.writeln("-lib"); |
| 87 | + argFile.writeln("-of=libphobos3"); |
| 88 | + } |
| 89 | + else |
| 90 | + { |
| 91 | + argFile.writeln("-debug"); |
| 92 | + argFile.writeln("-lib"); |
| 93 | + argFile.writeln("-of=libphobos3-debug"); |
| 94 | + } |
| 95 | + |
| 96 | + argFile.flush(); |
| 97 | + argFile.close(); |
| 98 | + |
| 99 | + //Run the build. |
| 100 | + result = runCommand("dmd @\"" ~ argFilePath ~ "\"", getcwd()); |
| 101 | + if (result != 0) |
| 102 | + { |
| 103 | + writeln("Build failed."); |
| 104 | + return result; |
| 105 | + } |
| 106 | + else |
| 107 | + { |
| 108 | + writeln("Build successful."); |
| 109 | + writeln(); |
| 110 | + } |
| 111 | + |
| 112 | + //Run unittests if built. |
| 113 | + if (buildUnittest) |
| 114 | + { |
| 115 | + writeln("Running tests..."); |
| 116 | + result = runCommand(unittestExecutable, getcwd()); |
| 117 | + |
| 118 | + if (result != 0) |
| 119 | + { |
| 120 | + writeln("Tests failed."); |
| 121 | + return result; |
| 122 | + } |
| 123 | + else |
| 124 | + { |
| 125 | + writeln("Tests successful."); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + return result; |
| 130 | +} |
| 131 | + |
| 132 | +private int runCommand(string command, string workDir) |
| 133 | +{ |
| 134 | + auto pid = pipeShell(command, Redirect.all, null, Config.none, workDir); |
| 135 | + int result = wait(pid.pid); |
| 136 | + foreach (line; pid.stdout.byLine) writeln(line); |
| 137 | + foreach (line; pid.stderr.byLine) writeln(line); |
| 138 | + writeln(); |
| 139 | + return result; |
| 140 | +} |
0 commit comments