From f77b43dad39a5140f3e39f32e98e9624368c16d6 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Fri, 12 Jul 2024 14:19:17 -0700 Subject: [PATCH] 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. --- src/main.zig | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/src/main.zig b/src/main.zig index 2fb49b74bc..0d942ea6c4 100644 --- a/src/main.zig +++ b/src/main.zig @@ -4691,6 +4691,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void { var verbose_llvm_cpu_features = false; var fetch_only = false; var system_pkg_dir_path: ?[]const u8 = null; + var debug_target: ?[]const u8 = null; const argv_index_exe = child_argv.items.len; _ = try child_argv.addOne(); @@ -4799,6 +4800,14 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void { } else { 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")) { verbose_link = true; } else if (mem.eql(u8, arg, "--verbose-cc")) { @@ -4857,11 +4866,27 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void { }); defer root_prog_node.end(); - const target_query: std.Target.Query = .{}; - const resolved_target: Package.Module.ResolvedTarget = .{ - .result = std.zig.resolveTargetQueryOrFatal(target_query), - .is_native_os = true, - .is_native_abi = true, + // Normally the build runner is compiled for the host target but here is + // 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), + .is_native_os = false, + .is_native_abi = false, + }; + } + } + break :t .{ + .result = std.zig.resolveTargetQueryOrFatal(.{}), + .is_native_os = true, + .is_native_abi = true, + }; }; const exe_basename = try std.zig.binNameAlloc(arena, .{