app.d 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. import std.stdio;
  2. import std.file;
  3. import std.path;
  4. import std.compiler;
  5. import std.system;
  6. import std.format;
  7. import std.conv;
  8. import std.process;
  9. import std.string;
  10. import iopipe.json.serialize;
  11. import iopipe.json.parser;
  12. import std.exception;
  13. import iopipe.traits;
  14. // tar support copied from arsd.archive: https://github.com/adamdruppe/arsd
  15. // Written by Adam D. Ruppe
  16. /++
  17. A header of a file in the archive. This represents the
  18. binary format of the header block.
  19. +/
  20. align(512)
  21. struct TarFileHeader {
  22. align(1):
  23. char[100] fileName_ = 0;
  24. char[8] fileMode_ = 0;
  25. char[8] ownerUid_ = 0;
  26. char[8] ownerGid_ = 0;
  27. char[12] size_ = 0; // in octal
  28. char[12] mtime_ = 0; // octal unix timestamp
  29. char[8] checksum_ = 0; // right?????
  30. char[1] fileType_ = 0; // hard link, soft link, etc
  31. char[100] linkFileName_ = 0;
  32. char[6] ustarMagic_ = 0; // if "ustar\0", remaining fields are set
  33. char[2] ustarVersion_ = 0;
  34. char[32] ownerName_ = 0;
  35. char[32] groupName_ = 0;
  36. char[8] deviceMajorNumber_ = 0;
  37. char[8] deviceMinorNumber_ = 0;
  38. char[155] filenamePrefix_ = 0;
  39. /// Returns the filename. You should cache the return value as long as TarFileHeader is in scope (it returns a slice after calling strlen)
  40. const(char)[] filename() {
  41. import core.stdc.string;
  42. if(filenamePrefix_[0])
  43. return upToZero(filenamePrefix_[]) ~ upToZero(fileName_[]);
  44. return upToZero(fileName_[]);
  45. }
  46. ///
  47. ulong size() {
  48. import core.stdc.stdlib;
  49. return strtoul(size_.ptr, null, 8);
  50. }
  51. ///
  52. TarFileType type() {
  53. if(fileType_[0] == 0)
  54. return TarFileType.normal;
  55. else
  56. return cast(TarFileType) (fileType_[0] - '0');
  57. }
  58. uint mode() {
  59. import std.conv : to;
  60. return fileMode_.upToZero.to!int(8);
  61. }
  62. }
  63. /// There's other types but this is all I care about. You can still detect the char by `((cast(char) type) + '0')`
  64. enum TarFileType {
  65. normal = 0, ///
  66. hardLink = 1, ///
  67. symLink = 2, ///
  68. characterSpecial = 3, ///
  69. blockSpecial = 4, ///
  70. directory = 5, ///
  71. fifo = 6 ///
  72. }
  73. /++
  74. Low level tar file processor. You must pass it a
  75. TarFileHeader buffer as well as a size_t for context.
  76. Both must be initialized to all zeroes on first call,
  77. then not modified in between calls.
  78. Each call must populate the dataBuffer with 512 bytes.
  79. returns true if still work to do.
  80. +/
  81. bool processTar(
  82. TarFileHeader* header,
  83. long* bytesRemainingOnCurrentFile,
  84. ubyte[] dataBuffer,
  85. scope void delegate(TarFileHeader* header, bool isNewFile, bool fileFinished, ubyte[] data) handleData
  86. )
  87. {
  88. assert(dataBuffer.length == 512);
  89. assert(bytesRemainingOnCurrentFile !is null);
  90. assert(header !is null);
  91. if(*bytesRemainingOnCurrentFile) {
  92. bool isNew = *bytesRemainingOnCurrentFile == header.size();
  93. if(*bytesRemainingOnCurrentFile <= 512) {
  94. handleData(header, isNew, true, dataBuffer[0 .. cast(size_t) *bytesRemainingOnCurrentFile]);
  95. *bytesRemainingOnCurrentFile = 0;
  96. } else {
  97. handleData(header, isNew, false, dataBuffer[]);
  98. *bytesRemainingOnCurrentFile -= 512;
  99. }
  100. } else {
  101. *header = *(cast(TarFileHeader*) dataBuffer.ptr);
  102. auto s = header.size();
  103. *bytesRemainingOnCurrentFile = s;
  104. if(header.type() == TarFileType.symLink)
  105. handleData(header, true, true, cast(ubyte[])header.linkFileName_.upToZero);
  106. if(header.type() == TarFileType.directory)
  107. handleData(header, true, false, null);
  108. if(s == 0 && header.type == TarFileType.normal)
  109. return false;
  110. }
  111. return true;
  112. }
  113. T[] upToZero(T)(T[] input)
  114. {
  115. foreach(i, v; input)
  116. if(v == 0)
  117. return input[0 .. i];
  118. return input;
  119. }
  120. version(X86)
  121. enum arch="x86";
  122. else version(X86_64)
  123. enum arch="x86_64";
  124. else version(ARM)
  125. enum arch="arm";
  126. else version(AArch64)
  127. enum arch="arm64";
  128. else
  129. static assert(false, "Unsupported architecture");
  130. // establish the runtime
  131. version(CRuntime_Microsoft)
  132. enum CRT="MSVC";
  133. else version(CRuntime_Glibc)
  134. enum CRT="glibc";
  135. else version(CppRuntime_Clang)
  136. enum CRT="llvm";
  137. else
  138. static assert(false, "Unsupported runtime");
  139. enum osStr = os.to!string;
  140. enum baseDir = buildPath("install", "lib", osStr, arch, CRT);
  141. auto writeTo(C, S)(C chain, S sink)
  142. {
  143. import iopipe.bufpipe;
  144. import iopipe.valve;
  145. return chain.push!(c => c.outputPipe(sink));
  146. }
  147. void extractArchive(char[] path)
  148. {
  149. import std.io : File, mode;
  150. import iopipe.bufpipe;
  151. import iopipe.refc;
  152. import iopipe.zip;
  153. auto archivePath = buildPath(path, "install", "lib.tgz");
  154. auto expectedPrefix = "lib/" ~ osStr ~ "/";
  155. enforce(exists(archivePath), "No lib archive found at " ~ archivePath ~ "!");
  156. // the input file
  157. auto inputFile = File(archivePath, mode!"rb").refCounted.bufd.unzip;
  158. bool doOutput;
  159. // open a file using iopipe
  160. auto openOutputFile(string fname)
  161. {
  162. return bufd.writeTo(File(fname, mode!"wb").refCounted);
  163. }
  164. typeof(openOutputFile("")) currentFile;
  165. string currentSymlinkText;
  166. void handleTar(TarFileHeader *header, bool isNewFile, bool fileFinished, ubyte[] data)
  167. {
  168. auto ft = header.type;
  169. if(isNewFile)
  170. {
  171. // check that the name matches
  172. auto fn = header.filename;
  173. if(!fn.startsWith(expectedPrefix))
  174. return;
  175. version(Posix)
  176. {
  177. // handle symlinks on posix
  178. if(ft == TarFileType.symLink)
  179. {
  180. doOutput = true;
  181. currentSymlinkText = "";
  182. }
  183. }
  184. if(ft == TarFileType.normal)
  185. {
  186. doOutput = true;
  187. auto newFilePath = buildPath(path, "install", fn);
  188. mkdirRecurse(dirName(newFilePath));
  189. currentFile = openOutputFile(newFilePath);
  190. }
  191. }
  192. if(doOutput)
  193. {
  194. if(ft == TarFileType.symLink)
  195. currentSymlinkText ~= cast(char[])data;
  196. else
  197. {
  198. currentFile.ensureElems(data.length);
  199. assert(currentFile.window.length >= data.length);
  200. currentFile.window[0 .. data.length] = data[];
  201. currentFile.release(data.length);
  202. }
  203. }
  204. if(fileFinished)
  205. {
  206. if(doOutput)
  207. {
  208. auto fn = header.filename;
  209. auto fp = buildPath(path, "install", fn);
  210. version(Posix)
  211. {
  212. if(ft == TarFileType.symLink)
  213. {
  214. mkdirRecurse(dirName(fp));
  215. symlink(currentSymlinkText, fp);
  216. }
  217. }
  218. if(ft == TarFileType.normal)
  219. {
  220. // close the file
  221. destroy(currentFile);
  222. // Effect the correct file permissions
  223. version(Posix)
  224. {
  225. setAttributes(fp, header.mode);
  226. }
  227. }
  228. doOutput = false;
  229. }
  230. }
  231. }
  232. // for tar
  233. TarFileHeader tfh;
  234. long size;
  235. while(inputFile.extend(0) > 0)
  236. {
  237. while(inputFile.window.length >= 512)
  238. {
  239. // big enough to process another tar chunk
  240. processTar(&tfh, &size, inputFile.window[0 .. 512], &handleTar);
  241. inputFile.release(512);
  242. }
  243. }
  244. }
  245. int main()
  246. {
  247. writeln("raylib-d library installation");
  248. // look at the dub.selections.json file
  249. auto dubConfig = execute(["dub", "describe"], null, Config.stderrPassThrough);
  250. string raylibdPath;
  251. if(dubConfig.status != 0)
  252. {
  253. stderr.writeln("Error executing dub describe");
  254. return dubConfig.status;
  255. }
  256. char[] getRaylibPath(char[] jsonStr)
  257. {
  258. auto tokens = jsonTokenizer(jsonStr);
  259. enforce(tokens.parseTo("packages"), "Could not find packages in dub json output!");
  260. auto nt = tokens.next.token;
  261. enforce(nt == JSONToken.ArrayStart, "Expected array start in packages");
  262. while(nt != JSONToken.ArrayEnd)
  263. {
  264. tokens.releaseParsed();
  265. tokens.startCache;
  266. enforce(tokens.parseTo("name"), "Could not find package name in json file");
  267. auto n = tokens.next;
  268. jsonExpect(n, JSONToken.String, "Expected string for package name");
  269. if(n.data(tokens.chain) == "raylib-d")
  270. {
  271. tokens.rewind;
  272. tokens.parseTo("path");
  273. auto p = tokens.next;
  274. jsonExpect(p, JSONToken.String, "Expected string for path");
  275. return p.data(tokens.chain);
  276. }
  277. tokens.rewind;
  278. tokens.endCache;
  279. nt = tokens.skipItem.token;
  280. }
  281. throw new Exception("Could not find raylib-d dependency for current project!");
  282. }
  283. try {
  284. auto path = getRaylibPath(dubConfig.output.dup);
  285. // check to see if the `lib` directory exists, and if not, see if we can extract it from a tarball
  286. writeln("Detected raylib dependency path as ", path);
  287. auto libpath = buildPath(path, baseDir);
  288. writeln("Copying library files from ", libpath);
  289. if(!exists(libpath))
  290. {
  291. // extract the data, but only for the detected OS
  292. writeln("Library path does not exist, trying archive");
  293. extractArchive(path);
  294. }
  295. foreach(ent; dirEntries(libpath, SpanMode.shallow))
  296. {
  297. auto newLoc = buildPath(".", ent.name.baseName(".lnk"));
  298. if(exists(newLoc))
  299. {
  300. writeln("Skipping existing file ", newLoc);
  301. continue;
  302. }
  303. version(Posix)
  304. {
  305. void makeLink(string origln)
  306. {
  307. writefln("Creating symlink %s -> %s", newLoc, origln);
  308. symlink(origln, newLoc);
  309. }
  310. if(ent.isSymlink)
  311. {
  312. // recreate the symlink
  313. makeLink(readLink(ent.name));
  314. continue;
  315. }
  316. else if(ent.name.endsWith(".lnk"))
  317. {
  318. // dub workaround. This is really a symlink but wasn't
  319. // properly downloaded by dub.
  320. makeLink(readText(ent.name));
  321. continue;
  322. }
  323. }
  324. writefln("Installing library file %s", newLoc);
  325. copy(ent.name, newLoc, PreserveAttributes.yes);
  326. }
  327. } catch(Exception ex) {
  328. stderr.writeln("Error: ", ex.msg);
  329. return 1;
  330. }
  331. // display what to put in the json or sdl file
  332. if(exists("dub.json"))
  333. {
  334. writeln(
  335. `If not already present, the following directives in dub.json will link the installed raylib library:
  336. "libs": [ "raylib" ],
  337. "lflags-posix" : ["-L."],
  338. "lflags-osx" : ["-rpath", "@executable_path/"],
  339. "lflags-linux" : ["-rpath=$$ORIGIN"]`);
  340. }
  341. else if(exists("dub.sdl"))
  342. {
  343. writeln(
  344. `If not already present, the following directives in dub.sdl will link the installed raylib library:
  345. libs "raylib"
  346. lflags "-rpath" "@executable_path/" platform="osx"
  347. lflags "-L." platform="posix"
  348. lflags "-rpath=$$ORIGIN" platform="linux"`);
  349. }
  350. return 0;
  351. }