build.zig 6.7 KB

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