From 59c9fe56d997d7d3d7d84c1c9f90b306e19f0b4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hendrik=20Niel=C3=A4nder?= Date: Thu, 25 Jan 2024 22:32:58 +0100 Subject: [PATCH] chore: upgrade zig 0.12 --- build.zig | 104 ++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 81 insertions(+), 23 deletions(-) diff --git a/build.zig b/build.zig index 7bc35eb..f65018e 100644 --- a/build.zig +++ b/build.zig @@ -1,4 +1,6 @@ const std = @import("std"); +const builtin = @import("builtin"); + const CrossTargetInfo = struct { crossTarget: std.zig.CrossTarget, name: []const u8, @@ -6,47 +8,103 @@ const CrossTargetInfo = struct { // Semantic version of your application const version = std.SemanticVersion{ .major = 0, .minor = 1, .patch = 2 }; +const min_zig_string = "0.12.0-dev.2341+92211135f"; + +const Build = blk: { + const current_zig = builtin.zig_version; + const min_zig = std.SemanticVersion.parse(min_zig_string) catch unreachable; + if (current_zig.order(min_zig) == .lt) { + @compileError(std.fmt.comptimePrint("Your Zig version v{} does not meet the minimum build requirement of v{}", .{ current_zig, min_zig })); + } + break :blk std.Build; +}; + pub fn build(b: *std.Build) void { + const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); - // Add a global option for versioning const options = b.addOptions(); options.addOption(std.SemanticVersion, "zvm_version", version); - const crossTargets = [_]CrossTargetInfo{ - CrossTargetInfo{ .crossTarget = std.zig.CrossTarget{ .cpu_arch = .x86_64, .os_tag = .macos }, .name = "zvm_macos-x86_64" }, - CrossTargetInfo{ .crossTarget = std.zig.CrossTarget{ .cpu_arch = .x86_64, .os_tag = .linux }, .name = "zvm_linux-x86_64" }, - // Add more targets as necessary + const exe = b.addExecutable(.{ + .name = "zvm", + .root_source_file = .{ .path = "src/main.zig" }, + .target = target, + .optimize = optimize, + .version = version, + }); + + // Link dependencies and set include paths + exe.linkLibC(); + + exe.addIncludePath(.{ .path = "src/deps/libarchive/libarchive" }); + exe.addLibraryPath(.{ .path = "src/deps" }); + exe.addLibraryPath(.{ .path = "/usr/lib/x86_64-linux-gnu" }); + exe.addLibraryPath(.{ .path = "/usr/local/lib" }); + exe.linkSystemLibrary("archive"); // libarchive + exe.linkSystemLibrary("lzma"); // liblzma + + exe.addOptions("options", options); + + b.installArtifact(exe); + + const release = b.step("release", "make an upstream binary release"); + const release_targets = [_]std.Target.Query{ + .{ + .cpu_arch = .aarch64, + .os_tag = .linux, + }, + .{ + .cpu_arch = .x86_64, + .os_tag = .linux, + }, + .{ + .cpu_arch = .x86, + .os_tag = .linux, + }, + .{ + .cpu_arch = .riscv64, + .os_tag = .linux, + }, + .{ + .cpu_arch = .x86_64, + .os_tag = .macos, + }, }; - // Function to create executables for each target - for (crossTargets) |targetInfo| { - const exe = b.addExecutable(.{ - .name = targetInfo.name, + for (release_targets) |target_query| { + const resolved_target = b.resolveTargetQuery(target_query); + const t = resolved_target.result; + const rel_exe = b.addExecutable(.{ + .name = "zvm", .root_source_file = .{ .path = "src/main.zig" }, - .target = targetInfo.crossTarget, - .optimize = optimize, - .version = version, + .target = resolved_target, + .optimize = .ReleaseSafe, + .strip = true, }); - // Link dependencies and set include paths - exe.linkLibC(); + rel_exe.linkLibC(); + + rel_exe.addIncludePath(.{ .path = "src/deps/libarchive/libarchive" }); + rel_exe.addLibraryPath(.{ .path = "src/deps" }); + rel_exe.addLibraryPath(.{ .path = "/usr/lib/x86_64-linux-gnu" }); + rel_exe.addLibraryPath(.{ .path = "/usr/local/lib" }); + rel_exe.linkSystemLibrary("archive"); // libarchive + rel_exe.linkSystemLibrary("lzma"); // liblzma - exe.addIncludePath(.{ .path = "src/deps/libarchive/libarchive" }); - exe.addLibraryPath(.{ .path = "src/deps" }); - exe.addLibraryPath(.{ .path = "/usr/lib/x86_64-linux-gnu" }); - exe.addLibraryPath(.{ .path = "/usr/local/lib" }); - exe.linkSystemLibrary("archive"); // libarchive - exe.linkSystemLibrary("lzma"); // liblzma + rel_exe.addOptions("options", options); - exe.addOptions("options", options); + const install = b.addInstallArtifact(rel_exe, .{}); + install.dest_dir = .prefix; + install.dest_sub_path = b.fmt("{s}-{s}-{s}", .{ + @tagName(t.cpu.arch), @tagName(t.os.tag), rel_exe.name, + }); - b.installArtifact(exe); + release.dependOn(&install.step); } // Creates a step for unit testing. This only builds the test executable // but does not run it. - const target = b.standardTargetOptions(.{}); const unit_tests = b.addTest(.{ .root_source_file = .{ .path = "src/main.zig" }, .target = target,