build2.zig 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. const std = @import("std");
  2. const builtin = @import("builtin");
  3. const Builder = std.build.Builder;
  4. const Pkg = std.build.Pkg;
  5. // TODO: make this work with "GitRepoStep.zig", there is a
  6. // problem with the -Dfetch option
  7. const GitRepoStep = @import("dep/ziget/GitRepoStep.zig");
  8. const zigetbuild = @import("dep/ziget/build.zig");
  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 = @embedFile("zigetsha"),
  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 target = if (b.option([]const u8, "ci_target", "the CI target being built")) |ci_target|
  30. try std.zig.CrossTarget.parse(.{ .arch_os_abi = ci_target_map.get(ci_target) orelse {
  31. std.log.err("unknown ci_target '{s}'", .{ci_target});
  32. std.os.exit(1);
  33. } })
  34. else
  35. b.standardTargetOptions(.{});
  36. const mode = b.standardReleaseOptions();
  37. const zigup_build_options = b.addOptions();
  38. const win32exelink: ?*std.build.LibExeObjStep = blk: {
  39. if (target.getOs().tag == .windows) {
  40. const exe = b.addExecutable("win32exelink", "win32exelink.zig");
  41. exe.setTarget(target);
  42. exe.setBuildMode(mode);
  43. // workaround @embedFile not working with absolute paths, see https://github.com/ziglang/zig/issues/14551
  44. //zigup_build_options.addOptionFileSource("win32exelink_filename", .{ .generated = &exe.output_path_source });
  45. const update_step = RelativeOutputPathSourceStep.create(exe);
  46. zigup_build_options.addOptionFileSource("win32exelink_filename", .{ .generated = &update_step.output_path_source });
  47. break :blk exe;
  48. }
  49. break :blk null;
  50. };
  51. // TODO: Maybe add more executables with different ssl backends
  52. const exe = try addZigupExe(b, ziget_repo, target, mode, zigup_build_options, win32exelink, zigetbuild.SslBackend.iguana);
  53. exe.install();
  54. const run_cmd = exe.run();
  55. run_cmd.step.dependOn(b.getInstallStep());
  56. const run_step = b.step("run", "Run the app");
  57. run_step.dependOn(&run_cmd.step);
  58. if (b.args) |args| {
  59. run_cmd.addArgs(args);
  60. }
  61. addTest(b, exe, target, mode);
  62. }
  63. // This whole step is a workaround to @embedFile not working with absolute paths, see https://github.com/ziglang/zig/issues/14551
  64. const RelativeOutputPathSourceStep = struct {
  65. step: std.build.Step,
  66. exe: *std.build.LibExeObjStep,
  67. output_path_source: std.build.GeneratedFile,
  68. pub fn create(exe: *std.build.LibExeObjStep) *RelativeOutputPathSourceStep {
  69. const s = exe.builder.allocator.create(RelativeOutputPathSourceStep) catch unreachable;
  70. s.* = .{
  71. .step = std.build.Step.init(.custom, "relative output path", exe.builder.allocator, make),
  72. .exe = exe,
  73. .output_path_source = .{
  74. .step = &s.step,
  75. },
  76. };
  77. return s;
  78. }
  79. fn make(step: *std.build.Step) !void {
  80. const self = @fieldParentPtr(RelativeOutputPathSourceStep, "step", step);
  81. const b = self.exe.builder;
  82. //std.log.info("output path is '{s}'", .{self.exe.output_path_source.path.?});
  83. const abs_path = self.exe.output_path_source.path.?;
  84. std.debug.assert(std.mem.startsWith(u8, abs_path, b.build_root));
  85. self.output_path_source.path = std.mem.trimLeft(u8, abs_path[b.build_root.len..], "\\/");
  86. }
  87. };
  88. fn addTest(b: *Builder, exe: *std.build.LibExeObjStep, target: std.zig.CrossTarget, mode: std.builtin.Mode) void {
  89. const test_exe = b.addExecutable("test", "test.zig");
  90. test_exe.setTarget(target);
  91. test_exe.setBuildMode(mode);
  92. const run_cmd = test_exe.run();
  93. // TODO: make this work, add exe install path as argument to test
  94. //run_cmd.addArg(exe.getInstallPath());
  95. _ = exe;
  96. run_cmd.step.dependOn(b.getInstallStep());
  97. const test_step = b.step("test", "test the executable");
  98. test_step.dependOn(&run_cmd.step);
  99. }
  100. fn addZigupExe(
  101. b: *Builder,
  102. ziget_repo: *GitRepoStep,
  103. target: std.zig.CrossTarget,
  104. mode: std.builtin.Mode,
  105. zigup_build_options: *std.build.OptionsStep,
  106. optional_win32exelink: ?*std.build.LibExeObjStep,
  107. ssl_backend: ?SslBackend
  108. ) !*std.build.LibExeObjStep {
  109. const require_ssl_backend = b.allocator.create(RequireSslBackendStep) catch unreachable;
  110. require_ssl_backend.* = RequireSslBackendStep.init(b, "the zigup exe", ssl_backend);
  111. const exe = b.addExecutable("zigup", "zigup.zig");
  112. exe.setTarget(target);
  113. exe.setBuildMode(mode);
  114. if (optional_win32exelink) |win32exelink| {
  115. exe.step.dependOn(&win32exelink.step);
  116. }
  117. exe.addOptions("build_options", zigup_build_options);
  118. exe.step.dependOn(&ziget_repo.step);
  119. zigetbuild.addZigetPkg(exe, ssl_backend, ziget_repo.getPath(&exe.step));
  120. if (targetIsWindows(target)) {
  121. const zarc_repo = GitRepoStep.create(b, .{
  122. .url = "https://github.com/marler8997/zarc",
  123. .branch = "protected",
  124. .sha = "ca9554ffbfceedec6aae5f39fc71a52dbdec2a15",
  125. });
  126. exe.step.dependOn(&zarc_repo.step);
  127. const zarc_repo_path = zarc_repo.getPath(&exe.step);
  128. exe.addPackage(Pkg {
  129. .name = "zarc",
  130. .source = .{ .path = try join(b, &[_][]const u8 { zarc_repo_path, "src", "main.zig" }) },
  131. });
  132. }
  133. exe.step.dependOn(&require_ssl_backend.step);
  134. return exe;
  135. }
  136. fn targetIsWindows(target: std.zig.CrossTarget) bool {
  137. if (target.os_tag) |tag|
  138. return tag == .windows;
  139. return builtin.target.os.tag == .windows;
  140. }
  141. const SslBackendFailedStep = struct {
  142. step: std.build.Step,
  143. context: []const u8,
  144. backend: SslBackend,
  145. pub fn init(b: *Builder, context: []const u8, backend: SslBackend) SslBackendFailedStep {
  146. return .{
  147. .step = std.build.Step.init(.custom, "SslBackendFailedStep", b.allocator, make),
  148. .context = context,
  149. .backend = backend,
  150. };
  151. }
  152. fn make(step: *std.build.Step) !void {
  153. const self = @fieldParentPtr(RequireSslBackendStep, "step", step);
  154. std.debug.print("error: the {s} failed to add the {s} SSL backend\n", .{self.context, self.backend});
  155. std.os.exit(1);
  156. }
  157. };
  158. const RequireSslBackendStep = struct {
  159. step: std.build.Step,
  160. context: []const u8,
  161. backend: ?SslBackend,
  162. pub fn init(b: *Builder, context: []const u8, backend: ?SslBackend) RequireSslBackendStep {
  163. return .{
  164. .step = std.build.Step.init(.custom, "RequireSslBackend", b.allocator, make),
  165. .context = context,
  166. .backend = backend,
  167. };
  168. }
  169. fn make(step: *std.build.Step) !void {
  170. const self = @fieldParentPtr(RequireSslBackendStep, "step", step);
  171. if (self.backend) |_| { } else {
  172. std.debug.print("error: {s} requires an SSL backend:\n", .{self.context});
  173. inline for (zigetbuild.ssl_backends) |field| {
  174. std.debug.print(" -D{s}\n", .{field.name});
  175. }
  176. std.os.exit(1);
  177. }
  178. }
  179. };
  180. fn addGithubReleaseExe(b: *Builder, github_release_step: *std.build.Step, ziget_repo: []const u8, comptime target_triple: []const u8, comptime ssl_backend: SslBackend) !void {
  181. const small_release = true;
  182. const target = try std.zig.CrossTarget.parse(.{ .arch_os_abi = target_triple });
  183. const mode = if (small_release) .ReleaseSafe else .Debug;
  184. const exe = try addZigupExe(b, ziget_repo, target, mode, ssl_backend);
  185. if (small_release) {
  186. exe.strip = true;
  187. }
  188. exe.setOutputDir("github-release" ++ std.fs.path.sep_str ++ target_triple ++ std.fs.path.sep_str ++ @tagName(ssl_backend));
  189. github_release_step.dependOn(&exe.step);
  190. }
  191. fn join(b: *Builder, parts: []const []const u8) ![]const u8 {
  192. return try std.fs.path.join(b.allocator, parts);
  193. }
  194. const ci_target_map = std.ComptimeStringMap([]const u8, .{
  195. .{ "ubuntu-latest-x86_64", "x86_64-linux" },
  196. .{ "macos-latest-x86_64", "x86_64-macos" },
  197. .{ "windows-latest-x86_64", "x86_64-windows" },
  198. .{ "ubuntu-latest-aarch64", "aarch64-linux" },
  199. .{ "macos-latest-aarch64", "aarch64-macos" },
  200. });