Skip to content

Improvements to prebuilt library configuration handling #7433

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
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
58 changes: 29 additions & 29 deletions Sources/PackageModel/UserToolchain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -533,40 +533,25 @@ public final class UserToolchain: Toolchain {
if let customInstalledSwiftPMConfiguration {
self.installedSwiftPMConfiguration = customInstalledSwiftPMConfiguration
} else {
let path = self.swiftCompilerPath.parentDirectory.parentDirectory.appending(components: ["share", "pm", "config.json"])
if localFileSystem.exists(path) {
self.installedSwiftPMConfiguration = try JSONDecoder.makeWithDefaults().decode(path: path, fileSystem: localFileSystem, as: InstalledSwiftPMConfiguration.self)
} else {
// We *could* eventually make this an error, but not for a few releases.
self.installedSwiftPMConfiguration = InstalledSwiftPMConfiguration.default
}
let path = swiftCompilerPath.parentDirectory.parentDirectory.appending(components: [
"share", "pm", "config.json",
])
self.installedSwiftPMConfiguration = try Self.loadJSONResource(
config: path,
type: InstalledSwiftPMConfiguration.self,
default: InstalledSwiftPMConfiguration.default)
}

if let customProvidedLibraries {
self.providedLibraries = customProvidedLibraries
} else {
// When building with CMake or `swift build --build-system xcode`, we need to skip resource support.
#if SKIP_RESOURCE_SUPPORT
let path = self.swiftCompilerPath.parentDirectory.parentDirectory.appending(components: ["share", "pm", "provided-libraries.json"])
#else
let path: AbsolutePath
if let developmentPath = Bundle.module.path(forResource: "provided-libraries", ofType: "json") {
// During development, we should be able to find the metadata file using `Bundle.module`.
path = try AbsolutePath(validating: developmentPath)
} else {
// When deployed, we can find the metadata file in the toolchain.
path = self.swiftCompilerPath.parentDirectory.parentDirectory.appending(components: ["share", "pm", "provided-libraries.json"])
}
#endif
if localFileSystem.exists(path) {
self.providedLibraries = try JSONDecoder.makeWithDefaults().decode(
path: path,
fileSystem: localFileSystem,
as: [LibraryMetadata].self
)
} else {
self.providedLibraries = []
}
let path = swiftCompilerPath.parentDirectory.parentDirectory.appending(components: [
"share", "pm", "provided-libraries.json",
])
self.providedLibraries = try Self.loadJSONResource(
config: path,
type: [LibraryMetadata].self,
default: [])
}

// Use the triple from Swift SDK or compute the host triple using swiftc.
Expand Down Expand Up @@ -895,4 +880,19 @@ public final class UserToolchain: Toolchain {
}
}
}

private static func loadJSONResource<T: Decodable>(
config: AbsolutePath, type: T.Type, `default`: T
)
throws -> T
{
if localFileSystem.exists(config) {
return try JSONDecoder.makeWithDefaults().decode(
path: config,
fileSystem: localFileSystem,
as: type)
}

return `default`
}
}
3 changes: 3 additions & 0 deletions Utilities/bootstrap
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,9 @@ def install(args):
config_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "config.json")
install_file(args, config_path, os.path.join(os.path.join(prefix, "share"), "pm"))

libs_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "provided-libraries.json")
install_file(args, config_path, os.path.join(os.path.join(prefix, "share"), "pm"))

# Install libSwiftPM if an install directory was provided.
if args.libswiftpm_install_dir:
libswiftpm_modules = [
Expand Down