build.zig 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. const std = @import("std");
  2. const builtin = @import("builtin");
  3. const Pkg = std.build.Pkg;
  4. fn unwrapOptionalBool(optionalBool: ?bool) bool {
  5. if (optionalBool) |b| return b;
  6. return false;
  7. }
  8. pub fn build(b: *std.Build) !void {
  9. //var github_release_step = b.step("github-release", "Build the github-release binaries");
  10. //try addGithubReleaseExe(b, github_release_step, ziget_repo, "x86_64-linux", .std);
  11. const maybe_ci_target = b.option([]const u8, "ci_target", "the CI target being built");
  12. const target = if (maybe_ci_target) |ci_target|
  13. b.resolveTargetQuery(try std.zig.CrossTarget.parse(.{ .arch_os_abi = ci_target_map.get(ci_target) orelse {
  14. std.log.err("unknown ci_target '{s}'", .{ci_target});
  15. std.process.exit(1);
  16. } }))
  17. else
  18. b.standardTargetOptions(.{});
  19. // Compile in ReleaseSafe on Windows for faster extraction
  20. const optimize: std.builtin.OptimizeMode = if (
  21. (maybe_ci_target != null) and (target.result.os.tag == .windows)
  22. ) .ReleaseSafe else b.standardOptimizeOption(.{});
  23. const win32exelink_mod: ?*std.Build.Module = blk: {
  24. if (target.result.os.tag == .windows) {
  25. const exe = b.addExecutable(.{
  26. .name = "win32exelink",
  27. .root_source_file = .{ .path = "win32exelink.zig" },
  28. .target = target,
  29. .optimize = optimize,
  30. });
  31. break :blk b.createModule(.{
  32. .root_source_file = exe.getEmittedBin(),
  33. });
  34. }
  35. break :blk null;
  36. };
  37. const exe = try addZigupExe(
  38. b,
  39. target,
  40. optimize,
  41. win32exelink_mod,
  42. );
  43. b.installArtifact(exe);
  44. const run_cmd = b.addRunArtifact(exe);
  45. run_cmd.step.dependOn(b.getInstallStep());
  46. const run_step = b.step("run", "Run the app");
  47. run_step.dependOn(&run_cmd.step);
  48. if (b.args) |args| {
  49. run_cmd.addArgs(args);
  50. }
  51. addTest(b, exe, target, optimize);
  52. {
  53. const unzip = b.addExecutable(.{
  54. .name = "unzip",
  55. .root_source_file = b.path("unzip.zig"),
  56. .target = target,
  57. .optimize = optimize,
  58. });
  59. const install = b.addInstallArtifact(unzip, .{});
  60. b.step("unzip", "Build/install the unzip cmdline tool").dependOn(&install.step);
  61. }
  62. {
  63. const zip = b.addExecutable(.{
  64. .name = "zip",
  65. .root_source_file = b.path("zip.zig"),
  66. .target = target,
  67. .optimize = optimize,
  68. });
  69. const install = b.addInstallArtifact(zip, .{});
  70. b.step("zip", "Build/install the zip cmdline tool").dependOn(&install.step);
  71. }
  72. }
  73. fn addTest(
  74. b: *std.Build,
  75. exe: *std.Build.Step.Compile,
  76. target: std.Build.ResolvedTarget,
  77. optimize: std.builtin.Mode,
  78. ) void {
  79. const test_exe = b.addExecutable(.{
  80. .name = "test",
  81. .root_source_file = .{ .path = "test.zig" },
  82. .target = target,
  83. .optimize = optimize,
  84. });
  85. const run_cmd = b.addRunArtifact(test_exe);
  86. // TODO: make this work, add exe install path as argument to test
  87. //run_cmd.addArg(exe.getInstallPath());
  88. _ = exe;
  89. run_cmd.step.dependOn(b.getInstallStep());
  90. const test_step = b.step("test", "test the executable");
  91. test_step.dependOn(&run_cmd.step);
  92. }
  93. fn addZigupExe(
  94. b: *std.Build,
  95. target: std.Build.ResolvedTarget,
  96. optimize: std.builtin.Mode,
  97. win32exelink_mod: ?*std.Build.Module,
  98. ) !*std.Build.Step.Compile {
  99. const exe = b.addExecutable(.{
  100. .name = "zigup",
  101. .root_source_file = .{ .path = "zigup.zig" },
  102. .target = target,
  103. .optimize = optimize,
  104. });
  105. if (target.result.os.tag == .windows) {
  106. exe.root_module.addImport("win32exelink", win32exelink_mod.?);
  107. }
  108. return exe;
  109. }
  110. fn addGithubReleaseExe(
  111. b: *std.Build,
  112. github_release_step: *std.build.Step,
  113. comptime target_triple: []const u8,
  114. ) !void {
  115. const small_release = true;
  116. const target = try std.zig.CrossTarget.parse(.{ .arch_os_abi = target_triple });
  117. const mode = if (small_release) .ReleaseSafe else .Debug;
  118. const exe = try addZigupExe(b, target, mode);
  119. if (small_release) {
  120. exe.strip = true;
  121. }
  122. exe.setOutputDir("github-release" ++ std.fs.path.sep_str ++ target_triple);
  123. github_release_step.dependOn(&exe.step);
  124. }
  125. const ci_target_map = std.ComptimeStringMap([]const u8, .{
  126. .{ "ubuntu-latest-x86_64", "x86_64-linux" },
  127. .{ "macos-latest-x86_64", "x86_64-macos" },
  128. .{ "windows-latest-x86_64", "x86_64-windows" },
  129. .{ "ubuntu-latest-aarch64", "aarch64-linux" },
  130. .{ "macos-latest-aarch64", "aarch64-macos" },
  131. });