authorgravatar for kkartaltepe@gmail.comKurt Kartaltepe <kkartaltepe@gmail.com> 2021-07-09 20:41:35-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-11-24 17:14:20-07:00
loga950cb42bdf7b3f339071868c6675e7dcc892bae
treeddf75e85213e2b1ea0d8accb78471b6d25d01b60
parentfdcac5ecbd324170f7281f6704ead18b7d904e72

Coff linker: Add IMPLIB support

Allow --out-implib and -implib as passed by cmake and meson to be correctly passed through to the linker to generate import libraries.

4 files changed, 40 insertions(+), 1 deletions(-)

src/Compilation.zig+5-1
......@@ -767,6 +767,8 @@ pub const InitOptions = struct {
767767 test_filter: ?[]const u8 = null,
768768 test_name_prefix: ?[]const u8 = null,
769769 subsystem: ?std.Target.SubSystem = null,
770 /// Windows/PE only. Where to output the import library, can contain directories.
771 out_implib: ?[]const u8 = null,
770772 /// WASI-only. Type of WASI execution model ("command" or "reactor").
771773 wasi_exec_model: ?std.builtin.WasiExecModel = null,
772774 /// (Zig compiler development) Enable dumping linker's state as JSON.
......@@ -946,7 +948,8 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
946948 options.output_mode == .Lib or
947949 options.lld_argv.len != 0 or
948950 options.image_base_override != null or
949 options.linker_script != null or options.version_script != null)
951 options.linker_script != null or options.version_script != null or
952 options.out_implib != null)
950953 {
951954 break :blk true;
952955 }
......@@ -1461,6 +1464,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
14611464 .each_lib_rpath = options.each_lib_rpath orelse options.is_native_os,
14621465 .disable_lld_caching = options.disable_lld_caching,
14631466 .subsystem = options.subsystem,
1467 .out_implib = options.out_implib,
14641468 .is_test = options.is_test,
14651469 .wasi_exec_model = wasi_exec_model,
14661470 .use_stage1 = use_stage1,
src/link.zig+1
......@@ -127,6 +127,7 @@ pub const Options = struct {
127127 gc_sections: ?bool = null,
128128 allow_shlib_undefined: ?bool,
129129 subsystem: ?std.Target.SubSystem,
130 out_implib: ?[]const u8,
130131 linker_script: ?[]const u8,
131132 version_script: ?[]const u8,
132133 soname: ?[]const u8,
src/link/Coff.zig+5
......@@ -948,6 +948,7 @@ fn linkWithLLD(self: *Coff, comp: *Compilation) !void {
948948 man.hash.add(self.base.options.dynamicbase);
949949 man.hash.addOptional(self.base.options.major_subsystem_version);
950950 man.hash.addOptional(self.base.options.minor_subsystem_version);
951 man.hash.addOptionalBytes(self.base.options.out_implib);
951952
952953 // We don't actually care whether it's a cache hit or miss; we just need the digest and the lock.
953954 _ = try man.hit();
......@@ -1094,6 +1095,10 @@ fn linkWithLLD(self: *Coff, comp: *Compilation) !void {
10941095 try argv.append(p);
10951096 }
10961097
1098 if (self.base.options.out_implib != null) {
1099 try argv.append(try allocPrint(arena, "-IMPLIB:{s}.lib", .{full_out_path}));
1100 }
1101
10971102 const resolved_subsystem: ?std.Target.SubSystem = blk: {
10981103 if (self.base.options.subsystem) |explicit| break :blk explicit;
10991104 switch (target.os.tag) {
src/main.zig+29
......@@ -654,6 +654,7 @@ fn buildOutputType(
654654 var main_pkg_path: ?[]const u8 = null;
655655 var clang_preprocessor_mode: Compilation.ClangPreprocessorMode = .no;
656656 var subsystem: ?std.Target.SubSystem = null;
657 var out_implib: ?[]const u8 = null;
657658 var major_subsystem_version: ?u32 = null;
658659 var minor_subsystem_version: ?u32 = null;
659660 var wasi_exec_model: ?std.builtin.WasiExecModel = null;
......@@ -1637,6 +1638,14 @@ fn buildOutputType(
16371638 fatal("unable to parse -current_version '{s}': {s}", .{ linker_args.items[i], @errorName(err) });
16381639 };
16391640 have_version = true;
1641 } else if (mem.eql(u8, arg, "--out-implib") or
1642 mem.eql(u8, arg, "-implib"))
1643 {
1644 i += 1;
1645 if (i >= linker_args.items.len) {
1646 fatal("expected linker arg after '{s}'", .{arg});
1647 }
1648 out_implib = linker_args.items[i];
16401649 } else {
16411650 warn("unsupported linker arg: {s}", .{arg});
16421651 }
......@@ -2154,6 +2163,16 @@ fn buildOutputType(
21542163 else => false,
21552164 };
21562165
2166 // Always output import libraries (.lib) when building for msvc to replicate
2167 // `link` behavior. lld does not always output import libraries so on the
2168 // gnu abi users must set out_implib.
2169 if (output_mode == .Lib and emit_bin == .yes and target_info.target.abi == .msvc and out_implib == null) {
2170 const emit_bin_ext = fs.path.extension(emit_bin.yes);
2171 out_implib = try std.fmt.allocPrint(gpa, "{s}.lib", .{
2172 emit_bin.yes[0 .. emit_bin.yes.len - emit_bin_ext.len],
2173 });
2174 }
2175
21572176 gimmeMoreOfThoseSweetSweetFileDescriptors();
21582177
21592178 const comp = Compilation.create(gpa, .{
......@@ -2263,6 +2282,7 @@ fn buildOutputType(
22632282 .test_name_prefix = test_name_prefix,
22642283 .disable_lld_caching = !have_enable_cache,
22652284 .subsystem = subsystem,
2285 .out_implib = out_implib,
22662286 .wasi_exec_model = wasi_exec_model,
22672287 .debug_compile_errors = debug_compile_errors,
22682288 .enable_link_snapshots = enable_link_snapshots,
......@@ -2661,6 +2681,15 @@ fn updateModule(gpa: *Allocator, comp: *Compilation, hook: AfterUpdateHook) !voi
26612681
26622682 _ = try cache_dir.updateFile(src_pdb_path, cwd, dst_pdb_path, .{});
26632683 }
2684
2685 if (comp.bin_file.options.out_implib) |out_implib| {
2686 const src_implib_path = try std.fmt.allocPrint(gpa, "{s}.lib", .{bin_sub_path});
2687 defer gpa.free(src_implib_path);
2688 if (std.fs.path.dirname(out_implib)) |implib_dir| {
2689 try cwd.makePath(implib_dir);
2690 }
2691 _ = try cache_dir.updateFile(src_implib_path, cwd, out_implib, .{});
2692 }
26642693 },
26652694 }
26662695}