Skip to content

Commit

Permalink
[clang] Fall back on Android triple w/o API level for runtimes search
Browse files Browse the repository at this point in the history
Clang searches for runtimes (e.g. libclang_rt*) first in a
subdirectory named for the target triple (corresponding to
LLVM_ENABLE_PER_TARGET_RUNTIME_DIR=ON), then if it's not found uses
.../lib/<os>/libclang_rt* with a suffix corresponding to the arch and
environment name.

Android triples optionally include an API level indicating the minimum
Android version to be run on
(e.g. aarch64-unknown-linux-android21). When compiler-rt is built with
LLVM_ENABLE_PER_TARGET_RUNTIME_DIR=ON this API level is part of the
output path.

Linking code built for a later API level against a runtime built for
an earlier one is safe. In projects with several API level targets
this is desireable to avoid re-building the same runtimes many
times. This is difficult with the current runtime search method: if
the API levels don't exactly match Clang gives up on the per-target
runtime directory path.

To enable this more simply, this change tries target triple without
the API level before falling back on the old layout.

Another option would be to try every API level in the triple,
e.g. check aarch-64-unknown-linux-android21, then ...20, then ...19,
etc.

Differential Revision: https://reviews.llvm.org/D115049
  • Loading branch information
chbaker0 authored and nico committed Jan 5, 2022
1 parent 4ca5e95 commit 7e08a12
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 27 deletions.
8 changes: 4 additions & 4 deletions clang/include/clang/Driver/ToolChain.h
Expand Up @@ -452,11 +452,11 @@ class ToolChain {
StringRef Component,
FileType Type = ToolChain::FT_Static) const;

// Returns target specific runtime path if it exists.
virtual std::string getRuntimePath() const;
// Returns target specific runtime paths.
path_list getRuntimePaths() const;

// Returns target specific standard library path if it exists.
virtual std::string getStdlibPath() const;
// Returns target specific standard library paths.
path_list getStdlibPaths() const;

// Returns <ResourceDir>/lib/<OSName>/<arch>. This is used by runtimes (such
// as OpenMP) to find arch-specific libraries.
Expand Down
13 changes: 10 additions & 3 deletions clang/lib/Driver/Driver.cpp
Expand Up @@ -1869,9 +1869,16 @@ bool Driver::HandleImmediateArgs(const Compilation &C) {
}

if (C.getArgs().hasArg(options::OPT_print_runtime_dir)) {
std::string CandidateRuntimePath = TC.getRuntimePath();
if (getVFS().exists(CandidateRuntimePath))
llvm::outs() << CandidateRuntimePath << '\n';
std::string RuntimePath;
// Get the first existing path, if any.
for (auto Path : TC.getRuntimePaths()) {
if (getVFS().exists(Path)) {
RuntimePath = Path;
break;
}
}
if (!RuntimePath.empty())
llvm::outs() << RuntimePath << '\n';
else
llvm::outs() << TC.getCompilerRTPath() << '\n';
return false;
Expand Down
50 changes: 34 additions & 16 deletions clang/lib/Driver/ToolChain.cpp
Expand Up @@ -75,17 +75,16 @@ ToolChain::ToolChain(const Driver &D, const llvm::Triple &T,
const ArgList &Args)
: D(D), Triple(T), Args(Args), CachedRTTIArg(GetRTTIArgument(Args)),
CachedRTTIMode(CalculateRTTIMode(Args, Triple, CachedRTTIArg)) {
std::string RuntimePath = getRuntimePath();
if (getVFS().exists(RuntimePath))
getLibraryPaths().push_back(RuntimePath);

std::string StdlibPath = getStdlibPath();
if (getVFS().exists(StdlibPath))
getFilePaths().push_back(StdlibPath);
auto addIfExists = [this](path_list &List, const std::string &Path) {
if (getVFS().exists(Path))
List.push_back(Path);
};

std::string CandidateLibPath = getArchSpecificLibPath();
if (getVFS().exists(CandidateLibPath))
getFilePaths().push_back(CandidateLibPath);
for (const auto &Path : getRuntimePaths())
addIfExists(getLibraryPaths(), Path);
for (const auto &Path : getStdlibPaths())
addIfExists(getFilePaths(), Path);
addIfExists(getFilePaths(), getArchSpecificLibPath());
}

void ToolChain::setTripleEnvironment(llvm::Triple::EnvironmentType Env) {
Expand Down Expand Up @@ -485,16 +484,35 @@ const char *ToolChain::getCompilerRTArgString(const llvm::opt::ArgList &Args,
return Args.MakeArgString(getCompilerRT(Args, Component, Type));
}

std::string ToolChain::getRuntimePath() const {
SmallString<128> P(D.ResourceDir);
llvm::sys::path::append(P, "lib", getTripleString());
return std::string(P.str());
ToolChain::path_list ToolChain::getRuntimePaths() const {
path_list Paths;
auto addPathForTriple = [this, &Paths](const llvm::Triple &Triple) {
SmallString<128> P(D.ResourceDir);
llvm::sys::path::append(P, "lib", Triple.str());
Paths.push_back(std::string(P.str()));
};

addPathForTriple(getTriple());

// Android targets may include an API level at the end. We still want to fall
// back on a path without the API level.
if (getTriple().isAndroid() &&
getTriple().getEnvironmentName() != "android") {
llvm::Triple TripleWithoutLevel = getTriple();
TripleWithoutLevel.setEnvironmentName("android");
addPathForTriple(TripleWithoutLevel);
}

return Paths;
}

std::string ToolChain::getStdlibPath() const {
ToolChain::path_list ToolChain::getStdlibPaths() const {
path_list Paths;
SmallString<128> P(D.Dir);
llvm::sys::path::append(P, "..", "lib", getTripleString());
return std::string(P.str());
Paths.push_back(std::string(P.str()));

return Paths;
}

std::string ToolChain::getArchSpecificLibPath() const {
Expand Down
8 changes: 5 additions & 3 deletions clang/lib/Driver/ToolChains/Fuchsia.cpp
Expand Up @@ -191,9 +191,11 @@ Fuchsia::Fuchsia(const Driver &D, const llvm::Triple &Triple,

auto FilePaths = [&](const Multilib &M) -> std::vector<std::string> {
std::vector<std::string> FP;
SmallString<128> P(getStdlibPath());
llvm::sys::path::append(P, M.gccSuffix());
FP.push_back(std::string(P.str()));
for (const std::string &Path : getStdlibPaths()) {
SmallString<128> P(Path);
llvm::sys::path::append(P, M.gccSuffix());
FP.push_back(std::string(P.str()));
}
return FP;
};

Expand Down
3 changes: 2 additions & 1 deletion clang/lib/Driver/ToolChains/VEToolchain.cpp
Expand Up @@ -48,7 +48,8 @@ VEToolChain::VEToolChain(const Driver &D, const llvm::Triple &Triple,
// ${BINPATH}/../lib/ve-unknown-linux-gnu, (== getStdlibPath)
// ${RESOURCEDIR}/lib/linux/ve, (== getArchSpecificLibPath)
// ${SYSROOT}/opt/nec/ve/lib,
getFilePaths().push_back(getStdlibPath());
for (auto &Path : getStdlibPaths())
getFilePaths().push_back(std::move(Path));
getFilePaths().push_back(getArchSpecificLibPath());
getFilePaths().push_back(computeSysRoot() + "/opt/nec/ve/lib");
}
Expand Down
18 changes: 18 additions & 0 deletions clang/test/Driver/linux-per-target-runtime-dir.c
Expand Up @@ -25,3 +25,21 @@
// RUN: -resource-dir=%S/Inputs/resource_dir_with_per_target_subdir \
// RUN: | FileCheck --check-prefix=CHECK-FILE-NAME-X8664 %s
// CHECK-FILE-NAME-X8664: lib{{/|\\}}x86_64-unknown-linux-gnu{{/|\\}}libclang_rt.builtins.a

// RUN: %clang -rtlib=compiler-rt -print-file-name=libclang_rt.builtins.a 2>&1 \
// RUN: --target=aarch64-unknown-linux-android21 \
// RUN: -resource-dir=%S/Inputs/resource_dir_with_per_target_subdir \
// RUN: | FileCheck --check-prefix=CHECK-FILE-NAME-ANDROID21 %s
// CHECK-FILE-NAME-ANDROID21: lib{{/|\\}}aarch64-unknown-linux-android21{{/|\\}}libclang_rt.builtins.a

// RUN: %clang -rtlib=compiler-rt -print-file-name=libclang_rt.builtins.a 2>&1 \
// RUN: --target=aarch64-unknown-linux-android23 \
// RUN: -resource-dir=%S/Inputs/resource_dir_with_per_target_subdir \
// RUN: | FileCheck --check-prefix=CHECK-FILE-NAME-ANDROID23 %s
// CHECK-FILE-NAME-ANDROID23: lib{{/|\\}}aarch64-unknown-linux-android{{/|\\}}libclang_rt.builtins.a

// RUN: %clang -rtlib=compiler-rt -print-file-name=libclang_rt.builtins.a 2>&1 \
// RUN: --target=aarch64-unknown-linux-android \
// RUN: -resource-dir=%S/Inputs/resource_dir_with_per_target_subdir \
// RUN: | FileCheck --check-prefix=CHECK-FILE-NAME-ANDROID %s
// CHECK-FILE-NAME-ANDROID: lib{{/|\\}}aarch64-unknown-linux-android{{/|\\}}libclang_rt.builtins.a

0 comments on commit 7e08a12

Please sign in to comment.