|
| 1 | +import fs = std.file; |
| 2 | +import std.algorithm; |
| 3 | +import std.array; |
| 4 | +import std.process; |
| 5 | +import std.stdio; |
| 6 | +import std.string; |
| 7 | + |
| 8 | +int main(string[] args) |
| 9 | +{ |
| 10 | + auto archIndex = args.countUntil("ARCH"); |
| 11 | + auto platformIndex = args.countUntil("PLATFORM"); |
| 12 | + |
| 13 | + if (archIndex == -1 || platformIndex == -1) |
| 14 | + { |
| 15 | + stderr.writeln("Usage: ", args[0], " ARCH [archs...] PLATFORM [platforms...]"); |
| 16 | + return 1; |
| 17 | + } |
| 18 | + |
| 19 | + auto platforms = determinePlatforms(args[archIndex + 1 .. platformIndex], args[platformIndex + 1 .. $]); |
| 20 | + |
| 21 | + if (!platforms.mapNames.length) |
| 22 | + { |
| 23 | + stderr.writeln("The target platform is not yet supported by DORM, ", |
| 24 | + "please open an issue or pull request for the target binary name! (DUB arguments: ", |
| 25 | + args[1 .. $], ")"); |
| 26 | + |
| 27 | + return 1; |
| 28 | + } |
| 29 | + |
| 30 | + if (fs.exists(platforms.libname)) |
| 31 | + return 0; // already downloaded & validated or manually compiled |
| 32 | + |
| 33 | + try |
| 34 | + { |
| 35 | + downloadTool("rorm-cli", platforms.cliname, platforms, false, "`dub run dorm` will not work properly - download rorm-cli manually!"); |
| 36 | + downloadTool("rorm-lib", platforms.libname, platforms, true, "Please manually compile rorm-lib for this platform."); |
| 37 | + |
| 38 | + return 0; |
| 39 | + } |
| 40 | + catch (Exception e) |
| 41 | + { |
| 42 | + stderr.writeln("Failed to download precompiled rorm binaries: ", e.msg); |
| 43 | + stderr.writeln(); |
| 44 | + stderr.writeln("TIP: you can manually compile rorm-lib to skip the automatic download step"); |
| 45 | + stderr.writeln("Expected to find or download '", platforms.libname, "' in '", fs.getcwd, "'"); |
| 46 | + stderr.writeln("DUB target: ", args[1 .. $]); |
| 47 | + return 1; |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +void downloadTool(string name, string output, TargetPlatform target, bool required, string errorSupplemental = null) |
| 52 | +{ |
| 53 | + bool inSection = false; |
| 54 | + MapLine[] foundLines; |
| 55 | + foreach (line; File("download-map.txt", "r").byLine) |
| 56 | + { |
| 57 | + if (line.startsWith("# " ~ name)) |
| 58 | + inSection = true; |
| 59 | + else if (line.startsWith("#")) |
| 60 | + inSection = false; |
| 61 | + else if (inSection) |
| 62 | + { |
| 63 | + auto mapLine = MapLine.fromString(line.idup); |
| 64 | + if (target.mapNames.canFind(mapLine.platformName)) |
| 65 | + foundLines ~= mapLine; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + if (!foundLines.length) |
| 70 | + { |
| 71 | + if (required) |
| 72 | + throw new Exception("Does not have any precompiled binaries for this platform for " ~ name |
| 73 | + ~ (errorSupplemental.length ? " - " ~ errorSupplemental : "")); |
| 74 | + else |
| 75 | + stderr.writeln("Warning: Does not have any precompiled binaries for this platform for " ~ name |
| 76 | + ~ (errorSupplemental.length ? " - " ~ errorSupplemental : "")); |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + // first mapName takes priority |
| 81 | + foreach (wanted; target.mapNames) |
| 82 | + { |
| 83 | + foreach (found; foundLines) |
| 84 | + { |
| 85 | + if (found.platformName == wanted) |
| 86 | + { |
| 87 | + auto unverifiedOutput = output ~ ".unverified"; |
| 88 | + download(found.url, unverifiedOutput); |
| 89 | + validateSHA512(unverifiedOutput, found.sha512); |
| 90 | + fs.rename(unverifiedOutput, output); |
| 91 | + extractAndDelete(output); |
| 92 | + return; |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + assert(false); |
| 98 | +} |
| 99 | + |
| 100 | +struct MapLine |
| 101 | +{ |
| 102 | + string platformName; |
| 103 | + string url; |
| 104 | + string sha512; |
| 105 | + |
| 106 | + static MapLine fromString(string s) |
| 107 | + { |
| 108 | + auto parts = s.split("\t"); |
| 109 | + if (parts.length != 3) |
| 110 | + throw new Exception("malformed download-map.txt line: " ~ s); |
| 111 | + return MapLine(parts[0], parts[1], parts[2]); |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +struct TargetPlatform |
| 116 | +{ |
| 117 | + string[] mapNames, archs, platforms; |
| 118 | + string os; |
| 119 | + |
| 120 | + static TargetPlatform osx(string[] mapNames, string[] archs, string[] platforms) |
| 121 | + { |
| 122 | + return TargetPlatform(mapNames, archs, platforms, "osx"); |
| 123 | + } |
| 124 | + |
| 125 | + static TargetPlatform linux(string[] mapNames, string[] archs, string[] platforms) |
| 126 | + { |
| 127 | + return TargetPlatform(mapNames, archs, platforms, "linux"); |
| 128 | + } |
| 129 | + |
| 130 | + static TargetPlatform windows(string[] mapNames, string[] archs, string[] platforms) |
| 131 | + { |
| 132 | + return TargetPlatform(mapNames, archs, platforms, "windows"); |
| 133 | + } |
| 134 | + |
| 135 | + string libname() const @property |
| 136 | + { |
| 137 | + if (platforms.canFind("posix")) |
| 138 | + return "librorm.a"; |
| 139 | + else if (platforms.canFind("windows")) |
| 140 | + return "rorm.lib"; |
| 141 | + else |
| 142 | + throw new Exception("unsupported platform for libname"); |
| 143 | + } |
| 144 | + |
| 145 | + string cliname() const @property |
| 146 | + { |
| 147 | + if (platforms.canFind("posix")) |
| 148 | + return "rorm-cli"; |
| 149 | + else if (platforms.canFind("windows")) |
| 150 | + return "rorm-cli.exe"; |
| 151 | + else |
| 152 | + throw new Exception("unsupported platform for cliname"); |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +TargetPlatform determinePlatforms(string[] archs, string[] platforms) |
| 157 | +{ |
| 158 | + if (archs.canFind("aarch64")) |
| 159 | + { |
| 160 | + if (platforms.canFind("osx")) |
| 161 | + return TargetPlatform.osx(["osx_arm64", "osx_x86"], archs, platforms); |
| 162 | + else if (platforms.canFind("linux")) |
| 163 | + return TargetPlatform.linux(["linux_arm64"], archs, platforms); |
| 164 | + else |
| 165 | + return TargetPlatform.init; |
| 166 | + } |
| 167 | + else if (archs.canFind("x86_64")) |
| 168 | + { |
| 169 | + if (platforms.canFind("osx")) |
| 170 | + return TargetPlatform.osx(["osx_x86"], archs, platforms); |
| 171 | + else if (platforms.canFind("linux")) |
| 172 | + return TargetPlatform.linux(["linux_x86"], archs, platforms); |
| 173 | + else if (platforms.canFind("windows")) |
| 174 | + return TargetPlatform.windows(["windows_x86"], archs, platforms); |
| 175 | + else |
| 176 | + return TargetPlatform.init; |
| 177 | + } |
| 178 | + else |
| 179 | + return TargetPlatform.init; |
| 180 | +} |
| 181 | + |
| 182 | +void download(string url, string file) |
| 183 | +{ |
| 184 | + auto res = spawnProcess(["wget", url, "-O", file]).wait; |
| 185 | + if (res != 0) |
| 186 | + throw new Exception("Failed to download " ~ url ~ " to " ~ file); |
| 187 | +} |
| 188 | + |
| 189 | +void validateSHA512(string file, string sha512) |
| 190 | +{ |
| 191 | + import std.digest; |
| 192 | + import std.digest.sha; |
| 193 | + import std.string; |
| 194 | + |
| 195 | + auto got = sha512Of(cast(ubyte[]) fs.read(file)); |
| 196 | + |
| 197 | + if (got.toHexString.toUpper != sha512.toUpper) |
| 198 | + throw new Exception("SHA512 validation failed for file " ~ file); |
| 199 | +} |
| 200 | + |
| 201 | +void extractAndDelete(string file) |
| 202 | +{ |
| 203 | + if (file.endsWith(".tar.gz")) |
| 204 | + { |
| 205 | + if (spawnProcess(["tar", "-xvf", file]).wait != 0) |
| 206 | + throw new Exception("Failed extracting release binary " ~ file); |
| 207 | + } |
| 208 | + else if (file.endsWith(".zip")) |
| 209 | + { |
| 210 | + import std.zip; |
| 211 | + |
| 212 | + auto zip = new ZipArchive(fs.read(file)); |
| 213 | + |
| 214 | + foreach (name, am; zip.directory) |
| 215 | + { |
| 216 | + zip.expand(am); |
| 217 | + name = name.chompPrefix("/").chompPrefix("\\"); |
| 218 | + if (!name.startsWith(".")) |
| 219 | + fs.write(name, am.expandedData); |
| 220 | + } |
| 221 | + } |
| 222 | +} |
0 commit comments