Packages

Zig-first BEAM NIF toolkit (Mix compiler, generator, and cement Zig runtime)

Current section

Files

Jump to
zig_barnacle build.zig
Raw

build.zig

const std = @import("std");

pub const OtpVersion = enum {
    otp_27,
    otp_28,
    otp_29,

    pub fn fromNumber(n: u32) OtpVersion {
        return switch (n) {
            27 => .otp_27,
            28 => .otp_28,
            29 => .otp_29,
            else => @panic("Unsupported OTP version; currently supported: 27, 28, 29"),
        };
    }

    pub fn erlSourcePath(self: OtpVersion) []const u8 {
        return switch (self) {
            .otp_27 => "src/erl/otp_27.zig",
            .otp_28 => "src/erl/otp_28.zig",
            .otp_29 => "src/erl/otp_29.zig",
        };
    }
};

pub fn build(b: *std.Build) void {
    const target = b.standardTargetOptions(.{});
    const optimize = b.standardOptimizeOption(.{});
    const otp_num = b.option(u32, "otp", "Target Erlang/OTP major version (default: 28)") orelse 28;
    const otp_ver = OtpVersion.fromNumber(otp_num);

    const c_builtins_mod = b.createModule(.{
        .root_source_file = b.path("src/erl/c_builtins.zig"),
        .target = target,
        .optimize = optimize,
    });

    const helpers_mod = b.createModule(.{
        .root_source_file = b.path("src/erl/helpers.zig"),
        .target = target,
        .optimize = optimize,
    });

    const erl_mod = b.createModule(.{
        .root_source_file = b.path(otp_ver.erlSourcePath()),
        .target = target,
        .optimize = optimize,
        .imports = &.{
            .{ .name = "c_builtins", .module = c_builtins_mod },
            .{ .name = "helpers", .module = helpers_mod },
        },
    });

    const erl_nif_mod = b.addModule("erl_nif", .{
        .root_source_file = b.path("src/erl_nif.zig"),
        .target = target,
        .optimize = optimize,
        .imports = &.{
            .{ .name = "erl", .module = erl_mod },
        },
    });

    const reflect_mod = b.createModule(.{
        .root_source_file = b.path("src/reflect.zig"),
        .target = target,
        .optimize = optimize,
    });

    const cement_mod = b.addModule("cement", .{
        .root_source_file = b.path("src/beam.zig"),
        .target = target,
        .optimize = optimize,
        .imports = &.{
            .{ .name = "erl_nif", .module = erl_nif_mod },
            .{ .name = "reflect", .module = reflect_mod },
        },
    });

    const test_mod = b.createModule(.{
        .root_source_file = b.path("src/beam.zig"),
        .target = target,
        .optimize = optimize,
        .imports = &.{
            .{ .name = "erl_nif", .module = erl_nif_mod },
            .{ .name = "reflect", .module = reflect_mod },
            .{ .name = "cement", .module = cement_mod },
        },
    });

    const showcase_core_mod = b.createModule(.{
        .root_source_file = b.path("examples/showcase_core.zig"),
        .target = target,
        .optimize = optimize,
    });

    const showcase_nif_mod = b.createModule(.{
        .root_source_file = b.path("examples/showcase_nif.zig"),
        .target = target,
        .optimize = optimize,
        .imports = &.{
            .{ .name = "cement", .module = cement_mod },
            .{ .name = "erl_nif", .module = erl_nif_mod },
            .{ .name = "showcase", .module = showcase_core_mod },
        },
    });

    const showcase_lib = b.addLibrary(.{
        .name = "showcase_nif",
        .linkage = .dynamic,
        .root_module = showcase_nif_mod,
    });
    // BEAM provides enif_* symbols dynamically at load time.
    showcase_lib.linker_allow_shlib_undefined = true;

    const install_showcase = b.addInstallArtifact(showcase_lib, .{});
    const examples_step = b.step("examples", "Build example NIF libraries");
    examples_step.dependOn(&install_showcase.step);

    const cement_tests = b.addTest(.{ .root_module = test_mod });
    const showcase_tests = b.addTest(.{ .root_module = showcase_core_mod });

    const test_step = b.step("test", "Run cement and example Zig unit tests");
    test_step.dependOn(&b.addRunArtifact(cement_tests).step);
    test_step.dependOn(&b.addRunArtifact(showcase_tests).step);
}