std.Build: implement --host-target, --host-cpu, --host-dynamic-linker
This also makes a long-overdue change of extracting common state from Build into a shared Graph object. Getting the semantics right for these flags turned out to be quite tricky. In the end it works like this: * The override only happens when the target is fully native, with no additional query parameters, such as versions or CPU features added. * The override affects the resolved Target but leaves the original Query unmodified. * The "is native?" detection logic operates on the original, unmodified query. This makes it possible to provide invalid host target information, causing confusing errors to occur. Don't do that. There are some minor breaking changes to std.Build API such as the fact that `b.zig_exe` is now moved to `b.graph.zig_exe`, as well as a handful of other similar flags.
This commit is contained in:
@@ -314,7 +314,7 @@ pub fn evalZigProcess(
|
||||
try handleVerbose(s.owner, null, argv);
|
||||
|
||||
var child = std.ChildProcess.init(argv, arena);
|
||||
child.env_map = b.env_map;
|
||||
child.env_map = &b.graph.env_map;
|
||||
child.stdin_behavior = .Pipe;
|
||||
child.stdout_behavior = .Pipe;
|
||||
child.stderr_behavior = .Pipe;
|
||||
|
||||
@@ -923,7 +923,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
|
||||
var zig_args = ArrayList([]const u8).init(arena);
|
||||
defer zig_args.deinit();
|
||||
|
||||
try zig_args.append(b.zig_exe);
|
||||
try zig_args.append(b.graph.zig_exe);
|
||||
|
||||
const cmd = switch (self.kind) {
|
||||
.lib => "build-lib",
|
||||
@@ -933,6 +933,16 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
|
||||
};
|
||||
try zig_args.append(cmd);
|
||||
|
||||
if (!mem.eql(u8, b.graph.host_query_options.arch_os_abi, "native")) {
|
||||
try zig_args.appendSlice(&.{ "--host-target", b.graph.host_query_options.arch_os_abi });
|
||||
}
|
||||
if (b.graph.host_query_options.cpu_features) |cpu| {
|
||||
try zig_args.appendSlice(&.{ "--host-cpu", cpu });
|
||||
}
|
||||
if (b.graph.host_query_options.dynamic_linker) |dl| {
|
||||
try zig_args.appendSlice(&.{ "--host-dynamic-linker", dl });
|
||||
}
|
||||
|
||||
if (b.reference_trace) |some| {
|
||||
try zig_args.append(try std.fmt.allocPrint(arena, "-freference-trace={d}", .{some}));
|
||||
}
|
||||
@@ -1393,7 +1403,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
|
||||
try zig_args.append(b.cache_root.path orelse ".");
|
||||
|
||||
try zig_args.append("--global-cache-dir");
|
||||
try zig_args.append(b.global_cache_root.path orelse ".");
|
||||
try zig_args.append(b.graph.global_cache_root.path orelse ".");
|
||||
|
||||
try zig_args.append("--name");
|
||||
try zig_args.append(self.name);
|
||||
|
||||
@@ -171,7 +171,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
|
||||
const gpa = b.allocator;
|
||||
const arena = b.allocator;
|
||||
|
||||
var man = b.cache.obtain();
|
||||
var man = b.graph.cache.obtain();
|
||||
defer man.deinit();
|
||||
|
||||
// Random bytes to make ConfigHeader unique. Refresh this with new
|
||||
|
||||
@@ -52,7 +52,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
|
||||
var argv: std.ArrayListUnmanaged([]const u8) = .{};
|
||||
try argv.ensureUnusedCapacity(arena, 2 + 1 + self.paths.len + 2 * self.exclude_paths.len);
|
||||
|
||||
argv.appendAssumeCapacity(b.zig_exe);
|
||||
argv.appendAssumeCapacity(b.graph.zig_exe);
|
||||
argv.appendAssumeCapacity("fmt");
|
||||
|
||||
if (self.check) {
|
||||
|
||||
@@ -94,7 +94,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
|
||||
const b = step.owner;
|
||||
const self = @fieldParentPtr(ObjCopy, "step", step);
|
||||
|
||||
var man = b.cache.obtain();
|
||||
var man = b.graph.cache.obtain();
|
||||
defer man.deinit();
|
||||
|
||||
// Random bytes to make ObjCopy unique. Refresh this with new random
|
||||
@@ -133,7 +133,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
|
||||
};
|
||||
|
||||
var argv = std.ArrayList([]const u8).init(b.allocator);
|
||||
try argv.appendSlice(&.{ b.zig_exe, "objcopy" });
|
||||
try argv.appendSlice(&.{ b.graph.zig_exe, "objcopy" });
|
||||
|
||||
if (self.only_section) |only_section| {
|
||||
try argv.appendSlice(&.{ "-j", only_section });
|
||||
|
||||
@@ -222,7 +222,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
|
||||
const basename = "options.zig";
|
||||
|
||||
// Hash contents to file name.
|
||||
var hash = b.cache.hash;
|
||||
var hash = b.graph.cache.hash;
|
||||
// Random bytes to make unique. Refresh this with new random bytes when
|
||||
// implementation is modified in a non-backwards-compatible way.
|
||||
hash.add(@as(u32, 0xad95e922));
|
||||
@@ -301,27 +301,28 @@ test Options {
|
||||
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer arena.deinit();
|
||||
|
||||
const host: std.Build.ResolvedTarget = .{
|
||||
.query = .{},
|
||||
.result = try std.zig.system.resolveTargetQuery(.{}),
|
||||
};
|
||||
|
||||
var cache: std.Build.Cache = .{
|
||||
.gpa = arena.allocator(),
|
||||
.manifest_dir = std.fs.cwd(),
|
||||
var graph: std.Build.Graph = .{
|
||||
.arena = arena.allocator(),
|
||||
.cache = .{
|
||||
.gpa = arena.allocator(),
|
||||
.manifest_dir = std.fs.cwd(),
|
||||
},
|
||||
.zig_exe = "test",
|
||||
.env_map = std.process.EnvMap.init(arena.allocator()),
|
||||
.global_cache_root = .{ .path = "test", .handle = std.fs.cwd() },
|
||||
};
|
||||
|
||||
var builder = try std.Build.create(
|
||||
arena.allocator(),
|
||||
"test",
|
||||
&graph,
|
||||
.{ .path = "test", .handle = std.fs.cwd() },
|
||||
.{ .path = "test", .handle = std.fs.cwd() },
|
||||
.{ .path = "test", .handle = std.fs.cwd() },
|
||||
host,
|
||||
&cache,
|
||||
&.{},
|
||||
);
|
||||
defer builder.destroy();
|
||||
|
||||
builder.host = .{
|
||||
.query = .{},
|
||||
.result = try std.zig.system.resolveTargetQuery(.{}),
|
||||
};
|
||||
|
||||
const options = builder.addOptions();
|
||||
|
||||
|
||||
@@ -463,7 +463,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
|
||||
var argv_list = ArrayList([]const u8).init(arena);
|
||||
var output_placeholders = ArrayList(IndexedOutput).init(arena);
|
||||
|
||||
var man = b.cache.obtain();
|
||||
var man = b.graph.cache.obtain();
|
||||
defer man.deinit();
|
||||
|
||||
for (self.argv.items) |arg| {
|
||||
@@ -747,7 +747,7 @@ fn runCommand(
|
||||
exe.is_linking_libc;
|
||||
const other_target = exe.root_module.resolved_target.?.result;
|
||||
switch (std.zig.system.getExternalExecutor(b.host.result, &other_target, .{
|
||||
.qemu_fixes_dl = need_cross_glibc and b.glibc_runtimes_dir != null,
|
||||
.qemu_fixes_dl = need_cross_glibc and b.graph.glibc_runtimes_dir != null,
|
||||
.link_libc = exe.is_linking_libc,
|
||||
})) {
|
||||
.native, .rosetta => {
|
||||
@@ -755,7 +755,7 @@ fn runCommand(
|
||||
break :interpret;
|
||||
},
|
||||
.wine => |bin_name| {
|
||||
if (b.enable_wine) {
|
||||
if (b.graph.enable_wine) {
|
||||
try interp_argv.append(bin_name);
|
||||
try interp_argv.appendSlice(argv);
|
||||
} else {
|
||||
@@ -763,9 +763,9 @@ fn runCommand(
|
||||
}
|
||||
},
|
||||
.qemu => |bin_name| {
|
||||
if (b.enable_qemu) {
|
||||
if (b.graph.enable_qemu) {
|
||||
const glibc_dir_arg = if (need_cross_glibc)
|
||||
b.glibc_runtimes_dir orelse
|
||||
b.graph.glibc_runtimes_dir orelse
|
||||
return failForeign(self, "--glibc-runtimes", argv[0], exe)
|
||||
else
|
||||
null;
|
||||
@@ -798,7 +798,7 @@ fn runCommand(
|
||||
}
|
||||
},
|
||||
.darling => |bin_name| {
|
||||
if (b.enable_darling) {
|
||||
if (b.graph.enable_darling) {
|
||||
try interp_argv.append(bin_name);
|
||||
try interp_argv.appendSlice(argv);
|
||||
} else {
|
||||
@@ -806,7 +806,7 @@ fn runCommand(
|
||||
}
|
||||
},
|
||||
.wasmtime => |bin_name| {
|
||||
if (b.enable_wasmtime) {
|
||||
if (b.graph.enable_wasmtime) {
|
||||
try interp_argv.append(bin_name);
|
||||
try interp_argv.append("--dir=.");
|
||||
try interp_argv.append(argv[0]);
|
||||
@@ -1036,7 +1036,7 @@ fn spawnChildAndCollect(
|
||||
child.cwd = b.build_root.path;
|
||||
child.cwd_dir = b.build_root.handle;
|
||||
}
|
||||
child.env_map = self.env_map orelse b.env_map;
|
||||
child.env_map = self.env_map orelse &b.graph.env_map;
|
||||
child.request_resource_usage_statistics = true;
|
||||
|
||||
child.stdin_behavior = switch (self.stdio) {
|
||||
|
||||
@@ -121,7 +121,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
|
||||
const self = @fieldParentPtr(TranslateC, "step", step);
|
||||
|
||||
var argv_list = std.ArrayList([]const u8).init(b.allocator);
|
||||
try argv_list.append(b.zig_exe);
|
||||
try argv_list.append(b.graph.zig_exe);
|
||||
try argv_list.append("translate-c");
|
||||
if (self.link_libc) {
|
||||
try argv_list.append("-lc");
|
||||
|
||||
@@ -190,7 +190,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
|
||||
// If, for example, a hard-coded path was used as the location to put WriteFile
|
||||
// files, then two WriteFiles executing in parallel might clobber each other.
|
||||
|
||||
var man = b.cache.obtain();
|
||||
var man = b.graph.cache.obtain();
|
||||
defer man.deinit();
|
||||
|
||||
// Random bytes to make WriteFile unique. Refresh this with
|
||||
|
||||
Reference in New Issue
Block a user