Downloading/install binary assets in build.zig

i have a small number of binary assets (pre-built executables) which i’d like to download/install as a step within my build.zig… i control the assets themselves and could package them in any format… also note that these assets are specific to the host OS on which zig build is being run…

should i bundle these assets into a package, which i’d call out through build.zig.zon??? or should i do somethiing a little more adhoc inside the build.zig file itself???

a minimal example that codifies best practice would probably answer these and other questions…

You probably want to use lazy dependencies and b.lazyDependency:

Example from mach:

For installing prebuilt executables from the lazy dependency, you could use one of b.installBinFile/b.installBinDirectory/b.addInstallFileWithDir/b.addInstallDirectory, and use dep.path("foo") to get the src path (where dep is the return from b.lazyDependency).

Or, if you want to just package them all together and then choose which to install at build-time, then you don’t need .lazy = true/b.lazyDependency (instead just use b.dependency), but the rest is the same.

some simple follow-on questions, as i’m trying to increase my understanding:

are the URLs found in the build.zig.zon assumed to be .tar.gz files??? i can certainly create a single archive that includes all of my individual binary assets…

once this dependency is “fetched”, is it automatically deflated into a directory structure that lives in the cache???

do i then get some actual path into the cache and copy files manually, or do i use one of the b.install* methods???

See here for the filetypes currently supported.

Yes, it lives in p/<hash>/ in the global cache.

You use dep.path("relative/path") where dep is the return of either b.dependency or b.lazyDependency as mentioned in my previous post. The path is resolved relative to the root folder of the uncompressed package.

If you don’t want to “install” files, then you can use b.addWriteFiles() to copy files.

2 Likes

i tried something like this:

const dep = b.dependency("mytool", .{});   // URL in build.zig.zon for mytool.zip
var wf = b.addWriteFiles();
wf.addCopyDirectory(dep.path("mytool"), ".tools", .{});

i’m trying to populate a .tools folder in my build site which contains the (unzipped) contents of mytool.zip – which contains a folder named mytool which itself contains other artifacts… said another way, i want to unzip mytool.zip into my .tools folder…

when i run zig build, however, i receive an error on my addCopyDirectory call stating that value of type 'Build.LazyPath' ignored

help!!!

i was able to get the following to work, for anyone looking for a small gist:

const dep = b.dependency("mytool", .{});
const install_step = b.addInstallDirectory(.{
    .source_dir = dep.path("."),
     .install_dir = std.Build.InstallDir{ .custom = "tools" },
     .install_subdir = "mytool",
});
exe.step.dependOn(&install_step.step);
1 Like