Commit 70563aeac3

kcbanner <kcbanner@gmail.com>
2023-09-21 08:26:05
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.
1 parent de4d1ea
Changed files (2)
src
test
standalone
issue_5825
src/main.zig
@@ -2688,6 +2688,13 @@ fn buildOutputType(
         lib: Compilation.SystemLib,
     }) = .{};
 
+    var libc_installation: ?LibCInstallation = null;
+    if (libc_paths_file) |paths_file| {
+        libc_installation = LibCInstallation.parse(arena, paths_file, cross_target) catch |err| {
+            fatal("unable to parse libc paths file at path {s}: {s}", .{ paths_file, @errorName(err) });
+        };
+    }
+
     for (system_libs.keys(), system_libs.values()) |lib_name, info| {
         if (target_util.is_libc_lib_name(target_info.target, lib_name)) {
             link_libc = true;
@@ -2709,7 +2716,7 @@ fn buildOutputType(
             },
         }
 
-        if (target_info.target.os.tag == .windows) {
+        if (target_info.target.isMinGW()) {
             const exists = mingw.libExists(arena, target_info.target, zig_lib_directory, lib_name) catch |err| {
                 fatal("failed to check zig installation for DLL import libs: {s}", .{
                     @errorName(err),
@@ -2768,6 +2775,21 @@ fn buildOutputType(
         try rpath_list.appendSlice(paths.rpaths.items);
     }
 
+    if (builtin.target.os.tag == .windows and
+        target_info.target.abi == .msvc and
+        external_system_libs.len != 0)
+    {
+        if (libc_installation == null) {
+            libc_installation = try LibCInstallation.findNative(.{
+                .allocator = arena,
+                .verbose = true,
+                .target = cross_target.toTarget(),
+            });
+
+            try lib_dirs.appendSlice(&.{ libc_installation.?.msvc_lib_dir.?, libc_installation.?.kernel32_lib_dir.? });
+        }
+    }
+
     // If any libs in this list are statically provided, we omit them from the
     // resolved list and populate the link_objects array instead.
     {
@@ -3240,15 +3262,6 @@ fn buildOutputType(
     try thread_pool.init(.{ .allocator = gpa });
     defer thread_pool.deinit();
 
-    var libc_installation: ?LibCInstallation = null;
-    defer if (libc_installation) |*l| l.deinit(gpa);
-
-    if (libc_paths_file) |paths_file| {
-        libc_installation = LibCInstallation.parse(gpa, paths_file, cross_target) catch |err| {
-            fatal("unable to parse libc paths file at path {s}: {s}", .{ paths_file, @errorName(err) });
-        };
-    }
-
     var global_cache_directory: Compilation.Directory = l: {
         if (override_global_cache_dir) |p| {
             break :l .{
test/standalone/issue_5825/build.zig
@@ -1,9 +1,13 @@
+const builtin = @import("builtin");
 const std = @import("std");
 
 pub fn build(b: *std.Build) void {
     const test_step = b.step("test", "Test it");
     b.default_step = test_step;
 
+    // Building for the msvc abi requires a native MSVC installation
+    if (builtin.os.tag != .windows or builtin.cpu.arch != .x86_64) return;
+
     const target = .{
         .cpu_arch = .x86_64,
         .os_tag = .windows,