From fb53b3aa78b707a31f46f22ee8c0dea3ee4deb1e Mon Sep 17 00:00:00 2001 From: Owen Voorhees Date: Sun, 16 Mar 2025 16:31:39 -0700 Subject: [PATCH] Reapply "Include Swift Build support in second stage bootstrap builds" This reverts commit 6a2e45f2a625cb9920285f77c638c342b837e382. - Stop setting SWIFTPM_NO_SWBUILD_DEPENDENCY in the bootstrap script - Copy resource bundles required by Swift Build into the toolchain when installing --- Sources/CoreCommands/BuildSystemSupport.swift | 1 + Sources/CoreCommands/Options.swift | 6 ++++++ Sources/CoreCommands/SwiftCommandState.swift | 14 ++++++++++++++ Sources/SwiftBuildSupport/SwiftBuildSystem.swift | 8 ++++++-- Sources/swift-bootstrap/main.swift | 1 + Utilities/bootstrap | 8 ++++++-- 6 files changed, 34 insertions(+), 4 deletions(-) diff --git a/Sources/CoreCommands/BuildSystemSupport.swift b/Sources/CoreCommands/BuildSystemSupport.swift index 30a00599b9f..9407a10f8a4 100644 --- a/Sources/CoreCommands/BuildSystemSupport.swift +++ b/Sources/CoreCommands/BuildSystemSupport.swift @@ -121,6 +121,7 @@ private struct SwiftBuildSystemFactory: BuildSystemFactory { explicitProduct: explicitProduct ) }, + packageManagerResourcesDirectory: swiftCommandState.packageManagerResourcesDirectory, outputStream: outputStream ?? self.swiftCommandState.outputStream, logLevel: logLevel ?? self.swiftCommandState.logLevel, fileSystem: self.swiftCommandState.fileSystem, diff --git a/Sources/CoreCommands/Options.swift b/Sources/CoreCommands/Options.swift index 2b3f1265953..f2e0ec7f741 100644 --- a/Sources/CoreCommands/Options.swift +++ b/Sources/CoreCommands/Options.swift @@ -146,6 +146,12 @@ public struct LocationOptions: ParsableArguments { completion: .directory ) public var pkgConfigDirectories: [AbsolutePath] = [] + + @Option( + help: .init("Specify alternate path to search for resources required for SwiftPM to operate. (default: /usr/share/pm)", visibility: .hidden), + completion: .directory + ) + public var packageManagerResourcesDirectory: AbsolutePath? @Flag(name: .customLong("ignore-lock"), help: .hidden) public var ignoreLock: Bool = false diff --git a/Sources/CoreCommands/SwiftCommandState.swift b/Sources/CoreCommands/SwiftCommandState.swift index e3d9d6a89ae..0a1e7187ee4 100644 --- a/Sources/CoreCommands/SwiftCommandState.swift +++ b/Sources/CoreCommands/SwiftCommandState.swift @@ -227,6 +227,9 @@ public final class SwiftCommandState { /// Path to the shared configuration directory public let sharedConfigurationDirectory: AbsolutePath + + /// Path to the package manager's own resources directory. + public let packageManagerResourcesDirectory: AbsolutePath? /// Path to the cross-compilation Swift SDKs directory. public let sharedSwiftSDKsDirectory: AbsolutePath @@ -371,6 +374,17 @@ public final class SwiftCommandState { warning: "`--experimental-swift-sdks-path` is deprecated and will be removed in a future version of SwiftPM. Use `--swift-sdks-path` instead." ) } + + if let packageManagerResourcesDirectory = options.locations.packageManagerResourcesDirectory { + self.packageManagerResourcesDirectory = packageManagerResourcesDirectory + } else if let cwd = localFileSystem.currentWorkingDirectory { + self.packageManagerResourcesDirectory = try? AbsolutePath(validating: CommandLine.arguments[0], relativeTo: cwd) + .parentDirectory.parentDirectory.appending(components: ["share", "pm"]) + } else { + self.packageManagerResourcesDirectory = try? AbsolutePath(validating: CommandLine.arguments[0]) + .parentDirectory.parentDirectory.appending(components: ["share", "pm"]) + } + self.sharedSwiftSDKsDirectory = try fileSystem.getSharedSwiftSDKsDirectory( explicitDirectory: options.locations.swiftSDKsDirectory ?? options.locations.deprecatedSwiftSDKsDirectory ) diff --git a/Sources/SwiftBuildSupport/SwiftBuildSystem.swift b/Sources/SwiftBuildSupport/SwiftBuildSystem.swift index 2055628f179..76db3cd61f5 100644 --- a/Sources/SwiftBuildSupport/SwiftBuildSystem.swift +++ b/Sources/SwiftBuildSupport/SwiftBuildSystem.swift @@ -62,12 +62,13 @@ func withService( func withSession( service: SWBBuildService, name: String, + packageManagerResourcesDirectory: Basics.AbsolutePath?, body: @escaping ( _ session: SWBBuildServiceSession, _ diagnostics: [SwiftBuild.SwiftBuildMessage.DiagnosticInfo] ) async throws -> Void ) async throws { - switch await service.createSession(name: name, cachePath: nil, inferiorProductsPath: nil, environment: nil) { + switch await service.createSession(name: name, resourceSearchPaths: packageManagerResourcesDirectory.map { [$0.pathString] } ?? [], cachePath: nil, inferiorProductsPath: nil, environment: nil) { case (.success(let session), let diagnostics): do { try await body(session, diagnostics) @@ -159,6 +160,7 @@ private final class PlanningOperationDelegate: SWBPlanningOperationDelegate, Sen public final class SwiftBuildSystem: SPMBuildCore.BuildSystem { private let buildParameters: BuildParameters private let packageGraphLoader: () async throws -> ModulesGraph + private let packageManagerResourcesDirectory: Basics.AbsolutePath? private let logLevel: Basics.Diagnostic.Severity private var packageGraph: AsyncThrowingValueMemoizer = .init() private var pifBuilder: AsyncThrowingValueMemoizer = .init() @@ -209,6 +211,7 @@ public final class SwiftBuildSystem: SPMBuildCore.BuildSystem { public init( buildParameters: BuildParameters, packageGraphLoader: @escaping () async throws -> ModulesGraph, + packageManagerResourcesDirectory: Basics.AbsolutePath?, outputStream: OutputByteStream, logLevel: Basics.Diagnostic.Severity, fileSystem: FileSystem, @@ -216,6 +219,7 @@ public final class SwiftBuildSystem: SPMBuildCore.BuildSystem { ) throws { self.buildParameters = buildParameters self.packageGraphLoader = packageGraphLoader + self.packageManagerResourcesDirectory = packageManagerResourcesDirectory self.outputStream = outputStream self.logLevel = logLevel self.fileSystem = fileSystem @@ -257,7 +261,7 @@ public final class SwiftBuildSystem: SPMBuildCore.BuildSystem { ) do { - try await withSession(service: service, name: self.buildParameters.pifManifest.pathString) { session, _ in + try await withSession(service: service, name: self.buildParameters.pifManifest.pathString, packageManagerResourcesDirectory: self.packageManagerResourcesDirectory) { session, _ in self.outputStream.send("Building for \(self.buildParameters.configuration == .debug ? "debugging" : "production")...\n") // Load the workspace, and set the system information to the default diff --git a/Sources/swift-bootstrap/main.swift b/Sources/swift-bootstrap/main.swift index aaac11435eb..ce22ee20825 100644 --- a/Sources/swift-bootstrap/main.swift +++ b/Sources/swift-bootstrap/main.swift @@ -361,6 +361,7 @@ struct SwiftBootstrapBuildTool: AsyncParsableCommand { return try SwiftBuildSystem( buildParameters: buildParameters, packageGraphLoader: asyncUnsafePackageGraphLoader, + packageManagerResourcesDirectory: nil, outputStream: TSCBasic.stdoutStream, logLevel: logLevel, fileSystem: self.fileSystem, diff --git a/Utilities/bootstrap b/Utilities/bootstrap index 85c5fc30a1b..156bf002ab0 100755 --- a/Utilities/bootstrap +++ b/Utilities/bootstrap @@ -544,6 +544,11 @@ def install_swiftpm(prefix, args): dest = os.path.join(prefix, "lib", "swift", "pm", "PluginAPI") install_dylib(args, "PackagePlugin", dest, ["PackagePlugin"]) + # Install resource bundles produced during the build. + for file in os.listdir(args.bin_dir): + if file.endswith('.bundle') or file.endswith('.resources'): + install_binary(args, file, os.path.join(os.path.join(prefix, "share"), "pm")) + # Helper function that installs a dynamic library and a set of modules to a particular directory. @log_entry_exit @@ -587,7 +592,7 @@ def install_file(args, src, destination, destination_is_directory=True, ignored_ logging.info("Installing %s to %s", src, dest) if os.path.isdir(src): - shutil.copytree(src, dest, ignore=shutil.ignore_patterns(*ignored_patterns)) + shutil.copytree(src, dest, ignore=shutil.ignore_patterns(*ignored_patterns), dirs_exist_ok=True) else: shutil.copy2(src, dest) @@ -851,7 +856,6 @@ def get_swiftpm_env_cmd(args): if args.llbuild_link_framework: env_cmd.append("SWIFTPM_LLBUILD_FWK=1") - env_cmd.append("SWIFTPM_NO_SWBUILD_DEPENDENCY=1") env_cmd.append("SWIFTCI_USE_LOCAL_DEPS=1") env_cmd.append("SWIFTPM_MACOS_DEPLOYMENT_TARGET=%s" % g_macos_deployment_target)