build2.zig 9.2 KB

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