@@ -78,6 +78,23 @@ using namespace clang::driver::tools;
7878using namespace clang ;
7979using namespace llvm ::opt;
8080
81+ static bool addRPathCmdArg (const llvm::opt::ArgList &Args,
82+ ArgStringList &CmdArgs,
83+ const std::string pathCandidate,
84+ bool onlyIfPathExists = true ) {
85+ SmallString<0 > simplifiedPathCandidate (pathCandidate);
86+ llvm::sys::path::remove_dots (simplifiedPathCandidate, true );
87+
88+ bool pathExists = llvm::sys::fs::exists (simplifiedPathCandidate);
89+
90+ if (onlyIfPathExists && !pathExists)
91+ return false ;
92+
93+ CmdArgs.push_back (" -rpath" );
94+ CmdArgs.push_back (Args.MakeArgString (simplifiedPathCandidate));
95+ return pathExists;
96+ }
97+
8198static bool useFramePointerForTargetByDefault (const llvm::opt::ArgList &Args,
8299 const llvm::Triple &Triple) {
83100 if (Args.hasArg (clang::driver::options::OPT_pg) &&
@@ -1261,12 +1278,8 @@ void tools::addOpenMPRuntimeSpecificRPath(const ToolChain &TC,
12611278 // one of the LIBRARY_PATH directories.
12621279 ArgStringList EnvLibraryPaths;
12631280 addDirectoryList (Args, EnvLibraryPaths, " " , " LIBRARY_PATH" );
1264- for (auto &EnvLibraryPath : EnvLibraryPaths) {
1265- if (llvm::sys::fs::exists (EnvLibraryPath)) {
1266- CmdArgs.push_back (" -rpath" );
1267- CmdArgs.push_back (Args.MakeArgString (EnvLibraryPath));
1268- }
1269- }
1281+ for (auto &EnvLibraryPath : EnvLibraryPaths)
1282+ addRPathCmdArg (Args, CmdArgs, EnvLibraryPath);
12701283
12711284 if (Args.hasFlag (options::OPT_fopenmp_implicit_rpath,
12721285 options::OPT_fno_openmp_implicit_rpath, true )) {
@@ -1275,46 +1288,33 @@ void tools::addOpenMPRuntimeSpecificRPath(const ToolChain &TC,
12751288 SmallString<256 > DefaultLibPath =
12761289 llvm::sys::path::parent_path (TC.getDriver ().Dir );
12771290 llvm::sys::path::append (DefaultLibPath, CLANG_INSTALL_LIBDIR_BASENAME);
1278- if (TC.getSanitizerArgs (Args).needsAsanRt ()) {
1279- CmdArgs.push_back (" -rpath" );
1280- CmdArgs.push_back (Args.MakeArgString (TC.getCompilerRTPath ()));
1281- }
1291+ if (TC.getSanitizerArgs (Args).needsAsanRt ())
1292+ addRPathCmdArg (Args, CmdArgs, TC.getCompilerRTPath (),
1293+ /* onlyIfPathExists=*/ false );
12821294
12831295 // In case LibSuffix was not built, try lib
12841296 std::string CandidateRPath_suf = D.Dir + " /../" + LibSuffix;
1285- CmdArgs.push_back (" -rpath" );
1286- CmdArgs.push_back (Args.MakeArgString (CandidateRPath_suf.c_str ()));
1287-
12881297 // Add lib directory in case LibSuffix does not exist
12891298 std::string CandidateRPath_lib = D.Dir + " /../lib" ;
1290- if ((!llvm::sys::fs::exists (CandidateRPath_suf)) &&
1291- (llvm::sys::fs::exists (CandidateRPath_lib))) {
1292- CmdArgs.push_back (" -rpath" );
1293- CmdArgs.push_back (Args.MakeArgString (CandidateRPath_lib.c_str ()));
1294- }
1299+ if (!addRPathCmdArg (Args, CmdArgs, CandidateRPath_suf,
1300+ /* onlyIfPathExists=*/ false ))
1301+ addRPathCmdArg (Args, CmdArgs, CandidateRPath_lib);
12951302
12961303 std::string rocmPath =
12971304 Args.getLastArgValue (clang::driver::options::OPT_rocm_path_EQ).str ();
12981305 if (rocmPath.size () != 0 ) {
12991306 std::string rocmPath_lib = rocmPath + " /lib" ;
13001307 std::string rocmPath_suf = rocmPath + " /" + LibSuffix;
1301- if (llvm::sys::fs::exists (rocmPath_suf)) {
1302- CmdArgs.push_back (" -rpath" );
1303- CmdArgs.push_back (Args.MakeArgString (rocmPath_suf.c_str ()));
1304- } else if (llvm::sys::fs::exists (rocmPath_lib)) {
1305- CmdArgs.push_back (" -rpath" );
1306- CmdArgs.push_back (Args.MakeArgString (rocmPath_lib.c_str ()));
1307- }
1308+ if (!addRPathCmdArg (Args, CmdArgs, rocmPath_suf))
1309+ addRPathCmdArg (Args, CmdArgs, rocmPath_lib);
13081310 }
13091311
13101312 // Add Default lib path to ensure llvm dynamic library is picked up for
13111313 // lib-debug/lib-perf
1312- if (LibSuffix != " lib" && llvm::sys::fs::exists (DefaultLibPath)){
1313- CmdArgs.push_back (" -rpath" );
1314- CmdArgs.push_back (Args.MakeArgString (DefaultLibPath.c_str ()));
1315- }
1314+ if (LibSuffix != " lib" )
1315+ addRPathCmdArg (Args, CmdArgs, DefaultLibPath.c_str ());
13161316
1317- if (llvm::find_if (CmdArgs, [](StringRef str) {
1317+ if (llvm::find_if (CmdArgs, [](StringRef str) {
13181318 return !str.compare (" --enable-new-dtags" );
13191319 }) == CmdArgs.end ())
13201320 CmdArgs.push_back (" --disable-new-dtags" );
@@ -1351,10 +1351,8 @@ void tools::addArchSpecificRPath(const ToolChain &TC, const ArgList &Args,
13511351 CandidateRPaths.emplace_back (*CandidateRPath);
13521352
13531353 for (const auto &CandidateRPath : CandidateRPaths) {
1354- if (TC.getVFS ().exists (CandidateRPath)) {
1355- CmdArgs.push_back (" -rpath" );
1356- CmdArgs.push_back (Args.MakeArgString (CandidateRPath));
1357- }
1354+ if (TC.getVFS ().exists (CandidateRPath))
1355+ addRPathCmdArg (Args, CmdArgs, CandidateRPath, /* onlyIfPathExists=*/ false );
13581356 }
13591357}
13601358
0 commit comments