mcpField
Z
zig-build-system

zig-build-system

github.com/mohitmishra786开发269518 次安装18 周前更新
代码仓库

关于 zig-build-system

面向编程 Agent 的 Zig 构建系统技能,指导编写 build.zig 和 build.zig.zon。涵盖可执行文件、库、C 源码、构建选项、测试、依赖及自定义步骤。

主要功能

  1. 01编写 build.zig
  2. 02配置可执行文件与库
  3. 03集成 C 源码
  4. 04设置构建选项与测试
  5. 05管理 build.zig.zon 依赖
  6. 06创建自定义构建步骤

适用场景

  1. 01为新 Zig 项目初始化构建文件
  2. 02在 Zig 构建中加入 C 源码
  3. 03配置构建时选项与测试
  4. 04添加 Zig 包依赖

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/mohitmishra786/low-level-dev-skills --skill zig-build-system

安装后在 Claude Code 中输入/zig-build-system确认已加载。

附带文件references/build-zig-patterns.md

详细介绍

Zig 构建系统

用途

指导 Agent 编写 build.zig 文件:可执行文件、库、C 源码集成、构建选项、测试配置,以及 build.zig.zon 软件包清单。

触发条件

  • “如何设置 build.zig 文件?”
  • “如何将 C 库添加到 Zig 项目中?”
  • “如何在 Zig 中定义构建时选项?”
  • “如何使用 zig build test 运行 Zig 测试?”
  • “什么是 build.zig.zon,以及如何使用它?”
  • “如何添加 Zig 软件包依赖项?”

工作流

1. 项目初始化
# Initialize a new project
mkdir myproject && cd myproject
zig init          # creates src/main.zig and build.zig

# Build
zig build

# Run
zig build run

# Test
zig build test
2. build.zig 结构
const std = @import("std");

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

    // Executable
    const exe = b.addExecutable(.{
        .name = "myapp",
        .root_source_file = b.path("src/main.zig"),
        .target = target,
        .optimize = optimize,
    });

    // Install step (zig build → copies to zig-out/bin/)
    b.installArtifact(exe);

    // Run step (zig build run)
    const run_cmd = b.addRunArtifact(exe);
    run_cmd.step.dependOn(b.getInstallStep());
    if (b.args) |args| {
        run_cmd.addArgs(args);
    }
    const run_step = b.step("run", "Run the app");
    run_step.dependOn(&run_cmd.step);

    // Test step (zig build test)
    const unit_tests = b.addTest(.{
        .root_source_file = b.path("src/main.zig"),
        .target = target,
        .optimize = optimize,
    });
    const run_unit_tests = b.addRunArtifact(unit_tests);
    const test_step = b.step("test", "Run unit tests");
    test_step.dependOn(&run_unit_tests.step);
}
3. 库
// Static library
const lib = b.addStaticLibrary(.{
    .name = "mylib",
    .root_source_file = b.path("src/mylib.zig"),
    .target = target,
    .optimize = optimize,
});
b.installArtifact(lib);

// Shared library
const shared_lib = b.addSharedLibrary(.{
    .name = "mylib",
    .root_source_file = b.path("src/mylib.zig"),
    .target = target,
    .optimize = optimize,
    .version = .{ .major = 1, .minor = 0, .patch = 0 },
});
b.installArtifact(shared_lib);

// Link library into executable
exe.linkLibrary(lib);
4. 添加 C 源文件
// Single C file
exe.addCSourceFile(.{
    .file = b.path("src/legacy.c"),
    .flags = &.{ "-std=c11", "-Wall", "-Wextra" },
});

// Multiple C files
exe.addCSourceFiles(.{
    .files = &.{
        "src/a.c",
        "src/b.c",
        "src/c.c",
    },
    .flags = &.{ "-std=c11", "-O2" },
});

// Include directories
exe.addIncludePath(b.path("include/"));
exe.addIncludePath(.{ .cwd_relative = "/usr/local/include" });

// System libraries
exe.linkSystemLibrary("curl");
exe.linkSystemLibrary("ssl");
exe.linkLibC();  // link libc (required if calling C stdlib)
5. 构建时选项
pub fn build(b: *std.Build) void {
    // Boolean option
    const enable_logging = b.option(
        bool,
        "logging",
        "Enable debug logging",
    ) orelse false;

    // Enum option
    const Backend = enum { opengl, vulkan, software };
    const backend = b.option(
        Backend,
        "backend",
        "Rendering backend",
    ) orelse .opengl;

    // Integer option
    const max_connections = b.option(
        u32,
        "max-connections",
        "Maximum concurrent connections",
    ) orelse 64;

    // Pass to Zig code as compile-time constant
    const options = b.addOptions();
    options.addOption(bool, "enable_logging", enable_logging);
    options.addOption(Backend, "backend", backend);
    options.addOption(u32, "max_connections", max_connections);

    exe.root_module.addOptions("build_options", options);
}

在 Zig 源码中:

const build_options = @import("build_options");

pub fn main() void {
    if (build_options.enable_logging) {
        std.debug.print("Logging enabled\n", .{});
    }
}
# Pass options on command line
zig build -Dlogging=true -Dbackend=vulkan -Dmax-connections=256
6. 模块系统
// Create a module (reusable across targets)
const mymodule = b.addModule("mymodule", .{
    .root_source_file = b.path("src/mymodule.zig"),
});

// Use module in executable
exe.root_module.addImport("mymodule", mymodule);

// Share module between exe and tests
const utils = b.addModule("utils", .{
    .root_source_file = b.path("src/utils.zig"),
});
exe.root_module.addImport("utils", utils);
unit_tests.root_module.addImport("utils", utils);

在 Zig 源码中:

const utils = @import("utils");
const mymodule = @import("mymodule");
7. 使用 build.zig.zon 进行软件包管理
// build.zig.zon
.{
    .name = "myapp",
    .version = "0.1.0",
    .minimum_zig_version = "0.13.0",

    .dependencies = .{
        .zig_clap = .{
            .url = "https://github.com/Hejsil/zig-clap/archive/refs/tags/0.9.1.tar.gz",
            .hash = "1220...",  // Run zig build to get the hash
        },
        .known_folders = .{
            .url = "https://github.com/ziglibs/known-folders/archive/refs/heads/master.tar.gz",
            .hash = "1220...",
        },
    },

    .paths = .{
        "build.zig",
        "build.zig.zon",
        "src",
        "LICENSE",
        "README.md",
    },
}
// build.zig — use the dependency
const clap_dep = b.dependency("zig_clap", .{
    .target = target,
    .optimize = optimize,
});
exe.root_module.addImport("clap", clap_dep.module("clap"));
# Fetch dependencies (creates zig-cache/packages/)
zig build    # auto-fetches on first run

# Zig will print the hash if missing — copy it into build.zig.zon
8. 自定义构建步骤
// Code generation step
const gen_step = b.addSystemCommand(&.{
    "python3", "scripts/gen.py", "--output", "src/generated.zig",
});
exe.step.dependOn(&gen_step.step);

// Custom install step
const install_config = b.addInstallFile(
    b.path("config/default.toml"),
    "share/myapp/config.toml",
);
b.getInstallStep().dependOn(&install_config.step);

有关高级 build.zig 模式,请参阅 references/build-zig-patterns.md。

相关技能

  • 单文件构建和编译器标志请使用 skills/zig/zig-compiler
  • build.zig 中的 C 库集成请使用 skills/zig/zig-cinterop
  • build.zig 中的交叉编译请使用 skills/zig/zig-cross
  • 将 Zig 嵌入 CMake 项目时,请使用 skills/build-systems/cmake

评论

支持 Markdown