build.zig 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. //! This build.zig file boostraps the real build in build2.zig
  2. // NOTE: need to wait on https://github.com/ziglang/zig/pull/9989 before doing this
  3. // to make build errors reasonable
  4. const std = @import("std");
  5. const Builder = std.build.Builder;
  6. const GitRepoStep = @import("GitRepoStep.zig");
  7. pub fn build(b: *Builder) !void {
  8. buildNoreturn(b);
  9. }
  10. fn buildNoreturn(b: *Builder) noreturn {
  11. const err = buildOrFail(b);
  12. std.log.err("{s}", .{@errorName(err)});
  13. if (@errorReturnTrace()) |trace| {
  14. std.debug.dumpStackTrace(trace.*);
  15. }
  16. std.os.exit(0xff);
  17. }
  18. fn buildOrFail(b: *Builder) anyerror {
  19. const ziget_repo = GitRepoStep.create(b, .{
  20. .url = "https://github.com/marler8997/ziget",
  21. .branch = null,
  22. .sha = @embedFile("zigetsha"),
  23. .fetch_enabled = true,
  24. });
  25. const build2 = addBuild(b, .{ .path = "build2.zig" }, .{});
  26. build2.addArgs(try getBuildArgs(b));
  27. var progress = std.Progress{};
  28. {
  29. var prog_node = progress.start("clone ziget", 1);
  30. ziget_repo.step.make(prog_node) catch |e| return e;
  31. prog_node.end();
  32. }
  33. {
  34. var prog_node = progress.start("run build2.zig", 1);
  35. build2.step.make(prog_node) catch |err| switch (err) {
  36. error.MakeFailed => std.os.exit(0xff), // error already printed by subprocess, hopefully?
  37. error.MakeSkipped => @panic("impossible?"),
  38. };
  39. prog_node.end();
  40. }
  41. std.os.exit(0);
  42. }
  43. // TODO: remove the following if https://github.com/ziglang/zig/pull/9987 is integrated
  44. fn getBuildArgs(self: *Builder) ! []const [:0]const u8 {
  45. const args = try std.process.argsAlloc(self.allocator);
  46. return args[5..];
  47. }
  48. pub fn addBuild(self: *Builder, build_file: std.build.FileSource, _: struct { }) *std.build.RunStep {
  49. const run_step = std.build.RunStep.create(
  50. self,
  51. self.fmt("zig build {s}", .{build_file.getDisplayName()}),
  52. );
  53. run_step.addArg(self.zig_exe);
  54. run_step.addArg("build");
  55. run_step.addArg("--build-file");
  56. run_step.addFileSourceArg(build_file);
  57. run_step.addArg("--cache-dir");
  58. const cache_root_path = self.cache_root.path orelse @panic("todo");
  59. run_step.addArg(self.pathFromRoot(cache_root_path));
  60. return run_step;
  61. }