Skip to content

Quick Start

buildx.zig wraps std.Build with a single project() function. One call sets up compilation, installation, testing, and running.

IMPORTANT

buildx.zig is not a replacement for std.Build. It is an enhancement that simplifies your build.zig with a high-level API while giving you full access to std.Build when you need explicit customization.

Installation

Add buildx.zig as a dependency in your build.zig.zon:

zig
.{
    .name = .myapp,
    .version = "0.1.0",
    .fingerprint = 0x...,
    .dependencies = .{
        .buildx = .{
            .path = "/path/to/buildx.zig",
        },
    },
    .paths = .{
        "build.zig",
        "build.zig.zon",
        "src",
    },
}

Minimal build.zig

zig
const std = @import("std");
const buildx = @import("buildx");

pub fn build(b: *std.Build) void {
    _ = buildx.project(b, .{
        .name = "hello",
        .root = "src/main.zig",
        .install = true,
    });
}

This gives you:

  • zig build - compile the executable
  • zig build install - install to zig-out/bin/

TIP

The project() function returns *std.Build.Step.Compile. You can use it for further customization with std.Build APIs.

Build Options

FieldTypeDefaultDescription
name[]const u8requiredOutput artifact name
root[]const u8requiredRoot source file path
kindKind.executable.executable or .library
installboolfalseAdd install step
runboolfalseAdd run step
testsboolfalseAdd test step
target?ResolvedTargetnullOverride target (defaults to -Dtarget)
optimize?OptimizeModenullOverride optimize (defaults to -Doptimize)
linkLinkConfig.{}Linking configuration
version?SemanticVersionnullSemantic version for the artifact
linkage?LinkMode.staticLibrary only: .static or .dynamic

Library with Tests

zig
const std = @import("std");
const buildx = @import("buildx");

pub fn build(b: *std.Build) void {
    _ = buildx.project(b, .{
        .name = "math",
        .root = "src/root.zig",
        .kind = .library,
        .tests = true,
    });
}

This gives you zig build test.

Run Step

zig
_ = buildx.project(b, .{
    .name = "app",
    .root = "src/main.zig",
    .install = true,
    .run = true,
});

Run with zig build run. Pass arguments: zig build run -- --flag value.

Cross Compilation

zig
_ = buildx.project(b, .{
    .name = "app",
    .root = "src/main.zig",
    .cross = .{
        .targets = buildx.targets.desktop(),
    },
});

Run with zig build cross. See Cross Compilation for all target presets.

System Libraries and Linking

zig
_ = buildx.project(b, .{
    .name = "myapp",
    .root = "src/main.zig",
    .link = .{
        .include_paths = &.{"vendor/include"},
        .lib_paths = &.{"vendor/lib"},
        .system_libs = &.{
            .{ .name = "ssl", .needs_libc = true },
        },
        .frameworks = &.{"CoreFoundation"},
        .link_libc = true,
    },
    .install = true,
});

NOTE

Frameworks are macOS system libraries like CoreFoundation, Security, IOKit. They are linked via -framework flag. Use .frameworks only when targeting macOS.

Next Steps

Released under the MIT License.