build.zig 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. const std = @import("std");
  2. const Builder = std.build.Builder;
  3. const Pkg = std.build.Pkg;
  4. const zigetbuild = @import("ziget-build-files-copy/build.zig");
  5. // TODO: use this if/when we get @tryImport
  6. //const SslBackend = if (zigetbuild) zigetbuild.SslBackend else enum {};
  7. const SslBackend = zigetbuild.SslBackend;
  8. fn unwrapOptionalBool(optionalBool: ?bool) bool {
  9. if (optionalBool) |b| return b;
  10. return false;
  11. }
  12. pub fn build(b: *Builder) !void {
  13. const ziget_repo = try (GitRepo {
  14. .url = "https://github.com/marler8997/ziget",
  15. .branch = null,
  16. .sha = @embedFile("zigetsha"),
  17. }).resolve(b.allocator);
  18. // TODO: implement this if/when we get @tryImport
  19. //if (zigetbuild) |_| { } else {
  20. // std.log.err("TODO: add zigetbuild package and recompile/reinvoke build.d", .{});
  21. // return;
  22. //}
  23. var github_release_step = b.step("github-release", "Build the github-release binaries");
  24. // TODO: need to implement some interesting logic to make this work without
  25. // having the iguana repo copied into this one
  26. //try addGithubReleaseExe(b, github_release_step, ziget_repo, "x86_64-linux", SslBackend.iguana);
  27. const ssl_backend = zigetbuild.getSslBackend(b);
  28. const target = b.standardTargetOptions(.{});
  29. const mode = b.standardReleaseOptions();
  30. const exe = try addZigupExe(b, ziget_repo, target, mode, ssl_backend);
  31. exe.install();
  32. const run_cmd = exe.run();
  33. run_cmd.step.dependOn(b.getInstallStep());
  34. const run_step = b.step("run", "Run the app");
  35. run_step.dependOn(&run_cmd.step);
  36. addTest(b, exe, target, mode);
  37. }
  38. fn addTest(b: *Builder, exe: *std.build.LibExeObjStep, target: std.zig.CrossTarget, mode: std.builtin.Mode) void {
  39. const test_exe = b.addExecutable("test", "test.zig");
  40. test_exe.setTarget(target);
  41. test_exe.setBuildMode(mode);
  42. const run_cmd = test_exe.run();
  43. // TODO: make this work, add exe install path as argument to test
  44. //run_cmd.addArg(exe.getInstallPath());
  45. run_cmd.step.dependOn(b.getInstallStep());
  46. const test_step = b.step("test", "test the executable");
  47. test_step.dependOn(&run_cmd.step);
  48. }
  49. fn addZigupExe(b: *Builder, ziget_repo: []const u8, target: std.zig.CrossTarget, mode: std.builtin.Mode, ssl_backend: ?SslBackend) !*std.build.LibExeObjStep {
  50. const require_ssl_backend = b.allocator.create(RequireSslBackendStep) catch unreachable;
  51. require_ssl_backend.* = RequireSslBackendStep.init(b, "the zigup exe", ssl_backend);
  52. const exe = b.addExecutable("zigup", "zigup.zig");
  53. exe.setTarget(target);
  54. exe.setBuildMode(mode);
  55. const ziget_ssl_pkg = blk: {
  56. if (ssl_backend) |backend| {
  57. break :blk zigetbuild.addSslBackend(exe, backend, ziget_repo) catch |err| {
  58. const ssl_backend_failed = b.allocator.create(SslBackendFailedStep) catch unreachable;
  59. ssl_backend_failed.* = SslBackendFailedStep.init(b, "the zigup exe", backend);
  60. break :blk Pkg {
  61. .name = "missing-ssl-backend-files",
  62. .path = .{ .path = "missing-ssl-backend-files.zig" },
  63. };
  64. };
  65. }
  66. break :blk Pkg {
  67. .name = "no-ssl-backend-configured",
  68. .path = .{ .path = "no-ssl-backend-configured.zig" },
  69. };
  70. };
  71. exe.addPackage(Pkg {
  72. .name = "ziget",
  73. .path = .{ .path = try join(b, &[_][]const u8 { ziget_repo, "ziget.zig" }) },
  74. .dependencies = &[_]Pkg {ziget_ssl_pkg},
  75. });
  76. exe.step.dependOn(&require_ssl_backend.step);
  77. return exe;
  78. }
  79. const SslBackendFailedStep = struct {
  80. step: std.build.Step,
  81. context: []const u8,
  82. backend: SslBackend,
  83. pub fn init(b: *Builder, context: []const u8, backend: SslBackend) SslBackendFailedStep {
  84. return .{
  85. .step = std.build.Step.init(.custom, "SslBackendFailedStep", b.allocator, make),
  86. .context = context,
  87. .backend = backend,
  88. };
  89. }
  90. fn make(step: *std.build.Step) !void {
  91. const self = @fieldParentPtr(RequireSslBackendStep, "step", step);
  92. std.debug.print("error: the {s} failed to add the {s} SSL backend\n", .{self.context, self.backend});
  93. std.os.exit(1);
  94. }
  95. };
  96. const RequireSslBackendStep = struct {
  97. step: std.build.Step,
  98. context: []const u8,
  99. backend: ?SslBackend,
  100. pub fn init(b: *Builder, context: []const u8, backend: ?SslBackend) RequireSslBackendStep {
  101. return .{
  102. .step = std.build.Step.init(.custom, "RequireSslBackend", b.allocator, make),
  103. .context = context,
  104. .backend = backend,
  105. };
  106. }
  107. fn make(step: *std.build.Step) !void {
  108. const self = @fieldParentPtr(RequireSslBackendStep, "step", step);
  109. if (self.backend) |_| { } else {
  110. std.debug.print("error: {s} requires an SSL backend:\n", .{self.context});
  111. inline for (zigetbuild.ssl_backends) |field, i| {
  112. std.debug.print(" -D{s}\n", .{field.name});
  113. }
  114. std.os.exit(1);
  115. }
  116. }
  117. };
  118. fn addGithubReleaseExe(b: *Builder, github_release_step: *std.build.Step, ziget_repo: []const u8, comptime target_triple: []const u8, comptime ssl_backend: SslBackend) !void {
  119. const small_release = true;
  120. const target = try std.zig.CrossTarget.parse(.{ .arch_os_abi = target_triple });
  121. const mode = if (small_release) .ReleaseSafe else .Debug;
  122. const exe = try addZigupExe(b, ziget_repo, target, mode, ssl_backend);
  123. if (small_release) {
  124. exe.strip = true;
  125. }
  126. exe.setOutputDir("github-release" ++ std.fs.path.sep_str ++ target_triple ++ std.fs.path.sep_str ++ @tagName(ssl_backend));
  127. github_release_step.dependOn(&exe.step);
  128. }
  129. fn join(b: *Builder, parts: []const []const u8) ![]const u8 {
  130. return try std.fs.path.join(b.allocator, parts);
  131. }
  132. pub const GitRepo = struct {
  133. url: []const u8,
  134. branch: ?[]const u8,
  135. sha: []const u8,
  136. path: ?[]const u8 = null,
  137. pub fn defaultReposDir(allocator: *std.mem.Allocator) ![]const u8 {
  138. const cwd = try std.process.getCwdAlloc(allocator);
  139. defer allocator.free(cwd);
  140. return try std.fs.path.join(allocator, &[_][]const u8 { cwd, "dep" });
  141. }
  142. pub fn resolve(self: GitRepo, allocator: *std.mem.Allocator) ![]const u8 {
  143. var optional_repos_dir_to_clean: ?[]const u8 = null;
  144. defer {
  145. if (optional_repos_dir_to_clean) |p| {
  146. allocator.free(p);
  147. }
  148. }
  149. const path = if (self.path) |p| try allocator.dupe(u8, p) else blk: {
  150. const repos_dir = try defaultReposDir(allocator);
  151. optional_repos_dir_to_clean = repos_dir;
  152. break :blk try std.fs.path.join(allocator, &[_][]const u8{ repos_dir, std.fs.path.basename(self.url) });
  153. };
  154. errdefer self.allocator.free(path);
  155. std.fs.accessAbsolute(path, std.fs.File.OpenFlags { .read = true }) catch |err| {
  156. std.debug.print("Error: repository '{s}' does not exist\n", .{path});
  157. std.debug.print(" Run the following to clone it:\n", .{});
  158. const branch_args = if (self.branch) |b| &[2][]const u8 {" -b ", b} else &[2][]const u8 {"", ""};
  159. std.debug.print(" git clone {s}{s}{s} {s} && git -C {3s} checkout {s} -b for_zigup\n",
  160. .{self.url, branch_args[0], branch_args[1], path, self.sha});
  161. std.os.exit(1);
  162. };
  163. // TODO: check if the SHA matches an print a message and/or warning if it is different
  164. return path;
  165. }
  166. pub fn resolveOneFile(self: GitRepo, allocator: *std.mem.Allocator, index_sub_path: []const u8) ![]const u8 {
  167. const repo_path = try self.resolve(allocator);
  168. defer allocator.free(repo_path);
  169. return try std.fs.path.join(allocator, &[_][]const u8 { repo_path, index_sub_path });
  170. }
  171. };