Skip to content

Commit

Permalink
build: Don't link system libraries when building a static library
Browse files Browse the repository at this point in the history
Previously, system libraries like `libX11.so` were passed to `ar rcs`.
Consumers of the archive receive warnings about invalid contents.

Fixes #20476
  • Loading branch information
jayschwa committed Feb 26, 2025
1 parent e0a955a commit 0e043d4
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions lib/std/Build/Step/Compile.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1172,11 +1172,13 @@ fn getZigArgs(compile: *Compile, fuzz: bool) ![][]const u8 {
break :prefix "-l";
};
switch (system_lib.use_pkg_config) {
.no => try zig_args.append(b.fmt("{s}{s}", .{ prefix, system_lib.name })),
.no => if (!compile.isStaticLibrary())
try zig_args.append(b.fmt("{s}{s}", .{ prefix, system_lib.name })),
.yes, .force => {
if (compile.runPkgConfig(system_lib.name)) |result| {
try zig_args.appendSlice(result.cflags);
try zig_args.appendSlice(result.libs);
if (!compile.isStaticLibrary())
try zig_args.appendSlice(result.libs);
try seen_system_libs.put(arena, system_lib.name, result.cflags);
} else |err| switch (err) {
error.PkgConfigInvalidOutput,
Expand All @@ -1188,10 +1190,11 @@ fn getZigArgs(compile: *Compile, fuzz: bool) ![][]const u8 {
.yes => {
// pkg-config failed, so fall back to linking the library
// by name directly.
try zig_args.append(b.fmt("{s}{s}", .{
prefix,
system_lib.name,
}));
if (!compile.isStaticLibrary())
try zig_args.append(b.fmt("{s}{s}", .{
prefix,
system_lib.name,
}));
},
.force => {
panic("pkg-config failed for library {s}", .{system_lib.name});
Expand Down

0 comments on commit 0e043d4

Please sign in to comment.