From 58d8c1434d578d652dd31774c11d6bd15f4a7a82 Mon Sep 17 00:00:00 2001 From: Lee Cannon Date: Tue, 27 Sep 2022 19:49:59 +0100 Subject: [PATCH] cleanup `processPkgConfig` --- src/special/build_runner.zig | 74 ++++++++++++++++++------------------ 1 file changed, 37 insertions(+), 37 deletions(-) diff --git a/src/special/build_runner.zig b/src/special/build_runner.zig index baa69ee..fe17799 100644 --- a/src/special/build_runner.zig +++ b/src/special/build_runner.zig @@ -150,9 +150,7 @@ fn processIncludeDirs( else => continue, }; - if (include_dirs.contains(candidate)) continue; - - include_dirs.putAssumeCapacityNoClobber(candidate, {}); + include_dirs.putAssumeCapacity(candidate, {}); } } @@ -162,45 +160,47 @@ fn processPkgConfig( exe: *std.build.LibExeObjStep, ) !void { for (exe.link_objects.items) |link_object| { - switch (link_object) { - .system_lib => |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, - }, + if (link_object != .system_lib) continue; + const system_lib = link_object.system_lib; - 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 { switch (@typeInfo(@typeInfo(@TypeOf(root.build)).Fn.return_type.?)) { .Void => root.build(builder),