Skip to content

SwiftBuild: Fix executable can't find dynamic lib runtime error #8650

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ extension PackagePIFProjectBuilder {
case macro
}

/// Constructs a *PIF target* for building a *module* target as a particular type.
/// Constructs a *PIF target* for building a *module* as a particular type.
/// An optional target identifier suffix is passed when building variants of a target.
@discardableResult
private mutating func buildSourceModule(
Expand All @@ -245,7 +245,8 @@ extension PackagePIFProjectBuilder {

switch desiredModuleType {
case .dynamicLibrary:
if pifBuilder.createDylibForDynamicProducts { // We are re-using this default for dynamic targets as well.
// We are re-using this default for dynamic targets as well.
if pifBuilder.createDylibForDynamicProducts {
pifProductName = "lib\(sourceModule.name).dylib"
executableName = pifProductName
productType = .dynamicLibrary
Expand Down Expand Up @@ -762,18 +763,27 @@ extension PackagePIFProjectBuilder {
}
}

// Set the imparted settings, which are ones that clients (both direct and indirect ones) use.
var debugImpartedSettings = impartedSettings
debugImpartedSettings[.LD_RUNPATH_SEARCH_PATHS] =
["$(BUILT_PRODUCTS_DIR)/PackageFrameworks"] +
(debugImpartedSettings[.LD_RUNPATH_SEARCH_PATHS] ?? ["$(inherited)"])
// Set the **imparted** settings, which are ones that clients (both direct and indirect ones) use.
// For instance, given targets A, B, C with the following dependency graph:
//
// A (executable) -> B (dynamicLibrary) -> C (objectFile)
//
// An imparted build setting on C will propagate back to both B and A.
let additionalRunPaths = if productType == .framework {
["$(BUILT_PRODUCTS_DIR)/PackageFrameworks"]
Copy link
Contributor

Choose a reason for hiding this comment

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

This is changing the behavior a bit from previously since the settings are no longer being added to a separate debugImpartedSettings variable. Since $(BUILT_PRODUCTS_DIR)/PackageFrameworks is an absolute path to the build outputs, that should only be present in debug configurations, not in release configurations.

@loader_path on the other hand should be present in both places.

} else {
["@loader_path"]
}
impartedSettings[.LD_RUNPATH_SEARCH_PATHS] =
additionalRunPaths +
(impartedSettings[.LD_RUNPATH_SEARCH_PATHS] ?? ["$(inherited)"])

self.project[keyPath: sourceModuleTargetKeyPath].common.addBuildConfig { id in
BuildConfig(
id: id,
name: "Debug",
settings: debugSettings,
impartedBuildSettings: debugImpartedSettings
impartedBuildSettings: impartedSettings
)
}
self.project[keyPath: sourceModuleTargetKeyPath].common.addBuildConfig { id in
Expand Down