#avoid duplication in build.zig

1 messages · Page 1 of 1 (latest)

sinful moon
#

hello hello I was wondering how to create a test step without duplicating all the includes etc of the actual exe/lib

const std = @import("std");

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

    const optimize = b.standardOptimizeOption(.{});

    const lib = b.addStaticLibrary(.{
        .name = "test_init",
        .root_source_file = b.path("src/gc.zig"),
        .target = target,
        .optimize = optimize,
    });

    const c_source_files = [_][]const u8{
        ...
    };

    //------------------------------------------- this
    lib.linkLibC();
    if (target.result.isDarwin()) lib.linkFramework("Foundation");

    lib.addIncludePath(b.path("bdwgc-master/include"));

    inline for (c_source_files) |src| {
        lib.addCSourceFile(.{
            .file = b.path("bdwgc-master/" ++ src),
        });
    }
    //------------------------------------------------

    b.installArtifact(lib);
    const lib_unit_tests = b.addTest(.{
        .root_source_file = b.path("src/gc.zig"),
        .target = target,
        .optimize = optimize,
    });
    //-------------------------------------------- and this 
    lib_unit_tests.linkLibC();
    if (target.result.isDarwin()) lib_unit_tests.linkFramework("Foundation");

    lib_unit_tests.addIncludePath(b.path("bdwgc-master/include"));

    inline for (c_source_files) |src| {
        lib_unit_tests.addCSourceFile(.{
            .file = b.path("bdwgc-master/" ++ src),
        });
    }
    //------------------------------------------------
    const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);

    const test_step = b.step("test", "Run unit tests");
    test_step.dependOn(&run_lib_unit_tests.step);
}

thanks

smoky wyvern
#

you'd have to put that logic into a helper method