From 3ae6dbb13af3f31947cb8dae321e62f2ee267373 Mon Sep 17 00:00:00 2001 From: Kurt Kartaltepe Date: Fri, 9 Jul 2021 20:41:35 -0700 Subject: [PATCH] Coff linker: Add IMPLIB support Allow --out-implib as passed by cmake and meson to be correctly passed through to the linker to generate import libraries. --- src/Compilation.zig | 2 ++ src/link.zig | 1 + src/link/Coff.zig | 9 +++++++-- src/main.zig | 8 ++++++++ 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/Compilation.zig b/src/Compilation.zig index 58425197c1d0..0994f81cd41b 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -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, }; @@ -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, diff --git a/src/link.zig b/src/link.zig index 61a46e8b06f0..00d7a572b013 100644 --- a/src/link.zig +++ b/src/link.zig @@ -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, diff --git a/src/link/Coff.zig b/src/link/Coff.zig index b466cf913698..eb531dc4e994 100644 --- a/src/link/Coff.zig +++ b/src/link/Coff.zig @@ -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(); @@ -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); @@ -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) { diff --git a/src/main.zig b/src/main.zig index 2b961bb64c6b..10cf9602f8e7 100644 --- a/src/main.zig +++ b/src/main.zig @@ -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; @@ -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}); } @@ -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)});