authorgravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2022-04-12 00:25:47+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-04-12 06:12:44-04:00
log38d6e1d8a85ff77bc98dd80f604525e0804dec11
tree7244813c9400376f8efc71c8011f89b4683a08a0
parentb9d86c6bc8e5d475ed8613bb241d1520377e629c

std.build: Fix transitive linkSystemLibraryName() dependencies

Currently transitive system library dependencies are always linked using linkSystemLibrary() and therefore pkg-config even if they were originally specified with linkSystemLibraryName() instead. This causes problems in practice for projects needing total control over exactly what library is linked, such as the mach game engine. This is fixed by keeping track of whether libraries are to be linked with pkg-config or not and holding off on actually running pkg-config until after transitive dependency resolution in LibExeObjStep.make(). This also fixes a separate issue with the pkg-config handling that could cause partial application of pkg-config flags if the first part of the pkg-config output parses correctly but there is an error later on. This error isn't always fatal as we fall back to a plain -lfoo in the case of linkSystemLibrary().

1 files changed, 119 insertions(+), 49 deletions(-)

lib/std/build.zig+119-49
......@@ -1600,12 +1600,26 @@ pub const LibExeObjStep = struct {
16001600 pub const LinkObject = union(enum) {
16011601 static_path: FileSource,
16021602 other_step: *LibExeObjStep,
1603 system_lib: []const u8,
1603 system_lib: SystemLib,
16041604 assembly_file: FileSource,
16051605 c_source_file: *CSourceFile,
16061606 c_source_files: *CSourceFiles,
16071607 };
16081608
1609 pub const SystemLib = struct {
1610 name: []const u8,
1611 use_pkg_config: enum {
1612 /// Don't use pkg-config, just pass -lfoo where foo is name.
1613 no,
1614 /// Try to get information on how to link the library from pkg-config.
1615 /// If that fails, fall back to passing -lfoo where foo is name.
1616 yes,
1617 /// Try to get information on how to link the library from pkg-config.
1618 /// If that fails, error out.
1619 force,
1620 },
1621 };
1622
16091623 pub const IncludeDir = union(enum) {
16101624 raw_path: []const u8,
16111625 raw_path_system: []const u8,
......@@ -1854,7 +1868,7 @@ pub const LibExeObjStep = struct {
18541868 }
18551869 for (self.link_objects.items) |link_object| {
18561870 switch (link_object) {
1857 .system_lib => |n| if (mem.eql(u8, n, name)) return true,
1871 .system_lib => |lib| if (mem.eql(u8, lib.name, name)) return true,
18581872 else => continue,
18591873 }
18601874 }
......@@ -1879,14 +1893,24 @@ pub const LibExeObjStep = struct {
18791893 pub fn linkLibC(self: *LibExeObjStep) void {
18801894 if (!self.is_linking_libc) {
18811895 self.is_linking_libc = true;
1882 self.link_objects.append(.{ .system_lib = "c" }) catch unreachable;
1896 self.link_objects.append(.{
1897 .system_lib = .{
1898 .name = "c",
1899 .use_pkg_config = .no,
1900 },
1901 }) catch unreachable;
18831902 }
18841903 }
18851904
18861905 pub fn linkLibCpp(self: *LibExeObjStep) void {
18871906 if (!self.is_linking_libcpp) {
18881907 self.is_linking_libcpp = true;
1889 self.link_objects.append(.{ .system_lib = "c++" }) catch unreachable;
1908 self.link_objects.append(.{
1909 .system_lib = .{
1910 .name = "c++",
1911 .use_pkg_config = .no,
1912 },
1913 }) catch unreachable;
18901914 }
18911915 }
18921916
......@@ -1905,12 +1929,28 @@ pub const LibExeObjStep = struct {
19051929 /// This one has no integration with anything, it just puts -lname on the command line.
19061930 /// Prefer to use `linkSystemLibrary` instead.
19071931 pub fn linkSystemLibraryName(self: *LibExeObjStep, name: []const u8) void {
1908 self.link_objects.append(.{ .system_lib = self.builder.dupe(name) }) catch unreachable;
1932 self.link_objects.append(.{
1933 .system_lib = .{
1934 .name = self.builder.dupe(name),
1935 .use_pkg_config = .no,
1936 },
1937 }) catch unreachable;
19091938 }
19101939
19111940 /// This links against a system library, exclusively using pkg-config to find the library.
19121941 /// Prefer to use `linkSystemLibrary` instead.
1913 pub fn linkSystemLibraryPkgConfigOnly(self: *LibExeObjStep, lib_name: []const u8) !void {
1942 pub fn linkSystemLibraryPkgConfigOnly(self: *LibExeObjStep, lib_name: []const u8) void {
1943 self.link_objects.append(.{
1944 .system_lib = .{
1945 .name = self.builder.dupe(lib_name),
1946 .use_pkg_config = .force,
1947 },
1948 }) catch unreachable;
1949 }
1950
1951 /// Run pkg-config for the given library name and parse the output, returning the arguments
1952 /// that should be passed to zig to link the given library.
1953 fn runPkgConfig(self: *LibExeObjStep, lib_name: []const u8) ![]const []const u8 {
19141954 const pkg_name = match: {
19151955 // First we have to map the library name to pkg config name. Unfortunately,
19161956 // there are several examples where this is not straightforward:
......@@ -1970,34 +2010,38 @@ pub const LibExeObjStep = struct {
19702010 error.ChildExecFailed => return error.PkgConfigFailed,
19712011 else => return err,
19722012 };
2013
2014 var zig_args = std.ArrayList([]const u8).init(self.builder.allocator);
2015 defer zig_args.deinit();
2016
19732017 var it = mem.tokenize(u8, stdout, " \r\n\t");
19742018 while (it.next()) |tok| {
19752019 if (mem.eql(u8, tok, "-I")) {
19762020 const dir = it.next() orelse return error.PkgConfigInvalidOutput;
1977 self.addIncludePath(dir);
2021 try zig_args.appendSlice(&[_][]const u8{ "-I", dir });
19782022 } else if (mem.startsWith(u8, tok, "-I")) {
1979 self.addIncludePath(tok["-I".len..]);
2023 try zig_args.append(tok);
19802024 } else if (mem.eql(u8, tok, "-L")) {
19812025 const dir = it.next() orelse return error.PkgConfigInvalidOutput;
1982 self.addLibraryPath(dir);
2026 try zig_args.appendSlice(&[_][]const u8{ "-L", dir });
19832027 } else if (mem.startsWith(u8, tok, "-L")) {
1984 self.addLibraryPath(tok["-L".len..]);
2028 try zig_args.append(tok);
19852029 } else if (mem.eql(u8, tok, "-l")) {
19862030 const lib = it.next() orelse return error.PkgConfigInvalidOutput;
1987 self.linkSystemLibraryName(lib);
2031 try zig_args.appendSlice(&[_][]const u8{ "-l", lib });
19882032 } else if (mem.startsWith(u8, tok, "-l")) {
1989 self.linkSystemLibraryName(tok["-l".len..]);
2033 try zig_args.append(tok);
19902034 } else if (mem.eql(u8, tok, "-D")) {
19912035 const macro = it.next() orelse return error.PkgConfigInvalidOutput;
1992 self.defineCMacroRaw(macro);
2036 try zig_args.appendSlice(&[_][]const u8{ "-D", macro });
19932037 } else if (mem.startsWith(u8, tok, "-D")) {
1994 self.defineCMacroRaw(tok["-D".len..]);
1995 } else if (mem.eql(u8, tok, "-pthread")) {
1996 self.linkLibC();
2038 try zig_args.append(tok);
19972039 } else if (self.builder.verbose) {
19982040 warn("Ignoring pkg-config flag '{s}'\n", .{tok});
19992041 }
20002042 }
2043
2044 return zig_args.toOwnedSlice();
20012045 }
20022046
20032047 pub fn linkSystemLibrary(self: *LibExeObjStep, name: []const u8) void {
......@@ -2009,21 +2053,13 @@ pub const LibExeObjStep = struct {
20092053 self.linkLibCpp();
20102054 return;
20112055 }
2012 if (self.linkSystemLibraryPkgConfigOnly(name)) |_| {
2013 // pkg-config worked, so nothing further needed to do.
2014 return;
2015 } else |err| switch (err) {
2016 error.PkgConfigInvalidOutput,
2017 error.PkgConfigCrashed,
2018 error.PkgConfigFailed,
2019 error.PkgConfigNotInstalled,
2020 error.PackageNotFound,
2021 => {},
2022
2023 else => unreachable,
2024 }
20252056
2026 self.linkSystemLibraryName(name);
2057 self.link_objects.append(.{
2058 .system_lib = .{
2059 .name = self.builder.dupe(name),
2060 .use_pkg_config = .yes,
2061 },
2062 }) catch unreachable;
20272063 }
20282064
20292065 pub fn setNamePrefix(self: *LibExeObjStep, text: []const u8) void {
......@@ -2317,27 +2353,34 @@ pub const LibExeObjStep = struct {
23172353 var prev_has_extra_flags = false;
23182354
23192355 // Resolve transitive dependencies
2320 for (self.link_objects.items) |link_object| {
2321 switch (link_object) {
2322 .other_step => |other| {
2323 // Inherit dependency on system libraries
2324 for (other.link_objects.items) |other_link_object| {
2325 switch (other_link_object) {
2326 .system_lib => |name| self.linkSystemLibrary(name),
2327 else => continue,
2356 {
2357 var transitive_dependencies = std.ArrayList(LinkObject).init(builder.allocator);
2358 defer transitive_dependencies.deinit();
2359
2360 for (self.link_objects.items) |link_object| {
2361 switch (link_object) {
2362 .other_step => |other| {
2363 // Inherit dependency on system libraries
2364 for (other.link_objects.items) |other_link_object| {
2365 switch (other_link_object) {
2366 .system_lib => try transitive_dependencies.append(other_link_object),
2367 else => continue,
2368 }
23282369 }
2329 }
23302370
2331 // Inherit dependencies on darwin frameworks
2332 if (!other.isDynamicLibrary()) {
2333 var it = other.frameworks.iterator();
2334 while (it.next()) |framework| {
2335 self.frameworks.insert(framework.*) catch unreachable;
2371 // Inherit dependencies on darwin frameworks
2372 if (!other.isDynamicLibrary()) {
2373 var it = other.frameworks.iterator();
2374 while (it.next()) |framework| {
2375 self.frameworks.insert(framework.*) catch unreachable;
2376 }
23362377 }
2337 }
2338 },
2339 else => continue,
2378 },
2379 else => continue,
2380 }
23402381 }
2382
2383 try self.link_objects.appendSlice(transitive_dependencies.items);
23412384 }
23422385
23432386 for (self.link_objects.items) |link_object| {
......@@ -2363,8 +2406,35 @@ pub const LibExeObjStep = struct {
23632406 }
23642407 },
23652408 },
2366 .system_lib => |name| {
2367 try zig_args.append(builder.fmt("-l{s}", .{name}));
2409
2410 .system_lib => |system_lib| {
2411 switch (system_lib.use_pkg_config) {
2412 .no => try zig_args.append(builder.fmt("-l{s}", .{system_lib.name})),
2413 .yes, .force => {
2414 if (self.runPkgConfig(system_lib.name)) |args| {
2415 try zig_args.appendSlice(args);
2416 } else |err| switch (err) {
2417 error.PkgConfigInvalidOutput,
2418 error.PkgConfigCrashed,
2419 error.PkgConfigFailed,
2420 error.PkgConfigNotInstalled,
2421 error.PackageNotFound,
2422 => switch (system_lib.use_pkg_config) {
2423 .yes => {
2424 // pkg-config failed, so fall back to linking the library
2425 // by name directly.
2426 try zig_args.append(builder.fmt("-l{s}", .{system_lib.name}));
2427 },
2428 .force => {
2429 panic("pkg-config failed for library {s}", .{system_lib.name});
2430 },
2431 .no => unreachable,
2432 },
2433
2434 else => |e| return e,
2435 }
2436 },
2437 }
23682438 },
23692439
23702440 .assembly_file => |asm_file| {