cleanup processPkgConfig

This commit is contained in:
Lee Cannon 2022-09-27 19:49:59 +01:00
parent 137edcd527
commit 58d8c1434d
No known key found for this signature in database
GPG Key ID: 983D5E5CC5E1401F

View File

@ -150,9 +150,7 @@ fn processIncludeDirs(
else => continue, else => continue,
}; };
if (include_dirs.contains(candidate)) continue; include_dirs.putAssumeCapacity(candidate, {});
include_dirs.putAssumeCapacityNoClobber(candidate, {});
} }
} }
@ -162,45 +160,47 @@ fn processPkgConfig(
exe: *std.build.LibExeObjStep, exe: *std.build.LibExeObjStep,
) !void { ) !void {
for (exe.link_objects.items) |link_object| { for (exe.link_objects.items) |link_object| {
switch (link_object) { if (link_object != .system_lib) continue;
.system_lib => |system_lib| { const system_lib = link_object.system_lib;
switch (system_lib.use_pkg_config) {
.no => {},
.yes, .force => {
if (exe.runPkgConfig(system_lib.name)) |args| {
for (args) |arg| {
if (std.mem.startsWith(u8, arg, "-I")) {
const candidate = arg[2..];
if (include_dirs.contains(candidate)) continue;
try include_dirs.putNoClobber(allocator, candidate, {});
}
}
} else |err| switch (err) {
error.PkgConfigInvalidOutput,
error.PkgConfigCrashed,
error.PkgConfigFailed,
error.PkgConfigNotInstalled,
error.PackageNotFound,
=> switch (system_lib.use_pkg_config) {
.yes => {
// pkg-config failed, so zig will not add any include paths
},
.force => {
log.warn("pkg-config failed for library {s}", .{system_lib.name});
},
.no => unreachable,
},
else => |e| return e, if (system_lib.use_pkg_config == .no) continue;
}
}, getPkgConfigIncludes(allocator, include_dirs, exe, system_lib.name) catch |err| switch (err) {
} error.PkgConfigInvalidOutput,
error.PkgConfigCrashed,
error.PkgConfigFailed,
error.PkgConfigNotInstalled,
error.PackageNotFound,
=> switch (system_lib.use_pkg_config) {
.yes => {
// pkg-config failed, so zig will not add any include paths
},
.force => {
log.warn("pkg-config failed for library {s}", .{system_lib.name});
},
.no => unreachable,
}, },
else => {}, else => |e| return e,
} };
} }
} }
fn getPkgConfigIncludes(
allocator: std.mem.Allocator,
include_dirs: *std.StringArrayHashMapUnmanaged(void),
exe: *std.build.LibExeObjStep,
name: []const u8,
) !void {
if (exe.runPkgConfig(name)) |args| {
for (args) |arg| {
if (std.mem.startsWith(u8, arg, "-I")) {
const candidate = arg[2..];
try include_dirs.put(allocator, candidate, {});
}
}
} else |err| return err;
}
fn runBuild(builder: *Builder) anyerror!void { fn runBuild(builder: *Builder) anyerror!void {
switch (@typeInfo(@typeInfo(@TypeOf(root.build)).Fn.return_type.?)) { switch (@typeInfo(@typeInfo(@TypeOf(root.build)).Fn.return_type.?)) {
.Void => root.build(builder), .Void => root.build(builder),