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