build2.zig 7.1 KB

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