app.d 13 KB

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