Commit 986a3d23ab

Andrew Kelley <andrew@ziglang.org>
2023-08-02 08:48:14
frontend: make SystemLib.path optional
This can be null in two cases right now: 1. Windows DLLs that zig ships such as advapi32. 2. extern "foo" fn declarations where we find out about libraries too late TODO: make this non-optional and resolve those two cases somehow.
1 parent e565ff3
Changed files (4)
src/link/MachO/zld.zig
@@ -3554,7 +3554,7 @@ pub fn linkWithZld(macho_file: *MachO, comp: *Compilation, prog_node: *std.Progr
         {
             const vals = options.system_libs.values();
             try libs.ensureUnusedCapacity(vals.len);
-            for (vals) |v| libs.putAssumeCapacity(v.path, v);
+            for (vals) |v| libs.putAssumeCapacity(v.path.?, v);
         }
 
         try MachO.resolveLibSystem(arena, comp, options.sysroot, target, options.lib_dirs, &libs);
src/link/Elf.zig
@@ -1842,7 +1842,7 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v
                 // libraries and not static libraries (the check for that needs to be earlier),
                 // but they could be full paths to .so files, in which case we
                 // want to avoid prepending "-l".
-                argv.appendAssumeCapacity(lib_info.path);
+                argv.appendAssumeCapacity(lib_info.path.?);
             }
 
             if (!as_needed) {
src/Compilation.zig
@@ -1728,7 +1728,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
                 try comp.bin_file.options.system_libs.put(comp.gpa, name, .{
                     .needed = false,
                     .weak = false,
-                    .path = name,
+                    .path = null,
                 });
             }
         }
@@ -5621,7 +5621,7 @@ pub fn addLinkLib(comp: *Compilation, lib_name: []const u8) !void {
         gop.value_ptr.* = .{
             .needed = true,
             .weak = false,
-            .path = undefined,
+            .path = null,
         };
         try comp.work_queue.writeItem(.{
             .windows_import_lib = comp.bin_file.options.system_libs.count() - 1,
src/link.zig
@@ -26,7 +26,11 @@ const TypedValue = @import("TypedValue.zig");
 pub const SystemLib = struct {
     needed: bool,
     weak: bool,
-    path: []const u8,
+    /// This can be null in two cases right now:
+    /// 1. Windows DLLs that zig ships such as advapi32.
+    /// 2. extern "foo" fn declarations where we find out about libraries too late
+    /// TODO: make this non-optional and resolve those two cases somehow.
+    path: ?[]const u8,
 };
 
 /// When adding a new field, remember to update `hashAddFrameworks`.
@@ -48,7 +52,7 @@ pub fn hashAddSystemLibs(
     for (hm.values()) |value| {
         man.hash.add(value.needed);
         man.hash.add(value.weak);
-        _ = try man.addFile(value.path, null);
+        if (value.path) |p| _ = try man.addFile(p, null);
     }
 }