build2.zig 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 win32exelink_mod: ?*std.Build.Module = blk: {
  36. if (target.getOs().tag == .windows) {
  37. const exe = b.addExecutable(.{
  38. .name = "win32exelink",
  39. .root_source_file = .{ .path = "win32exelink.zig" },
  40. .target = target,
  41. .optimize = optimize,
  42. });
  43. break :blk b.createModule(.{
  44. .source_file = exe.getEmittedBin(),
  45. });
  46. }
  47. break :blk null;
  48. };
  49. // TODO: Maybe add more executables with different ssl backends
  50. const exe = try addZigupExe(
  51. b,
  52. ziget_repo,
  53. target,
  54. optimize,
  55. win32exelink_mod,
  56. .iguana,
  57. );
  58. b.installArtifact(exe);
  59. const run_cmd = b.addRunArtifact(exe);
  60. run_cmd.step.dependOn(b.getInstallStep());
  61. const run_step = b.step("run", "Run the app");
  62. run_step.dependOn(&run_cmd.step);
  63. if (b.args) |args| {
  64. run_cmd.addArgs(args);
  65. }
  66. addTest(b, exe, target, optimize);
  67. }
  68. fn addTest(b: *Builder, exe: *std.build.LibExeObjStep, target: std.zig.CrossTarget, optimize: std.builtin.Mode) void {
  69. const test_exe = b.addExecutable(.{
  70. .name = "test",
  71. .root_source_file = .{ .path = "test.zig" },
  72. .target = target,
  73. .optimize = optimize,
  74. });
  75. const run_cmd = b.addRunArtifact(test_exe);
  76. // TODO: make this work, add exe install path as argument to test
  77. //run_cmd.addArg(exe.getInstallPath());
  78. _ = exe;
  79. run_cmd.step.dependOn(b.getInstallStep());
  80. const test_step = b.step("test", "test the executable");
  81. test_step.dependOn(&run_cmd.step);
  82. }
  83. fn addZigupExe(
  84. b: *Builder,
  85. ziget_repo: *GitRepoStep,
  86. target: std.zig.CrossTarget,
  87. optimize: std.builtin.Mode,
  88. win32exelink_mod: ?*std.build.Module,
  89. ssl_backend: ?SslBackend,
  90. ) !*std.build.LibExeObjStep {
  91. const require_ssl_backend = b.allocator.create(RequireSslBackendStep) catch unreachable;
  92. require_ssl_backend.* = RequireSslBackendStep.init(b, "the zigup exe", ssl_backend);
  93. const exe = b.addExecutable(.{
  94. .name = "zigup",
  95. .root_source_file = .{ .path = "zigup.zig" },
  96. .target = target,
  97. .optimize = optimize,
  98. });
  99. exe.step.dependOn(&ziget_repo.step);
  100. zigetbuild.addZigetModule(exe, ssl_backend, ziget_repo.getPath(&exe.step));
  101. if (targetIsWindows(target)) {
  102. exe.addModule("win32exelink", win32exelink_mod.?);
  103. const zarc_repo = GitRepoStep.create(b, .{
  104. .url = "https://github.com/marler8997/zarc",
  105. .branch = "protected",
  106. .sha = "2e5256624d7871180badc9784b96dd66d927d604",
  107. .fetch_enabled = true,
  108. });
  109. exe.step.dependOn(&zarc_repo.step);
  110. const zarc_repo_path = zarc_repo.getPath(&exe.step);
  111. const zarc_mod = b.addModule("zarc", .{
  112. .source_file = .{ .path = b.pathJoin(&.{ zarc_repo_path, "src", "main.zig" }) },
  113. });
  114. exe.addModule("zarc", zarc_mod);
  115. }
  116. exe.step.dependOn(&require_ssl_backend.step);
  117. return exe;
  118. }
  119. fn targetIsWindows(target: std.zig.CrossTarget) bool {
  120. if (target.os_tag) |tag|
  121. return tag == .windows;
  122. return builtin.target.os.tag == .windows;
  123. }
  124. const SslBackendFailedStep = struct {
  125. step: std.build.Step,
  126. context: []const u8,
  127. backend: SslBackend,
  128. pub fn init(b: *Builder, context: []const u8, backend: SslBackend) SslBackendFailedStep {
  129. return .{
  130. .step = std.build.Step.init(.custom, "SslBackendFailedStep", b.allocator, make),
  131. .context = context,
  132. .backend = backend,
  133. };
  134. }
  135. fn make(step: *std.build.Step) !void {
  136. const self = @fieldParentPtr(RequireSslBackendStep, "step", step);
  137. std.debug.print("error: the {s} failed to add the {s} SSL backend\n", .{ self.context, self.backend });
  138. std.os.exit(1);
  139. }
  140. };
  141. const RequireSslBackendStep = struct {
  142. step: std.build.Step,
  143. context: []const u8,
  144. backend: ?SslBackend,
  145. pub fn init(b: *Builder, context: []const u8, backend: ?SslBackend) RequireSslBackendStep {
  146. return .{
  147. .step = std.build.Step.init(.{
  148. .id = .custom,
  149. .name = "RequireSslBackend",
  150. .owner = b,
  151. .makeFn = make,
  152. }),
  153. .context = context,
  154. .backend = backend,
  155. };
  156. }
  157. fn make(step: *std.build.Step, prog_node: *std.Progress.Node) !void {
  158. _ = prog_node;
  159. const self = @fieldParentPtr(RequireSslBackendStep, "step", step);
  160. if (self.backend) |_| {} else {
  161. std.debug.print("error: {s} requires an SSL backend:\n", .{self.context});
  162. inline for (zigetbuild.ssl_backends) |field| {
  163. std.debug.print(" -D{s}\n", .{field.name});
  164. }
  165. std.os.exit(1);
  166. }
  167. }
  168. };
  169. fn addGithubReleaseExe(b: *Builder, github_release_step: *std.build.Step, ziget_repo: []const u8, comptime target_triple: []const u8, comptime ssl_backend: SslBackend) !void {
  170. const small_release = true;
  171. const target = try std.zig.CrossTarget.parse(.{ .arch_os_abi = target_triple });
  172. const mode = if (small_release) .ReleaseSafe else .Debug;
  173. const exe = try addZigupExe(b, ziget_repo, target, mode, ssl_backend);
  174. if (small_release) {
  175. exe.strip = true;
  176. }
  177. exe.setOutputDir("github-release" ++ std.fs.path.sep_str ++ target_triple ++ std.fs.path.sep_str ++ @tagName(ssl_backend));
  178. github_release_step.dependOn(&exe.step);
  179. }
  180. const ci_target_map = std.ComptimeStringMap([]const u8, .{
  181. .{ "ubuntu-latest-x86_64", "x86_64-linux" },
  182. .{ "macos-latest-x86_64", "x86_64-macos" },
  183. .{ "windows-latest-x86_64", "x86_64-windows" },
  184. .{ "ubuntu-latest-aarch64", "aarch64-linux" },
  185. .{ "macos-latest-aarch64", "aarch64-macos" },
  186. });