zigup.zig 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  1. const std = @import("std");
  2. const builtin = @import("builtin");
  3. const mem = std.mem;
  4. const build_options = @import("build_options");
  5. const ArrayList = std.ArrayList;
  6. const Allocator = mem.Allocator;
  7. const ziget = @import("ziget");
  8. const zarc = @import("zarc");
  9. const fixdeletetree = @import("fixdeletetree.zig");
  10. const arch = switch(builtin.cpu.arch) {
  11. .x86_64 => "x86_64",
  12. .aarch64 => "aarch64",
  13. .riscv64 => "riscv64",
  14. else => @compileError("Unsupported CPU Architecture"),
  15. };
  16. const os = switch(builtin.os.tag) {
  17. .windows => "windows",
  18. .linux => "linux",
  19. .macos => "macos",
  20. else => @compileError("Unsupported OS"),
  21. };
  22. const url_platform = os ++ "-" ++ arch;
  23. const json_platform = arch ++ "-" ++ os;
  24. const archive_ext = if (builtin.os.tag == .windows) "zip" else "tar.xz";
  25. var global_optional_install_dir: ?[]const u8 = null;
  26. var global_optional_path_link: ?[]const u8 = null;
  27. var global_enable_log = true;
  28. fn loginfo(comptime fmt: []const u8, args: anytype) void {
  29. if (global_enable_log) {
  30. std.debug.print(fmt ++ "\n", args);
  31. }
  32. }
  33. fn download(allocator: Allocator, url: []const u8, writer: anytype) !void {
  34. var download_options = ziget.request.DownloadOptions{
  35. .flags = 0,
  36. .allocator = allocator,
  37. .maxRedirects = 10,
  38. .forwardBufferSize = 4096,
  39. .maxHttpResponseHeaders = 8192,
  40. .onHttpRequest = ignoreHttpCallback,
  41. .onHttpResponse = ignoreHttpCallback,
  42. };
  43. var dowload_state = ziget.request.DownloadState.init();
  44. try ziget.request.download(
  45. ziget.url.parseUrl(url) catch unreachable,
  46. writer,
  47. download_options,
  48. &dowload_state,
  49. );
  50. }
  51. fn downloadToFileAbsolute(allocator: Allocator, url: []const u8, file_absolute: []const u8) !void {
  52. const file = try std.fs.createFileAbsolute(file_absolute, .{});
  53. defer file.close();
  54. try download(allocator, url, file.writer());
  55. }
  56. fn downloadToString(allocator: Allocator, url: []const u8) ![]u8 {
  57. var response_array_list = try ArrayList(u8).initCapacity(allocator, 20 * 1024); // 20 KB (modify if response is expected to be bigger)
  58. errdefer response_array_list.deinit();
  59. try download(allocator, url, response_array_list.writer());
  60. return response_array_list.toOwnedSlice();
  61. }
  62. fn ignoreHttpCallback(request: []const u8) void { _ = request; }
  63. fn getHomeDir() ![]const u8 {
  64. return std.os.getenv("HOME") orelse {
  65. std.log.err("cannot find install directory, $HOME environment variable is not set", .{});
  66. return error.MissingHomeEnvironmentVariable;
  67. };
  68. }
  69. fn allocInstallDirString(allocator: Allocator) ![]const u8 {
  70. // TODO: maybe support ZIG_INSTALL_DIR environment variable?
  71. // TODO: maybe support a file on the filesystem to configure install dir?
  72. if (builtin.os.tag == .windows) {
  73. const self_exe_dir = try std.fs.selfExeDirPathAlloc(allocator);
  74. defer allocator.free(self_exe_dir);
  75. return std.fs.path.join(allocator, &.{ self_exe_dir, "zig" });
  76. }
  77. const home = try getHomeDir();
  78. if (!std.fs.path.isAbsolute(home)) {
  79. std.log.err("$HOME environment variable '{s}' is not an absolute path", .{home});
  80. return error.BadHomeEnvironmentVariable;
  81. }
  82. return std.fs.path.join(allocator, &[_][]const u8{ home, "zig" });
  83. }
  84. const GetInstallDirOptions = struct {
  85. create: bool,
  86. };
  87. fn getInstallDir(allocator: Allocator, options: GetInstallDirOptions) ![]const u8 {
  88. var optional_dir_to_free_on_error: ?[]const u8 = null;
  89. errdefer if (optional_dir_to_free_on_error) |dir| allocator.free(dir);
  90. const install_dir = init: {
  91. if (global_optional_install_dir) |dir| break :init dir;
  92. optional_dir_to_free_on_error = try allocInstallDirString(allocator);
  93. break :init optional_dir_to_free_on_error.?;
  94. };
  95. std.debug.assert(std.fs.path.isAbsolute(install_dir));
  96. loginfo("install directory '{s}'", .{install_dir});
  97. if (options.create) {
  98. loggyMakeDirAbsolute(install_dir) catch |e| switch (e) {
  99. error.PathAlreadyExists => {},
  100. else => return e,
  101. };
  102. }
  103. return install_dir;
  104. }
  105. fn makeZigPathLinkString(allocator: Allocator) ![]const u8 {
  106. if (global_optional_path_link) |path| return path;
  107. const zigup_dir = try std.fs.selfExeDirPathAlloc(allocator);
  108. defer allocator.free(zigup_dir);
  109. return try std.fs.path.join(allocator, &[_][]const u8{ zigup_dir, comptime "zig" ++ builtin.target.exeFileExt() });
  110. }
  111. // TODO: this should be in standard lib
  112. fn toAbsolute(allocator: Allocator, path: []const u8) ![]u8 {
  113. std.debug.assert(!std.fs.path.isAbsolute(path));
  114. const cwd = try std.process.getCwdAlloc(allocator);
  115. defer allocator.free(cwd);
  116. return std.fs.path.join(allocator, &[_][]const u8{ cwd, path });
  117. }
  118. fn help() void {
  119. std.io.getStdErr().writeAll(
  120. \\Download and manage zig compilers.
  121. \\
  122. \\Common Usage:
  123. \\
  124. \\ zigup VERSION download and set VERSION compiler as default
  125. \\ zigup fetch VERSION download VERSION compiler
  126. \\ zigup default [VERSION] get or set the default compiler
  127. \\ zigup clean [VERSION] deletes the given compiler version, otherwise, cleans all compilers
  128. \\ that aren't the default, master, or marked to keep.
  129. \\ zigup keep VERSION mark a compiler to be kept during clean
  130. \\ zigup run VERSION ARGS... run the given VERSION of the compiler with the given ARGS...
  131. \\
  132. \\Uncommon Usage:
  133. \\
  134. \\ zigup fetch-index download and print the download index json
  135. \\
  136. \\Common Options:
  137. \\ --install-dir DIR override the default install location
  138. \\ --path-link PATH path to the `zig` symlink that points to the default compiler
  139. \\ this will typically be a file path within a PATH directory so
  140. \\ that the user can just run `zig`
  141. \\
  142. ) catch unreachable;
  143. }
  144. fn getCmdOpt(args: [][]const u8, i: *usize) ![]const u8 {
  145. i.* += 1;
  146. if (i.* == args.len) {
  147. std.log.err("option '{s}' requires an argument", .{args[i.* - 1]});
  148. return error.AlreadyReported;
  149. }
  150. return args[i.*];
  151. }
  152. pub fn main() !u8 {
  153. return main2() catch |e| switch (e) {
  154. error.AlreadyReported => return 1,
  155. else => return e,
  156. };
  157. }
  158. pub fn main2() !u8 {
  159. if (builtin.os.tag == .windows) {
  160. _ = try std.os.windows.WSAStartup(2, 2);
  161. }
  162. var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
  163. const allocator = arena.allocator();
  164. const args_array = try std.process.argsAlloc(allocator);
  165. // no need to free, os will do it
  166. //defer std.process.argsFree(allocator, argsArray);
  167. var args = if (args_array.len == 0) args_array else args_array[1..];
  168. // parse common options
  169. //
  170. {
  171. var i: usize = 0;
  172. var newlen: usize = 0;
  173. while (i < args.len) : (i += 1) {
  174. const arg = args[i];
  175. if (std.mem.eql(u8, "--install-dir", arg)) {
  176. global_optional_install_dir = try getCmdOpt(args, &i);
  177. if (!std.fs.path.isAbsolute(global_optional_install_dir.?)) {
  178. global_optional_install_dir = try toAbsolute(allocator, global_optional_install_dir.?);
  179. }
  180. } else if (std.mem.eql(u8, "--path-link", arg)) {
  181. global_optional_path_link = try getCmdOpt(args, &i);
  182. if (!std.fs.path.isAbsolute(global_optional_path_link.?)) {
  183. global_optional_path_link = try toAbsolute(allocator, global_optional_path_link.?);
  184. }
  185. } else if (std.mem.eql(u8, "-h", arg) or std.mem.eql(u8, "--help", arg)) {
  186. help();
  187. return 0;
  188. } else {
  189. if (newlen == 0 and std.mem.eql(u8, "run", arg)) {
  190. return try runCompiler(allocator, args[i+1..]);
  191. }
  192. args[newlen] = args[i];
  193. newlen += 1;
  194. }
  195. }
  196. args = args[0..newlen];
  197. }
  198. if (args.len == 0) {
  199. help();
  200. return 1;
  201. }
  202. if (std.mem.eql(u8, "fetch-index", args[0])) {
  203. if (args.len != 1) {
  204. std.log.err("'index' command requires 0 arguments but got {d}", .{args.len - 1});
  205. return 1;
  206. }
  207. var download_index = try fetchDownloadIndex(allocator);
  208. defer download_index.deinit(allocator);
  209. try std.io.getStdOut().writeAll(download_index.text);
  210. return 0;
  211. }
  212. if (std.mem.eql(u8, "fetch", args[0])) {
  213. if (args.len != 2) {
  214. std.log.err("'fetch' command requires 1 argument but got {d}", .{args.len - 1});
  215. return 1;
  216. }
  217. try fetchCompiler(allocator, args[1], .leave_default);
  218. return 0;
  219. }
  220. if (std.mem.eql(u8, "clean", args[0])) {
  221. if (args.len == 1) {
  222. try cleanCompilers(allocator, null);
  223. } else if (args.len == 2) {
  224. try cleanCompilers(allocator, args[1]);
  225. } else {
  226. std.log.err("'clean' command requires 0 or 1 arguments but got {d}", .{args.len - 1});
  227. return 1;
  228. }
  229. return 0;
  230. }
  231. if (std.mem.eql(u8, "keep", args[0])) {
  232. if (args.len != 2) {
  233. std.log.err("'keep' command requires 1 argument but got {d}", .{args.len - 1});
  234. return 1;
  235. }
  236. try keepCompiler(allocator, args[1]);
  237. return 0;
  238. }
  239. if (std.mem.eql(u8, "list", args[0])) {
  240. if (args.len != 1) {
  241. std.log.err("'list' command requires 0 arguments but got {d}", .{args.len - 1});
  242. return 1;
  243. }
  244. try listCompilers(allocator);
  245. return 0;
  246. }
  247. if (std.mem.eql(u8, "default", args[0])) {
  248. if (args.len == 1) {
  249. try printDefaultCompiler(allocator);
  250. return 0;
  251. }
  252. if (args.len == 2) {
  253. const version_string = args[1];
  254. const install_dir_string = try getInstallDir(allocator, .{ .create = true });
  255. defer allocator.free(install_dir_string);
  256. const resolved_version_string = init_resolved: {
  257. if (!std.mem.eql(u8, version_string, "master"))
  258. break :init_resolved version_string;
  259. var optional_master_dir: ?[]const u8 = blk: {
  260. var install_dir = std.fs.openIterableDirAbsolute(install_dir_string, .{}) catch |e| switch (e) {
  261. error.FileNotFound => break :blk null,
  262. else => return e,
  263. };
  264. defer install_dir.close();
  265. break :blk try getMasterDir(allocator, &install_dir.dir);
  266. };
  267. // no need to free master_dir, this is a short lived program
  268. break :init_resolved optional_master_dir orelse {
  269. std.log.err("master has not been fetched", .{});
  270. return 1;
  271. };
  272. };
  273. const compiler_dir = try std.fs.path.join(allocator, &[_][]const u8{ install_dir_string, resolved_version_string });
  274. defer allocator.free(compiler_dir);
  275. try setDefaultCompiler(allocator, compiler_dir, .verify_existence);
  276. return 0;
  277. }
  278. std.log.err("'default' command requires 1 or 2 arguments but got {d}", .{args.len - 1});
  279. return 1;
  280. }
  281. if (args.len == 1) {
  282. try fetchCompiler(allocator, args[0], .set_default);
  283. return 0;
  284. }
  285. const command = args[0];
  286. args = args[1..];
  287. std.log.err("command not impl '{s}'", .{command});
  288. return 1;
  289. //const optionalInstallPath = try find_zigs(allocator);
  290. }
  291. pub fn runCompiler(allocator: Allocator, args: []const []const u8) !u8 {
  292. // disable log so we don't add extra output to whatever the compiler will output
  293. global_enable_log = false;
  294. if (args.len <= 1) {
  295. std.log.err("zigup run requires at least 2 arguments: zigup run VERSION PROG ARGS...", .{});
  296. return 1;
  297. }
  298. const version_string = args[0];
  299. const install_dir_string = try getInstallDir(allocator, .{ .create = true });
  300. defer allocator.free(install_dir_string);
  301. const compiler_dir = try std.fs.path.join(allocator, &[_][]const u8{ install_dir_string, version_string });
  302. defer allocator.free(compiler_dir);
  303. if (!try existsAbsolute(compiler_dir)) {
  304. std.log.err("compiler '{s}' does not exist, fetch it first with: zigup fetch {0s}", .{version_string});
  305. return 1;
  306. }
  307. var argv = std.ArrayList([]const u8).init(allocator);
  308. try argv.append(try std.fs.path.join(allocator, &.{ compiler_dir, "files", comptime "zig" ++ builtin.target.exeFileExt() }));
  309. try argv.appendSlice(args[1..]);
  310. // TODO: use "execve" if on linux
  311. var proc = std.ChildProcess.init(argv.items, allocator);
  312. const ret_val = try proc.spawnAndWait();
  313. switch (ret_val) {
  314. .Exited => |code| return code,
  315. else => |result| {
  316. std.log.err("compiler exited with {}", .{result});
  317. return 0xff;
  318. },
  319. }
  320. }
  321. const SetDefault = enum { set_default, leave_default };
  322. fn fetchCompiler(allocator: Allocator, version_arg: []const u8, set_default: SetDefault) !void {
  323. const install_dir = try getInstallDir(allocator, .{ .create = true });
  324. defer allocator.free(install_dir);
  325. var optional_download_index: ?DownloadIndex = null;
  326. // This is causing an LLVM error
  327. //defer if (optionalDownloadIndex) |_| optionalDownloadIndex.?.deinit(allocator);
  328. // Also I would rather do this, but it doesn't work because of const issues
  329. //defer if (optionalDownloadIndex) |downloadIndex| downloadIndex.deinit(allocator);
  330. const VersionUrl = struct { version: []const u8, url: []const u8 };
  331. // NOTE: we only fetch the download index if the user wants to download 'master', we can skip
  332. // this step for all other versions because the version to URL mapping is fixed (see getDefaultUrl)
  333. const is_master = std.mem.eql(u8, version_arg, "master");
  334. const version_url = blk: {
  335. if (!is_master)
  336. break :blk VersionUrl{ .version = version_arg, .url = try getDefaultUrl(allocator, version_arg) };
  337. optional_download_index = try fetchDownloadIndex(allocator);
  338. const master = optional_download_index.?.json.value.object.get("master").?;
  339. const compiler_version = master.object.get("version").?.string;
  340. const master_linux = master.object.get(json_platform).?;
  341. const master_linux_tarball = master_linux.object.get("tarball").?.string;
  342. break :blk VersionUrl{ .version = compiler_version, .url = master_linux_tarball };
  343. };
  344. const compiler_dir = try std.fs.path.join(allocator, &[_][]const u8{ install_dir, version_url.version });
  345. defer allocator.free(compiler_dir);
  346. try installCompiler(allocator, compiler_dir, version_url.url);
  347. if (is_master) {
  348. const master_symlink = try std.fs.path.join(allocator, &[_][]const u8{ install_dir, "master" });
  349. defer allocator.free(master_symlink);
  350. if (builtin.os.tag == .windows) {
  351. var file = try std.fs.createFileAbsolute(master_symlink, .{});
  352. defer file.close();
  353. try file.writer().writeAll(version_url.version);
  354. } else {
  355. _ = try loggyUpdateSymlink(version_url.version, master_symlink, .{ .is_directory = true });
  356. }
  357. }
  358. if (set_default == .set_default) {
  359. try setDefaultCompiler(allocator, compiler_dir, .existence_verified);
  360. }
  361. }
  362. const download_index_url = "https://ziglang.org/download/index.json";
  363. const DownloadIndex = struct {
  364. text: []u8,
  365. json: std.json.Parsed(std.json.Value),
  366. pub fn deinit(self: *DownloadIndex, allocator: Allocator) void {
  367. self.json.deinit();
  368. allocator.free(self.text);
  369. }
  370. };
  371. fn fetchDownloadIndex(allocator: Allocator) !DownloadIndex {
  372. const text = downloadToString(allocator, download_index_url) catch |e| switch (e) {
  373. else => {
  374. std.log.err("failed to download '{s}': {}", .{ download_index_url, e });
  375. return e;
  376. },
  377. };
  378. errdefer allocator.free(text);
  379. var json = try std.json.parseFromSlice(std.json.Value, allocator, text, .{});
  380. errdefer json.deinit();
  381. return DownloadIndex{ .text = text, .json = json };
  382. }
  383. fn loggyMakeDirAbsolute(dir_absolute: []const u8) !void {
  384. if (builtin.os.tag == .windows) {
  385. loginfo("mkdir \"{s}\"", .{dir_absolute});
  386. } else {
  387. loginfo("mkdir '{s}'", .{dir_absolute});
  388. }
  389. try std.fs.makeDirAbsolute(dir_absolute);
  390. }
  391. fn loggyDeleteTreeAbsolute(dir_absolute: []const u8) !void {
  392. if (builtin.os.tag == .windows) {
  393. loginfo("rd /s /q \"{s}\"", .{dir_absolute});
  394. } else {
  395. loginfo("rm -rf '{s}'", .{dir_absolute});
  396. }
  397. try fixdeletetree.deleteTreeAbsolute(dir_absolute);
  398. }
  399. pub fn loggyRenameAbsolute(old_path: []const u8, new_path: []const u8) !void {
  400. loginfo("mv '{s}' '{s}'", .{ old_path, new_path });
  401. try std.fs.renameAbsolute(old_path, new_path);
  402. }
  403. pub fn loggySymlinkAbsolute(target_path: []const u8, sym_link_path: []const u8, flags: std.fs.SymLinkFlags) !void {
  404. loginfo("ln -s '{s}' '{s}'", .{ target_path, sym_link_path });
  405. // NOTE: can't use symLinkAbsolute because it requires target_path to be absolute but we don't want that
  406. // not sure if it is a bug in the standard lib or not
  407. //try std.fs.symLinkAbsolute(target_path, sym_link_path, flags);
  408. _ = flags;
  409. try std.os.symlink(target_path, sym_link_path);
  410. }
  411. /// returns: true if the symlink was updated, false if it was already set to the given `target_path`
  412. pub fn loggyUpdateSymlink(target_path: []const u8, sym_link_path: []const u8, flags: std.fs.SymLinkFlags) !bool {
  413. var current_target_path_buffer: [std.fs.MAX_PATH_BYTES]u8 = undefined;
  414. if (std.fs.readLinkAbsolute(sym_link_path, &current_target_path_buffer)) |current_target_path| {
  415. if (std.mem.eql(u8, target_path, current_target_path)) {
  416. loginfo("symlink '{s}' already points to '{s}'", .{ sym_link_path, target_path });
  417. return false; // already up-to-date
  418. }
  419. try std.os.unlink(sym_link_path);
  420. } else |e| switch (e) {
  421. error.FileNotFound => {},
  422. else => return e,
  423. }
  424. try loggySymlinkAbsolute(target_path, sym_link_path, flags);
  425. return true; // updated
  426. }
  427. // TODO: this should be in std lib somewhere
  428. fn existsAbsolute(absolutePath: []const u8) !bool {
  429. std.fs.cwd().access(absolutePath, .{}) catch |e| switch (e) {
  430. error.FileNotFound => return false,
  431. error.PermissionDenied => return e,
  432. error.InputOutput => return e,
  433. error.SystemResources => return e,
  434. error.SymLinkLoop => return e,
  435. error.FileBusy => return e,
  436. error.Unexpected => unreachable,
  437. error.InvalidUtf8 => unreachable,
  438. error.ReadOnlyFileSystem => unreachable,
  439. error.NameTooLong => unreachable,
  440. error.BadPathName => unreachable,
  441. };
  442. return true;
  443. }
  444. fn listCompilers(allocator: Allocator) !void {
  445. const install_dir_string = try getInstallDir(allocator, .{ .create = false });
  446. defer allocator.free(install_dir_string);
  447. var install_dir = std.fs.openIterableDirAbsolute(install_dir_string, .{ }) catch |e| switch (e) {
  448. error.FileNotFound => return,
  449. else => return e,
  450. };
  451. defer install_dir.close();
  452. const stdout = std.io.getStdOut().writer();
  453. {
  454. var it = install_dir.iterate();
  455. while (try it.next()) |entry| {
  456. if (entry.kind != .directory)
  457. continue;
  458. if (std.mem.endsWith(u8, entry.name, ".installing"))
  459. continue;
  460. try stdout.print("{s}\n", .{entry.name});
  461. }
  462. }
  463. }
  464. fn keepCompiler(allocator: Allocator, compiler_version: []const u8) !void {
  465. const install_dir_string = try getInstallDir(allocator, .{ .create = true });
  466. defer allocator.free(install_dir_string);
  467. var install_dir = try std.fs.openIterableDirAbsolute(install_dir_string, .{ });
  468. defer install_dir.close();
  469. var compiler_dir = install_dir.dir.openDir(compiler_version, .{}) catch |e| switch (e) {
  470. error.FileNotFound => {
  471. std.log.err("compiler not found: {s}", .{compiler_version});
  472. return error.AlreadyReported;
  473. },
  474. else => return e,
  475. };
  476. var keep_fd = try compiler_dir.createFile("keep", .{});
  477. keep_fd.close();
  478. loginfo("created '{s}{c}{s}{c}{s}'", .{ install_dir_string, std.fs.path.sep, compiler_version, std.fs.path.sep, "keep" });
  479. }
  480. fn cleanCompilers(allocator: Allocator, compiler_name_opt: ?[]const u8) !void {
  481. const install_dir_string = try getInstallDir(allocator, .{ .create = true });
  482. defer allocator.free(install_dir_string);
  483. // getting the current compiler
  484. const default_comp_opt = try getDefaultCompiler(allocator);
  485. defer if (default_comp_opt) |default_compiler| allocator.free(default_compiler);
  486. var install_dir = std.fs.openIterableDirAbsolute(install_dir_string, .{}) catch |e| switch (e) {
  487. error.FileNotFound => return,
  488. else => return e,
  489. };
  490. defer install_dir.close();
  491. const master_points_to_opt = try getMasterDir(allocator, &install_dir.dir);
  492. defer if (master_points_to_opt) |master_points_to| allocator.free(master_points_to);
  493. if (compiler_name_opt) |compiler_name| {
  494. if (getKeepReason(master_points_to_opt, default_comp_opt, compiler_name)) |reason| {
  495. std.log.err("cannot clean '{s}' ({s})", .{ compiler_name, reason });
  496. return error.AlreadyReported;
  497. }
  498. loginfo("deleting '{s}{c}{s}'", .{ install_dir_string, std.fs.path.sep, compiler_name });
  499. try fixdeletetree.deleteTree(install_dir.dir, compiler_name);
  500. } else {
  501. var it = install_dir.iterate();
  502. while (try it.next()) |entry| {
  503. if (entry.kind != .directory)
  504. continue;
  505. if (getKeepReason(master_points_to_opt, default_comp_opt, entry.name)) |reason| {
  506. loginfo("keeping '{s}' ({s})", .{ entry.name, reason });
  507. continue;
  508. }
  509. {
  510. var compiler_dir = try install_dir.dir.openDir(entry.name, .{});
  511. defer compiler_dir.close();
  512. if (compiler_dir.access("keep", .{})) |_| {
  513. loginfo("keeping '{s}' (has keep file)", .{entry.name});
  514. continue;
  515. } else |e| switch (e) {
  516. error.FileNotFound => {},
  517. else => return e,
  518. }
  519. }
  520. loginfo("deleting '{s}{c}{s}'", .{ install_dir_string, std.fs.path.sep, entry.name });
  521. try fixdeletetree.deleteTree(install_dir.dir, entry.name);
  522. }
  523. }
  524. }
  525. fn readDefaultCompiler(allocator: Allocator, buffer: *[std.fs.MAX_PATH_BYTES + 1]u8) !?[]const u8 {
  526. const path_link = try makeZigPathLinkString(allocator);
  527. defer allocator.free(path_link);
  528. if (builtin.os.tag == .windows) {
  529. var file = std.fs.openFileAbsolute(path_link, .{}) catch |e| switch (e) {
  530. error.FileNotFound => return null,
  531. else => return e,
  532. };
  533. defer file.close();
  534. try file.seekTo(win32exelink.exe_offset);
  535. const len = try file.readAll(buffer);
  536. if (len != buffer.len) {
  537. std.log.err("path link file '{s}' is too small", .{path_link});
  538. return error.AlreadyReported;
  539. }
  540. const target_exe = std.mem.sliceTo(buffer, 0);
  541. return try allocator.dupe(u8, targetPathToVersion(target_exe));
  542. }
  543. const target_path = std.fs.readLinkAbsolute(path_link, buffer[0 .. std.fs.MAX_PATH_BYTES]) catch |e| switch (e) {
  544. error.FileNotFound => return null,
  545. else => return e,
  546. };
  547. defer allocator.free(target_path);
  548. return try allocator.dupe(u8, targetPathToVersion(target_path));
  549. }
  550. fn targetPathToVersion(target_path: []const u8) []const u8 {
  551. return std.fs.path.basename(std.fs.path.dirname(std.fs.path.dirname(target_path).?).?);
  552. }
  553. fn readMasterDir(buffer: *[std.fs.MAX_PATH_BYTES]u8, install_dir: *std.fs.Dir) !?[]const u8 {
  554. if (builtin.os.tag == .windows) {
  555. var file = install_dir.openFile("master", .{}) catch |e| switch (e) {
  556. error.FileNotFound => return null,
  557. else => return e,
  558. };
  559. defer file.close();
  560. return buffer[0..try file.readAll(buffer)];
  561. }
  562. return install_dir.readLink("master", buffer) catch |e| switch (e) {
  563. error.FileNotFound => return null,
  564. else => return e,
  565. };
  566. }
  567. fn getDefaultCompiler(allocator: Allocator) !?[]const u8 {
  568. var buffer: [std.fs.MAX_PATH_BYTES + 1]u8 = undefined;
  569. const slice_path = (try readDefaultCompiler(allocator, &buffer)) orelse return null;
  570. var path_to_return = try allocator.alloc(u8, slice_path.len);
  571. std.mem.copy(u8, path_to_return, slice_path);
  572. return path_to_return;
  573. }
  574. fn getMasterDir(allocator: Allocator, install_dir: *std.fs.Dir) !?[]const u8 {
  575. var buffer: [std.fs.MAX_PATH_BYTES]u8 = undefined;
  576. const slice_path = (try readMasterDir(&buffer, install_dir)) orelse return null;
  577. var path_to_return = try allocator.alloc(u8, slice_path.len);
  578. std.mem.copy(u8, path_to_return, slice_path);
  579. return path_to_return;
  580. }
  581. fn printDefaultCompiler(allocator: Allocator) !void {
  582. const default_compiler_opt = try getDefaultCompiler(allocator);
  583. defer if (default_compiler_opt) |default_compiler| allocator.free(default_compiler);
  584. const stdout = std.io.getStdOut().writer();
  585. if (default_compiler_opt) |default_compiler| {
  586. try stdout.print("{s}\n", .{default_compiler});
  587. } else {
  588. try stdout.writeAll("<no-default>\n");
  589. }
  590. }
  591. const ExistVerify = enum { existence_verified, verify_existence };
  592. fn setDefaultCompiler(allocator: Allocator, compiler_dir: []const u8, exist_verify: ExistVerify) !void {
  593. switch (exist_verify) {
  594. .existence_verified => {},
  595. .verify_existence => {
  596. var dir = std.fs.openDirAbsolute(compiler_dir, .{}) catch |err| switch (err) {
  597. error.FileNotFound => {
  598. std.log.err("compiler '{s}' is not installed", .{std.fs.path.basename(compiler_dir)});
  599. return error.AlreadyReported;
  600. },
  601. else => |e| return e,
  602. };
  603. dir.close();
  604. },
  605. }
  606. const path_link = try makeZigPathLinkString(allocator);
  607. defer allocator.free(path_link);
  608. const link_target = try std.fs.path.join(allocator, &[_][]const u8{ compiler_dir, "files", comptime "zig" ++ builtin.target.exeFileExt() });
  609. defer allocator.free(link_target);
  610. if (builtin.os.tag == .windows) {
  611. try createExeLink(link_target, path_link);
  612. } else {
  613. _ = try loggyUpdateSymlink(link_target, path_link, .{});
  614. }
  615. try verifyPathLink(allocator, path_link);
  616. }
  617. /// Verify that path_link will work. It verifies that `path_link` is
  618. /// in PATH and there is no zig executable in an earlier directory in PATH.
  619. fn verifyPathLink(allocator: Allocator, path_link: []const u8) !void {
  620. const path_link_dir = std.fs.path.dirname(path_link) orelse {
  621. std.log.err("invalid '--path-link' '{s}', it must be a file (not the root directory)", .{path_link});
  622. return error.AlreadyReported;
  623. };
  624. const path_link_dir_id = blk: {
  625. var dir = std.fs.openDirAbsolute(path_link_dir, .{}) catch |err| {
  626. std.log.err("unable to open the path-link directory '{s}': {s}", .{path_link_dir, @errorName(err)});
  627. return error.AlreadyReported;
  628. };
  629. defer dir.close();
  630. break :blk try FileId.initFromDir(dir, path_link);
  631. };
  632. if (builtin.os.tag == .windows) {
  633. const path_env = std.process.getEnvVarOwned(allocator, "PATH") catch |err| switch (err) {
  634. error.EnvironmentVariableNotFound => return,
  635. else => |e| return e,
  636. };
  637. defer allocator.free(path_env);
  638. var free_pathext: ?[]const u8 = null;
  639. defer if (free_pathext) |p| allocator.free(p);
  640. const pathext_env = blk: {
  641. if (std.process.getEnvVarOwned(allocator, "PATHEXT")) |env| {
  642. free_pathext = env;
  643. break :blk env;
  644. } else |err| switch (err) {
  645. error.EnvironmentVariableNotFound => break :blk "",
  646. else => |e| return e,
  647. }
  648. break :blk "";
  649. };
  650. var path_it = std.mem.tokenize(u8, path_env, ";");
  651. while (path_it.next()) |path| {
  652. switch (try compareDir(path_link_dir_id, path)) {
  653. .missing => continue,
  654. .match => return,
  655. .mismatch => {},
  656. }
  657. {
  658. const exe = try std.fs.path.join(allocator, &.{ path, "zig" });
  659. defer allocator.free(exe);
  660. try enforceNoZig(path_link, exe);
  661. }
  662. var ext_it = std.mem.tokenize(u8, pathext_env, ";");
  663. while (ext_it.next()) |ext| {
  664. if (ext.len == 0) continue;
  665. const basename = try std.mem.concat(allocator, u8, &.{"zig", ext});
  666. defer allocator.free(basename);
  667. const exe = try std.fs.path.join(allocator, &.{path, basename});
  668. defer allocator.free(exe);
  669. try enforceNoZig(path_link, exe);
  670. }
  671. }
  672. } else {
  673. var path_it = std.mem.tokenize(u8, std.os.getenv("PATH") orelse "", ":");
  674. while (path_it.next()) |path| {
  675. switch (try compareDir(path_link_dir_id, path)) {
  676. .missing => continue,
  677. .match => return,
  678. .mismatch => {},
  679. }
  680. const exe = try std.fs.path.join(allocator, &.{ path, "zig" });
  681. defer allocator.free(exe);
  682. try enforceNoZig(path_link, exe);
  683. }
  684. }
  685. std.log.err("the path link '{s}' is not in PATH", .{path_link});
  686. return error.AlreadyReported;
  687. }
  688. fn compareDir(dir_id: FileId, other_dir: []const u8) !enum { missing, match, mismatch } {
  689. var dir = std.fs.cwd().openDir(other_dir, .{}) catch |err| switch (err) {
  690. error.FileNotFound, error.NotDir, error.BadPathName => return .missing,
  691. else => |e| return e,
  692. };
  693. defer dir.close();
  694. return if (dir_id.eql(try FileId.initFromDir(dir, other_dir))) .match else .mismatch;
  695. }
  696. fn enforceNoZig(path_link: []const u8, exe: []const u8) !void {
  697. var file = std.fs.cwd().openFile(exe, .{}) catch |err| switch (err) {
  698. error.FileNotFound, error.IsDir => return,
  699. else => |e| return e,
  700. };
  701. defer file.close();
  702. // todo: on posix systems ignore the file if it is not executable
  703. std.log.err("zig compiler '{s}' is higher priority in PATH than the path-link '{s}'", .{exe, path_link});
  704. }
  705. const FileId = struct {
  706. dev: if (builtin.os.tag == .windows) u32 else blk: {
  707. var st: std.os.Stat = undefined;
  708. break :blk @TypeOf(st.dev);
  709. },
  710. ino: if (builtin.os.tag == .windows) u64 else blk: {
  711. var st: std.os.Stat = undefined;
  712. break :blk @TypeOf(st.ino);
  713. },
  714. pub fn initFromFile(file: std.fs.File, filename_for_error: []const u8) !FileId {
  715. if (builtin.os.tag == .windows) {
  716. var info: win32.BY_HANDLE_FILE_INFORMATION = undefined;
  717. if (0 == win32.GetFileInformationByHandle(file.handle, &info)) {
  718. std.log.err("GetFileInformationByHandle on '{s}' failed, error={}", .{filename_for_error, std.os.windows.kernel32.GetLastError()});
  719. return error.AlreadyReported;
  720. }
  721. return FileId{
  722. .dev = info.dwVolumeSerialNumber,
  723. .ino = (@as(u64, @intCast(info.nFileIndexHigh)) << 32) | @as(u64, @intCast(info.nFileIndexLow)),
  724. };
  725. }
  726. const st = try std.os.fstat(file.handle);
  727. return FileId{
  728. .dev = st.dev,
  729. .ino = st.ino,
  730. };
  731. }
  732. pub fn initFromDir(dir: std.fs.Dir, name_for_error: []const u8) !FileId {
  733. if (builtin.os.tag == .windows) {
  734. return initFromFile(std.fs.File { .handle = dir.fd}, name_for_error);
  735. }
  736. return initFromFile(std.fs.File { .handle = dir.fd }, name_for_error);
  737. }
  738. pub fn eql(self: FileId, other: FileId) bool {
  739. return self.dev == other.dev and self.ino == other.ino;
  740. }
  741. };
  742. const win32 = struct {
  743. pub const BOOL = i32;
  744. pub const FILETIME = extern struct {
  745. dwLowDateTime: u32,
  746. dwHighDateTime: u32,
  747. };
  748. pub const BY_HANDLE_FILE_INFORMATION = extern struct {
  749. dwFileAttributes: u32,
  750. ftCreationTime: FILETIME,
  751. ftLastAccessTime: FILETIME,
  752. ftLastWriteTime: FILETIME,
  753. dwVolumeSerialNumber: u32,
  754. nFileSizeHigh: u32,
  755. nFileSizeLow: u32,
  756. nNumberOfLinks: u32,
  757. nFileIndexHigh: u32,
  758. nFileIndexLow: u32,
  759. };
  760. pub extern "kernel32" fn GetFileInformationByHandle(
  761. hFile: ?@import("std").os.windows.HANDLE,
  762. lpFileInformation: ?*BY_HANDLE_FILE_INFORMATION,
  763. ) callconv(@import("std").os.windows.WINAPI) BOOL;
  764. };
  765. const win32exelink = struct {
  766. const content = @embedFile(build_options.win32exelink_filename);
  767. const exe_offset: usize = if (builtin.os.tag != .windows) 0 else blk: {
  768. @setEvalBranchQuota(content.len * 2);
  769. const marker = "!!!THIS MARKS THE zig_exe_string MEMORY!!#";
  770. const offset = std.mem.indexOf(u8, content, marker) orelse {
  771. @compileError("win32exelink is missing the marker: " ++ marker);
  772. };
  773. if (std.mem.indexOf(u8, content[offset + 1 ..], marker) != null) {
  774. @compileError("win32exelink contains multiple markers (not implemented)");
  775. }
  776. break :blk offset + marker.len;
  777. };
  778. };
  779. fn createExeLink(link_target: []const u8, path_link: []const u8) !void {
  780. if (path_link.len > std.fs.MAX_PATH_BYTES) {
  781. std.debug.print("Error: path_link (size {}) is too large (max {})\n", .{path_link.len, std.fs.MAX_PATH_BYTES});
  782. return error.AlreadyReported;
  783. }
  784. const file = try std.fs.cwd().createFile(path_link, .{});
  785. defer file.close();
  786. try file.writer().writeAll(win32exelink.content[0 .. win32exelink.exe_offset]);
  787. try file.writer().writeAll(link_target);
  788. try file.writer().writeAll(win32exelink.content[win32exelink.exe_offset + link_target.len ..]);
  789. }
  790. const VersionKind = enum { release, dev };
  791. fn determineVersionKind(version: []const u8) VersionKind {
  792. return if (std.mem.indexOfAny(u8, version, "-+")) |_| .dev else .release;
  793. }
  794. fn getDefaultUrl(allocator: Allocator, compiler_version: []const u8) ![]const u8 {
  795. return switch (determineVersionKind(compiler_version)) {
  796. .dev => try std.fmt.allocPrint(allocator, "https://ziglang.org/builds/zig-" ++ url_platform ++ "-{0s}." ++ archive_ext, .{ compiler_version }),
  797. .release => try std.fmt.allocPrint(allocator, "https://ziglang.org/download/{s}/zig-" ++ url_platform ++ "-{0s}." ++ archive_ext, .{ compiler_version }),
  798. };
  799. }
  800. fn installCompiler(allocator: Allocator, compiler_dir: []const u8, url: []const u8) !void {
  801. if (try existsAbsolute(compiler_dir)) {
  802. loginfo("compiler '{s}' already installed", .{compiler_dir});
  803. return;
  804. }
  805. const installing_dir = try std.mem.concat(allocator, u8, &[_][]const u8{ compiler_dir, ".installing" });
  806. defer allocator.free(installing_dir);
  807. try loggyDeleteTreeAbsolute(installing_dir);
  808. try loggyMakeDirAbsolute(installing_dir);
  809. const archive_basename = std.fs.path.basename(url);
  810. var archive_root_dir: []const u8 = undefined;
  811. // download and extract archive
  812. {
  813. const archive_absolute = try std.fs.path.join(allocator, &[_][]const u8{ installing_dir, archive_basename });
  814. defer allocator.free(archive_absolute);
  815. loginfo("downloading '{s}' to '{s}'", .{ url, archive_absolute });
  816. downloadToFileAbsolute(allocator, url, archive_absolute) catch |e| switch (e) {
  817. error.HttpNon200StatusCode => {
  818. // TODO: more information would be good
  819. std.log.err("HTTP request failed (TODO: improve ziget library to get better error)", .{});
  820. // this removes the installing dir if the http request fails so we dont have random directories
  821. try loggyDeleteTreeAbsolute(installing_dir);
  822. return error.AlreadyReported;
  823. },
  824. else => return e,
  825. };
  826. if (std.mem.endsWith(u8, archive_basename, ".tar.xz")) {
  827. archive_root_dir = archive_basename[0 .. archive_basename.len - ".tar.xz".len];
  828. _ = try run(allocator, &[_][]const u8{ "tar", "xf", archive_absolute, "-C", installing_dir });
  829. } else {
  830. var recognized = false;
  831. if (builtin.os.tag == .windows) {
  832. if (std.mem.endsWith(u8, archive_basename, ".zip")) {
  833. recognized = true;
  834. archive_root_dir = archive_basename[0 .. archive_basename.len - ".zip".len];
  835. var installing_dir_opened = try std.fs.openDirAbsolute(installing_dir, .{});
  836. defer installing_dir_opened.close();
  837. loginfo("extracting archive to \"{s}\"", .{installing_dir});
  838. var timer = try std.time.Timer.start();
  839. var archive_file = try std.fs.openFileAbsolute(archive_absolute, .{});
  840. defer archive_file.close();
  841. const reader = archive_file.reader();
  842. var archive = try zarc.zip.load(allocator, reader);
  843. defer archive.deinit(allocator);
  844. _ = try archive.extract(reader, installing_dir_opened, .{});
  845. const time = timer.read();
  846. loginfo("extracted archive in {d:.2} s", .{@as(f32, @floatFromInt(time)) / @as(f32, @floatFromInt(std.time.ns_per_s))});
  847. }
  848. }
  849. if (!recognized) {
  850. std.log.err("unknown archive extension '{s}'", .{archive_basename});
  851. return error.UnknownArchiveExtension;
  852. }
  853. }
  854. try loggyDeleteTreeAbsolute(archive_absolute);
  855. }
  856. {
  857. const extracted_dir = try std.fs.path.join(allocator, &[_][]const u8{ installing_dir, archive_root_dir });
  858. defer allocator.free(extracted_dir);
  859. const normalized_dir = try std.fs.path.join(allocator, &[_][]const u8{ installing_dir, "files" });
  860. defer allocator.free(normalized_dir);
  861. try loggyRenameAbsolute(extracted_dir, normalized_dir);
  862. }
  863. // TODO: write date information (so users can sort compilers by date)
  864. // finish installation by renaming the install dir
  865. try loggyRenameAbsolute(installing_dir, compiler_dir);
  866. }
  867. pub fn run(allocator: Allocator, argv: []const []const u8) !std.ChildProcess.Term {
  868. try logRun(allocator, argv);
  869. var proc = std.ChildProcess.init(argv, allocator);
  870. return proc.spawnAndWait();
  871. }
  872. fn logRun(allocator: Allocator, argv: []const []const u8) !void {
  873. var buffer = try allocator.alloc(u8, getCommandStringLength(argv));
  874. defer allocator.free(buffer);
  875. var prefix = false;
  876. var offset: usize = 0;
  877. for (argv) |arg| {
  878. if (prefix) {
  879. buffer[offset] = ' ';
  880. offset += 1;
  881. } else {
  882. prefix = true;
  883. }
  884. std.mem.copy(u8, buffer[offset .. offset + arg.len], arg);
  885. offset += arg.len;
  886. }
  887. std.debug.assert(offset == buffer.len);
  888. loginfo("[RUN] {s}", .{buffer});
  889. }
  890. pub fn getCommandStringLength(argv: []const []const u8) usize {
  891. var len: usize = 0;
  892. var prefix_length: u8 = 0;
  893. for (argv) |arg| {
  894. len += prefix_length + arg.len;
  895. prefix_length = 1;
  896. }
  897. return len;
  898. }
  899. pub fn getKeepReason(master_points_to_opt: ?[]const u8, default_compiler_opt: ?[]const u8, name: []const u8) ?[]const u8 {
  900. if (default_compiler_opt) |default_comp| {
  901. if (mem.eql(u8, default_comp, name)) {
  902. return "is default compiler";
  903. }
  904. }
  905. if (master_points_to_opt) |master_points_to| {
  906. if (mem.eql(u8, master_points_to, name)) {
  907. return "it is master";
  908. }
  909. }
  910. return null;
  911. }