build.zig 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. });
  24. const build2 = addBuild(b, .{ .path = "build2.zig" }, .{});
  25. build2.addArgs(try getBuildArgs(b));
  26. ziget_repo.step.make() catch |e| return e;
  27. build2.step.make() catch |err| switch (err) {
  28. error.UnexpectedExitCode => std.os.exit(0xff), // error already printed by subprocess
  29. else => |e| return e,
  30. };
  31. std.os.exit(0);
  32. }
  33. // TODO: remove the following if https://github.com/ziglang/zig/pull/9987 is integrated
  34. fn getBuildArgs(self: *Builder) ! []const [:0]const u8 {
  35. const args = try std.process.argsAlloc(self.allocator);
  36. return args[5..];
  37. }
  38. pub fn addBuild(self: *Builder, build_file: std.build.FileSource, _: struct { }) *std.build.RunStep {
  39. const run_step = std.build.RunStep.create(
  40. self,
  41. self.fmt("zig build {s}", .{build_file.getDisplayName()}),
  42. );
  43. run_step.addArg(self.zig_exe);
  44. run_step.addArg("build");
  45. run_step.addArg("--build-file");
  46. run_step.addFileSourceArg(build_file);
  47. run_step.addArg("--cache-dir");
  48. run_step.addArg(self.pathFromRoot(self.cache_root));
  49. return run_step;
  50. }