Skip to content

Commit

Permalink
Simplify dynamic linking process
Browse files Browse the repository at this point in the history
  • Loading branch information
Jorge Revuelta Herrero committed Oct 7, 2021
1 parent c09f1e7 commit 9046725
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 67 deletions.
72 changes: 6 additions & 66 deletions Sources/PackageConfig/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ enum Package {
let linkedLibraries = try libraryLinkingArguments()
var arguments = [String]()
arguments += ["--driver-mode=swift"] // Eval in swift mode, I think?
arguments += getSwiftPMManifestArgs(swiftPath: swiftC) // SwiftPM lib
let swiftPMLib = getSwiftPMManifestArgs(swiftPath: swiftC)
arguments += swiftPMLib // SwiftPM lib
arguments += linkedLibraries
arguments += ["-suppress-warnings"] // SPM does that too
arguments += linkDynamicLibrary(path: ".build/debug")
arguments += linkDynamicLibrary(path: swiftPMLib[1])
arguments += ["-sdk"]
arguments += [findSDKPath()] // Add the SDK on which we need to compile into
arguments += ["Package.swift"] // The Package.swift in the CWD
Expand All @@ -38,71 +41,8 @@ enum Package {
debugLog("Finished launching swiftc")
}

private static func installNameTool(rpath: String, dylib: String?) throws {
#if os(Linux)
let swiftC = try findPath(tool: "swiftc")
#else
let swiftC = try runXCRun(tool: "swiftc")
#endif
let process = Process()
let pipe = Pipe()

process.launchPath = "/usr/bin/install_name_tool"
let arguments = ["-change", rpath, dylib ?? (getSwiftPMManifestArgs(swiftPath: swiftC)[1] + "/libPackageDescription.dylib"), "Package"]
process.arguments = arguments
process.standardOutput = pipe

debugLog("CMD: \(process.launchPath!) \(arguments.joined(separator: " "))")

process.launch()
process.waitUntilExit()

debugLog(String(data: pipe.fileHandleForReading.readDataToEndOfFile(), encoding: .utf8)!)
}

static func otool() throws {
guard FileManager.default.fileExists(atPath: FileManager.default.currentDirectoryPath + "/Package") else { return }

let process = Process()
let pipe = Pipe()

process.launchPath = "/usr/bin/otool"
let arguments = ["-L", "Package"]
process.arguments = arguments
process.standardOutput = pipe

debugLog("CMD: \(process.launchPath!) \(arguments.joined(separator: " "))")

process.launch()
process.waitUntilExit()

let output = String(data: pipe.fileHandleForReading.readDataToEndOfFile(), encoding: .utf8)!

let lines = output.trimmingCharacters(in: .whitespacesAndNewlines).replacingOccurrences(of: "\t", with: "").split(separator: "\n")
let missingLinks = lines.filter { $0.starts(with: "@rpath") }

if let packageDescription = missingLinks.filter({ $0.contains("libPackageDescription") }).first {
try installNameTool(rpath: String(packageDescription.split(separator: " ").first!), dylib: nil)
}

for missingLink in missingLinks.filter({ !$0.contains("libPackageDescription") }) {
try installNameTool(rpath: String(missingLink.split(separator: " ").first!), dylib: ".build/debug/\(missingLink.split(separator: " ").first!.replacingOccurrences(of: "@rpath/", with: ""))")
}

let resultProcess = Process()
let resultPipe = Pipe()

resultProcess.launchPath = "/usr/bin/otool"
let resultArguments = ["-L", "Package"]
resultProcess.arguments = resultArguments
resultProcess.standardOutput = resultPipe

debugLog("CMD: \(process.launchPath!) \(arguments.joined(separator: " "))")

resultProcess.launch()
resultProcess.waitUntilExit()

debugLog(String(data: resultPipe.fileHandleForReading.readDataToEndOfFile(), encoding: .utf8)!)
private static func linkDynamicLibrary(path: String) -> [String] {
["-Xlinker", "-rpath", "-Xlinker", path]
}

static func runIfNeeded() throws {
Expand Down
1 change: 0 additions & 1 deletion Sources/PackageConfig/PackageConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ extension PackageConfig {

public static func load() throws -> Self {
try Package.compile()
try Package.otool()
try Package.runIfNeeded()
return try Loader.load()
}
Expand Down

0 comments on commit 9046725

Please sign in to comment.