Skip to content

Commit

Permalink
Coff linker: Add IMPLIB support
Browse files Browse the repository at this point in the history
Allow --out-implib as passed by cmake and meson to be correctly passed
through to the linker to generate import libraries.
  • Loading branch information
kkartaltepe committed Jul 10, 2021
1 parent 7b8a968 commit 3ae6dbb
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/Compilation.zig
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,7 @@ pub const InitOptions = struct {
test_filter: ?[]const u8 = null,
test_name_prefix: ?[]const u8 = null,
subsystem: ?std.Target.SubSystem = null,
out_implib: ?[]const u8 = null,
/// WASI-only. Type of WASI execution model ("command" or "reactor").
wasi_exec_model: ?std.builtin.WasiExecModel = null,
};
Expand Down Expand Up @@ -1364,6 +1365,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
.each_lib_rpath = options.each_lib_rpath orelse options.is_native_os,
.disable_lld_caching = options.disable_lld_caching,
.subsystem = options.subsystem,
.out_implib = options.out_implib,
.is_test = options.is_test,
.wasi_exec_model = wasi_exec_model,
.use_stage1 = use_stage1,
Expand Down
1 change: 1 addition & 0 deletions src/link.zig
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ pub const Options = struct {
gc_sections: ?bool = null,
allow_shlib_undefined: ?bool,
subsystem: ?std.Target.SubSystem,
out_implib: ?[]const u8,
linker_script: ?[]const u8,
version_script: ?[]const u8,
soname: ?[]const u8,
Expand Down
9 changes: 7 additions & 2 deletions src/link/Coff.zig
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,7 @@ fn linkWithLLD(self: *Coff, comp: *Compilation) !void {
man.hash.add(self.base.options.dynamicbase);
man.hash.addOptional(self.base.options.major_subsystem_version);
man.hash.addOptional(self.base.options.minor_subsystem_version);
man.hash.addOptionalBytes(self.base.options.out_implib);

// We don't actually care whether it's a cache hit or miss; we just need the digest and the lock.
_ = try man.hit();
Expand Down Expand Up @@ -1020,8 +1021,8 @@ fn linkWithLLD(self: *Coff, comp: *Compilation) !void {
}
}

for (self.base.options.lib_dirs) |lib_dir| {
try argv.append(try allocPrint(arena, "-LIBPATH:{s}", .{lib_dir}));
if (self.base.options.out_implib) |out_implib| {
try argv.append(try allocPrint(arena, "-IMPLIB:{s}", .{out_implib}));
}

try argv.appendSlice(self.base.options.objects);
Expand All @@ -1034,6 +1035,10 @@ fn linkWithLLD(self: *Coff, comp: *Compilation) !void {
try argv.append(p);
}

for (self.base.options.lib_dirs) |lib_dir| {
try argv.append(try allocPrint(arena, "-LIBPATH:{s}", .{lib_dir}));
}

const resolved_subsystem: ?std.Target.SubSystem = blk: {
if (self.base.options.subsystem) |explicit| break :blk explicit;
switch (target.os.tag) {
Expand Down
8 changes: 8 additions & 0 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,7 @@ fn buildOutputType(
var main_pkg_path: ?[]const u8 = null;
var clang_preprocessor_mode: Compilation.ClangPreprocessorMode = .no;
var subsystem: ?std.Target.SubSystem = null;
var out_implib: ?[]const u8 = null;
var major_subsystem_version: ?u32 = null;
var minor_subsystem_version: ?u32 = null;
var wasi_exec_model: ?std.builtin.WasiExecModel = null;
Expand Down Expand Up @@ -1476,6 +1477,12 @@ fn buildOutputType(
) catch |err| {
fatal("unable to parse '{s}': {s}", .{ arg, @errorName(err) });
};
} else if (mem.eql(u8, arg, "--out-implib")) {
i += 1;
if (i >= linker_args.items.len) {
fatal("expected linker arg after '{s}'", .{arg});
}
out_implib = linker_args.items[i];
} else {
warn("unsupported linker arg: {s}", .{arg});
}
Expand Down Expand Up @@ -2069,6 +2076,7 @@ fn buildOutputType(
.test_name_prefix = test_name_prefix,
.disable_lld_caching = !have_enable_cache,
.subsystem = subsystem,
.out_implib = out_implib,
.wasi_exec_model = wasi_exec_model,
}) catch |err| {
fatal("unable to create compilation: {s}", .{@errorName(err)});
Expand Down

0 comments on commit 3ae6dbb

Please sign in to comment.