@@ -110,8 +110,30 @@ std::string toolchains::GenericUnix::getTargetForLinker() const {
110
110
return getTriple ().str ();
111
111
}
112
112
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 ());
115
137
}
116
138
117
139
ToolChain::InvocationInfo
@@ -209,9 +231,7 @@ toolchains::GenericUnix::constructInvocation(const DynamicLinkJobAction &job,
209
231
getRuntimeLibraryPaths (RuntimeLibPaths, context.Args , context.OI .SDKPath ,
210
232
/* Shared=*/ !(staticExecutable || staticStdlib));
211
233
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 )) {
215
235
for (auto path : RuntimeLibPaths) {
216
236
Arguments.push_back (" -Xlinker" );
217
237
Arguments.push_back (" -rpath" );
@@ -381,8 +401,6 @@ std::string toolchains::Android::getTargetForLinker() const {
381
401
}
382
402
}
383
403
384
- bool toolchains::Android::shouldProvideRPathToLinker () const { return false ; }
385
-
386
404
std::string toolchains::Cygwin::getDefaultLinker () const {
387
405
// Cygwin uses the default BFD linker, even on ARM.
388
406
return " " ;
0 commit comments