Skip to content

Include Swift Build support in second stage bootstrap builds #8334

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 2 commits into from
Mar 10, 2025
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
1 change: 1 addition & 0 deletions Sources/CoreCommands/BuildSystemSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ private struct SwiftBuildSystemFactory: BuildSystemFactory {
explicitProduct: explicitProduct
)
},
packageManagerResourcesDirectory: swiftCommandState.packageManagerResourcesDirectory,
outputStream: outputStream ?? self.swiftCommandState.outputStream,
logLevel: logLevel ?? self.swiftCommandState.logLevel,
fileSystem: self.swiftCommandState.fileSystem,
Expand Down
6 changes: 6 additions & 0 deletions Sources/CoreCommands/Options.swift
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@ public struct LocationOptions: ParsableArguments {
completion: .directory
)
public var pkgConfigDirectories: [AbsolutePath] = []

@Option(
help: .init("Specify alternate path to search for SwiftPM resources.", visibility: .hidden),
completion: .directory
)
public var packageManagerResourcesDirectory: AbsolutePath?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: Should this be added to the command-line options in swift-bootstrap/main.swift too so that we can bootstrap building SwiftPM itself with the swiftbuild system someday?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Swift Build still has a fallback to the resources directory in the build directory, which I think will cover this case


@Flag(name: .customLong("ignore-lock"), help: .hidden)
public var ignoreLock: Bool = false
Expand Down
14 changes: 14 additions & 0 deletions Sources/CoreCommands/SwiftCommandState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
)
Expand Down
8 changes: 6 additions & 2 deletions Sources/SwiftBuildSupport/SwiftBuildSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@ struct SessionFailedError: Error {
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)
Expand Down Expand Up @@ -143,6 +144,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<ModulesGraph> = .init()
private var pifBuilder: AsyncThrowingValueMemoizer<PIFBuilder> = .init()
Expand Down Expand Up @@ -193,13 +195,15 @@ 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,
observabilityScope: ObservabilityScope
) throws {
self.buildParameters = buildParameters
self.packageGraphLoader = packageGraphLoader
self.packageManagerResourcesDirectory = packageManagerResourcesDirectory
self.outputStream = outputStream
self.logLevel = logLevel
self.fileSystem = fileSystem
Expand Down Expand Up @@ -241,7 +245,7 @@ public final class SwiftBuildSystem: SPMBuildCore.BuildSystem {
)

do {
try await withSession(service: service, name: buildParameters.pifManifest.pathString) { session, _ in
try await withSession(service: service, name: buildParameters.pifManifest.pathString, packageManagerResourcesDirectory: packageManagerResourcesDirectory) { session, _ in
// Load the workspace, and set the system information to the default
do {
try await session.loadWorkspace(containerPath: self.buildParameters.pifManifest.pathString)
Expand Down
1 change: 1 addition & 0 deletions Sources/swift-bootstrap/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ struct SwiftBootstrapBuildTool: AsyncParsableCommand {
return try SwiftBuildSystem(
buildParameters: buildParameters,
packageGraphLoader: asyncUnsafePackageGraphLoader,
packageManagerResourcesDirectory: nil,
outputStream: TSCBasic.stdoutStream,
logLevel: logLevel,
fileSystem: self.fileSystem,
Expand Down
6 changes: 5 additions & 1 deletion Utilities/bootstrap
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,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.
def install_dylib(args, library_name, install_dir, module_names):
Expand Down Expand Up @@ -809,7 +814,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")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thought: I wonder if the smoke test builds will pass with the dependency.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was able to get an earlier run passing, the latest test run just has minor tweaks so I think it'll work

env_cmd.append("SWIFTCI_USE_LOCAL_DEPS=1")
env_cmd.append("SWIFTPM_MACOS_DEPLOYMENT_TARGET=%s" % g_macos_deployment_target)

Expand Down