build.zig 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. fn addTest(
  50. b: *std.Build,
  51. exe: *std.Build.Step.Compile,
  52. target: std.Build.ResolvedTarget,
  53. optimize: std.builtin.Mode,
  54. ) void {
  55. const test_exe = b.addExecutable(.{
  56. .name = "test",
  57. .root_source_file = .{ .path = "test.zig" },
  58. .target = target,
  59. .optimize = optimize,
  60. });
  61. const run_cmd = b.addRunArtifact(test_exe);
  62. // TODO: make this work, add exe install path as argument to test
  63. //run_cmd.addArg(exe.getInstallPath());
  64. _ = exe;
  65. run_cmd.step.dependOn(b.getInstallStep());
  66. const test_step = b.step("test", "test the executable");
  67. test_step.dependOn(&run_cmd.step);
  68. }
  69. fn addZigupExe(
  70. b: *std.Build,
  71. target: std.Build.ResolvedTarget,
  72. optimize: std.builtin.Mode,
  73. win32exelink_mod: ?*std.Build.Module,
  74. ) !*std.Build.Step.Compile {
  75. const exe = b.addExecutable(.{
  76. .name = "zigup",
  77. .root_source_file = .{ .path = "zigup.zig" },
  78. .target = target,
  79. .optimize = optimize,
  80. });
  81. if (target.result.os.tag == .windows) {
  82. exe.root_module.addImport("win32exelink", win32exelink_mod.?);
  83. if (b.lazyDependency("zarc", .{})) |zarc_dep| {
  84. exe.root_module.addImport("zarc", zarc_dep.module("zarc"));
  85. }
  86. }
  87. return exe;
  88. }
  89. fn addGithubReleaseExe(
  90. b: *std.Build,
  91. github_release_step: *std.build.Step,
  92. comptime target_triple: []const u8,
  93. ) !void {
  94. const small_release = true;
  95. const target = try std.zig.CrossTarget.parse(.{ .arch_os_abi = target_triple });
  96. const mode = if (small_release) .ReleaseSafe else .Debug;
  97. const exe = try addZigupExe(b, target, mode);
  98. if (small_release) {
  99. exe.strip = true;
  100. }
  101. exe.setOutputDir("github-release" ++ std.fs.path.sep_str ++ target_triple);
  102. github_release_step.dependOn(&exe.step);
  103. }
  104. const ci_target_map = std.ComptimeStringMap([]const u8, .{
  105. .{ "ubuntu-latest-x86_64", "x86_64-linux" },
  106. .{ "macos-latest-x86_64", "x86_64-macos" },
  107. .{ "windows-latest-x86_64", "x86_64-windows" },
  108. .{ "ubuntu-latest-aarch64", "aarch64-linux" },
  109. .{ "macos-latest-aarch64", "aarch64-macos" },
  110. });