Skip to content

Commit 5df1709

Browse files
authored
Merge pull request #24 from minuscorp/dynamic-imports
Allow to link dynamic libraries when importing *Config modules
2 parents 84d4d70 + 57b6eeb commit 5df1709

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

Sources/PackageConfig/DynamicLibraries.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11

22
import class Foundation.Process
33
import class Foundation.Pipe
4+
import class Foundation.NSRegularExpression
5+
import struct Foundation.NSRange
46

57
enum DynamicLibraries {
68

@@ -55,4 +57,24 @@ enum DynamicLibraries {
5557
.replacingOccurrences(of: "\"\"", with: "\"")
5658
.split(separator: "\"").map(String.init)
5759
}
60+
61+
static func listImports() -> [String] {
62+
let lines = read()
63+
64+
var matches: [String] = []
65+
66+
for line in lines {
67+
if let match = line.range(of: "import .*Config", options: .regularExpression) {
68+
matches.append(String(line[match]))
69+
}
70+
}
71+
72+
debugLog("MATCHES: \(matches)")
73+
74+
return matches
75+
.compactMap { $0.split(separator: " ") }
76+
.compactMap { $0.last }
77+
.map(String.init)
78+
.filter { !$0.contains("PackageDescription") }
79+
}
5880
}

Sources/PackageConfig/Package.swift

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,21 @@ enum Package {
121121
guard let packageConfigPath = libraryPath(for: packageConfigLib) else {
122122
throw Error("PackageConfig: Could not find lib\(packageConfigLib) to link against, is it possible you've not built yet?")
123123
}
124+
let dyLibs = try DynamicLibraries.listImports().map { (libraryName: String) -> [String] in
125+
guard let path = libraryPath(for: libraryName) else {
126+
throw Error("PackageConfig: Could not find lib\(libraryName) to link against, is it possible you've not built yet?")
127+
}
128+
129+
return [
130+
"-L", path,
131+
"-I", path,
132+
"-l\(libraryName)",
133+
]
134+
}.reduce([], +)
135+
136+
debugLog("DYLIBS by IMPORT: \(dyLibs)")
124137

125-
return try DynamicLibraries.list().map { libraryName in
138+
let configLibs = try DynamicLibraries.list().map { libraryName in
126139
guard let path = libraryPath(for: libraryName) else {
127140
throw Error("PackageConfig: Could not find lib\(libraryName) to link against, is it possible you've not built yet?")
128141
}
@@ -137,6 +150,10 @@ enum Package {
137150
"-I", packageConfigPath,
138151
"-l\(packageConfigLib)",
139152
], +)
153+
154+
debugLog("CONFIG LIBS: \(configLibs)")
155+
156+
return dyLibs + configLibs
140157
}
141158

142159
private static func getSwiftPMManifestArgs(swiftPath: String) -> [String] {

0 commit comments

Comments
 (0)