@@ -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
117139ToolChain::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-
386404std::string toolchains::Cygwin::getDefaultLinker () const {
387405 // Cygwin uses the default BFD linker, even on ARM.
388406 return " " ;
0 commit comments