authorgravatar for kcbanner@gmail.comCasey Banner <kcbanner@gmail.com> 2023-09-21 02:26:05-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-09-27 04:07:12-07:00
log70563aeac3e9efee7e8fb24744bc97197fc0ca9b
treefc58b7cf3ee8340c46c13a4569caeb3651741d8e
parentde4d1ea250f37c03cf2eec6c6bdf7436c8816893

windows: fix not finding system libs when compiling for *-windows-msvc

When compiling for *-windows-msvc, find the native libc_installation and add the lib dirs to lib_dirs, so that system libs can be found. Previously, `version` and `ole32` were detected via the mingw.libExists logic, even on .msvc, which was a false positive. This detection logic for mingw doesn't find uuid.lib, which was the failure that triggered this bugfix. Only build the issue_5825 test if the native target is x86_64-windows-msvc, since it requires the .msvc abi.

2 files changed, 27 insertions(+), 10 deletions(-)

src/main.zig+23-10
......@@ -2688,6 +2688,13 @@ fn buildOutputType(
26882688 lib: Compilation.SystemLib,
26892689 }) = .{};
26902690
2691 var libc_installation: ?LibCInstallation = null;
2692 if (libc_paths_file) |paths_file| {
2693 libc_installation = LibCInstallation.parse(arena, paths_file, cross_target) catch |err| {
2694 fatal("unable to parse libc paths file at path {s}: {s}", .{ paths_file, @errorName(err) });
2695 };
2696 }
2697
26912698 for (system_libs.keys(), system_libs.values()) |lib_name, info| {
26922699 if (target_util.is_libc_lib_name(target_info.target, lib_name)) {
26932700 link_libc = true;
......@@ -2709,7 +2716,7 @@ fn buildOutputType(
27092716 },
27102717 }
27112718
2712 if (target_info.target.os.tag == .windows) {
2719 if (target_info.target.isMinGW()) {
27132720 const exists = mingw.libExists(arena, target_info.target, zig_lib_directory, lib_name) catch |err| {
27142721 fatal("failed to check zig installation for DLL import libs: {s}", .{
27152722 @errorName(err),
......@@ -2768,6 +2775,21 @@ fn buildOutputType(
27682775 try rpath_list.appendSlice(paths.rpaths.items);
27692776 }
27702777
2778 if (builtin.target.os.tag == .windows and
2779 target_info.target.abi == .msvc and
2780 external_system_libs.len != 0)
2781 {
2782 if (libc_installation == null) {
2783 libc_installation = try LibCInstallation.findNative(.{
2784 .allocator = arena,
2785 .verbose = true,
2786 .target = cross_target.toTarget(),
2787 });
2788
2789 try lib_dirs.appendSlice(&.{ libc_installation.?.msvc_lib_dir.?, libc_installation.?.kernel32_lib_dir.? });
2790 }
2791 }
2792
27712793 // If any libs in this list are statically provided, we omit them from the
27722794 // resolved list and populate the link_objects array instead.
27732795 {
......@@ -3240,15 +3262,6 @@ fn buildOutputType(
32403262 try thread_pool.init(.{ .allocator = gpa });
32413263 defer thread_pool.deinit();
32423264
3243 var libc_installation: ?LibCInstallation = null;
3244 defer if (libc_installation) |*l| l.deinit(gpa);
3245
3246 if (libc_paths_file) |paths_file| {
3247 libc_installation = LibCInstallation.parse(gpa, paths_file, cross_target) catch |err| {
3248 fatal("unable to parse libc paths file at path {s}: {s}", .{ paths_file, @errorName(err) });
3249 };
3250 }
3251
32523265 var global_cache_directory: Compilation.Directory = l: {
32533266 if (override_global_cache_dir) |p| {
32543267 break :l .{
test/standalone/issue_5825/build.zig+4
......@@ -1,9 +1,13 @@
1const builtin = @import("builtin");
12const std = @import("std");
23
34pub fn build(b: *std.Build) void {
45 const test_step = b.step("test", "Test it");
56 b.default_step = test_step;
67
8 // Building for the msvc abi requires a native MSVC installation
9 if (builtin.os.tag != .windows or builtin.cpu.arch != .x86_64) return;
10
711 const target = .{
812 .cpu_arch = .x86_64,
913 .os_tag = .windows,