Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions Sources/SwiftBundler/Bundler/DynamicLibraryBundler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,11 @@ enum DynamicLibraryBundler {
)
}

// Frameworks from xcframework binary targets are copied out of SwiftPM's
// read-only dependency checkouts, so we need to make the copy writable
// before we can update its install names or sign it.
try makeWritable(outputDependency)

// Recursively copy the dependencies of this library's dependencies.
try await copyDynamicDependencies(
dependedOnBy: outputDylib,
Expand All @@ -342,4 +347,35 @@ enum DynamicLibraryBundler {
includeSystemWideDependencies: includeSystemWideDependencies
)
}

/// Makes a file or a directory and all of its contents writable by its owner.
/// - Parameter location: The file or directory to make writable.
private static func makeWritable(_ location: URL) throws(Error) {
var items = [location]
if let enumerator = FileManager.default.enumerator(
at: location,
includingPropertiesForKeys: [.isSymbolicLinkKey]
) {
for case let item as URL in Array(enumerator) {
items.append(item)
}
}

try Error.catch(withMessage: .failedToMakeLibraryWritable(library: location)) {
for item in items {
// skip symlinks
let values = try item.resourceValues(forKeys: [.isSymbolicLinkKey])
guard values.isSymbolicLink != true else {
continue
}

let attributes = try FileManager.default.attributesOfItem(atPath: item.path)
let permissions = (attributes[.posixPermissions] as? NSNumber)?.intValue ?? 0o644
try FileManager.default.setAttributes(
[.posixPermissions: NSNumber(value: permissions | 0o200)],
ofItemAtPath: item.path
)
}
}
}
}
4 changes: 4 additions & 0 deletions Sources/SwiftBundler/Bundler/DynamicLibraryBundlerError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ extension DynamicLibraryBundler {
case failedToEnumerateDynamicDependencies(binary: URL)
case failedToSignMovedLibrary
case failedToLocateDynamicDependency(installName: String)
case failedToMakeLibraryWritable(library: URL)

var userFriendlyMessage: String {
switch self {
Expand All @@ -39,6 +40,9 @@ extension DynamicLibraryBundler {
return "Failed to sign relocated dynamic library using the ad-hoc signing method"
case .failedToLocateDynamicDependency(let installName):
return "Failed to locate dynamic dependency with install name '\(installName)'"
case .failedToMakeLibraryWritable(let library):
let libraryPath = library.path(relativeTo: .currentDirectory)
return "Failed to make '\(libraryPath)' writable"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,29 @@ extension SwiftPackageManager {
// Each dependency task can return transitive dependencies to process.
// These dependencies have already been de-duplicated so all we have to
// do here is queue them for processing.
//
// Loading a package launches multiple Swift processes, so we only load as
// many packages at once as there are processors. Otherwise large package
// graphs can exhaust the machine's memory.
let maximumRunningTasks = max(ProcessInfo.processInfo.activeProcessorCount, 1)
var runningTasks = 1
var pendingDependencies: [PackageDependency] = []
for await result in taskGroup {
runningTasks -= 1

switch result {
case .failure(let error):
taskGroup.cancelAll()
return .failure(error)
case .success(let transitiveDependencies):
for dependency in transitiveDependencies {
taskGroup.addTask {
await processDependency(dependency)
}
}
pendingDependencies += transitiveDependencies
}

while runningTasks < maximumRunningTasks, let dependency = pendingDependencies.popLast() {
runningTasks += 1
taskGroup.addTask {
await processDependency(dependency)
}
}
}

Expand Down
Loading