lens

posix system fetch tool
git clone git://brookjeynes.dev/bjeynes/lens.git
Log | Files | Refs | README

build.zig (2613B)


      1 const std = @import("std");
      2 
      3 ///Must match the `version` in `build.zig.zon`.
      4 const version = std.SemanticVersion{ .major = 2, .minor = 0, .patch = 0 };
      5 
      6 const release_targets: []const std.Target.Query = &.{
      7     .{ .cpu_arch = .aarch64, .os_tag = .macos },
      8     .{ .cpu_arch = .aarch64, .os_tag = .linux },
      9     .{ .cpu_arch = .x86_64, .os_tag = .linux },
     10     .{ .cpu_arch = .x86_64, .os_tag = .macos },
     11 };
     12 
     13 fn createExe(
     14     b: *std.Build,
     15     exe_name: []const u8,
     16     target: std.Build.ResolvedTarget,
     17     optimize: std.builtin.OptimizeMode,
     18     build_options: *std.Build.Module,
     19 ) !*std.Build.Step.Compile {
     20     const ziggy = b.dependency("ziggy", .{
     21         .target = target,
     22         .optimize = optimize,
     23     }).module("ziggy");
     24 
     25     const exe = b.addExecutable(.{
     26         .name = exe_name,
     27         .root_module = b.createModule(.{
     28             .root_source_file = b.path("src/main.zig"),
     29             .target = target,
     30             .optimize = optimize,
     31         }),
     32     });
     33 
     34     exe.root_module.addImport("options", build_options);
     35     exe.root_module.addImport("ziggy", ziggy);
     36 
     37     return exe;
     38 }
     39 
     40 pub fn build(b: *std.Build) !void {
     41     const target = b.standardTargetOptions(.{});
     42     const optimize = b.standardOptimizeOption(.{});
     43 
     44     const build_options = b.addOptions();
     45     build_options.step.name = "build options";
     46     build_options.addOption(std.SemanticVersion, "version", version);
     47     const build_options_module = build_options.createModule();
     48 
     49     const build_all = b.option(
     50         bool,
     51         "all-targets",
     52         "Build all targets in ReleaseSafe mode.",
     53     ) orelse false;
     54     if (build_all) {
     55         try build_targets(b, build_options_module);
     56         return;
     57     }
     58 
     59     const exe = try createExe(b, "lens", target, optimize, build_options_module);
     60     b.installArtifact(exe);
     61 
     62     const run_cmd = b.addRunArtifact(exe);
     63     run_cmd.step.dependOn(b.getInstallStep());
     64     if (b.args) |args| {
     65         run_cmd.addArgs(args);
     66     }
     67 
     68     const run_step = b.step("run", "Run the app");
     69     run_step.dependOn(&run_cmd.step);
     70 }
     71 
     72 fn build_targets(b: *std.Build, build_options: *std.Build.Module) !void {
     73     for (release_targets) |t| {
     74         const target = b.resolveTargetQuery(t);
     75 
     76         const exe = try createExe(b, "lens", target, .ReleaseSafe, build_options);
     77         b.installArtifact(exe);
     78 
     79         const target_output = b.addInstallArtifact(exe, .{
     80             .dest_dir = .{
     81                 .override = .{
     82                     .custom = try t.zigTriple(b.allocator),
     83                 },
     84             },
     85         });
     86 
     87         b.getInstallStep().dependOn(&target_output.step);
     88     }
     89 }