Skip to content

windows-gnu: Adhere to MinGW convention for object file extension #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
4 changes: 2 additions & 2 deletions lib/compiler/aro/aro/Driver.zig
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,7 @@ fn processSource(
const fmt_template = "{s}{s}";
const fmt_args = .{
std.fs.path.stem(source.path),
d.comp.target.ofmt.fileExt(d.comp.target.cpu.arch),
d.comp.target.objFileExt(),
};
break :blk d.output_name orelse
std.fmt.bufPrint(&name_buf, fmt_template, fmt_args) catch return d.fatal("Filename too long for filesystem: " ++ fmt_template, fmt_args);
Expand All @@ -781,7 +781,7 @@ fn processSource(
const fmt_template = "/tmp/{s}{s}";
const fmt_args = .{
random_name,
d.comp.target.ofmt.fileExt(d.comp.target.cpu.arch),
d.comp.target.objFileExt(),
};
break :blk std.fmt.bufPrint(&name_buf, fmt_template, fmt_args) catch return d.fatal("Filename too long for filesystem: " ++ fmt_template, fmt_args);
};
Expand Down
22 changes: 14 additions & 8 deletions lib/std/Build/Module.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -239,7 +239,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,
Expand Down Expand Up @@ -280,7 +280,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 = .{},
Expand Down Expand Up @@ -561,11 +561,17 @@ pub fn appendZigProcessFlags(
.full => try zig_args.append("-fsanitize-c=full"),
};

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| {
Expand Down
15 changes: 13 additions & 2 deletions lib/std/Build/Step/Compile.zig
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,10 @@ pub fn create(owner: *std.Build, options: Options) *Compile {
compile.name_only_filename = owner.fmt("lib{s}.dylib", .{compile.name});
compile.out_lib_filename = compile.out_filename;
} else if (target.os.tag == .windows) {
compile.out_lib_filename = owner.fmt("{s}.lib", .{compile.name});
compile.out_lib_filename = if (target.abi.isGnu())
owner.fmt("lib{s}.dll.a", .{compile.name})
else
owner.fmt("{s}.lib", .{compile.name});
} else {
compile.major_only_filename = owner.fmt("lib{s}.so.{d}", .{ compile.name, version.major });
compile.name_only_filename = owner.fmt("lib{s}.so", .{compile.name});
Expand All @@ -490,7 +493,10 @@ pub fn create(owner: *std.Build, options: Options) *Compile {
if (target.os.tag.isDarwin()) {
compile.out_lib_filename = compile.out_filename;
} else if (target.os.tag == .windows) {
compile.out_lib_filename = owner.fmt("{s}.lib", .{compile.name});
compile.out_lib_filename = if (target.abi.isGnu())
owner.fmt("lib{s}.dll.a", .{compile.name})
else
owner.fmt("{s}.lib", .{compile.name});
} else {
compile.out_lib_filename = compile.out_filename;
}
Expand Down Expand Up @@ -667,6 +673,11 @@ pub fn producesPdbFile(compile: *Compile) bool {
.windows, .uefi => {},
else => return false,
}
if (compile.root_module.debug_format) |fmt| {
if (fmt != .code_view) return false;
} else {
if (target.abi.isGnu()) return false;
}
if (target.ofmt == .c) return false;
if (compile.use_llvm == false) return false;
if (compile.root_module.strip == true or
Expand Down
14 changes: 10 additions & 4 deletions lib/std/Target.zig
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ pub const Os = struct {
return switch (abi) {
.msvc, .itanium => ".lib",
else => switch (tag) {
.windows, .uefi => ".lib",
.uefi => ".lib",
.windows => if (abi.isGnu()) ".a" else ".lib",
else => ".a",
},
};
Expand All @@ -137,7 +138,8 @@ pub const Os = struct {
return switch (abi) {
.msvc, .itanium => "",
else => switch (tag) {
.windows, .uefi => "",
.uefi => "",
.windows => if (abi.isGnu()) "lib" else "",
else => "lib",
},
};
Expand Down Expand Up @@ -1046,10 +1048,10 @@ pub const ObjectFormat = enum {
// LLVM tags deliberately omitted:
// - dxcontainer

pub fn fileExt(of: ObjectFormat, arch: Cpu.Arch) [:0]const u8 {
pub fn fileExt(of: ObjectFormat, abi: Abi, arch: Cpu.Arch) [:0]const u8 {
return switch (of) {
.c => ".c",
.coff => ".obj",
.coff => if (abi.isGnu()) ".o" else ".obj",
.elf, .goff, .macho, .wasm, .xcoff => ".o",
.hex => ".ihex",
.plan9 => arch.plan9Ext(),
Expand Down Expand Up @@ -2019,6 +2021,10 @@ pub fn exeFileExt(target: *const Target) [:0]const u8 {
return target.os.tag.exeFileExt(target.cpu.arch);
}

pub fn objFileExt(target: *const Target) [:0]const u8 {
return target.ofmt.fileExt(target.abi, target.cpu.arch);
}

pub fn staticLibSuffix(target: *const Target) [:0]const u8 {
return target.os.tag.staticLibSuffix(target.abi);
}
Expand Down
8 changes: 8 additions & 0 deletions lib/std/builtin.zig
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,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.
Expand Down
2 changes: 1 addition & 1 deletion lib/std/debug/SelfInfo.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1003,7 +1003,7 @@ fn readCoffDebugInfo(allocator: Allocator, coff_obj: *coff.Coff) !Module {
di.dwarf = dwarf;
}

const raw_path = try coff_obj.getPdbPath() orelse return di;
const raw_path = (coff_obj.getPdbPath() catch return di) orelse return di;
const path = blk: {
if (fs.path.isAbsolute(raw_path)) {
break :blk raw_path;
Expand Down
20 changes: 14 additions & 6 deletions lib/std/zig.zig
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,17 @@ pub fn binNameAlloc(allocator: Allocator, options: BinNameOptions) error{OutOfMe
.coff => switch (options.output_mode) {
.Exe => return std.fmt.allocPrint(allocator, "{s}{s}", .{ root_name, t.exeFileExt() }),
.Lib => {
const prefix = if (t.abi.isGnu()) "lib" else "";
const suffix = switch (options.link_mode orelse .static) {
.static => ".lib",
.static => if (t.abi.isGnu()) ".a" else ".lib",
.dynamic => ".dll",
};
return std.fmt.allocPrint(allocator, "{s}{s}", .{ root_name, suffix });
return std.fmt.allocPrint(allocator, "{s}{s}{s}", .{ prefix, root_name, suffix });
},
.Obj => return std.fmt.allocPrint(allocator, "{s}.obj", .{root_name}),
.Obj => if (t.abi.isGnu())
return std.fmt.allocPrint(allocator, "{s}.o", .{root_name})
else
return std.fmt.allocPrint(allocator, "{s}.obj", .{root_name}),
},
.elf, .goff, .xcoff => switch (options.output_mode) {
.Exe => return allocator.dupe(u8, root_name),
Expand Down Expand Up @@ -226,7 +230,7 @@ pub fn binNameAlloc(allocator: Allocator, options: BinNameOptions) error{OutOfMe
.plan9 => switch (options.output_mode) {
.Exe => return allocator.dupe(u8, root_name),
.Obj => return std.fmt.allocPrint(allocator, "{s}{s}", .{
root_name, t.ofmt.fileExt(t.cpu.arch),
root_name, t.objFileExt(),
}),
.Lib => return std.fmt.allocPrint(allocator, "{s}{s}.a", .{
t.libPrefix(), root_name,
Expand Down Expand Up @@ -899,17 +903,21 @@ pub const EmitArtifact = enum {
/// paths under the output directory, where those paths are named according to this function.
/// Returned string is allocated with `gpa` and owned by the caller.
pub fn cacheName(ea: EmitArtifact, gpa: Allocator, opts: BinNameOptions) Allocator.Error![]const u8 {
const prefix: []const u8 = switch (ea) {
.implib => if (opts.target.abi.isGnu()) "lib" else "",
else => "",
};
const suffix: []const u8 = switch (ea) {
.bin => return binNameAlloc(gpa, opts),
.@"asm" => ".s",
.implib => ".lib",
.implib => if (opts.target.abi.isGnu()) ".dll.a" else ".lib",
.llvm_ir => ".ll",
.llvm_bc => ".bc",
.docs => "-docs",
.pdb => ".pdb",
.h => ".h",
};
return std.fmt.allocPrint(gpa, "{s}{s}", .{ opts.root_name, suffix });
return std.fmt.allocPrint(gpa, "{s}{s}{s}", .{ prefix, opts.root_name, suffix });
}
};

Expand Down
16 changes: 9 additions & 7 deletions src/Compilation.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1322,7 +1322,7 @@ pub const MiscTask = enum {

@"mingw-w64 crt2.o",
@"mingw-w64 dllcrt2.o",
@"mingw-w64 libmingw32.lib",
@"mingw-w64 libmingw32.a",
};

pub const MiscError = struct {
Expand Down Expand Up @@ -1378,13 +1378,13 @@ pub const cache_helpers = struct {
hh.add(resolved_target.is_explicit_dynamic_linker);
}

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 => {},
Expand Down Expand Up @@ -2394,7 +2394,7 @@ pub fn create(gpa: Allocator, arena: Allocator, options: CreateOptions) !*Compil

const main_crt_file: mingw.CrtFile = if (is_dyn_lib) .dllcrt2_o else .crt2_o;
comp.queued_jobs.mingw_crt_file[@intFromEnum(main_crt_file)] = true;
comp.queued_jobs.mingw_crt_file[@intFromEnum(mingw.CrtFile.libmingw32_lib)] = true;
comp.queued_jobs.mingw_crt_file[@intFromEnum(mingw.CrtFile.libmingw32_a)] = true;
comp.link_task_queue.pending_prelink_tasks += 2;

// When linking mingw-w64 there are some import libs we always need.
Expand Down Expand Up @@ -5704,7 +5704,7 @@ fn updateCObject(comp: *Compilation, c_object: *CObject, c_obj_prog_node: std.Pr
c_source_basename[0 .. c_source_basename.len - std.fs.path.extension(c_source_basename).len];

const target = comp.getTarget();
const o_ext = target.ofmt.fileExt(target.cpu.arch);
const o_ext = target.objFileExt();
const digest = if (!comp.disable_c_depfile and try man.hit()) man.final() else blk: {
var argv = std.ArrayList([]const u8).init(gpa);
defer argv.deinit();
Expand Down Expand Up @@ -7001,7 +7001,7 @@ pub const FileExt = enum {
.assembly => ".s",
.assembly_with_cpp => ".S",
.shared_library => target.dynamicLibSuffix(),
.object => target.ofmt.fileExt(target.cpu.arch),
.object => target.objFileExt(),
.static_library => target.staticLibSuffix(),
.zig => ".zig",
.def => ".def",
Expand Down Expand Up @@ -7298,6 +7298,7 @@ fn buildOutputFromZig(
.emit_bin = true,
.root_optimize_mode = optimize_mode,
.root_strip = strip,
.debug_format = comp.config.debug_format,
.link_libc = comp.config.link_libc,
.any_unwind_tables = comp.root_mod.unwind_tables != .none,
.any_error_tracing = false,
Expand Down Expand Up @@ -7426,6 +7427,7 @@ pub fn build_crt_file(
.emit_bin = true,
.root_optimize_mode = comp.compilerRtOptMode(),
.root_strip = comp.compilerRtStrip(),
.debug_format = comp.config.debug_format,
.link_libc = false,
.any_unwind_tables = options.unwind_tables != .none,
.lto = switch (output_mode) {
Expand Down
17 changes: 6 additions & 11 deletions src/Compilation/Config.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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_optimize_mode: std.builtin.OptimizeMode,
root_strip: bool,
root_error_tracing: bool,
Expand All @@ -66,12 +66,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,
resolved_target: Module.ResolvedTarget,
Expand Down Expand Up @@ -109,7 +103,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,
Expand Down Expand Up @@ -465,14 +459,15 @@ 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) {
.elf, .goff, .macho, .wasm, .xcoff => .{ .dwarf = .@"32" },
.coff => .code_view,
.coff => if (target.abi.isGnu()) .{ .dwarf = .@"32" } else .code_view,
.c => switch (target.os.tag) {
.windows, .uefi => .code_view,
.uefi => .code_view,
.windows => if (target.abi.isGnu()) .{ .dwarf = .@"32" } else .code_view,
else => .{ .dwarf = .@"32" },
},
.spirv, .hex, .raw, .plan9 => .strip,
Expand Down
18 changes: 16 additions & 2 deletions src/clang_options_data.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4222,7 +4222,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"),
Expand Down Expand Up @@ -7556,7 +7563,14 @@ joinpd1("mthreads"),
.pd2 = false,
.psl = false,
},
joinpd1("mwindows"),
.{
.name = "mwindows",
.syntax = .joined,
.zig_equivalent = .mingw_subsystem_windows,
.pd1 = true,
.pd2 = false,
.psl = false,
},
.{
.name = "offload=",
.syntax = .comma_joined,
Expand Down
Loading
Loading