From 38675707183a345b0151dd23cbcf13f2b411759a Mon Sep 17 00:00:00 2001 From: Chan Lee Date: Tue, 7 Jan 2025 10:56:42 +0800 Subject: [PATCH] windows-gnu: Add support for `-gcodeview` flag --- lib/std/Build/Module.zig | 22 ++++++++++++++-------- lib/std/Build/Step/Compile.zig | 1 - lib/std/builtin.zig | 8 ++++++++ src/Compilation.zig | 6 +++--- src/Compilation/Config.zig | 11 +++-------- src/clang_options_data.zig | 9 ++++++++- src/main.zig | 10 +++++++++- tools/update_clang_options.zig | 4 ++++ 8 files changed, 49 insertions(+), 22 deletions(-) diff --git a/lib/std/Build/Module.zig b/lib/std/Build/Module.zig index 1f2b4f3fcb7f..867978204755 100644 --- a/lib/std/Build/Module.zig +++ b/lib/std/Build/Module.zig @@ -8,7 +8,7 @@ import_table: std.StringArrayHashMapUnmanaged(*Module), resolved_target: ?std.Build.ResolvedTarget = null, optimize: ?std.builtin.OptimizeMode = null, -dwarf_format: ?std.dwarf.Format, +debug_format: ?std.builtin.DebugFormat, c_macros: std.ArrayListUnmanaged([]const u8), include_dirs: std.ArrayListUnmanaged(IncludeDir), @@ -219,7 +219,7 @@ pub const CreateOptions = struct { single_threaded: ?bool = null, strip: ?bool = null, unwind_tables: ?std.builtin.UnwindTables = null, - dwarf_format: ?std.dwarf.Format = null, + debug_format: ?std.builtin.DebugFormat = null, code_model: std.builtin.CodeModel = .default, stack_protector: ?bool = null, stack_check: ?bool = null, @@ -259,7 +259,7 @@ pub fn init( .optimize = options.optimize, .link_libc = options.link_libc, .link_libcpp = options.link_libcpp, - .dwarf_format = options.dwarf_format, + .debug_format = options.debug_format, .c_macros = .{}, .include_dirs = .{}, .lib_paths = .{}, @@ -525,11 +525,17 @@ pub fn appendZigProcessFlags( try addFlag(zig_args, m.pic, "-fPIC", "-fno-PIC"); try addFlag(zig_args, m.red_zone, "-mred-zone", "-mno-red-zone"); - if (m.dwarf_format) |dwarf_format| { - try zig_args.append(switch (dwarf_format) { - .@"32" => "-gdwarf32", - .@"64" => "-gdwarf64", - }); + if (m.debug_format) |debug_format| { + switch (debug_format) { + .strip => {}, + .dwarf => |fmt| switch (fmt) { + .@"32" => try zig_args.append("-gdwarf32"), + .@"64" => try zig_args.append("-gdwarf64"), + }, + .code_view => { + try zig_args.append("-gcodeview"); + }, + } } if (m.unwind_tables) |unwind_tables| { diff --git a/lib/std/Build/Step/Compile.zig b/lib/std/Build/Step/Compile.zig index 240847354043..497de69ba67d 100644 --- a/lib/std/Build/Step/Compile.zig +++ b/lib/std/Build/Step/Compile.zig @@ -657,7 +657,6 @@ pub fn producesPdbFile(compile: *Compile) bool { .windows, .uefi => {}, else => return false, } - if (target.abi.isGnu()) return false; if (target.ofmt == .c) return false; if (compile.root_module.strip == true or (compile.root_module.strip == null and compile.root_module.optimize == .ReleaseSmall)) diff --git a/lib/std/builtin.zig b/lib/std/builtin.zig index eda7f0ff4f5e..d7260e44bba4 100644 --- a/lib/std/builtin.zig +++ b/lib/std/builtin.zig @@ -160,6 +160,14 @@ pub const OptimizeMode = enum { /// Deprecated; use OptimizeMode. pub const Mode = OptimizeMode; +/// DebugFormat specifies which type of debug information to generate: +pub const DebugFormat = union(enum) { + /// No debug information. + strip, + dwarf: std.dwarf.Format, + code_view, +}; + /// The calling convention of a function defines how arguments and return values are passed, as well /// as any other requirements which callers and callees must respect, such as register preservation /// and stack alignment. diff --git a/src/Compilation.zig b/src/Compilation.zig index 4c8f3fefbedd..38a1ca11ef76 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -882,13 +882,13 @@ pub const cache_helpers = struct { addEmitLoc(hh, optional_emit_loc orelse return); } - pub fn addOptionalDebugFormat(hh: *Cache.HashHelper, x: ?Config.DebugFormat) void { + pub fn addOptionalDebugFormat(hh: *Cache.HashHelper, x: ?std.builtin.DebugFormat) void { hh.add(x != null); addDebugFormat(hh, x orelse return); } - pub fn addDebugFormat(hh: *Cache.HashHelper, x: Config.DebugFormat) void { - const tag: @typeInfo(Config.DebugFormat).@"union".tag_type.? = x; + pub fn addDebugFormat(hh: *Cache.HashHelper, x: std.builtin.DebugFormat) void { + const tag: @typeInfo(std.builtin.DebugFormat).@"union".tag_type.? = x; hh.add(tag); switch (x) { .strip, .code_view => {}, diff --git a/src/Compilation/Config.zig b/src/Compilation/Config.zig index 5670d4d6f979..e0adbabef339 100644 --- a/src/Compilation/Config.zig +++ b/src/Compilation/Config.zig @@ -56,7 +56,7 @@ import_memory: bool, export_memory: bool, shared_memory: bool, is_test: bool, -debug_format: DebugFormat, +debug_format: std.builtin.DebugFormat, root_strip: bool, root_error_tracing: bool, dll_export_fns: bool, @@ -65,11 +65,6 @@ san_cov_trace_pc_guard: bool, pub const CFrontend = enum { clang, aro }; -pub const DebugFormat = union(enum) { - strip, - dwarf: std.dwarf.Format, - code_view, -}; pub const Options = struct { output_mode: std.builtin.OutputMode, @@ -107,7 +102,7 @@ pub const Options = struct { import_memory: ?bool = null, export_memory: ?bool = null, shared_memory: ?bool = null, - debug_format: ?DebugFormat = null, + debug_format: ?std.builtin.DebugFormat = null, dll_export_fns: ?bool = null, rdynamic: ?bool = null, san_cov_trace_pc_guard: bool = false, @@ -439,7 +434,7 @@ pub fn resolve(options: Options) ResolveError!Config { break :b false; }; - const debug_format: DebugFormat = b: { + const debug_format: std.builtin.DebugFormat = b: { if (root_strip and !options.any_non_stripped) break :b .strip; if (options.debug_format) |x| break :b x; break :b switch (target.ofmt) { diff --git a/src/clang_options_data.zig b/src/clang_options_data.zig index 89de37e8ac73..75a83bf61387 100644 --- a/src/clang_options_data.zig +++ b/src/clang_options_data.zig @@ -4148,7 +4148,14 @@ flagpd1("g3"), .pd2 = false, .psl = false, }, -flagpd1("gcodeview"), +.{ + .name = "gcodeview", + .syntax = .flag, + .zig_equivalent = .gcodeview, + .pd1 = true, + .pd2 = false, + .psl = false, +}, flagpd1("gcodeview-command-line"), flagpd1("gcodeview-ghash"), flagpd1("gcolumn-info"), diff --git a/src/main.zig b/src/main.zig index 10fec66fa751..662f0b05eb2d 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1538,7 +1538,10 @@ fn buildOutputType( create_module.opts.debug_format = .{ .dwarf = .@"32" }; } else if (mem.eql(u8, arg, "-gdwarf64")) { create_module.opts.debug_format = .{ .dwarf = .@"64" }; - } else if (mem.eql(u8, arg, "-fformatted-panics")) { + } else if (mem.eql(u8, arg, "-gcodeview")) { + create_module.opts.debug_format = .code_view; + } + else if (mem.eql(u8, arg, "-fformatted-panics")) { // Remove this after 0.15.0 is tagged. warn("-fformatted-panics is deprecated and does nothing", .{}); } else if (mem.eql(u8, arg, "-fno-formatted-panics")) { @@ -2160,6 +2163,10 @@ fn buildOutputType( try cc_argv.appendSlice(arena, it.other_args); } }, + .gcodeview => { + mod_opts.strip = false; + create_module.opts.debug_format = .code_view; + }, .gdwarf32 => { mod_opts.strip = false; create_module.opts.debug_format = .{ .dwarf = .@"32" }; @@ -5776,6 +5783,7 @@ pub const ClangArgIterator = struct { asm_only, optimize, debug, + gcodeview, gdwarf32, gdwarf64, sanitize, diff --git a/tools/update_clang_options.zig b/tools/update_clang_options.zig index b8916e5e6aac..de8caa1f59d9 100644 --- a/tools/update_clang_options.zig +++ b/tools/update_clang_options.zig @@ -252,6 +252,10 @@ const known_options = [_]KnownOpt{ .name = "debug", .ident = "debug", }, + .{ + .name = "gcodeview", + .ident = "gcodeview", + }, .{ .name = "gdwarf32", .ident = "gdwarf32",