Skip to content

Commit b6a1473

Browse files
committed
[driver] Refactor getRuntimePaths. NFC
This used to be getRuntimePath till https://reviews.llvm.org/D115049 added a fallback search path for Android. As far as I can tell, the intent has always been to use the first existing path though instead of actually supporting multiple runtime paths. We can move the existence checks into getRuntimePath and have it return std::optional, which also makes the `--print-runtime-dir` behavior much cleaner. The motivation is a follow-up change to Android runtime path searches, which is much nicer with this in place. Reviewed By: phosek, MaskRay Differential Revision: https://reviews.llvm.org/D158475
1 parent dcafbd0 commit b6a1473

File tree

3 files changed

+20
-22
lines changed

3 files changed

+20
-22
lines changed

clang/include/clang/Driver/ToolChain.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <cassert>
3030
#include <climits>
3131
#include <memory>
32+
#include <optional>
3233
#include <string>
3334
#include <utility>
3435

@@ -500,8 +501,8 @@ class ToolChain {
500501
StringRef Component,
501502
FileType Type = ToolChain::FT_Static) const;
502503

503-
// Returns target specific runtime paths.
504-
path_list getRuntimePaths() const;
504+
// Returns the target specific runtime path if it exists.
505+
std::optional<std::string> getRuntimePath() const;
505506

506507
// Returns target specific standard library paths.
507508
path_list getStdlibPaths() const;

clang/lib/Driver/Driver.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2163,16 +2163,8 @@ bool Driver::HandleImmediateArgs(const Compilation &C) {
21632163
}
21642164

21652165
if (C.getArgs().hasArg(options::OPT_print_runtime_dir)) {
2166-
std::string RuntimePath;
2167-
// Get the first existing path, if any.
2168-
for (auto Path : TC.getRuntimePaths()) {
2169-
if (getVFS().exists(Path)) {
2170-
RuntimePath = Path;
2171-
break;
2172-
}
2173-
}
2174-
if (!RuntimePath.empty())
2175-
llvm::outs() << RuntimePath << '\n';
2166+
if (std::optional<std::string> RuntimePath = TC.getRuntimePath())
2167+
llvm::outs() << *RuntimePath << '\n';
21762168
else
21772169
llvm::outs() << TC.getCompilerRTPath() << '\n';
21782170
return false;

clang/lib/Driver/ToolChain.cpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ ToolChain::ToolChain(const Driver &D, const llvm::Triple &T,
8686
List.push_back(Path);
8787
};
8888

89-
for (const auto &Path : getRuntimePaths())
90-
addIfExists(getLibraryPaths(), Path);
89+
if (std::optional<std::string> Path = getRuntimePath())
90+
getLibraryPaths().push_back(*Path);
9191
for (const auto &Path : getStdlibPaths())
9292
addIfExists(getFilePaths(), Path);
9393
for (const auto &Path : getArchSpecificLibPaths())
@@ -677,15 +677,18 @@ const char *ToolChain::getCompilerRTArgString(const llvm::opt::ArgList &Args,
677677
return Args.MakeArgString(getCompilerRT(Args, Component, Type));
678678
}
679679

680-
ToolChain::path_list ToolChain::getRuntimePaths() const {
681-
path_list Paths;
682-
auto addPathForTriple = [this, &Paths](const llvm::Triple &Triple) {
680+
std::optional<std::string> ToolChain::getRuntimePath() const {
681+
auto getPathForTriple =
682+
[this](const llvm::Triple &Triple) -> std::optional<std::string> {
683683
SmallString<128> P(D.ResourceDir);
684684
llvm::sys::path::append(P, "lib", Triple.str());
685-
Paths.push_back(std::string(P.str()));
685+
if (getVFS().exists(P))
686+
return std::string(P);
687+
return {};
686688
};
687689

688-
addPathForTriple(getTriple());
690+
if (auto Path = getPathForTriple(getTriple()))
691+
return *Path;
689692

690693
// When building with per target runtime directories, various ways of naming
691694
// the Arm architecture may have been normalised to simply "arm".
@@ -705,7 +708,8 @@ ToolChain::path_list ToolChain::getRuntimePaths() const {
705708
if (getTriple().getArch() == Triple::arm && !getTriple().isArmMClass()) {
706709
llvm::Triple ArmTriple = getTriple();
707710
ArmTriple.setArch(Triple::arm);
708-
addPathForTriple(ArmTriple);
711+
if (auto Path = getPathForTriple(ArmTriple))
712+
return *Path;
709713
}
710714

711715
// Android targets may include an API level at the end. We still want to fall
@@ -714,10 +718,11 @@ ToolChain::path_list ToolChain::getRuntimePaths() const {
714718
getTriple().getEnvironmentName() != "android") {
715719
llvm::Triple TripleWithoutLevel = getTriple();
716720
TripleWithoutLevel.setEnvironmentName("android");
717-
addPathForTriple(TripleWithoutLevel);
721+
if (auto Path = getPathForTriple(TripleWithoutLevel))
722+
return *Path;
718723
}
719724

720-
return Paths;
725+
return {};
721726
}
722727

723728
ToolChain::path_list ToolChain::getStdlibPaths() const {

0 commit comments

Comments
 (0)