Skip to content

Instantly share code, notes, and snippets.

@marler8997
Last active February 25, 2021 08:48
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/db2a97faad5c4d58b68689c5efc95787 to your computer and use it in GitHub Desktop.
Save marler8997/db2a97faad5c4d58b68689c5efc95787 to your computer and use it in GitHub Desktop.
Dyanamic Dependencies for build.zig
pub fn build(b: *Builder) !void {
if (b.option(bool, "android", "build an android APK")) {
if (@tryImport("androidbuild")) |androidbuild| {
androidbuild.createManifest(.{ ... });
androidbuild.setTarget(...);
...
} else {
b.needPackage("androidbuild");
}
}
}
{
"git_repositories": {
"android_sdk": {
"url": "https://github.com/zig-community/android-zig-sdk",
"branch": "1.0",
"sha": "56c433bda1ccf726efcb9923d5537dc41fc738bc"
}
}
"build_zig_dependnecies": [
{
"name": "androidbuild",
"COMMENT": "NOTE: setting optional to true means this dependency will not be downloaded unless build.zig needs it",
"optional": true,
"source": {
"kind": "git_repository",
"name": "android_sdk",
"file": "androidbuild.zig"
}
}
]
}
  • Run zig build -Dandroid
  • 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
  • The package manager will look at zig-packages.json. It will see that build.zig has a dependency on the "androidbuild" package. However, it's marked as "optional" so it isn't downloaed yet.
  • 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 importing "androidbuild" fails, 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 "androidbuild" package
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment