Skip to content
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

[SR-2048] Look for libs in the same path of the executable #5419

Closed
swift-ci opened this issue Jul 11, 2016 · 12 comments
Closed

[SR-2048] Look for libs in the same path of the executable #5419

swift-ci opened this issue Jul 11, 2016 · 12 comments

Comments

@swift-ci
Copy link
Contributor

Previous ID SR-2048
Radar rdar://problem/40205646
Original Reporter paulofaria (JIRA User)
Type Improvement
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 1
Component/s Package Manager
Labels Improvement
Assignee None
Priority Medium

md5: 9912c89cdfeb36318e9252e969668d17

relates to:

  • SR-1968 [SwiftPM] Need support for managing RPATH entries w.r.t. distribution

Issue Description:

Suppose I have a binary quark built with SPM. If I run:

otool -L quark

I get:

quark:
/usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1237.0.0)
/Users/travis/build/QuarkX/Tool/.build/release/libCLibvenice.dylib (compatibility version 0.0.0, current version 0.0.0)
@rpath/libswiftCore.dylib (compatibility version 0.0.0, current version 0.0.0)
@rpath/libswiftDarwin.dylib (compatibility version 0.0.0, current version 0.0.0)

I would like to have:

/Users/travis/build/QuarkX/Tool/.build/release/libCLibvenice.dylib (compatibility version 0.0.0, current version 0.0.0)

be:

@rpath/libCLibvenice.dylib (compatibility version 0.0.0, current version 0.0.0)

and have @rpath contain the directory in which the executable is on. So I just copy libCLibvenice.dylib to the same directory of the quark executable, and everything works.

Thanks!

@ddunbar

@ddunbar
Copy link
Member

ddunbar commented Jul 11, 2016

CC @abertelrud

@aciidb0mb3r do you know if we have another bug on this?

I think that for now we should probably default to building everything so that it works if it is relocated (so no absolute paths). One tricky thing about this is how to handle the fact that usually libraries would be put somewhere other than executables.

Usually, on Darwin, you might end up with something like -rpath @loader_path/../lib in the main executable to allow finding libraries relative to a ../lib/ install location. But if we build into debug/ during iterative development that won't work, and we would either need an extra rpath (which is wasteful, and potentially a security concern), or we would need to change how we lay out the built products (which would be my vote).

In general, we need to clean up this area.

@ankitspd
Copy link
Member

I think rpath is being added automatically on Darwin? I remember adding rpath only for linux because it wasn't a problem on Darwin. Explicitly setting rpath to look in current directory should fix this.

I agree we should change how we layout products in the build folder, it'll probably also be a step towards supporting installable packages.

@ankitspd
Copy link
Member

I tried on Darwin and it doesn't add absolute path if passed -install_name @rpath/libname.dylib while creating the shared library.
Is that the correct fix?

@abertelrud
Copy link
Contributor

@ddunbar, do you think this is still in scope for Swift 3.0? It feels late to me to be addressing this now, though it does seem like an important issue to fix.

@abertelrud
Copy link
Contributor

Out of time for Swift 3.

@ankitspd
Copy link
Member

@swift-ci create

@ankitspd
Copy link
Member

This was done at some point.

@krzyzanowskim
Copy link
Contributor

I think this issue still exists. For example repo: https://github.com/apple/sourcekit-lsp

after build with most recent SPM, the path to libSwiftPM.dylib is hardcoded

$ otool -L .build/x86_64-apple-macosx/release/sourcekit-lsp
.build/x86_64-apple-macosx/release/sourcekit-lsp:
    /usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1252.200.5)
    /usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 400.9.4)
    /Users/marcinkrzyzanowski/Devel/swift-source/sourcekit-lsp/.build/x86_64-apple-macosx/release/libSwiftPM.dylib (compatibility version 0.0.0, current version 0.0.0)

isn't it the same issue?

@swift-ci
Copy link
Contributor Author

Comment by Jeremy David Giesbrecht (JIRA)

Yes. This is still an issue in Swift 5.0.1. (But only on macOS; Linux has never been a problem.)

mkdir Demonstration
cd Demonstration
swift package init --type executable

rm -rf Package.swift
echo '// swift-tools-version:5.0
import PackageDescription
let package = Package(
    name: "Demonstration",
    dependencies: [.package(url: "https://github.com/apple/swift-package-manager", .exact("0.4.0"))],
    targets: [.target(name: "Demonstration", dependencies: ["SwiftPM"])]
)' >> Package.swift

rm -rf Sources/Demonstration/main.swift
echo 'import Workspace
print(Workspace.self)' >> Sources/Demonstration/main.swift

swift build -c release

.build/x86_64-apple-macosx/release/Demonstration
# Workspace
# (Succeeded)

mv .build/x86_64-apple-macosx/release Install
Install/Demonstration
# dyld: Library not loaded: /Users/[...]/Demonstration/.build/x86_64-apple-macosx/release/libSwiftPM.dylib
#   Referenced from: /Users/[...]/Demonstration/Install/Demonstration
#   Reason: image not found
# Abort trap: 6

@swift-ci
Copy link
Contributor Author

Comment by Jeremy David Giesbrecht (JIRA)

It looks like fixing it would be as simple as removing the if statement here:

// On linux, set rpath such that dynamic libraries are looked up
// adjacent to the product. This happens by default on macOS.
if buildParameters.triple.isLinux() {
args += ["-Xlinker", "-rpath=$ORIGIN"]
}

(It clearly is not happening by default as the comment suggests.)

Thoughts, @aciidb0mb3r?

@swift-ci
Copy link
Contributor Author

Comment by Jeremy David Giesbrecht (JIRA)

I just submitted a PR: #2351

@swift-ci
Copy link
Contributor Author

Comment by Jeremy David Giesbrecht (JIRA)

The PR has been merged. This is now “resolved”, “re‐resolved”, “more fully resolved”, or something to that effect.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 4, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants