zig icon indicating copy to clipboard operation
zig copied to clipboard

std.Build: add `imports` field to `*Options` structs

Open FnControlOption opened this issue 1 year ago • 0 comments

Given the following build.zig:

const std = @import("std");

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

    const datetime_dep = b.dependency("zig_datetime", .{
        .target = target,
        .optimize = optimize,
    });

    // ...

    const imports = &.{
        .{ .name = "zig-datetime", .module = datetime_dep.module("zig-datetime") },
        // ...
    };

    _ = b.addModule("subunit", .{
        .root_source_file = b.path("subunit.zig"),
        .target = target,
        .optimize = optimize,
        .imports = imports,
    });

    // ...

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

we can replace this:

    const compile_tests = b.addTest(.{
        .root_source_file = b.path("subunit.zig"),
        .target = target,
        .optimize = optimize,
    });

    inline for (imports) |import|
        compile_tests.root_module.addImport(import.name, import.module);

with this:

    const compile_tests = b.addTest(.{
        .root_source_file = b.path("subunit.zig"),
        .target = target,
        .optimize = optimize,
        .imports = imports,
    });

FnControlOption avatar Jul 21 '24 23:07 FnControlOption