Running a configure script as a build step (trying to build HDF5)

Hi there,
I’m trying to use zig’s build system to build the HDF5 library (C lib + C++ bindings). HDF5 has a very complicated build system, so I would like to re-use some parts of it for the time being. In particular, I’d like to be able to run the configure bash script that comes with the script before building the library.

Currently, I have a config step set-up:

const config_step = b.step("configure", "configure hdf5 for building on this machine");
const config_path = try root_dir.realpathAlloc(alloc, "configure");
const run_config = b.addSystemCommand(&.{ config_path });
run_config.addArgs(&.{
    "--enable-cxx",
    b.fmt("--srcdir={s}", .{ root_path })
});
run_config.has_side_effects = true;

// Capture the output to create a dependency for the main code
const output = run_config.captureStdOut();
config_step.dependOn(&b.addInstallFile(output, "hdf5-config.log").step);
b.default_step.dependOn(config_step);

To make sure that the compile steps don’t run before the config step is finished, I figured I’d capture the output of the script and add it as a dependency for the other steps:

hdf5.root_module.addAnonymousImport("sometext", .{.root_source_file = output });
hdf5.step.dependOn(config_step);

However, this does not work :c.

The script seems to run first, followed by a compilation attempt (I get a blank line while the script is running, followed by the usual compiler output when it starts compiling the C files). However, the compiler cannot find the headers generated by the script after it is finished.

Running zig build a second time works just fine (after the headers were generated by the first run).

Any help is much appreciated!

Hi @smups welcome to ziggit :slight_smile:

You don’t need the output, the module and the final dependencies.

const config_step = b.step("configure", "configure hdf5 for building on this machine");
const config_path = try root_dir.realpathAlloc(alloc, "configure");
const run_config = b.addSystemCommand(&.{ config_path });
run_config.addArgs(&.{
    "--enable-cxx",
    b.fmt("--srcdir={s}", .{ root_path })
});

config_step.dependOn(&run_config.step);

The last statement says that config_step depends on the run_config.
So when you say zig build configure runs the configure script.

Additionally you need to say that your const exe = b.addExecutable( exe depends on config_step.

exe.step.dependOn(config_step);

b.addConfigHeader can sometimes replace configure scripts
example for zstd:

    const config_header = b.addConfigHeader(
        .{
            .style = .{ .autoconf = b.path("config.h.in") },
        },
        .{
            .ZSTD_MULTITHREAD_SUPPORT_DEFAULT = null,
            .ZSTD_LEGACY_SUPPORT = null,
        },
    );
    lib.addConfigHeader(config_header);
3 Likes

Thanks for the help, that worked!

1 Like