# build.zig · versecafe/mstat

[View on GitCafe](https://staging.git.cafe/versecafe/mstat/blob/HEAD/build.zig)

Repository: [versecafe/mstat](https://staging.git.cafe/versecafe/mstat)

Visibility: public

Requested revision: HEAD

Commit: 5aa3dbe04909ee7b23e470f7a4355340e3f8d1ef

Blob: 1e0e88d2061539aef4d088adc45ec2d6d6c14216

Size: 3846 bytes

[Immutable source](https://staging.git.cafe/versecafe/mstat/blob/5aa3dbe04909ee7b23e470f7a4355340e3f8d1ef/build.zig?format=markdown)

```
const std = @import("std");

pub fn build(b: *std.Build) void {
    const target = b.standardTargetOptions(.{});
    const optimize = b.standardOptimizeOption(.{});

    if (target.result.os.tag != .macos) @panic("Mstat supports macOS only");
    const swift_target = switch (target.result.cpu.arch) {
        .aarch64 => "arm64-apple-macosx13.0",
        .x86_64 => "x86_64-apple-macosx13.0",
        else => @panic("Mstat supports arm64 and x86_64 macOS only"),
    };
    const engine = b.addLibrary(.{
        .name = "mstat_engine",
        .linkage = .static,
        .root_module = b.createModule(.{
            .root_source_file = b.path("src/engine.zig"),
            .target = target,
            .optimize = optimize,
        }),
    });
    engine.root_module.linkFramework("IOKit", .{});
    engine.root_module.link_libc = true;

    const swift = b.addSystemCommand(&.{ "/usr/bin/xcrun", "swiftc" });
    const module_cache = b.pathFromRoot(".zig-cache/swift-module-cache");
    swift.setEnvironmentVariable("CLANG_MODULE_CACHE_PATH", module_cache);
    swift.setEnvironmentVariable("SWIFT_MODULECACHE_PATH", module_cache);
    swift.addArgs(&.{ "-O", "-parse-as-library", "-swift-version", "5", "-target", swift_target, "-import-objc-header" });
    swift.addFileArg(b.path("src/mstat_engine.h"));
    swift.addFileArg(b.path("src/MstatApp.swift"));
    swift.addArtifactArg(engine);
    swift.addArgs(&.{
        "-framework", "AppKit",
        "-framework", "Charts",
        "-framework", "IOKit",
        "-framework", "SwiftUI",
        "-Xlinker",   "-dead_strip",
        "-o",
    });
    const app_binary = swift.addOutputFileArg("mstat");

    const install_app_binary = b.addInstallFile(
        app_binary,
        "Mstat.app/Contents/MacOS/mstat",
    );
    const install_app_plist = b.addInstallFile(
        b.path("resources/Info.plist"),
        "Mstat.app/Contents/Info.plist",
    );
    const app_step = b.step("app", "Build the Mstat.app bundle");
    app_step.dependOn(&install_app_binary.step);
    app_step.dependOn(&install_app_plist.step);
    b.getInstallStep().dependOn(&install_app_binary.step);
    b.getInstallStep().dependOn(&install_app_plist.step);

    const open_app = b.addSystemCommand(&.{ "/usr/bin/open", b.getInstallPath(.prefix, "Mstat.app") });
    open_app.step.dependOn(app_step);
    const run_step = b.step("run", "Build and open Mstat.app");
    run_step.dependOn(&open_app.step);

    const cli = b.addExecutable(.{
        .name = "mstat-cli",
        .root_module = b.createModule(.{
            .root_source_file = b.path("src/main.zig"),
            .target = target,
            .optimize = optimize,
        }),
    });
    cli.root_module.linkFramework("AppKit", .{});
    cli.root_module.linkFramework("CoreFoundation", .{});
    cli.root_module.linkFramework("IOKit", .{});
    cli.root_module.linkSystemLibrary("objc", .{});

    const once_cmd = b.addRunArtifact(cli);
    once_cmd.setEnvironmentVariable("MSTAT_ONCE", "1");
    const once_step = b.step("once", "Print one metrics snapshot and exit");
    once_step.dependOn(&once_cmd.step);

    const once_json_cmd = b.addRunArtifact(cli);
    once_json_cmd.setEnvironmentVariable("MSTAT_ONCE", "1");
    once_json_cmd.setEnvironmentVariable("MSTAT_JSON", "1");
    const once_json_step = b.step("once-json", "Print one metrics snapshot as JSON and exit");
    once_json_step.dependOn(&once_json_cmd.step);

    const tests = b.addTest(.{
        .root_module = b.createModule(.{
            .root_source_file = b.path("src/tests.zig"),
            .target = target,
            .optimize = optimize,
        }),
    });
    tests.root_module.linkFramework("IOKit", .{});
    const run_tests = b.addRunArtifact(tests);
    const test_step = b.step("test", "Run unit tests");
    test_step.dependOn(&run_tests.step);
}

```
