Skip to content

Allow explicitly requesting all targets#82

Closed
ypsvlq wants to merge 1 commit intosilbinarywolf:mainfrom
ypsvlq:targets
Closed

Allow explicitly requesting all targets#82
ypsvlq wants to merge 1 commit intosilbinarywolf:mainfrom
ypsvlq:targets

Conversation

@ypsvlq
Copy link
Copy Markdown
Contributor

@ypsvlq ypsvlq commented Apr 10, 2026

My project uses zig-android-sdk as a lazy dependency, so I want to write something like:

const android_option = b.option(bool, "android", "Build for all Android targets") orelse false;

if (android_option or target.result.abi.isAndroid()) {
    if (b.lazyImport("android")) |android| {
        for (android.standardTargets(b, target)) |android_target| {
            // ...
        }
    }
}

but currently this doesn't work as a build option with a given name can only be declared once.

@silbinarywolf
Copy link
Copy Markdown
Owner

Thanks for the PR. Instead of breaking the existing simple function, I've opted to take the approach outlined in this PR here: #83

I wasn't aware of b.lazyImport and I'll need to look at maybe updating my projects to utilize it. (Then possibly the examples in this repo!)

silbinarywolf added a commit that referenced this pull request Apr 11, 2026
Based on request here:
#82

Example usage:
```zig
// Example usage in Zig 0.16.0-dev.3061+9b1eaad13
pub fn build(b: *std.Build) void {
    const exe_name: []const u8 = "minimal";
    const root_target = b.standardTargetOptions(.{});
    const optimize = b.standardOptimizeOption(.{});

    const all_android_targets = b.option(bool, "android", "Build for all Android targets (x86, x86_64, aarch64, arm, etc)") orelse false;
    const android_targets: []std.Build.ResolvedTarget = blk: {
        if (all_android_targets or root_target.result.abi.isAndroid()) {
            if (b.lazyImport(@this(), "android")) |android| {
                break :blk android.resolveTargets(b, .{
                    .default_target = root_target,
                    .all_targets = all_android_targets,
                });
            }
        }
        break :blk &[0]std.Build.ResolvedTarget{};
    };
```

I opted to look at how Zig separates build option creation for targets
and took a similar route, for example Zig has `resolveTarget` for
handling the logic without invoking `b.option`
```zig
/// Exposes standard `zig build` options for choosing a target and additionally
/// resolves the target query.
pub fn standardTargetOptions(b: *Build, args: StandardTargetOptionsArgs) ResolvedTarget {
    const query = b.standardTargetOptionsQueryOnly(args);
    return b.resolveTargetQuery(query);
}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants