build.zig 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. _ = exe;
  46. run_cmd.step.dependOn(b.getInstallStep());
  47. const test_step = b.step("test", "test the executable");
  48. test_step.dependOn(&run_cmd.step);
  49. }
  50. fn addZigupExe(b: *Builder, ziget_repo: []const u8, target: std.zig.CrossTarget, mode: std.builtin.Mode, ssl_backend: ?SslBackend) !*std.build.LibExeObjStep {
  51. const require_ssl_backend = b.allocator.create(RequireSslBackendStep) catch unreachable;
  52. require_ssl_backend.* = RequireSslBackendStep.init(b, "the zigup exe", ssl_backend);
  53. const exe = b.addExecutable("zigup", "zigup.zig");
  54. exe.setTarget(target);
  55. exe.setBuildMode(mode);
  56. const ziget_ssl_pkg = blk: {
  57. if (ssl_backend) |backend| {
  58. break :blk zigetbuild.addSslBackend(exe, backend, ziget_repo) catch {
  59. const ssl_backend_failed = b.allocator.create(SslBackendFailedStep) catch unreachable;
  60. ssl_backend_failed.* = SslBackendFailedStep.init(b, "the zigup exe", backend);
  61. break :blk Pkg {
  62. .name = "missing-ssl-backend-files",
  63. .path = .{ .path = "missing-ssl-backend-files.zig" },
  64. };
  65. };
  66. }
  67. break :blk Pkg {
  68. .name = "no-ssl-backend-configured",
  69. .path = .{ .path = "no-ssl-backend-configured.zig" },
  70. };
  71. };
  72. exe.addPackage(Pkg {
  73. .name = "ziget",
  74. .path = .{ .path = try join(b, &[_][]const u8 { ziget_repo, "ziget.zig" }) },
  75. .dependencies = &[_]Pkg {ziget_ssl_pkg},
  76. });
  77. exe.step.dependOn(&require_ssl_backend.step);
  78. return exe;
  79. }
  80. const SslBackendFailedStep = struct {
  81. step: std.build.Step,
  82. context: []const u8,
  83. backend: SslBackend,
  84. pub fn init(b: *Builder, context: []const u8, backend: SslBackend) SslBackendFailedStep {
  85. return .{
  86. .step = std.build.Step.init(.custom, "SslBackendFailedStep", b.allocator, make),
  87. .context = context,
  88. .backend = backend,
  89. };
  90. }
  91. fn make(step: *std.build.Step) !void {
  92. const self = @fieldParentPtr(RequireSslBackendStep, "step", step);
  93. std.debug.print("error: the {s} failed to add the {s} SSL backend\n", .{self.context, self.backend});
  94. std.os.exit(1);
  95. }
  96. };
  97. const RequireSslBackendStep = struct {
  98. step: std.build.Step,
  99. context: []const u8,
  100. backend: ?SslBackend,
  101. pub fn init(b: *Builder, context: []const u8, backend: ?SslBackend) RequireSslBackendStep {
  102. return .{
  103. .step = std.build.Step.init(.custom, "RequireSslBackend", b.allocator, make),
  104. .context = context,
  105. .backend = backend,
  106. };
  107. }
  108. fn make(step: *std.build.Step) !void {
  109. const self = @fieldParentPtr(RequireSslBackendStep, "step", step);
  110. if (self.backend) |_| { } else {
  111. std.debug.print("error: {s} requires an SSL backend:\n", .{self.context});
  112. inline for (zigetbuild.ssl_backends) |field| {
  113. std.debug.print(" -D{s}\n", .{field.name});
  114. }
  115. std.os.exit(1);
  116. }
  117. }
  118. };
  119. fn addGithubReleaseExe(b: *Builder, github_release_step: *std.build.Step, ziget_repo: []const u8, comptime target_triple: []const u8, comptime ssl_backend: SslBackend) !void {
  120. const small_release = true;
  121. const target = try std.zig.CrossTarget.parse(.{ .arch_os_abi = target_triple });
  122. const mode = if (small_release) .ReleaseSafe else .Debug;
  123. const exe = try addZigupExe(b, ziget_repo, target, mode, ssl_backend);
  124. if (small_release) {
  125. exe.strip = true;
  126. }
  127. exe.setOutputDir("github-release" ++ std.fs.path.sep_str ++ target_triple ++ std.fs.path.sep_str ++ @tagName(ssl_backend));
  128. github_release_step.dependOn(&exe.step);
  129. }
  130. fn join(b: *Builder, parts: []const []const u8) ![]const u8 {
  131. return try std.fs.path.join(b.allocator, parts);
  132. }
  133. pub const GitRepo = struct {
  134. url: []const u8,
  135. branch: ?[]const u8,
  136. sha: []const u8,
  137. path: ?[]const u8 = null,
  138. pub fn defaultReposDir(allocator: *std.mem.Allocator) ![]const u8 {
  139. const cwd = try std.process.getCwdAlloc(allocator);
  140. defer allocator.free(cwd);
  141. return try std.fs.path.join(allocator, &[_][]const u8 { cwd, "dep" });
  142. }
  143. pub fn resolve(self: GitRepo, allocator: *std.mem.Allocator) ![]const u8 {
  144. var optional_repos_dir_to_clean: ?[]const u8 = null;
  145. defer {
  146. if (optional_repos_dir_to_clean) |p| {
  147. allocator.free(p);
  148. }
  149. }
  150. const path = if (self.path) |p| try allocator.dupe(u8, p) else blk: {
  151. const repos_dir = try defaultReposDir(allocator);
  152. optional_repos_dir_to_clean = repos_dir;
  153. break :blk try std.fs.path.join(allocator, &[_][]const u8{ repos_dir, std.fs.path.basename(self.url) });
  154. };
  155. errdefer self.allocator.free(path);
  156. std.fs.accessAbsolute(path, std.fs.File.OpenFlags { .read = true }) catch {
  157. std.debug.print("Error: repository '{s}' does not exist\n", .{path});
  158. std.debug.print(" Run the following to clone it:\n", .{});
  159. const branch_args = if (self.branch) |b| &[2][]const u8 {" -b ", b} else &[2][]const u8 {"", ""};
  160. std.debug.print(" git clone {s}{s}{s} {s} && git -C {3s} checkout {s} -b for_zigup\n",
  161. .{self.url, branch_args[0], branch_args[1], path, self.sha});
  162. std.os.exit(1);
  163. };
  164. // TODO: check if the SHA matches an print a message and/or warning if it is different
  165. return path;
  166. }
  167. pub fn resolveOneFile(self: GitRepo, allocator: *std.mem.Allocator, index_sub_path: []const u8) ![]const u8 {
  168. const repo_path = try self.resolve(allocator);
  169. defer allocator.free(repo_path);
  170. return try std.fs.path.join(allocator, &[_][]const u8 { repo_path, index_sub_path });
  171. }
  172. };