zig build: add a --debug-target CLI flag

it's not advertised in the usage and only available in debug builds of
the compiler. Makes it easier to test changes to the build runner that
might affect targets differently.
This commit is contained in:
Andrew Kelley 2024-07-12 14:19:17 -07:00
parent 4f9a8b6843
commit f77b43dad3

View File

@ -4691,6 +4691,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
var verbose_llvm_cpu_features = false; var verbose_llvm_cpu_features = false;
var fetch_only = false; var fetch_only = false;
var system_pkg_dir_path: ?[]const u8 = null; var system_pkg_dir_path: ?[]const u8 = null;
var debug_target: ?[]const u8 = null;
const argv_index_exe = child_argv.items.len; const argv_index_exe = child_argv.items.len;
_ = try child_argv.addOne(); _ = try child_argv.addOne();
@ -4799,6 +4800,14 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
} else { } else {
warn("Zig was compiled without debug extensions. --debug-compile-errors has no effect.", .{}); warn("Zig was compiled without debug extensions. --debug-compile-errors has no effect.", .{});
} }
} else if (mem.eql(u8, arg, "--debug-target")) {
if (i + 1 >= args.len) fatal("expected argument after '{s}'", .{arg});
i += 1;
if (build_options.enable_debug_extensions) {
debug_target = args[i];
} else {
warn("Zig was compiled without debug extensions. --debug-target has no effect.", .{});
}
} else if (mem.eql(u8, arg, "--verbose-link")) { } else if (mem.eql(u8, arg, "--verbose-link")) {
verbose_link = true; verbose_link = true;
} else if (mem.eql(u8, arg, "--verbose-cc")) { } else if (mem.eql(u8, arg, "--verbose-cc")) {
@ -4857,12 +4866,28 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
}); });
defer root_prog_node.end(); defer root_prog_node.end();
const target_query: std.Target.Query = .{}; // Normally the build runner is compiled for the host target but here is
const resolved_target: Package.Module.ResolvedTarget = .{ // some code to help when debugging edits to the build runner so that you
// can make sure it compiles successfully on other targets.
const resolved_target: Package.Module.ResolvedTarget = t: {
if (build_options.enable_debug_extensions) {
if (debug_target) |triple| {
const target_query = try std.Target.Query.parse(.{
.arch_os_abi = triple,
});
break :t .{
.result = std.zig.resolveTargetQueryOrFatal(target_query), .result = std.zig.resolveTargetQueryOrFatal(target_query),
.is_native_os = false,
.is_native_abi = false,
};
}
}
break :t .{
.result = std.zig.resolveTargetQueryOrFatal(.{}),
.is_native_os = true, .is_native_os = true,
.is_native_abi = true, .is_native_abi = true,
}; };
};
const exe_basename = try std.zig.binNameAlloc(arena, .{ const exe_basename = try std.zig.binNameAlloc(arena, .{
.root_name = "build", .root_name = "build",