build.zig 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. fn addTest(
  64. b: *std.Build,
  65. exe: *std.Build.Step.Compile,
  66. target: std.Build.ResolvedTarget,
  67. optimize: std.builtin.Mode,
  68. ) 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: *std.Build,
  85. target: std.Build.ResolvedTarget,
  86. optimize: std.builtin.Mode,
  87. win32exelink_mod: ?*std.Build.Module,
  88. ) !*std.Build.Step.Compile {
  89. const exe = b.addExecutable(.{
  90. .name = "zigup",
  91. .root_source_file = .{ .path = "zigup.zig" },
  92. .target = target,
  93. .optimize = optimize,
  94. });
  95. if (target.result.os.tag == .windows) {
  96. exe.root_module.addImport("win32exelink", win32exelink_mod.?);
  97. }
  98. return exe;
  99. }
  100. fn addGithubReleaseExe(
  101. b: *std.Build,
  102. github_release_step: *std.build.Step,
  103. comptime target_triple: []const u8,
  104. ) !void {
  105. const small_release = true;
  106. const target = try std.zig.CrossTarget.parse(.{ .arch_os_abi = target_triple });
  107. const mode = if (small_release) .ReleaseSafe else .Debug;
  108. const exe = try addZigupExe(b, target, mode);
  109. if (small_release) {
  110. exe.strip = true;
  111. }
  112. exe.setOutputDir("github-release" ++ std.fs.path.sep_str ++ target_triple);
  113. github_release_step.dependOn(&exe.step);
  114. }
  115. const ci_target_map = std.ComptimeStringMap([]const u8, .{
  116. .{ "ubuntu-latest-x86_64", "x86_64-linux" },
  117. .{ "macos-latest-x86_64", "x86_64-macos" },
  118. .{ "windows-latest-x86_64", "x86_64-windows" },
  119. .{ "ubuntu-latest-aarch64", "aarch64-linux" },
  120. .{ "macos-latest-aarch64", "aarch64-macos" },
  121. });