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
12 changes: 6 additions & 6 deletions Sources/SwiftBuildSupport/PIFBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ package struct PIFBuilderParameters {
/// Add rpaths which allow loading libraries adjacent to the current image at runtime. This is desirable
/// when launching build products from the build directory, but should often be disabled when deploying
/// the build products to a different location.
let addLocalRpaths: Bool
let addLocalRpaths: PackagePIFBuilder.AddLocalRpaths

package init(isPackageAccessModifierSupported: Bool, enableTestability: Bool, shouldCreateDylibForDynamicProducts: Bool, materializeStaticArchiveProductsForRootPackages: Bool, createDynamicVariantsForLibraryProducts: Bool, toolchainLibDir: AbsolutePath, pkgConfigDirectories: [AbsolutePath], supportedSwiftVersions: [SwiftLanguageVersion], pluginScriptRunner: PluginScriptRunner, disableSandbox: Bool, pluginWorkingDirectory: AbsolutePath, additionalFileRules: [FileRuleDescription], addLocalRPaths: Bool) {
package init(isPackageAccessModifierSupported: Bool, enableTestability: Bool, shouldCreateDylibForDynamicProducts: Bool, materializeStaticArchiveProductsForRootPackages: Bool, createDynamicVariantsForLibraryProducts: Bool, toolchainLibDir: AbsolutePath, pkgConfigDirectories: [AbsolutePath], supportedSwiftVersions: [SwiftLanguageVersion], pluginScriptRunner: PluginScriptRunner, disableSandbox: Bool, pluginWorkingDirectory: AbsolutePath, additionalFileRules: [FileRuleDescription], addLocalRpaths: PackagePIFBuilder.AddLocalRpaths) {
self.isPackageAccessModifierSupported = isPackageAccessModifierSupported
self.enableTestability = enableTestability
self.shouldCreateDylibForDynamicProducts = shouldCreateDylibForDynamicProducts
Expand All @@ -96,7 +96,7 @@ package struct PIFBuilderParameters {
self.disableSandbox = disableSandbox
self.pluginWorkingDirectory = pluginWorkingDirectory
self.additionalFileRules = additionalFileRules
self.addLocalRpaths = addLocalRPaths
self.addLocalRpaths = addLocalRpaths
}
}

Expand Down Expand Up @@ -564,7 +564,7 @@ public final class PIFBuilder {
pluginWorkingDirectory: AbsolutePath,
pkgConfigDirectories: [Basics.AbsolutePath],
additionalFileRules: [FileRuleDescription],
addLocalRpaths: Bool,
addLocalRpaths: PackagePIFBuilder.AddLocalRpaths,
materializeStaticArchiveProductsForRootPackages: Bool,
createDynamicVariantsForLibraryProducts: Bool
) async throws -> PIFGenerationResult {
Expand Down Expand Up @@ -846,7 +846,7 @@ extension PIFBuilderParameters {
disableSandbox: Bool,
pluginWorkingDirectory: AbsolutePath,
additionalFileRules: [FileRuleDescription],
addLocalRpaths: Bool,
addLocalRpaths: PackagePIFBuilder.AddLocalRpaths,
materializeStaticArchiveProductsForRootPackages: Bool,
createDynamicVariantsForLibraryProducts: Bool
) {
Expand All @@ -863,7 +863,7 @@ extension PIFBuilderParameters {
disableSandbox: disableSandbox,
pluginWorkingDirectory: pluginWorkingDirectory,
additionalFileRules: additionalFileRules,
addLocalRPaths: addLocalRpaths,
addLocalRpaths: addLocalRpaths,
)
}
}
18 changes: 14 additions & 4 deletions Sources/SwiftBuildSupport/PackagePIFBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,20 @@ public final class PackagePIFBuilder {
/// Create dynamic library variants for automatic library products, for use by development-time features such as Previews and Swift Playgrounds.
let createDynamicVariantsForLibraryProducts: Bool

/// Controls whether local rpaths are imparted to package consumers and in which configurations.
public enum AddLocalRpaths: Sendable {
/// Do not add local rpaths.
case never
/// Add rpaths only in the Debug configuration.
case debugOnly
/// Add rpaths in both Debug and Release configurations.
case always
}

/// Add rpaths which allow loading libraries adjacent to the current image at runtime. This is desirable
/// when launching build products from the build directory, but should often be disabled when deploying
/// the build products to a different location.
let addLocalRpaths: Bool
/// the build products to a different location or building the release configuration.
let addLocalRpaths: AddLocalRpaths

/// Package display version, if any (i.e., it can be a version, branch or a git ref).
let packageDisplayVersion: String?
Expand Down Expand Up @@ -223,7 +233,7 @@ public final class PackagePIFBuilder {
createDylibForDynamicProducts: Bool = false,
materializeStaticArchiveProductsForRootPackages: Bool = false,
createDynamicVariantsForLibraryProducts: Bool = true,
addLocalRpaths: Bool = true,
addLocalRpaths: AddLocalRpaths = .always,
packageDisplayVersion: String?,
pkgConfigDirectories: [AbsolutePath],
fileSystem: FileSystem,
Expand Down Expand Up @@ -253,7 +263,7 @@ public final class PackagePIFBuilder {
createDylibForDynamicProducts: Bool = false,
materializeStaticArchiveProductsForRootPackages: Bool = false,
createDynamicVariantsForLibraryProducts: Bool = true,
addLocalRpaths: Bool = true,
addLocalRpaths: AddLocalRpaths = .always,
packageDisplayVersion: String?,
pkgConfigDirectories: [AbsolutePath],
fileSystem: FileSystem,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -825,17 +825,16 @@ extension PackagePIFProjectBuilder {
//
// An imparted build setting on C will propagate back to both B and A.
// FIXME: -rpath should not be given if -static is
var rpaths: [String] = []
if let existingRpaths = impartedSettings[.LD_RUNPATH_SEARCH_PATHS] {
rpaths.append(contentsOf: existingRpaths)
}
if pifBuilder.addLocalRpaths {
Comment thread
rbenegal marked this conversation as resolved.
var rpaths: [String] = impartedSettings[.LD_RUNPATH_SEARCH_PATHS] ?? []
if pifBuilder.addLocalRpaths != .never {
rpaths.append("$(RPATH_ORIGIN)")
impartedSettings[.LD_RUNPATH_SEARCH_PATHS] = rpaths + ["$(inherited)"]
if pifBuilder.addLocalRpaths == .always {
impartedSettings[.LD_RUNPATH_SEARCH_PATHS] = rpaths + ["$(inherited)"]
}
}

var impartedDebugSettings = impartedSettings
if pifBuilder.addLocalRpaths {
if pifBuilder.addLocalRpaths != .never {
Comment thread
rbenegal marked this conversation as resolved.
// FIXME: Why is this rpath only added to the debug config? We should investigate reworking this.
rpaths.append("$(BUILT_PRODUCTS_DIR)/PackageFrameworks")
impartedDebugSettings[.LD_RUNPATH_SEARCH_PATHS] = rpaths + ["$(inherited)"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ extension PackagePIFProjectBuilder {
settings[.BUILD_SERVER_PROTOCOL_TARGET_TAGS, default: ["$(inherited)"]].append("test")

// FIXME: we shouldn't always include both the deep and shallow bundle paths here, but for that we'll need rdar://31867023
if pifBuilder.addLocalRpaths {
if pifBuilder.addLocalRpaths != .never {
settings[.LD_RUNPATH_SEARCH_PATHS] = [
"$(RPATH_ORIGIN)/Frameworks",
"$(RPATH_ORIGIN)/../Frameworks",
Expand Down Expand Up @@ -1102,7 +1102,7 @@ extension PackagePIFProjectBuilder {

// A test-runner should always be adjacent to the dynamic library containing the tests,
// so add the appropriate rpaths.
if pifBuilder.addLocalRpaths {
if pifBuilder.addLocalRpaths != .never {
settings[.LD_RUNPATH_SEARCH_PATHS] = [
"$(inherited)",
"$(RPATH_ORIGIN)"
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftBuildSupport/SwiftBuildSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1300,7 +1300,7 @@ public final class SwiftBuildSystem: SPMBuildCore.BuildSystem {
disableSandbox: self.pluginConfiguration.disableSandbox,
pluginWorkingDirectory: self.pluginConfiguration.workDirectory,
additionalFileRules: additionalFileRules,
addLocalRpaths: !self.buildParameters.linkingParameters.shouldDisableLocalRpath,
addLocalRpaths: self.buildParameters.linkingParameters.shouldDisableLocalRpath ? .never : .always,
materializeStaticArchiveProductsForRootPackages: materializeStaticArchiveProductsForRootPackages,
createDynamicVariantsForLibraryProducts: false,
),
Expand Down
2 changes: 1 addition & 1 deletion Tests/SwiftBuildSupportTests/CGenPIFTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ import SwiftBuild
graph: graph,
parameters: try PIFBuilderParameters.constructDefaultParametersForTesting(
temporaryDirectory: AbsolutePath.root,
addLocalRpaths: true,
addLocalRpaths: .always,
pluginScriptRunner: pluginScriptRunner
),
fileSystem: fs,
Expand Down
63 changes: 55 additions & 8 deletions Tests/SwiftBuildSupportTests/PIFBuilderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import Workspace
extension PIFBuilderParameters {
static func constructDefaultParametersForTesting(
temporaryDirectory: Basics.AbsolutePath,
addLocalRpaths: Bool,
addLocalRpaths: PackagePIFBuilder.AddLocalRpaths,
shouldCreateDylibForDynamicProducts: Bool = false,
pluginScriptRunner: PluginScriptRunner? = nil
) throws -> Self {
Expand All @@ -49,14 +49,14 @@ extension PIFBuilderParameters {
disableSandbox: false,
pluginWorkingDirectory: temporaryDirectory.appending(component: "plugin-working-dir"),
additionalFileRules: [],
addLocalRPaths: addLocalRpaths
addLocalRpaths: addLocalRpaths
)
}
}

fileprivate func withGeneratedPIF(
fromFixture fixtureName: String,
addLocalRpaths: Bool = true,
addLocalRpaths: PackagePIFBuilder.AddLocalRpaths = .always,
shouldCreateDylibForDynamicProducts: Bool = true,
buildParameters: BuildParameters? = nil,
hostBuildParameters: BuildParameters? = nil,
Expand Down Expand Up @@ -384,7 +384,7 @@ struct PIFBuilderTests {
graph: graph,
parameters: try PIFBuilderParameters.constructDefaultParametersForTesting(
temporaryDirectory: AbsolutePath.root.appending("tmp"),
addLocalRpaths: true,
addLocalRpaths: .always,
),
fileSystem: fs,
observabilityScope: observabilityScope.topScope,
Expand Down Expand Up @@ -694,6 +694,15 @@ struct PIFBuilderTests {
$0.severity == .error
}.isEmpty)

do {
let debugConfig = try pif.workspace
.project(named: "Foo")
.target(named: "Foo")
.buildConfig(named: .debug)

#expect(debugConfig.impartedBuildProperties.settings[.LD_RUNPATH_SEARCH_PATHS] == ["$(RPATH_ORIGIN)", "$(BUILT_PRODUCTS_DIR)/PackageFrameworks", "$(inherited)"])
}

do {
let releaseConfig = try pif.workspace
.project(named: "Foo")
Expand All @@ -704,11 +713,49 @@ struct PIFBuilderTests {
}
}

try await withGeneratedPIF(fromFixture: "Miscellaneous/Simple", addLocalRpaths: false) { pif, observabilitySystem in
try await withGeneratedPIF(fromFixture: "Miscellaneous/Simple", addLocalRpaths: .never) { pif, observabilitySystem in
#expect(observabilitySystem.diagnostics.filter {
$0.severity == .error
}.isEmpty)

do {
let debugConfig = try pif.workspace
.project(named: "Foo")
.target(named: "Foo")
.buildConfig(named: .debug)

#expect(debugConfig.impartedBuildProperties.settings[.LD_RUNPATH_SEARCH_PATHS] == nil)
}

do {
let releaseConfig = try pif.workspace
.project(named: "Foo")
.target(named: "Foo")
.buildConfig(named: .release)

#expect(releaseConfig.impartedBuildProperties.settings[.LD_RUNPATH_SEARCH_PATHS] == nil)
}
}
}

@Test func debugOnlyLocalRpaths() async throws {
try await withGeneratedPIF(
fromFixture: "Miscellaneous/Simple",
addLocalRpaths: .debugOnly
) { pif, observabilitySystem in
#expect(observabilitySystem.diagnostics.filter {
$0.severity == .error
}.isEmpty)

do {
let debugConfig = try pif.workspace
.project(named: "Foo")
.target(named: "Foo")
.buildConfig(named: .debug)

#expect(debugConfig.impartedBuildProperties.settings[.LD_RUNPATH_SEARCH_PATHS] == ["$(RPATH_ORIGIN)", "$(BUILT_PRODUCTS_DIR)/PackageFrameworks", "$(inherited)"])
}

do {
let releaseConfig = try pif.workspace
.project(named: "Foo")
Expand Down Expand Up @@ -821,7 +868,7 @@ struct PIFBuilderTests {
graph: graph,
parameters: try PIFBuilderParameters.constructDefaultParametersForTesting(
temporaryDirectory: AbsolutePath.root,
addLocalRpaths: true
addLocalRpaths: .always
),
fileSystem: fs,
observabilityScope: observability.topScope
Expand Down Expand Up @@ -959,7 +1006,7 @@ struct PIFBuilderTests {
graph: graph,
parameters: try PIFBuilderParameters.constructDefaultParametersForTesting(
temporaryDirectory: AbsolutePath.root.appending("tmp"),
addLocalRpaths: true
addLocalRpaths: .always
),
fileSystem: fs,
observabilityScope: observability.topScope
Expand Down Expand Up @@ -1053,7 +1100,7 @@ struct PIFBuilderTests {
graph: graph,
parameters: try PIFBuilderParameters.constructDefaultParametersForTesting(
temporaryDirectory: AbsolutePath.root.appending("tmp"),
addLocalRpaths: true
addLocalRpaths: .always
),
fileSystem: fs,
observabilityScope: observability.topScope
Expand Down
4 changes: 2 additions & 2 deletions Tests/SwiftBuildSupportTests/PrebuiltsPIFTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ struct PrebuiltsPIFTests {
let pifBuilder: PIFBuilder = PIFBuilder(
graph: graph,
parameters: try PIFBuilderParameters.constructDefaultParametersForTesting(
temporaryDirectory: AbsolutePath.root, addLocalRpaths: true),
temporaryDirectory: AbsolutePath.root, addLocalRpaths: .always),
fileSystem: fs,
observabilityScope: observability.topScope
)
Expand Down Expand Up @@ -426,7 +426,7 @@ struct PrebuiltsPIFTests {
graph: graph,
parameters: try PIFBuilderParameters.constructDefaultParametersForTesting(
temporaryDirectory: AbsolutePath.root,
addLocalRpaths: true,
addLocalRpaths: .always,
pluginScriptRunner: MockPluginScriptRunner()
),
fileSystem: fs,
Expand Down
Loading