@@ -68,6 +68,23 @@ using namespace clang::driver::tools;
6868using namespace clang ;
6969using namespace llvm ::opt;
7070
71+ static bool addRPathCmdArg (const llvm::opt::ArgList &Args,
72+ ArgStringList &CmdArgs,
73+ const std::string pathCandidate,
74+ bool onlyIfPathExists = true ) {
75+ SmallString<0 > simplifiedPathCandidate (pathCandidate);
76+ llvm::sys::path::remove_dots (simplifiedPathCandidate, true );
77+
78+ bool pathExists = llvm::sys::fs::exists (simplifiedPathCandidate);
79+
80+ if (onlyIfPathExists && !pathExists)
81+ return false ;
82+
83+ CmdArgs.push_back (" -rpath" );
84+ CmdArgs.push_back (Args.MakeArgString (simplifiedPathCandidate));
85+ return pathExists;
86+ }
87+
7188static bool useFramePointerForTargetByDefault (const llvm::opt::ArgList &Args,
7289 const llvm::Triple &Triple) {
7390 if (Args.hasArg (clang::driver::options::OPT_pg) &&
@@ -1351,12 +1368,8 @@ void tools::addOpenMPRuntimeSpecificRPath(const ToolChain &TC,
13511368 // one of the LIBRARY_PATH directories.
13521369 ArgStringList EnvLibraryPaths;
13531370 addDirectoryList (Args, EnvLibraryPaths, " " , " LIBRARY_PATH" );
1354- for (auto &EnvLibraryPath : EnvLibraryPaths) {
1355- if (llvm::sys::fs::exists (EnvLibraryPath)) {
1356- CmdArgs.push_back (" -rpath" );
1357- CmdArgs.push_back (Args.MakeArgString (EnvLibraryPath));
1358- }
1359- }
1371+ for (auto &EnvLibraryPath : EnvLibraryPaths)
1372+ addRPathCmdArg (Args, CmdArgs, EnvLibraryPath);
13601373
13611374 if (Args.hasFlag (options::OPT_fopenmp_implicit_rpath,
13621375 options::OPT_fno_openmp_implicit_rpath, true )) {
@@ -1365,46 +1378,33 @@ void tools::addOpenMPRuntimeSpecificRPath(const ToolChain &TC,
13651378 SmallString<256 > DefaultLibPath =
13661379 llvm::sys::path::parent_path (TC.getDriver ().Dir );
13671380 llvm::sys::path::append (DefaultLibPath, CLANG_INSTALL_LIBDIR_BASENAME);
1368- if (TC.getSanitizerArgs (Args).needsAsanRt ()) {
1369- CmdArgs.push_back (" -rpath" );
1370- CmdArgs.push_back (Args.MakeArgString (TC.getCompilerRTPath ()));
1371- }
1381+ if (TC.getSanitizerArgs (Args).needsAsanRt ())
1382+ addRPathCmdArg (Args, CmdArgs, TC.getCompilerRTPath (),
1383+ /* onlyIfPathExists=*/ false );
13721384
13731385 // In case LibSuffix was not built, try lib
13741386 std::string CandidateRPath_suf = D.Dir + " /../" + LibSuffix;
1375- CmdArgs.push_back (" -rpath" );
1376- CmdArgs.push_back (Args.MakeArgString (CandidateRPath_suf.c_str ()));
1377-
13781387 // Add lib directory in case LibSuffix does not exist
13791388 std::string CandidateRPath_lib = D.Dir + " /../lib" ;
1380- if ((!llvm::sys::fs::exists (CandidateRPath_suf)) &&
1381- (llvm::sys::fs::exists (CandidateRPath_lib))) {
1382- CmdArgs.push_back (" -rpath" );
1383- CmdArgs.push_back (Args.MakeArgString (CandidateRPath_lib.c_str ()));
1384- }
1389+ if (!addRPathCmdArg (Args, CmdArgs, CandidateRPath_suf,
1390+ /* onlyIfPathExists=*/ false ))
1391+ addRPathCmdArg (Args, CmdArgs, CandidateRPath_lib);
13851392
13861393 std::string rocmPath =
13871394 Args.getLastArgValue (clang::driver::options::OPT_rocm_path_EQ).str ();
13881395 if (rocmPath.size () != 0 ) {
13891396 std::string rocmPath_lib = rocmPath + " /lib" ;
13901397 std::string rocmPath_suf = rocmPath + " /" + LibSuffix;
1391- if (llvm::sys::fs::exists (rocmPath_suf)) {
1392- CmdArgs.push_back (" -rpath" );
1393- CmdArgs.push_back (Args.MakeArgString (rocmPath_suf.c_str ()));
1394- } else if (llvm::sys::fs::exists (rocmPath_lib)) {
1395- CmdArgs.push_back (" -rpath" );
1396- CmdArgs.push_back (Args.MakeArgString (rocmPath_lib.c_str ()));
1397- }
1398+ if (!addRPathCmdArg (Args, CmdArgs, rocmPath_suf))
1399+ addRPathCmdArg (Args, CmdArgs, rocmPath_lib);
13981400 }
13991401
14001402 // Add Default lib path to ensure llvm dynamic library is picked up for
14011403 // lib-debug/lib-perf
1402- if (LibSuffix != " lib" && llvm::sys::fs::exists (DefaultLibPath)){
1403- CmdArgs.push_back (" -rpath" );
1404- CmdArgs.push_back (Args.MakeArgString (DefaultLibPath.c_str ()));
1405- }
1404+ if (LibSuffix != " lib" )
1405+ addRPathCmdArg (Args, CmdArgs, DefaultLibPath.c_str ());
14061406
1407- if (llvm::find_if (CmdArgs, [](StringRef str) {
1407+ if (llvm::find_if (CmdArgs, [](StringRef str) {
14081408 return !str.compare (" --enable-new-dtags" );
14091409 }) == CmdArgs.end ())
14101410 CmdArgs.push_back (" --disable-new-dtags" );
@@ -1444,10 +1444,8 @@ void tools::addArchSpecificRPath(const ToolChain &TC, const ArgList &Args,
14441444 CandidateRPaths.emplace_back (*CandidateRPath);
14451445
14461446 for (const auto &CandidateRPath : CandidateRPaths) {
1447- if (TC.getVFS ().exists (CandidateRPath)) {
1448- CmdArgs.push_back (" -rpath" );
1449- CmdArgs.push_back (Args.MakeArgString (CandidateRPath));
1450- }
1447+ if (TC.getVFS ().exists (CandidateRPath))
1448+ addRPathCmdArg (Args, CmdArgs, CandidateRPath, /* onlyIfPathExists=*/ false );
14511449 }
14521450}
14531451
0 commit comments