Skip to content

Commit

Permalink
[driver] Perform fallback target searches for stdlib
Browse files Browse the repository at this point in the history
Searching for target-specific standard library header and library paths
should perform fallback searches for targets, the same way searching for
the runtime libraries does. It's important for the header and library
searches to be consistent, otherwise we could end up using mismatched
headers and libraries. (See also https://reviews.llvm.org/D146664.)

Reviewed By: phosek

Differential Revision: https://reviews.llvm.org/D159293
  • Loading branch information
smeenai committed Sep 18, 2023
1 parent b1e3cd1 commit 1212d1b
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 25 deletions.
9 changes: 7 additions & 2 deletions clang/include/clang/Driver/ToolChain.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,11 @@ class ToolChain {
FileType Type,
bool AddArch) const;

/// Find the target-specific subdirectory for the current target triple under
/// \p BaseDir, doing fallback triple searches as necessary.
/// \return The subdirectory path if it exists.
std::optional<std::string> getTargetSubDirPath(StringRef BaseDir) const;

/// \name Utilities for implementing subclasses.
///@{
static void addSystemInclude(const llvm::opt::ArgList &DriverArgs,
Expand Down Expand Up @@ -504,8 +509,8 @@ class ToolChain {
// Returns the target specific runtime path if it exists.
std::optional<std::string> getRuntimePath() const;

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

// Returns <ResourceDir>/lib/<OSName>/<arch> or <ResourceDir>/lib/<triple>.
// This is used by runtimes (such as OpenMP) to find arch-specific libraries.
Expand Down
28 changes: 16 additions & 12 deletions clang/lib/Driver/ToolChain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ ToolChain::ToolChain(const Driver &D, const llvm::Triple &T,

if (std::optional<std::string> Path = getRuntimePath())
getLibraryPaths().push_back(*Path);
for (const auto &Path : getStdlibPaths())
addIfExists(getFilePaths(), Path);
if (std::optional<std::string> Path = getStdlibPath())
getFilePaths().push_back(*Path);
for (const auto &Path : getArchSpecificLibPaths())
addIfExists(getFilePaths(), Path);
}
Expand Down Expand Up @@ -677,11 +677,12 @@ const char *ToolChain::getCompilerRTArgString(const llvm::opt::ArgList &Args,
return Args.MakeArgString(getCompilerRT(Args, Component, Type));
}

std::optional<std::string> ToolChain::getRuntimePath() const {
std::optional<std::string>
ToolChain::getTargetSubDirPath(StringRef BaseDir) const {
auto getPathForTriple =
[this](const llvm::Triple &Triple) -> std::optional<std::string> {
SmallString<128> P(D.ResourceDir);
llvm::sys::path::append(P, "lib", Triple.str());
[&](const llvm::Triple &Triple) -> std::optional<std::string> {
SmallString<128> P(BaseDir);
llvm::sys::path::append(P, Triple.str());
if (getVFS().exists(P))
return std::string(P);
return {};
Expand Down Expand Up @@ -725,13 +726,16 @@ std::optional<std::string> ToolChain::getRuntimePath() const {
return {};
}

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

return Paths;
std::optional<std::string> ToolChain::getStdlibPath() const {
SmallString<128> P(D.Dir);
llvm::sys::path::append(P, "..", "lib");
return getTargetSubDirPath(P);
}

ToolChain::path_list ToolChain::getArchSpecificLibPaths() const {
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/Driver/ToolChains/Fuchsia.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,8 @@ Fuchsia::Fuchsia(const Driver &D, const llvm::Triple &Triple,

auto FilePaths = [&](const Multilib &M) -> std::vector<std::string> {
std::vector<std::string> FP;
for (const std::string &Path : getStdlibPaths()) {
SmallString<128> P(Path);
if (std::optional<std::string> Path = getStdlibPath()) {
SmallString<128> P(*Path);
llvm::sys::path::append(P, M.gccSuffix());
FP.push_back(std::string(P.str()));
}
Expand Down
17 changes: 11 additions & 6 deletions clang/lib/Driver/ToolChains/Gnu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3100,19 +3100,24 @@ Generic_GCC::addLibCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
llvm::opt::ArgStringList &CC1Args) const {
const Driver &D = getDriver();
std::string SysRoot = computeSysRoot();
std::string Target = getTripleString();

auto AddIncludePath = [&](StringRef Path, bool TargetDirRequired = false) {
std::string Version = detectLibcxxVersion(Path);
if (Version.empty())
return false;

// First add the per-target include path if it exists.
SmallString<128> TargetDir(Path);
llvm::sys::path::append(TargetDir, Target, "c++", Version);
if (D.getVFS().exists(TargetDir))
addSystemInclude(DriverArgs, CC1Args, TargetDir);
else if (TargetDirRequired)
bool TargetDirExists = false;
std::optional<std::string> TargetIncludeDir = getTargetSubDirPath(Path);
if (TargetIncludeDir) {
SmallString<128> TargetDir(*TargetIncludeDir);
llvm::sys::path::append(TargetDir, "c++", Version);
if (D.getVFS().exists(TargetDir)) {
addSystemInclude(DriverArgs, CC1Args, TargetDir);
TargetDirExists = true;
}
}
if (TargetDirRequired && !TargetDirExists)
return false;

// Second add the generic one.
Expand Down
6 changes: 3 additions & 3 deletions clang/lib/Driver/ToolChains/VEToolchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ VEToolChain::VEToolChain(const Driver &D, const llvm::Triple &Triple,
getFilePaths().clear();

// Add library directories:
// ${BINPATH}/../lib/ve-unknown-linux-gnu, (== getStdlibPaths)
// ${BINPATH}/../lib/ve-unknown-linux-gnu, (== getStdlibPath)
// ${RESOURCEDIR}/lib/linux/ve, (== getArchSpecificLibPaths)
// ${SYSROOT}/opt/nec/ve/lib,
for (auto &Path : getStdlibPaths())
getFilePaths().push_back(std::move(Path));
if (std::optional<std::string> Path = getStdlibPath())
getFilePaths().push_back(std::move(*Path));
for (const auto &Path : getArchSpecificLibPaths())
getFilePaths().push_back(Path);
getFilePaths().push_back(computeSysRoot() + "/opt/nec/ve/lib");
Expand Down
Empty file.
4 changes: 4 additions & 0 deletions clang/test/Driver/android-installed-libcxx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,9 @@
// RUN: --sysroot=%t2/sysroot -stdlib=libc++ -fsyntax-only \
// RUN: %s -### 2>&1 | FileCheck --check-prefix=ANDROID-DIR -DDIR=%/t2/bin %s

// RUN: %clang -target aarch64-none-linux-android21 -ccc-install-dir %/t2/bin \
// RUN: --sysroot=%t2/sysroot -stdlib=libc++ -fsyntax-only \
// RUN: %s -### 2>&1 | FileCheck --check-prefix=ANDROID-DIR -DDIR=%/t2/bin %s

// ANDROID-DIR: "-internal-isystem" "[[DIR]][[SEP:/|\\\\]]..[[SEP]]include[[SEP]]aarch64-none-linux-android[[SEP]]c++[[SEP]]v1"
// ANDROID-DIR-SAME: "-internal-isystem" "[[DIR]][[SEP]]..[[SEP]]include[[SEP]]c++[[SEP]]v1"
15 changes: 15 additions & 0 deletions clang/test/Driver/linux-per-target-runtime-dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@
// CHECK-PER-TARGET-RUNTIME: "--sysroot=[[SYSROOT]]"
// CHECK-PER-TARGET-RUNTIME: "-L{{.*}}{{/|\\\\}}..{{/|\\\\}}lib{{/|\\\\}}x86_64-unknown-linux-gnu"

// RUN: %clang --target=aarch64-unknown-linux-android21 -print-file-name=libc++.so 2>&1 \
// RUN: -ccc-install-dir %S/Inputs/basic_android_libcxx_tree/usr/bin \
// RUN: | FileCheck --check-prefix=CHECK-LIBCXX-ANDROID21 %s
// CHECK-LIBCXX-ANDROID21: ..{{/|\\}}lib{{/|\\}}aarch64-unknown-linux-android21{{/|\\}}libc++.so

// RUN: %clang --target=aarch64-unknown-linux-android23 -print-file-name=libc++.so 2>&1 \
// RUN: -ccc-install-dir %S/Inputs/basic_android_libcxx_tree/usr/bin \
// RUN: | FileCheck --check-prefix=CHECK-LIBCXX-ANDROID23 %s
// CHECK-LIBCXX-ANDROID23: ..{{/|\\}}lib{{/|\\}}aarch64-unknown-linux-android{{/|\\}}libc++.so

// RUN: %clang --target=aarch64-unknown-linux-android -print-file-name=libc++.so 2>&1 \
// RUN: -ccc-install-dir %S/Inputs/basic_android_libcxx_tree/usr/bin \
// RUN: | FileCheck --check-prefix=CHECK-LIBCXX-ANDROID %s
// CHECK-LIBCXX-ANDROID: ..{{/|\\}}lib{{/|\\}}aarch64-unknown-linux-android{{/|\\}}libc++.so

// RUN: %clang -rtlib=compiler-rt -print-libgcc-file-name 2>&1 \
// RUN: --target=x86_64-unknown-linux-gnu \
// RUN: -resource-dir=%S/Inputs/resource_dir_with_per_target_subdir \
Expand Down

0 comments on commit 1212d1b

Please sign in to comment.