Skip to content

Commit c7a198b

Browse files
committed
Driver: honour -no-toolchain-stdlib-rpath on Unices
Handle `-no-toolchain-stdlib-rpath` on Linux/android as well as Darwin. This enables the flags on other Unix platforms as it can be useful to control the embedded rpath for the library.
1 parent eeb7fa5 commit c7a198b

File tree

3 files changed

+39
-15
lines changed

3 files changed

+39
-15
lines changed

lib/Driver/ToolChains.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,8 @@ class LLVM_LIBRARY_VISIBILITY GenericUnix : public ToolChain {
114114
/// platforms.
115115
virtual std::string getTargetForLinker() const;
116116

117-
/// Whether to specify a linker -rpath to the Swift runtime library path.
118-
/// -rpath is not supported on all platforms, and subclasses may override
119-
/// this method to return false on platforms that don't support it. The
120-
/// default is to return true (and so specify an -rpath).
121-
virtual bool shouldProvideRPathToLinker() const;
117+
bool addRuntimeRPath(const llvm::Triple &T,
118+
const llvm::opt::ArgList &Args) const;
122119

123120
InvocationInfo constructInvocation(const DynamicLinkJobAction &job,
124121
const JobContext &context) const override;
@@ -137,8 +134,6 @@ class LLVM_LIBRARY_VISIBILITY Android : public GenericUnix {
137134
protected:
138135
std::string getTargetForLinker() const override;
139136

140-
bool shouldProvideRPathToLinker() const override;
141-
142137
public:
143138
Android(const Driver &D, const llvm::Triple &Triple)
144139
: GenericUnix(D, Triple) {}

lib/Driver/UnixToolChains.cpp

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,30 @@ std::string toolchains::GenericUnix::getTargetForLinker() const {
110110
return getTriple().str();
111111
}
112112

113-
bool toolchains::GenericUnix::shouldProvideRPathToLinker() const {
114-
return true;
113+
bool toolchains::GenericUnix::addRuntimeRPath(const llvm::Triple &T,
114+
const llvm::opt::ArgList &Args) const {
115+
// If we are building a static executable, do not add a rpath for the runtime
116+
// as it is a static binary and the loader will not be invoked.
117+
if (Args.hasFlag(options::OPT_static_executable,
118+
options::OPT_no_static_executable, false))
119+
return false;
120+
121+
// If we are building with a static standard library, do not add a rpath for
122+
// the runtime because the runtime will be part of the binary and the rpath is
123+
// no longer necessary.
124+
if (Args.hasFlag(options::OPT_static_stdlib, options::OPT_no_static_stdlib,
125+
false))
126+
return false;
127+
128+
// FIXME: We probably shouldn't be adding an rpath here unless we know ahead
129+
// of time the standard library won't be copied.
130+
131+
// Honour the user's request to add a rpath to the binary. This defaults to
132+
// `true` on non-android and `false` on android since the library must be
133+
// copied into the bundle.
134+
return Args.hasFlag(options::OPT_toolchain_stdlib_rpath,
135+
options::OPT_no_toolchain_stdlib_rpath,
136+
!T.isAndroid());
115137
}
116138

117139
ToolChain::InvocationInfo
@@ -209,9 +231,7 @@ toolchains::GenericUnix::constructInvocation(const DynamicLinkJobAction &job,
209231
getRuntimeLibraryPaths(RuntimeLibPaths, context.Args, context.OI.SDKPath,
210232
/*Shared=*/!(staticExecutable || staticStdlib));
211233

212-
if (!(staticExecutable || staticStdlib) && shouldProvideRPathToLinker()) {
213-
// FIXME: We probably shouldn't be adding an rpath here unless we know
214-
// ahead of time the standard library won't be copied.
234+
if (addRuntimeRPath(getTriple(), context.Args)) {
215235
for (auto path : RuntimeLibPaths) {
216236
Arguments.push_back("-Xlinker");
217237
Arguments.push_back("-rpath");
@@ -381,8 +401,6 @@ std::string toolchains::Android::getTargetForLinker() const {
381401
}
382402
}
383403

384-
bool toolchains::Android::shouldProvideRPathToLinker() const { return false; }
385-
386404
std::string toolchains::Cygwin::getDefaultLinker() const {
387405
// Cygwin uses the default BFD linker, even on ARM.
388406
return "";

test/Driver/linker.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
// RUN: %swiftc_driver -driver-print-jobs -target thumbv7-unknown-linux-gnueabihf -Ffoo -Fsystem car -F cdr -framework bar -Lbaz -lboo -Xlinker -undefined %s 2>&1 > %t.linux.txt
2828
// RUN: %FileCheck -check-prefix LINUX-thumbv7 %s < %t.linux.txt
2929

30-
// RUN: %swiftc_driver -driver-print-jobs -target armv7-none-linux-androideabi -Ffoo -Fsystem car -F cdr -framework bar -Lbaz -lboo -Xlinker -undefined %s 2>&1 > %t.android.txt
30+
// RUN: %swiftc_driver_plain -driver-print-jobs -target armv7-none-linux-androideabi -Ffoo -Fsystem car -F cdr -framework bar -Lbaz -lboo -Xlinker -undefined %s 2>&1 > %t.android.txt
3131
// RUN: %FileCheck -check-prefix ANDROID-armv7 %s < %t.android.txt
3232
// RUN: %FileCheck -check-prefix ANDROID-armv7-NEGATIVE %s < %t.android.txt
3333

@@ -63,6 +63,12 @@
6363

6464
// RUN: %swiftc_driver -driver-print-jobs -target x86_64-apple-macosx10.9 -g %s | %FileCheck -check-prefix DEBUG %s
6565

66+
// RUN: %swiftc_driver_plain -driver-print-jobs -target x86_64-unknown-linux-gnu -toolchain-stdlib-rpath %s 2>&1 | %FileCheck -check-prefix LINUX-STDLIB-RPATH %s
67+
// RUN: %swiftc_driver_plain -driver-print-jobs -target x86_64-unknown-linux-gnu -no-toolchain-stdlib-rpath %s 2>&1 | %FileCheck -check-prefix LINUX-NO-STDLIB-RPATH %s
68+
69+
// RUN: %swiftc_driver_plain -driver-print-jobs -target armv7-unknown-linux-androideabi -toolchain-stdlib-rpath %s 2>&1 | %FileCheck -check-prefix ANDROID-STDLIB-RPATH %s
70+
// RUN: %swiftc_driver_plain -driver-print-jobs -target armv7-unknown-linux-androideabi -no-toolchain-stdlib-rpath %s 2>&1 | %FileCheck -check-prefix ANDROID-NO-STDLIB-RPATH %s
71+
6672
// RUN: %empty-directory(%t)
6773
// RUN: touch %t/a.o
6874
// RUN: touch %t/a.swiftmodule
@@ -428,6 +434,11 @@
428434
// RELATIVE_ARCLITE: {{/|\\\\}}DISTINCTIVE-PATH{{/|\\\\}}usr{{/|\\\\}}lib{{/|\\\\}}arc{{/|\\\\}}libarclite_macosx.a
429435
// RELATIVE_ARCLITE: -o {{[^ ]+}}
430436

437+
// LINUX-STDLIB-RPATH: -Xlinker -rpath -Xlinker [[STDLIB_PATH:[^ ]+(/|\\\\)lib(/|\\\\)swift(/|\\\\)linux]]
438+
// LINUX-NO-STDLIB-RPATH-NOT: -Xlinker -rpath -Xlinker [[STDLIB_PATH:[^ ]+(/|\\\\)lib(/|\\\\)swift(/|\\\\)linux]]
439+
440+
// ANDROID-STDLIB-RPATH: -Xlinker -rpath -Xlinker [[STDLIB_PATH:[^ ]+(/|\\\\)lib(/|\\\\)swift(/|\\\\)android]]
441+
// ANDROID-NO-STDLIB-RPATH-NOT: -Xlinker -rpath -Xlinker [[STDLIB_PATH:[^ ]+(/|\\\\)lib(/|\\\\)swift(/|\\\\)android]]
431442

432443
// Clean up the test executable because hard links are expensive.
433444
// RUN: rm -rf %t/DISTINCTIVE-PATH/usr/bin/swiftc

0 commit comments

Comments
 (0)