Building a C(++) dependency with zig that requires the install directory path at configure time

Hi all!

I’m going through the process of updating my zig scripts to build a C++ project that depends on a bunch of libraries including LHAPDF, a physics library providing Parton Distribution Functions (PDFs). PDFs don’t have closed forms and are stored as numerical data that the library turns into a callable functions. The PDF data (to be installed separately from the library when needed) is stored in a hard-coded directory specified at build-time by a C macro.

In my 0.16 build script, I simply used:

const data_prefix = b.fmt("\"{s}/share\"", .{b.install_prefix});
lhapdf.addCMacro("LHAPDF_DATA_PREFIX", data_prefix);

// (...)

// Setup share directory
const temp_ddir = b.addNamedWriteFiles("share");
_ = temp_ddir.addCopyFile(upstream.path("lhapdf.conf"), "lhapdf.conf");
_ = temp_ddir.addCopyFile(upstream.path("pdfsets.index"), "pdfsets.index");

b.installArtifact(liblhapdf);
b.installDirectory(.{
    .install_dir = .{ .custom = "share" },
    .install_subdir = "LHAPDF",
    .source_dir = temp_ddir.getDirectory(),
});

However, I learned that in 0.17 the Make and Configure steps were separated and things such as b.install_prefix are no longer available. According to Claude, in 0.17, there is no longer a way to get the installation directory as a string at configure-time. However, LHAPDF_DATA_PREFIX really has to point to the correct place otherwise the library does not work!

I figured I’d ask if there is an intended path for such a use-case in 0.17 before I try to come up with an ugly work-around :stuck_out_tongue:

See https://codeberg.org/ziglang/zig/issues/37124

Ah great that does indeed cover my use case :slight_smile: I’ll wait until the next 0.17.1 before upgrading.