build.zig 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. const std = @import("std");
  2. const builtin = @import("builtin");
  3. const Builder = std.build.Builder;
  4. const Pkg = std.build.Pkg;
  5. const GitRepoStep = @import("ziget-build-files-copy/GitRepoStep.zig");
  6. const zigetbuild = @import("ziget-build-files-copy/build.zig");
  7. // TODO: use this if/when we get @tryImport
  8. //const SslBackend = if (zigetbuild) zigetbuild.SslBackend else enum {};
  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 = "0b43c12a395b67326f5f60dd593a2eea745178c2",
  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. // TODO: Maybe add more executables with different ssl backends
  32. const exe = try addZigupExe(b, ziget_repo, target, mode, zigetbuild.SslBackend.iguana);
  33. exe.install();
  34. const run_cmd = exe.run();
  35. run_cmd.step.dependOn(b.getInstallStep());
  36. const run_step = b.step("run", "Run the app");
  37. run_step.dependOn(&run_cmd.step);
  38. addTest(b, exe, target, mode);
  39. }
  40. fn addTest(b: *Builder, exe: *std.build.LibExeObjStep, target: std.zig.CrossTarget, mode: std.builtin.Mode) void {
  41. const test_exe = b.addExecutable("test", "test.zig");
  42. test_exe.setTarget(target);
  43. test_exe.setBuildMode(mode);
  44. const run_cmd = test_exe.run();
  45. // TODO: make this work, add exe install path as argument to test
  46. //run_cmd.addArg(exe.getInstallPath());
  47. _ = exe;
  48. run_cmd.step.dependOn(b.getInstallStep());
  49. const test_step = b.step("test", "test the executable");
  50. test_step.dependOn(&run_cmd.step);
  51. }
  52. fn addZigupExe(
  53. b: *Builder,
  54. ziget_repo: *GitRepoStep,
  55. target: std.zig.CrossTarget,
  56. mode: std.builtin.Mode,
  57. ssl_backend: ?SslBackend
  58. ) !*std.build.LibExeObjStep {
  59. const require_ssl_backend = b.allocator.create(RequireSslBackendStep) catch unreachable;
  60. require_ssl_backend.* = RequireSslBackendStep.init(b, "the zigup exe", ssl_backend);
  61. const exe = b.addExecutable("zigup", "zigup.zig");
  62. exe.setTarget(target);
  63. exe.setBuildMode(mode);
  64. exe.step.dependOn(&ziget_repo.step);
  65. zigetbuild.addZigetPkg(exe, ssl_backend, ziget_repo.getPath(&exe.step));
  66. if (targetIsWindows(target)) {
  67. const zarc_repo = GitRepoStep.create(b, .{
  68. .url = "https://github.com/SuperAuguste/zarc",
  69. .branch = null,
  70. .sha = "2a8fd27baa781b9de821b1b4e0b89283413054b8",
  71. });
  72. exe.step.dependOn(&zarc_repo.step);
  73. const zarc_repo_path = zarc_repo.getPath(&exe.step);
  74. exe.addPackage(Pkg {
  75. .name = "zarc",
  76. .path = .{ .path = try join(b, &[_][]const u8 { zarc_repo_path, "src", "main.zig" }) },
  77. });
  78. }
  79. exe.step.dependOn(&require_ssl_backend.step);
  80. return exe;
  81. }
  82. fn targetIsWindows(target: std.zig.CrossTarget) bool {
  83. if (target.os_tag) |tag|
  84. return tag == .windows;
  85. return builtin.target.os.tag == .windows;
  86. }
  87. const SslBackendFailedStep = struct {
  88. step: std.build.Step,
  89. context: []const u8,
  90. backend: SslBackend,
  91. pub fn init(b: *Builder, context: []const u8, backend: SslBackend) SslBackendFailedStep {
  92. return .{
  93. .step = std.build.Step.init(.custom, "SslBackendFailedStep", b.allocator, make),
  94. .context = context,
  95. .backend = backend,
  96. };
  97. }
  98. fn make(step: *std.build.Step) !void {
  99. const self = @fieldParentPtr(RequireSslBackendStep, "step", step);
  100. std.debug.print("error: the {s} failed to add the {s} SSL backend\n", .{self.context, self.backend});
  101. std.os.exit(1);
  102. }
  103. };
  104. const RequireSslBackendStep = struct {
  105. step: std.build.Step,
  106. context: []const u8,
  107. backend: ?SslBackend,
  108. pub fn init(b: *Builder, context: []const u8, backend: ?SslBackend) RequireSslBackendStep {
  109. return .{
  110. .step = std.build.Step.init(.custom, "RequireSslBackend", b.allocator, make),
  111. .context = context,
  112. .backend = backend,
  113. };
  114. }
  115. fn make(step: *std.build.Step) !void {
  116. const self = @fieldParentPtr(RequireSslBackendStep, "step", step);
  117. if (self.backend) |_| { } else {
  118. std.debug.print("error: {s} requires an SSL backend:\n", .{self.context});
  119. inline for (zigetbuild.ssl_backends) |field| {
  120. std.debug.print(" -D{s}\n", .{field.name});
  121. }
  122. std.os.exit(1);
  123. }
  124. }
  125. };
  126. fn addGithubReleaseExe(b: *Builder, github_release_step: *std.build.Step, ziget_repo: []const u8, comptime target_triple: []const u8, comptime ssl_backend: SslBackend) !void {
  127. const small_release = true;
  128. const target = try std.zig.CrossTarget.parse(.{ .arch_os_abi = target_triple });
  129. const mode = if (small_release) .ReleaseSafe else .Debug;
  130. const exe = try addZigupExe(b, ziget_repo, target, mode, ssl_backend);
  131. if (small_release) {
  132. exe.strip = true;
  133. }
  134. exe.setOutputDir("github-release" ++ std.fs.path.sep_str ++ target_triple ++ std.fs.path.sep_str ++ @tagName(ssl_backend));
  135. github_release_step.dependOn(&exe.step);
  136. }
  137. fn join(b: *Builder, parts: []const []const u8) ![]const u8 {
  138. return try std.fs.path.join(b.allocator, parts);
  139. }