What happened to addModule?

Up til now I’ve been working only in Zig 0.11. Today I decided to install master (using zvm) so that I can get a sense of what changes the upcoming release of Zig will entail. Immediately I hit the following error:

/tmp/e1bde732f5b919a56f97e346aa6d5683/linux/x64/build.zig:15:8: error: no field or member function named 'addModule' in 'Build.Step.Compile'

A scan of the documentation didn’t readily reveal what the replacement function is. I’m sure one of you must know what the new way of doing thing is. Here’s the build file:

const std = @import("std");
const cfg = @import("./build-cfg.zig");

pub fn build(b: *std.Build) void {
    const target = b.standardTargetOptions(.{
        .default_target = cfg.target,
    });
    const optimize = b.standardOptimizeOption(.{});
    const lib = b.addSharedLibrary(.{
        .name = cfg.package_name,
        .root_source_file = .{ .path = cfg.stub_path },
        .target = target,
        .optimize = optimize,
    });
    lib.addModule("exporter", b.createModule(.{
        .source_file = .{ .path = cfg.exporter_path },
    }));
    lib.addModule("package", b.createModule(.{
        .source_file = .{ .path = cfg.package_path },
    }));
    if (cfg.use_libc) {
        lib.linkLibC();
    }
    if (cfg.target.cpu_arch == .wasm32) {
        lib.use_lld = false;
        lib.rdynamic = true;
    }
    b.installArtifact(lib);
}

Thanks in advance.

3 Likes

Yeah, so the new way is kinda ugly, you need to substitute .addModule with root_module.addImport. I’m sure it’ll change several more times as the team iterates on the package manager.

7 Likes

Thanks a lot for the pointer! So the build file ended up looking like this:

const std = @import("std");
const cfg = @import("./build-cfg.zig");

pub fn build(b: *std.Build) void {
    const target = b.standardTargetOptions(.{
        .default_target = cfg.target,
    });
    const optimize = b.standardOptimizeOption(.{});
    const lib = b.addSharedLibrary(.{
        .name = cfg.package_name,
        .root_source_file = .{ .path = cfg.stub_path },
        .target = target,
        .optimize = optimize,
    });
    lib.root_module.addImport("exporter", b.createModule(.{
        .root_source_file = .{ .path = cfg.exporter_path },
    }));
    lib.root_module.addImport("package", b.createModule(.{
        .root_source_file = .{ .path = cfg.package_path },
    }));
    if (cfg.use_libc) {
        lib.linkLibC();
    }
    if (cfg.target.cpu_arch == .wasm32) {
        lib.use_lld = false;
        lib.rdynamic = true;
    }
    b.installArtifact(lib);
}

The only incompatibility I’m seeing so far is std.builtin.Endian.Little being all lowercase now.

5 Likes