Skip to content

Instantly share code, notes, and snippets.

@marler8997
Last active February 25, 2021 19:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marler8997/1428636ba03dab794002f4b530a4f78b to your computer and use it in GitHub Desktop.
Save marler8997/1428636ba03dab794002f4b530a4f78b to your computer and use it in GitHub Desktop.
const buildpkgs = @import("buildpkgs");
pub fn build(b: *Builder) !void {
if (b.option(bool, "android", "build an android APK")) {
// note: this hasImport function is comptime
if (buildpkgs.hasPackage("androidbuild")) {
const androidbuild = @import("androidbuild");
androidbuild.createManifest(.{ ... });
androidbuild.setTarget(...);
...
} else {
b.needPackage("androidbuild");
}
}
}
  • Run zig build -Dandroid
  • Zig generates the "buildpkgs" module with a list of the packages that are available

buildpkgs.zig

pub fn hasPackage(comptime name: []const u8) bool {
    return false;
}
  • build.zig gets compiled
  • the resulting build executable is executed
  • build_runner.zig detects that there is a "needPackage" configured so it prints this error:
$ zig build -Dandroid
error: build.zig is missing needed package "androidbuild"
  • Run the package manager with the "android" build option enabled
  • Zig generates the "buildpkgs" module with a list of the packages that are available

buildpkgs.zig

pub fn hasPackage(comptime name: []const u8) bool {
    return false;
}
  • build.zig gets compiled
  • the resulting build executable gets executed with the option "--get-needed-packages"
  • the build.zig executable sees that "android" is enabled, but hasPackage("androidbuild") returns false, so it lists "androidbuild" as a needed package
  • the package manager now downloads the android_sdk repository to retrieve the androidbuild package for build.zig
  • build.zig gets compiled a second time, this time with the following buildpkgs module:

buildpkgs.zig

pub fn hasPackage(comptime name: []const u8) bool {
    if (std.mem.eql(u8, name, "androidbuild") return true;
    return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment