build.zig 4.0 KB

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