Pass allocator to zig.system.resolveTargetQuery

For now the allocator is only used in `detectAndroidApiLevel`, but there
are probably other places in the detection logic that could benefit from
this too.
This commit is contained in:
Luna Schwalbe 2025-09-05 16:36:14 +02:00
parent c2cd8df622
commit b7264937d4
No known key found for this signature in database
GPG key ID: 7E20C1B6E0766C1D
19 changed files with 59 additions and 50 deletions

View file

@ -1017,7 +1017,7 @@ fn parseTarget(d: *Driver, arch_os_abi: []const u8, opt_cpu_features: ?[]const u
}
}
const zig_target = std.zig.system.resolveTargetQuery(d.comp.io, query) catch |e|
const zig_target = std.zig.system.resolveTargetQuery(d.comp.io, d.comp.gpa, query) catch |e|
return d.fatal("unable to resolve target: {s}", .{errorDescription(e)});
if (query.isNative()) {

View file

@ -83,7 +83,7 @@ test "minUnsignedBits" {
var comp = Compilation.init(std.testing.allocator, arena, std.testing.io, undefined, std.fs.cwd());
defer comp.deinit();
const target_query = try std.Target.Query.parse(.{ .arch_os_abi = "x86_64-linux-gnu" });
comp.target = .fromZigTarget(try std.zig.system.resolveTargetQuery(std.testing.io, target_query));
comp.target = .fromZigTarget(try std.zig.system.resolveTargetQuery(std.testing.io, comp.gpa, target_query));
try Test.checkIntBits(&comp, 0, 0);
try Test.checkIntBits(&comp, 1, 1);
@ -122,7 +122,7 @@ test "minSignedBits" {
var comp = Compilation.init(std.testing.allocator, arena, std.testing.io, undefined, std.fs.cwd());
defer comp.deinit();
const target_query = try std.Target.Query.parse(.{ .arch_os_abi = "x86_64-linux-gnu" });
comp.target = .fromZigTarget(try std.zig.system.resolveTargetQuery(std.testing.io, target_query));
comp.target = .fromZigTarget(try std.zig.system.resolveTargetQuery(std.testing.io, comp.gpa, target_query));
try Test.checkIntBits(&comp, -1, 1);
try Test.checkIntBits(&comp, -2, 2);

View file

@ -87,7 +87,7 @@ pub fn main() !void {
.zig_lib_directory = zig_lib_directory,
.host = .{
.query = .{},
.result = try std.zig.system.resolveTargetQuery(io, .{}),
.result = try std.zig.system.resolveTargetQuery(io, arena, .{}),
},
.time_report = false,
};

View file

@ -70,7 +70,7 @@ pub fn main() !void {
const target_query = std.zig.parseTargetQueryOrReportFatalError(gpa, .{
.arch_os_abi = target_arch_os_abi,
});
const target = std.zig.resolveTargetQueryOrFatal(io, target_query);
const target = std.zig.resolveTargetQueryOrFatal(io, gpa, target_query);
if (print_includes) {
const libc_installation: ?*LibCInstallation = libc: {

View file

@ -616,7 +616,7 @@ fn getIncludePaths(
.cpu_arch = includes_arch,
.abi = .msvc,
};
const target = std.zig.resolveTargetQueryOrFatal(io, target_query);
const target = std.zig.resolveTargetQueryOrFatal(io, arena, target_query);
const is_native_abi = target_query.isNativeAbi();
const detected_libc = std.zig.LibCDirs.detect(arena, zig_lib_dir, &target, is_native_abi, true, null) catch {
if (includes == .any) {
@ -642,7 +642,7 @@ fn getIncludePaths(
.cpu_arch = includes_arch,
.abi = .gnu,
};
const target = std.zig.resolveTargetQueryOrFatal(io, target_query);
const target = std.zig.resolveTargetQueryOrFatal(io, arena, target_query);
const is_native_abi = target_query.isNativeAbi();
const detected_libc = std.zig.LibCDirs.detect(arena, zig_lib_dir, &target, is_native_abi, true, null) catch |err| switch (err) {
error.OutOfMemory => |e| return e,

View file

@ -255,10 +255,13 @@ fn serveWasm(
const wasm_base_path = try buildWasmBinary(arena, context, optimize_mode);
const bin_name = try std.zig.binNameAlloc(arena, .{
.root_name = autodoc_root_name,
.target = &(std.zig.system.resolveTargetQuery(std.Build.parseTargetQuery(.{
.target = &(std.zig.system.resolveTargetQuery(gpa, std.Build.parseTargetQuery(.{
.arch_os_abi = autodoc_arch_os_abi,
.cpu_features = autodoc_cpu_features,
}) catch unreachable) catch unreachable),
}) catch unreachable) catch |err| switch (err) {
error.OutOfMemory => |e| return e,
else => unreachable,
}),
.output_mode = .Exe,
});
// std.http.Server does not have a sendfile API yet.

View file

@ -2664,7 +2664,7 @@ pub fn resolveTargetQuery(b: *Build, query: Target.Query) ResolvedTarget {
const io = b.graph.io;
return .{
.query = query,
.result = std.zig.system.resolveTargetQuery(io, query) catch
.result = std.zig.system.resolveTargetQuery(io, b.allocator, query) catch
@panic("unable to resolve target query"),
};
}

View file

@ -551,7 +551,7 @@ test Options {
.global_cache_root = .{ .path = "test", .handle = std.fs.cwd() },
.host = .{
.query = .{},
.result = try std.zig.system.resolveTargetQuery(io, .{}),
.result = try std.zig.system.resolveTargetQuery(io, arena.allocator(), .{}),
},
.zig_lib_directory = std.Build.Cache.Directory.cwd(),
.time_report = false,

View file

@ -673,10 +673,13 @@ fn buildClientWasm(ws: *WebServer, arena: Allocator, optimize: std.builtin.Optim
};
const bin_name = try std.zig.binNameAlloc(arena, .{
.root_name = root_name,
.target = &(std.zig.system.resolveTargetQuery(io, std.Build.parseTargetQuery(.{
.target = &(std.zig.system.resolveTargetQuery(io, ws.gpa, std.Build.parseTargetQuery(.{
.arch_os_abi = arch_os_abi,
.cpu_features = cpu_features,
}) catch unreachable) catch unreachable),
}) catch unreachable) catch |err| switch (err) {
error.OutOfMemory => |e| return e,
else => unreachable,
}),
.output_mode = .Exe,
});
return base_path.join(arena, bin_name);

View file

@ -663,7 +663,7 @@ test parse {
.arch_os_abi = "x86_64-linux-gnu",
.cpu_features = "x86_64-sse-sse2-avx-cx8",
});
const target = try std.zig.system.resolveTargetQuery(io, query);
const target = try std.zig.system.resolveTargetQuery(io, std.testing.allocator, query);
try std.testing.expect(target.os.tag == .linux);
try std.testing.expect(target.abi == .gnu);
@ -688,7 +688,7 @@ test parse {
.arch_os_abi = "arm-linux-musleabihf",
.cpu_features = "generic+v8a",
});
const target = try std.zig.system.resolveTargetQuery(io, query);
const target = try std.zig.system.resolveTargetQuery(io, std.testing.allocator, query);
try std.testing.expect(target.os.tag == .linux);
try std.testing.expect(target.abi == .musleabihf);
@ -705,7 +705,7 @@ test parse {
.arch_os_abi = "aarch64-linux.3.10...4.4.1-gnu.2.27",
.cpu_features = "generic+v8a",
});
const target = try std.zig.system.resolveTargetQuery(io, query);
const target = try std.zig.system.resolveTargetQuery(io, std.testing.allocator, query);
try std.testing.expect(target.cpu.arch == .aarch64);
try std.testing.expect(target.os.tag == .linux);
@ -728,7 +728,7 @@ test parse {
const query = try Query.parse(.{
.arch_os_abi = "aarch64-linux.3.10...4.4.1-android.30",
});
const target = try std.zig.system.resolveTargetQuery(io, query);
const target = try std.zig.system.resolveTargetQuery(io, std.testing.allocator, query);
try std.testing.expect(target.cpu.arch == .aarch64);
try std.testing.expect(target.os.tag == .linux);
@ -749,7 +749,7 @@ test parse {
const query = try Query.parse(.{
.arch_os_abi = "x86-windows.xp...win8-msvc",
});
const target = try std.zig.system.resolveTargetQuery(io, query);
const target = try std.zig.system.resolveTargetQuery(io, std.testing.allocator, query);
try std.testing.expect(target.cpu.arch == .x86);
try std.testing.expect(target.os.tag == .windows);

View file

@ -663,8 +663,8 @@ pub fn putAstErrorsIntoBundle(
try wip_errors.addZirErrorMessages(zir, tree, tree.source, path);
}
pub fn resolveTargetQueryOrFatal(io: Io, target_query: std.Target.Query) std.Target {
return std.zig.system.resolveTargetQuery(io, target_query) catch |err|
pub fn resolveTargetQueryOrFatal(io: Io, gpa: Allocator, target_query: std.Target.Query) std.Target {
return std.zig.system.resolveTargetQuery(io, gpa, target_query) catch |err|
std.process.fatal("unable to resolve target: {s}", .{@errorName(err)});
}

View file

@ -8,6 +8,7 @@ const Target = std.Target;
const native_endian = builtin.cpu.arch.endian();
const posix = std.posix;
const Io = std.Io;
const Allocator = mem.Allocator;
pub const NativePaths = @import("system/NativePaths.zig");
@ -201,6 +202,7 @@ pub fn getExternalExecutor(
}
pub const DetectError = error{
OutOfMemory,
FileSystem,
SystemResources,
SymLinkLoop,
@ -219,7 +221,7 @@ pub const DetectError = error{
/// and which are provided explicitly, this function resolves the native
/// components by detecting the native system, and then resolves
/// standard/default parts relative to that.
pub fn resolveTargetQuery(io: Io, query: Target.Query) DetectError!Target {
pub fn resolveTargetQuery(io: Io, gpa: Allocator, query: Target.Query) DetectError!Target {
// Until https://github.com/ziglang/zig/issues/4592 is implemented (support detecting the
// native CPU architecture as being different than the current target), we use this:
const query_cpu_arch = query.cpu_arch orelse builtin.cpu.arch;
@ -503,7 +505,7 @@ pub fn resolveTargetQuery(io: Io, query: Target.Query) DetectError!Target {
}
if (builtin.os.tag == .linux and result.isBionicLibC() and query.os_tag == null and query.android_api_level == null) {
result.os.version_range.linux.android = detectAndroidApiLevel(io) catch |err| return switch (err) {
result.os.version_range.linux.android = detectAndroidApiLevel(io, gpa) catch |err| return switch (err) {
error.InvalidWtf8,
error.CurrentWorkingDirectoryUnlinked,
error.InvalidBatchScriptArg,
@ -513,6 +515,7 @@ pub fn resolveTargetQuery(io: Io, query: Target.Query) DetectError!Target {
else => blk: {
std.log.err("spawning or reading from getprop failed ({s})", .{@errorName(err)});
switch (err) {
error.OutOfMemory,
error.SystemResources,
error.FileSystem,
error.ProcessFdQuotaExceeded,
@ -1158,14 +1161,10 @@ const LdInfo = struct {
abi: Target.Abi,
};
fn detectAndroidApiLevel(io: Io) !u32 {
fn detectAndroidApiLevel(io: Io, gpa: Allocator) !u32 {
comptime if (builtin.os.tag != .linux) unreachable;
// `child.spawn()` uses an arena and the exact memory requirement isn't easily determined,
// so we give it 128 * 3 bytes, which was shown in testing to be enough.
var alloc_buf: [128 * 3]u8 = undefined;
var fba = std.heap.FixedBufferAllocator.init(&alloc_buf);
var child = std.process.Child.init(&.{ "/system/bin/getprop", "ro.build.version.sdk" }, fba.allocator());
var child = std.process.Child.init(&.{ "/system/bin/getprop", "ro.build.version.sdk" }, gpa);
// pass empty EnvMap, no allocator and deinit() required
child.env_map = &std.process.EnvMap.init(undefined);
child.stdin_behavior = .Ignore;

View file

@ -5348,7 +5348,7 @@ fn workerDocsWasmFallible(comp: *Compilation, prog_node: std.Progress.Node) SubU
const optimize_mode = std.builtin.OptimizeMode.ReleaseSmall;
const output_mode = std.builtin.OutputMode.Exe;
const resolved_target: Package.Module.ResolvedTarget = .{
.result = std.zig.system.resolveTargetQuery(io, .{
.result = std.zig.system.resolveTargetQuery(io, gpa, .{
.cpu_arch = .wasm32,
.os_tag = .freestanding,
.cpu_features_add = std.Target.wasm.featureSet(&.{
@ -5361,7 +5361,10 @@ fn workerDocsWasmFallible(comp: *Compilation, prog_node: std.Progress.Node) SubU
//.simd128,
// .tail_call, not supported by Safari
}),
}) catch unreachable,
}) catch |err| switch (err) {
error.OutOfMemory => |e| return e,
else => unreachable,
},
.is_native_os = false,
.is_native_abi = false,

View file

@ -335,7 +335,7 @@ fn mainArgs(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
return cmdInit(gpa, arena, cmd_args);
} else if (mem.eql(u8, cmd, "targets")) {
dev.check(.targets_command);
const host = std.zig.resolveTargetQueryOrFatal(io, .{});
const host = std.zig.resolveTargetQueryOrFatal(io, gpa, .{});
var stdout_writer = fs.File.stdout().writer(&stdout_buffer);
try @import("print_targets.zig").cmdTargets(arena, cmd_args, &stdout_writer.interface, &host);
return stdout_writer.interface.flush();
@ -345,7 +345,7 @@ fn mainArgs(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
return;
} else if (mem.eql(u8, cmd, "env")) {
dev.check(.env_command);
const host = std.zig.resolveTargetQueryOrFatal(io, .{});
const host = std.zig.resolveTargetQueryOrFatal(io, gpa, .{});
var stdout_writer = fs.File.stdout().writer(&stdout_buffer);
try @import("print_env.zig").cmdEnv(
arena,
@ -369,7 +369,7 @@ fn mainArgs(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
} else if (mem.eql(u8, cmd, "ast-check")) {
return cmdAstCheck(arena, io, cmd_args);
} else if (mem.eql(u8, cmd, "detect-cpu")) {
return cmdDetectCpu(io, cmd_args);
return cmdDetectCpu(io, gpa, cmd_args);
} else if (build_options.enable_debug_extensions and mem.eql(u8, cmd, "changelist")) {
return cmdChangelist(arena, io, cmd_args);
} else if (build_options.enable_debug_extensions and mem.eql(u8, cmd, "dump-zir")) {
@ -3802,7 +3802,7 @@ fn createModule(
}
const target_query = std.zig.parseTargetQueryOrReportFatalError(arena, target_parse_options);
const target = std.zig.resolveTargetQueryOrFatal(io, target_query);
const target = std.zig.resolveTargetQueryOrFatal(io, gpa, target_query);
break :t .{
.result = target,
.is_native_os = target_query.isNativeOs(),
@ -4364,7 +4364,7 @@ fn runOrTest(
std.debug.lockStdErr();
const err = process.execve(gpa, argv.items, &env_map);
std.debug.unlockStdErr();
try warnAboutForeignBinaries(io, arena, arg_mode, target, link_libc);
try warnAboutForeignBinaries(io, gpa, arena, arg_mode, target, link_libc);
const cmd = try std.mem.join(arena, " ", argv.items);
fatal("the following command failed to execve with '{s}':\n{s}", .{ @errorName(err), cmd });
} else if (process.can_spawn) {
@ -4385,7 +4385,7 @@ fn runOrTest(
break :t child.spawnAndWait();
};
const term = term_result catch |err| {
try warnAboutForeignBinaries(io, arena, arg_mode, target, link_libc);
try warnAboutForeignBinaries(io, gpa, arena, arg_mode, target, link_libc);
const cmd = try std.mem.join(arena, " ", argv.items);
fatal("the following command failed with '{s}':\n{s}", .{ @errorName(err), cmd });
};
@ -5016,7 +5016,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, io: Io, args: []const []const u8)
.arch_os_abi = triple,
});
break :t .{
.result = std.zig.resolveTargetQueryOrFatal(io, target_query),
.result = std.zig.resolveTargetQueryOrFatal(io, gpa, target_query),
.is_native_os = false,
.is_native_abi = false,
.is_explicit_dynamic_linker = false,
@ -5024,7 +5024,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, io: Io, args: []const []const u8)
}
}
break :t .{
.result = std.zig.resolveTargetQueryOrFatal(io, .{}),
.result = std.zig.resolveTargetQueryOrFatal(io, gpa, .{}),
.is_native_os = true,
.is_native_abi = true,
.is_explicit_dynamic_linker = false,
@ -5445,7 +5445,7 @@ fn jitCmd(
const target_query: std.Target.Query = .{};
const resolved_target: Package.Module.ResolvedTarget = .{
.result = std.zig.resolveTargetQueryOrFatal(io, target_query),
.result = std.zig.resolveTargetQueryOrFatal(io, gpa, target_query),
.is_native_os = true,
.is_native_abi = true,
.is_explicit_dynamic_linker = false,
@ -6239,7 +6239,7 @@ fn cmdAstCheck(arena: Allocator, io: Io, args: []const []const u8) !void {
}
}
fn cmdDetectCpu(io: Io, args: []const []const u8) !void {
fn cmdDetectCpu(io: Io, gpa: Allocator, args: []const []const u8) !void {
dev.check(.detect_cpu_command);
const detect_cpu_usage =
@ -6284,7 +6284,7 @@ fn cmdDetectCpu(io: Io, args: []const []const u8) !void {
const cpu = try detectNativeCpuWithLLVM(builtin.cpu.arch, name, features);
try printCpu(cpu);
} else {
const host_target = std.zig.resolveTargetQueryOrFatal(io, .{});
const host_target = std.zig.resolveTargetQueryOrFatal(io, gpa, .{});
try printCpu(host_target.cpu);
}
}
@ -6546,13 +6546,14 @@ fn prefixedIntArg(arg: []const u8, prefix: []const u8) ?u64 {
fn warnAboutForeignBinaries(
io: Io,
gpa: Allocator,
arena: Allocator,
arg_mode: ArgMode,
target: *const std.Target,
link_libc: bool,
) !void {
const host_query: std.Target.Query = .{};
const host_target = std.zig.resolveTargetQueryOrFatal(io, host_query);
const host_target = std.zig.resolveTargetQueryOrFatal(io, gpa, host_query);
switch (std.zig.system.getExternalExecutor(&host_target, target, .{ .link_libc = link_libc })) {
.native => return,

View file

@ -133,7 +133,7 @@ fn printOutput(
var env_map = try process.getEnvMap(arena);
try env_map.put("CLICOLOR_FORCE", "1");
const host = try std.zig.system.resolveTargetQuery(io, .{});
const host = try std.zig.system.resolveTargetQuery(io, arena, .{});
const obj_ext = builtin.object_format.fileExt(builtin.cpu.arch);
const print = std.debug.print;
@ -248,7 +248,7 @@ fn printOutput(
const target_query = try std.Target.Query.parse(.{
.arch_os_abi = code.target_str orelse "native",
});
const target = try std.zig.system.resolveTargetQuery(io, target_query);
const target = try std.zig.system.resolveTargetQuery(io, arena, target_query);
const path_to_exe = try std.fmt.allocPrint(arena, "./{s}{s}", .{
code_name, target.exeFileExt(),
@ -326,7 +326,7 @@ fn printOutput(
const target_query = try std.Target.Query.parse(.{
.arch_os_abi = triple,
});
const target = try std.zig.system.resolveTargetQuery(io, target_query);
const target = try std.zig.system.resolveTargetQuery(io, arena, target_query);
switch (getExternalExecutor(&host, &target, .{
.link_libc = code.link_libc,
})) {

View file

@ -32,7 +32,7 @@ pub fn main() !void {
, .{if (args.len == 0) "dump-cov" else args[0]}),
};
const target = std.zig.resolveTargetQueryOrFatal(io, try .parse(.{
const target = std.zig.resolveTargetQueryOrFatal(io, gpa, try .parse(.{
.arch_os_abi = target_query_str,
}));

View file

@ -91,7 +91,7 @@ pub fn main() anyerror!void {
const io = threaded.io();
const sysroot_path = sysroot orelse blk: {
const target = try std.zig.system.resolveTargetQuery(io, .{});
const target = try std.zig.system.resolveTargetQuery(io, gpa, .{});
break :blk std.zig.system.darwin.getSdk(allocator, &target) orelse
fatal("no SDK found; you can provide one explicitly with '--sysroot' flag", .{});
};

View file

@ -44,7 +44,7 @@ pub fn main() !void {
const io = threaded.io();
const query = try std.Target.Query.parse(.{ .arch_os_abi = args[1] });
const target = try std.zig.system.resolveTargetQuery(io, query);
const target = try std.zig.system.resolveTargetQuery(io, gpa, query);
var buffer: [2000]u8 = undefined;
var stdout_writer = std.fs.File.stdout().writerStreaming(&buffer);

View file

@ -93,7 +93,7 @@ pub fn main() !void {
else
null;
const host = try std.zig.system.resolveTargetQuery(io, .{});
const host = try std.zig.system.resolveTargetQuery(io, arena, .{});
const debug_log_verbose = debug_zcu or debug_dwarf or debug_link;
@ -697,7 +697,7 @@ const Case = struct {
},
}) catch fatal("line {d}: invalid target query '{s}'", .{ line_n, query });
const resolved = try std.zig.system.resolveTargetQuery(io, parsed_query);
const resolved = try std.zig.system.resolveTargetQuery(io, arena, parsed_query);
try targets.append(arena, .{
.query = query,