Skip to content

Commit 2fed156

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 f8730b5 commit 2fed156

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
@@ -78,6 +78,23 @@ using namespace clang::driver::tools;
7878
using namespace clang;
7979
using 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+
8198
static 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

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)