Skip to content

Use correct FileSystem in the PIF builder for Swift Build #8574

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
Apr 30, 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
5 changes: 3 additions & 2 deletions Sources/SwiftBuildSupport/PIFBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public final class PIFBuilder {
encoder.userInfo[.encodeForSwiftBuild] = true
}

let topLevelObject = try self.construct(buildParameters: buildParameters)
let topLevelObject = try self.constructPIF(buildParameters: buildParameters)

// Sign the PIF objects before encoding it for Swift Build.
try PIF.sign(workspace: topLevelObject.workspace)
Expand Down Expand Up @@ -139,7 +139,7 @@ public final class PIFBuilder {
private var cachedPIF: PIF.TopLevelObject?

/// Constructs a `PIF.TopLevelObject` representing the package graph.
private func construct(buildParameters: BuildParameters) throws -> PIF.TopLevelObject {
private func constructPIF(buildParameters: BuildParameters) throws -> PIF.TopLevelObject {
try memoize(to: &self.cachedPIF) {
guard let rootPackage = self.graph.rootPackages.only else {
if self.graph.rootPackages.isEmpty {
Expand All @@ -164,6 +164,7 @@ public final class PIFBuilder {
buildToolPluginResultsByTargetName: [:],
createDylibForDynamicProducts: self.parameters.shouldCreateDylibForDynamicProducts,
packageDisplayVersion: package.manifest.displayName,
fileSystem: self.fileSystem,
observabilityScope: self.observabilityScope
)

Expand Down
13 changes: 7 additions & 6 deletions Sources/SwiftBuildSupport/PackagePIFBuilder+Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@

import Foundation

import protocol TSCBasic.FileSystem
import struct TSCUtility.Version

import struct Basics.AbsolutePath
import struct Basics.Diagnostic
import let Basics.localFileSystem
import struct Basics.ObservabilityMetadata
import class Basics.ObservabilityScope
import class Basics.ObservabilitySystem
import struct Basics.RelativePath
import struct Basics.SourceControlURL
import class Basics.ObservabilityScope
import class Basics.ObservabilitySystem
import class Basics.ThreadSafeArrayStore

import enum PackageModel.BuildConfiguration
Expand Down Expand Up @@ -440,13 +440,13 @@ extension PackageGraph.ResolvedModule {
}

/// Relative path of the module-map file, if any (*only* applies to C-language modules).
var moduleMapFileRelativePath: RelativePath? {
func moduleMapFileRelativePath(fileSystem: FileSystem) -> RelativePath? {
guard let clangModule = self.underlying as? ClangModule else { return nil }
let moduleMapFileAbsolutePath = clangModule.moduleMapPath

// Check whether there is actually a modulemap at the specified path.
// FIXME: Feels wrong to do file system access at this level —— instead, libSwiftPM's TargetBuilder should do that?
guard localFileSystem.isFile(moduleMapFileAbsolutePath) else { return nil }
guard fileSystem.isFile(moduleMapFileAbsolutePath) else { return nil }

let moduleMapFileRelativePath = moduleMapFileAbsolutePath.relative(to: clangModule.sources.root)
return try! RelativePath(validating: moduleMapFileRelativePath.pathString)
Expand Down Expand Up @@ -612,6 +612,7 @@ extension SystemLibraryModule {
/// Returns pkgConfig result for a system library target.
func pkgConfig(
package: PackageGraph.ResolvedPackage,
fileSystem: FileSystem,
observabilityScope: ObservabilityScope
) throws -> (cFlags: [String], libs: [String]) {
let diagnostics = ThreadSafeArrayStore<Basics.Diagnostic>()
Expand Down Expand Up @@ -651,7 +652,7 @@ extension SystemLibraryModule {
for: self,
pkgConfigDirectories: [],
brewPrefix: brewPrefix,
fileSystem: localFileSystem,
fileSystem: fileSystem,
observabilityScope: pkgConfigParsingScope
)
guard let pkgConfigResult else { return emptyPkgConfig }
Expand Down
9 changes: 5 additions & 4 deletions Sources/SwiftBuildSupport/PackagePIFBuilder+Plugins.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@

import Foundation

import struct Basics.AbsolutePath
import let Basics.localFileSystem
import protocol TSCBasic.FileSystem

import enum Basics.Sandbox
import struct Basics.AbsolutePath
import struct Basics.SourceControlURL

#if canImport(SwiftBuild)
Expand Down Expand Up @@ -123,10 +124,10 @@ extension PackagePIFBuilder {
}

/// Applies the sandbox profile to the given command line, and return the modified command line.
public func apply(to command: [String]) throws -> [String] {
public func apply(to command: [String], fileSystem: FileSystem) throws -> [String] {
try Sandbox.apply(
command: command,
fileSystem: localFileSystem,
fileSystem: fileSystem,
strictness: self.strictness,
writableDirectories: self.writableDirectories,
readOnlyDirectories: self.readOnlyDirectories
Expand Down
17 changes: 12 additions & 5 deletions Sources/SwiftBuildSupport/PackagePIFBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,22 @@

import Foundation

import protocol TSCBasic.FileSystem

import struct Basics.AbsolutePath
import struct Basics.SourceControlURL
import struct Basics.Diagnostic
import struct Basics.ObservabilityMetadata
import class Basics.ObservabilityScope

import class PackageModel.Manifest
import class PackageModel.Package
import class PackageModel.Product
import struct PackageModel.Platform
import struct PackageModel.PlatformVersion
import class PackageModel.Product
import enum PackageModel.ProductType
import struct PackageModel.Resource
import enum PackageModel.ProductType

import struct Basics.Diagnostic
import struct Basics.ObservabilityMetadata
import class Basics.ObservabilityScope
import struct PackageGraph.ModulesGraph
import struct PackageGraph.ResolvedModule
import struct PackageGraph.ResolvedPackage
Expand Down Expand Up @@ -169,6 +171,9 @@ public final class PackagePIFBuilder {
/// Package display version, if any (i.e., it can be a version, branch or a git ref).
let packageDisplayVersion: String?

/// The file system to read from.
let fileSystem: FileSystem

/// Whether to suppress warnings from compilers, linkers, and other build tools for package dependencies.
private var suppressWarningsForPackageDependencies: Bool {
UserDefaults.standard.bool(forKey: "SuppressWarningsForPackageDependencies", defaultValue: true)
Expand All @@ -191,6 +196,7 @@ public final class PackagePIFBuilder {
buildToolPluginResultsByTargetName: [String: BuildToolPluginInvocationResult],
createDylibForDynamicProducts: Bool = false,
packageDisplayVersion: String?,
fileSystem: FileSystem,
observabilityScope: ObservabilityScope
) {
self.package = resolvedPackage
Expand All @@ -200,6 +206,7 @@ public final class PackagePIFBuilder {
self.buildToolPluginResultsByTargetName = buildToolPluginResultsByTargetName
self.createDylibForDynamicProducts = createDylibForDynamicProducts
self.packageDisplayVersion = packageDisplayVersion
self.fileSystem = fileSystem
self.observabilityScope = observabilityScope
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ extension PackagePIFProjectBuilder {

// We only need to impart this to C clients.
impartedSettings[.OTHER_CFLAGS] = ["-fmodule-map-file=\(moduleMapFile)", "$(inherited)"]
} else if sourceModule.moduleMapFileRelativePath == nil {
} else if sourceModule.moduleMapFileRelativePath(fileSystem: self.pifBuilder.fileSystem) == nil {
// Otherwise, this is a C library module and we generate a modulemap if one is already not provided.
if case .umbrellaHeader(let path) = sourceModule.moduleMapType {
log(.debug, "\(package.name).\(sourceModule.name) generated umbrella header")
Expand Down Expand Up @@ -824,6 +824,7 @@ extension PackagePIFProjectBuilder {
let settings: ProjectModel.BuildSettings = self.package.underlying.packageBaseBuildSettings
let pkgConfig = try systemLibrary.pkgConfig(
package: self.package,
fileSystem: self.pifBuilder.fileSystem,
observabilityScope: pifBuilder.observabilityScope
)

Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftBuildSupport/PackagePIFProjectBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ struct PackagePIFProjectBuilder {
) {
var commandLine = [command.executable] + command.arguments
if let sandbox = command.sandboxProfile, !pifBuilder.delegate.isPluginExecutionSandboxingDisabled {
commandLine = try! sandbox.apply(to: commandLine)
commandLine = try! sandbox.apply(to: commandLine, fileSystem: self.pifBuilder.fileSystem)
}

self.project[keyPath: targetKeyPath].customTasks.append(
Expand Down