@@ -69,6 +69,23 @@ using namespace clang::driver::tools;
6969using namespace clang ;
7070using namespace llvm ::opt;
7171
72+ static bool addRPathCmdArg (const llvm::opt::ArgList &Args,
73+ ArgStringList &CmdArgs,
74+ const std::string pathCandidate,
75+ bool onlyIfPathExists = true ) {
76+ SmallString<0 > simplifiedPathCandidate (pathCandidate);
77+ llvm::sys::path::remove_dots (simplifiedPathCandidate, true );
78+
79+ bool pathExists = llvm::sys::fs::exists (simplifiedPathCandidate);
80+
81+ if (onlyIfPathExists && !pathExists)
82+ return false ;
83+
84+ CmdArgs.push_back (" -rpath" );
85+ CmdArgs.push_back (Args.MakeArgString (simplifiedPathCandidate));
86+ return pathExists;
87+ }
88+
7289static bool useFramePointerForTargetByDefault (const llvm::opt::ArgList &Args,
7390 const llvm::Triple &Triple) {
7491 if (Args.hasArg (clang::driver::options::OPT_pg) &&
@@ -1386,12 +1403,8 @@ void tools::addOpenMPRuntimeSpecificRPath(const ToolChain &TC,
13861403 // one of the LIBRARY_PATH directories.
13871404 ArgStringList EnvLibraryPaths;
13881405 addDirectoryList (Args, EnvLibraryPaths, " " , " LIBRARY_PATH" );
1389- for (auto &EnvLibraryPath : EnvLibraryPaths) {
1390- if (llvm::sys::fs::exists (EnvLibraryPath)) {
1391- CmdArgs.push_back (" -rpath" );
1392- CmdArgs.push_back (Args.MakeArgString (EnvLibraryPath));
1393- }
1394- }
1406+ for (auto &EnvLibraryPath : EnvLibraryPaths)
1407+ addRPathCmdArg (Args, CmdArgs, EnvLibraryPath);
13951408
13961409 if (Args.hasFlag (options::OPT_fopenmp_implicit_rpath,
13971410 options::OPT_fno_openmp_implicit_rpath, true )) {
@@ -1400,46 +1413,33 @@ void tools::addOpenMPRuntimeSpecificRPath(const ToolChain &TC,
14001413 SmallString<256 > DefaultLibPath =
14011414 llvm::sys::path::parent_path (TC.getDriver ().Dir );
14021415 llvm::sys::path::append (DefaultLibPath, CLANG_INSTALL_LIBDIR_BASENAME);
1403- if (TC.getSanitizerArgs (Args).needsAsanRt ()) {
1404- CmdArgs.push_back (" -rpath" );
1405- CmdArgs.push_back (Args.MakeArgString (TC.getCompilerRTPath ()));
1406- }
1416+ if (TC.getSanitizerArgs (Args).needsAsanRt ())
1417+ addRPathCmdArg (Args, CmdArgs, TC.getCompilerRTPath (),
1418+ /* onlyIfPathExists=*/ false );
14071419
14081420 // In case LibSuffix was not built, try lib
14091421 std::string CandidateRPath_suf = D.Dir + " /../" + LibSuffix;
1410- CmdArgs.push_back (" -rpath" );
1411- CmdArgs.push_back (Args.MakeArgString (CandidateRPath_suf.c_str ()));
1412-
14131422 // Add lib directory in case LibSuffix does not exist
14141423 std::string CandidateRPath_lib = D.Dir + " /../lib" ;
1415- if ((!llvm::sys::fs::exists (CandidateRPath_suf)) &&
1416- (llvm::sys::fs::exists (CandidateRPath_lib))) {
1417- CmdArgs.push_back (" -rpath" );
1418- CmdArgs.push_back (Args.MakeArgString (CandidateRPath_lib.c_str ()));
1419- }
1424+ if (!addRPathCmdArg (Args, CmdArgs, CandidateRPath_suf,
1425+ /* onlyIfPathExists=*/ false ))
1426+ addRPathCmdArg (Args, CmdArgs, CandidateRPath_lib);
14201427
14211428 std::string rocmPath =
14221429 Args.getLastArgValue (clang::driver::options::OPT_rocm_path_EQ).str ();
14231430 if (rocmPath.size () != 0 ) {
14241431 std::string rocmPath_lib = rocmPath + " /lib" ;
14251432 std::string rocmPath_suf = rocmPath + " /" + LibSuffix;
1426- if (llvm::sys::fs::exists (rocmPath_suf)) {
1427- CmdArgs.push_back (" -rpath" );
1428- CmdArgs.push_back (Args.MakeArgString (rocmPath_suf.c_str ()));
1429- } else if (llvm::sys::fs::exists (rocmPath_lib)) {
1430- CmdArgs.push_back (" -rpath" );
1431- CmdArgs.push_back (Args.MakeArgString (rocmPath_lib.c_str ()));
1432- }
1433+ if (!addRPathCmdArg (Args, CmdArgs, rocmPath_suf))
1434+ addRPathCmdArg (Args, CmdArgs, rocmPath_lib);
14331435 }
14341436
14351437 // Add Default lib path to ensure llvm dynamic library is picked up for
14361438 // lib-debug/lib-perf
1437- if (LibSuffix != " lib" && llvm::sys::fs::exists (DefaultLibPath)){
1438- CmdArgs.push_back (" -rpath" );
1439- CmdArgs.push_back (Args.MakeArgString (DefaultLibPath.c_str ()));
1440- }
1439+ if (LibSuffix != " lib" )
1440+ addRPathCmdArg (Args, CmdArgs, DefaultLibPath.c_str ());
14411441
1442- if (llvm::find_if (CmdArgs, [](StringRef str) {
1442+ if (llvm::find_if (CmdArgs, [](StringRef str) {
14431443 return !str.compare (" --enable-new-dtags" );
14441444 }) == CmdArgs.end ())
14451445 CmdArgs.push_back (" --disable-new-dtags" );
@@ -1479,10 +1479,8 @@ void tools::addArchSpecificRPath(const ToolChain &TC, const ArgList &Args,
14791479 CandidateRPaths.emplace_back (*CandidateRPath);
14801480
14811481 for (const auto &CandidateRPath : CandidateRPaths) {
1482- if (TC.getVFS ().exists (CandidateRPath)) {
1483- CmdArgs.push_back (" -rpath" );
1484- CmdArgs.push_back (Args.MakeArgString (CandidateRPath));
1485- }
1482+ if (TC.getVFS ().exists (CandidateRPath))
1483+ addRPathCmdArg (Args, CmdArgs, CandidateRPath, /* onlyIfPathExists=*/ false );
14861484 }
14871485}
14881486
0 commit comments