From 57b6eeb6c30f03ea0413abb7875d8f295451a0d5 Mon Sep 17 00:00:00 2001 From: Jorge Revuelta Herrero Date: Tue, 26 Oct 2021 10:40:04 +0200 Subject: [PATCH] Allow to link dynamic libraries when importing *Config modules --- Sources/PackageConfig/DynamicLibraries.swift | 22 ++++++++++++++++++++ Sources/PackageConfig/Package.swift | 19 ++++++++++++++++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/Sources/PackageConfig/DynamicLibraries.swift b/Sources/PackageConfig/DynamicLibraries.swift index e1461bc..9071b65 100644 --- a/Sources/PackageConfig/DynamicLibraries.swift +++ b/Sources/PackageConfig/DynamicLibraries.swift @@ -1,6 +1,8 @@ import class Foundation.Process import class Foundation.Pipe +import class Foundation.NSRegularExpression +import struct Foundation.NSRange enum DynamicLibraries { @@ -55,4 +57,24 @@ enum DynamicLibraries { .replacingOccurrences(of: "\"\"", with: "\"") .split(separator: "\"").map(String.init) } + + static func listImports() -> [String] { + let lines = read() + + var matches: [String] = [] + + for line in lines { + if let match = line.range(of: "import .*Config", options: .regularExpression) { + matches.append(String(line[match])) + } + } + + debugLog("MATCHES: \(matches)") + + return matches + .compactMap { $0.split(separator: " ") } + .compactMap { $0.last } + .map(String.init) + .filter { !$0.contains("PackageDescription") } + } } diff --git a/Sources/PackageConfig/Package.swift b/Sources/PackageConfig/Package.swift index b83bcbf..5cf4c19 100644 --- a/Sources/PackageConfig/Package.swift +++ b/Sources/PackageConfig/Package.swift @@ -121,8 +121,21 @@ enum Package { guard let packageConfigPath = libraryPath(for: packageConfigLib) else { throw Error("PackageConfig: Could not find lib\(packageConfigLib) to link against, is it possible you've not built yet?") } + let dyLibs = try DynamicLibraries.listImports().map { (libraryName: String) -> [String] in + guard let path = libraryPath(for: libraryName) else { + throw Error("PackageConfig: Could not find lib\(libraryName) to link against, is it possible you've not built yet?") + } + + return [ + "-L", path, + "-I", path, + "-l\(libraryName)", + ] + }.reduce([], +) + + debugLog("DYLIBS by IMPORT: \(dyLibs)") - return try DynamicLibraries.list().map { libraryName in + let configLibs = try DynamicLibraries.list().map { libraryName in guard let path = libraryPath(for: libraryName) else { throw Error("PackageConfig: Could not find lib\(libraryName) to link against, is it possible you've not built yet?") } @@ -137,6 +150,10 @@ enum Package { "-I", packageConfigPath, "-l\(packageConfigLib)", ], +) + + debugLog("CONFIG LIBS: \(configLibs)") + + return dyLibs + configLibs } private static func getSwiftPMManifestArgs(swiftPath: String) -> [String] {