Skip to content

Commit

Permalink
Merge pull request #24 from minuscorp/dynamic-imports
Browse files Browse the repository at this point in the history
Allow to link dynamic libraries when importing *Config modules
  • Loading branch information
orta authored Oct 27, 2021
2 parents 84d4d70 + 57b6eeb commit 5df1709
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
22 changes: 22 additions & 0 deletions Sources/PackageConfig/DynamicLibraries.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

import class Foundation.Process
import class Foundation.Pipe
import class Foundation.NSRegularExpression
import struct Foundation.NSRange

enum DynamicLibraries {

Expand Down Expand Up @@ -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") }
}
}
19 changes: 18 additions & 1 deletion Sources/PackageConfig/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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?")
}
Expand All @@ -137,6 +150,10 @@ enum Package {
"-I", packageConfigPath,
"-l\(packageConfigLib)",
], +)

debugLog("CONFIG LIBS: \(configLibs)")

return dyLibs + configLibs
}

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

0 comments on commit 5df1709

Please sign in to comment.