Skip to content

Commit 3f7c3ff

Browse files
committed
[OpenMP] Handle sysroot option in offloading linker wrapper
Summary: This patch correctly handles the `--sysroot=` option when passed to the linker wrapper. This allows users to correctly find libraries that may contain offloading code if using this option.
1 parent 758ddba commit 3f7c3ff

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ static const char *LinkerExecutable;
133133
/// Filename of the executable being created.
134134
static StringRef ExecutableName;
135135

136+
/// System root if passed in to the linker via. '--sysroot='.
137+
static StringRef Sysroot = "";
138+
136139
/// Binary path for the CUDA installation.
137140
static std::string CudaBinaryPath;
138141

@@ -169,8 +172,8 @@ void printCommands(ArrayRef<StringRef> CmdArgs) {
169172
return;
170173

171174
llvm::errs() << " \"" << CmdArgs.front() << "\" ";
172-
for (auto IC = CmdArgs.begin() + 1, IE = CmdArgs.end(); IC != IE; ++IC)
173-
llvm::errs() << *IC << (IC + 1 != IE ? " " : "\n");
175+
for (auto IC = std::next(CmdArgs.begin()), IE = CmdArgs.end(); IC != IE; ++IC)
176+
llvm::errs() << *IC << (std::next(IC) != IE ? " " : "\n");
174177
}
175178

176179
static StringRef getDeviceFileExtension(StringRef DeviceTriple,
@@ -726,10 +729,10 @@ Expected<std::string> link(ArrayRef<std::string> InputFiles, Triple TheTriple,
726729
CmdArgs.push_back(Arg);
727730
else if (Arg.startswith("-rpath")) {
728731
CmdArgs.push_back(Arg);
729-
CmdArgs.push_back(*(AI + 1));
732+
CmdArgs.push_back(*std::next(AI));
730733
} else if (Arg.startswith("-dynamic-linker")) {
731734
CmdArgs.push_back(Arg);
732-
CmdArgs.push_back(*(AI + 1));
735+
CmdArgs.push_back(*std::next(AI));
733736
}
734737
}
735738
CmdArgs.push_back("-Bsymbolic");
@@ -1157,8 +1160,11 @@ Expected<std::string> wrapDeviceImages(ArrayRef<std::string> Images) {
11571160

11581161
Optional<std::string> findFile(StringRef Dir, const Twine &Name) {
11591162
SmallString<128> Path;
1160-
// TODO: Parse `--sysroot` somewhere and use it here.
1161-
sys::path::append(Path, Dir, Name);
1163+
if (Dir.startswith("="))
1164+
sys::path::append(Path, Sysroot, Dir.substr(1), Name);
1165+
else
1166+
sys::path::append(Path, Dir, Name);
1167+
11621168
if (sys::fs::exists(Path))
11631169
return static_cast<std::string>(Path);
11641170
return None;
@@ -1229,7 +1235,13 @@ int main(int argc, const char **argv) {
12291235
if (!CudaPath.empty())
12301236
CudaBinaryPath = CudaPath + "/bin";
12311237

1232-
ExecutableName = *(llvm::find(HostLinkerArgs, "-o") + 1);
1238+
auto RootIt = llvm::find_if(HostLinkerArgs, [](StringRef Arg) {
1239+
return Arg.startswith("--sysroot=");
1240+
});
1241+
if (RootIt != HostLinkerArgs.end())
1242+
Sysroot = StringRef(*RootIt).split('=').second;
1243+
1244+
ExecutableName = *std::next(llvm::find(HostLinkerArgs, "-o"));
12331245
SmallVector<std::string, 16> LinkerArgs;
12341246
for (const std::string &Arg : HostLinkerArgs)
12351247
LinkerArgs.push_back(Arg);

0 commit comments

Comments
 (0)