Skip to content

Commit b2a4d5d

Browse files
Amd/dev/catmoore/rel path (llvm#4422)
Fix for SWDEV-550687 - incorrect rpath for certain Red Hat systems --------- Co-authored-by: Nicole Aschenbrenner <[email protected]>
1 parent ffd4bcc commit b2a4d5d

File tree

3 files changed

+60
-53
lines changed

3 files changed

+60
-53
lines changed

clang/lib/Driver/ToolChains/CommonArgs.cpp

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,23 @@ using namespace clang::driver::tools;
6868
using namespace clang;
6969
using 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+
7188
static 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

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,48 @@
11
// REQUIRES: amdgpu-registered-target
22

3-
// Asan-Debug: /lib-debug/asan
4-
// Asan-Devel: /lib/asan
5-
// Asan-Perf: /lib-perf/asan
6-
73
// RUN: %clang -### -fopenmp -nogpuinc -nogpulib --offload-arch=gfx90a -fopenmp-runtimelib=lib-debug %s -O3 2>&1 \
8-
// RUN: | FileCheck -check-prefixes=Debug %s
4+
// RUN: | FileCheck -check-prefixes=Debug,Debug-Rel %s
95

106
// RUN: %clang -### -fopenmp -nogpuinc -nogpulib --offload-arch=gfx90a -fopenmp-runtimelib=lib-perf %s -O3 2>&1 \
11-
// RUN: | FileCheck -check-prefixes=Perf %s
7+
// RUN: | FileCheck -check-prefixes=Perf,Perf-Rel %s
128

139
// RUN: %clang -### -fopenmp -nogpuinc -nogpulib --offload-arch=gfx90a -fopenmp-runtimelib=lib %s -O3 2>&1 \
14-
// RUN: | FileCheck -check-prefixes=Devel %s
10+
// RUN: | FileCheck -check-prefixes=Devel,Devel-Rel %s
1511

1612
// RUN: %clang -### -fopenmp -nogpuinc -nogpulib --offload-arch=gfx90a -fopenmp-target-fast %s -O3 2>&1 \
17-
// RUN: | FileCheck -check-prefixes=Default %s
13+
// RUN: | FileCheck -check-prefixes=Devel,Devel-Rel %s
1814

1915
// RUN: not %clang -### -fopenmp -nogpuinc -nogpulib --offload-arch=gfx90a -fopenmp-runtimelib=oopsy %s -O3 2>&1 \
2016
// RUN: | FileCheck -check-prefixes=Error %s
2117

2218
// RUN: %clang -### -fopenmp -nogpuinc -nogpulib --offload-arch=gfx90a:xnack+ -fopenmp-runtimelib=lib-debug -fsanitize=address -shared-libasan %s -O3 2>&1 \
23-
// RUN: | FileCheck -check-prefix=Asan-Debug %s
19+
// RUN: | FileCheck -check-prefixes=Asan-Debug,Asan-Debug-Rel %s
2420

2521
// RUN: %clang -### -fopenmp -nogpuinc -nogpulib --offload-arch=gfx90a:xnack+ -fopenmp-runtimelib=lib -fsanitize=address -shared-libasan %s -O3 2>&1 \
26-
// RUN: | FileCheck -check-prefix=Asan-Devel %s
22+
// RUN: | FileCheck -check-prefixes=Asan-Devel,Asan-Devel-Rel %s
2723

2824
// RUN: %clang -### -fopenmp -nogpuinc -nogpulib --offload-arch=gfx90a:xnack+ -fopenmp-runtimelib=lib-perf -fsanitize=address -shared-libasan %s -O3 2>&1 \
29-
// RUN: | FileCheck -check-prefix=Asan-Perf %s
25+
// RUN: | FileCheck -check-prefixes=Asan-Perf,Asan-Perf-Rel %s
3026

3127
// RUN: %clang -### -fopenmp -nogpuinc -nogpulib --offload-arch=gfx90a:xnack+ -fopenmp-target-fast -fsanitize=address -shared-libasan %s -O3 2>&1 \
32-
// RUN: | FileCheck -check-prefix=Asan-Devel %s
28+
// RUN: | FileCheck -check-prefixes=Asan-Devel,Asan-Devel-Rel %s
29+
30+
// Devel: "-rpath" "{{[^"]*}}[[LIB:(/|\\\\)lib]]"
31+
// Devel-Rel-NOT: "-rpath" "{{[^"]*(/|\\\\)\.\.}}[[LIB]]"
32+
33+
// Debug: "-rpath" "{{[^"]*}}[[LIB:(/|\\\\)lib-debug]]"
34+
// Debug-Rel-NOT: "-rpath" "{{[^"]*(/|\\\\)\.\.}}[[LIB]]"
35+
36+
// Perf: "-rpath" "{{[^"]*}}[[LIB:(/|\\\\)lib-perf]]"
37+
// Perf-Rel-NOT: "-rpath" "{{[^"]*(/|\\\\)\.\.}}[[LIB]]"
38+
39+
// Asan-Devel: "-rpath" "{{[^"]*}}[[LIB:(/|\\\\)lib(/|\\\\)asan]]"
40+
// Asan-Devel-Rel-NOT: "-rpath" "{{[^"]*(/|\\\\)\.\.}}[[LIB]]"
41+
42+
// Asan-Debug: "-rpath" "{{[^"]*}}[[LIB:(/|\\\\)lib-debug(/|\\\\)asan]]"
43+
// Asan-Debug-Rel-NOT: "-rpath" "{{[^"]*(/|\\\\)\.\.}}[[LIB]]"
44+
45+
// Asan-Perf: "-rpath" "{{[^"]*}}[[LIB:(/|\\\\)lib-perf(/|\\\\)asan]]"
46+
// Asan-Perf-Rel-NOT: "-rpath" "{{[^"]*(/|\\\\)\.\.}}[[LIB]]"
3347

34-
// Debug: /lib-debug
35-
// Perf: /lib-perf
36-
// Devel: /../lib
37-
// Default: /../lib
3848
// Error: clang: error: unsupported argument 'oopsy' to option '-fopenmp-runtimelib='

flang/test/Driver/arch-specific-libdir-rpath.f95

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@
3232
!
3333
!
3434
! RESDIR: "-resource-dir" "[[RESDIR:[^"]*]]"
35-
!
3635
! LIBPATH-X86_64: -L[[RESDIR]]{{(/|\\\\)lib(/|\\\\)linux(/|\\\\)x86_64}}
37-
! RPATH-X86_64: "-rpath" "[[RESDIR]]{{(/|\\\\)lib(/|\\\\)linux(/|\\\\)x86_64}}"
3836
!
39-
! NO-RPATH-X86_64-NOT: "-rpath" "[[RESDIR]]{{(/|\\\\)lib(/|\\\\)linux(/|\\\\)x86_64}}"
37+
! RPATH-X86_64: "-rpath" "{{[^"]*(/|\\\\)resource_dir_with_arch_subdir(/|\\\\)lib(/|\\\\)linux(/|\\\\)x86_64}}"
38+
! NO-RPATH-X86_64-NOT: "-rpath" "{{[^"]*(/|\\\\)resource_dir_with_arch_subdir(/|\\\\)lib(/|\\\\)linux(/|\\\\)x86_64}}"

0 commit comments

Comments
 (0)