From 5e5d21462d1ea7dbaa13bcd9bcf0156cee45b97c Mon Sep 17 00:00:00 2001 From: Manoj Gupta Date: Thu, 22 Sep 2022 13:53:46 -0700 Subject: [PATCH] BareMetal: detect usr/include/c++/v1 path in sysroot Currently baremetal driver adds /include/c++/v1 for libc++ headers. However on ChromeOS, all include files are inside /usr/include. So add /usr/include/c++/v1 if it exists in baremetal driver. Reviewed By: MaskRay Differential Revision: https://reviews.llvm.org/D134478 --- clang/lib/Driver/ToolChains/BareMetal.cpp | 14 +++++++++++--- clang/test/Driver/baremetal.cpp | 16 ++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/clang/lib/Driver/ToolChains/BareMetal.cpp b/clang/lib/Driver/ToolChains/BareMetal.cpp index 7b4b5a35205f..135843463b00 100644 --- a/clang/lib/Driver/ToolChains/BareMetal.cpp +++ b/clang/lib/Driver/ToolChains/BareMetal.cpp @@ -217,12 +217,21 @@ void BareMetal::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs, DriverArgs.hasArg(options::OPT_nostdincxx)) return; + const Driver &D = getDriver(); std::string SysRoot(computeSysRoot()); if (SysRoot.empty()) return; switch (GetCXXStdlibType(DriverArgs)) { case ToolChain::CST_Libcxx: { + // First check sysroot/usr/include/c++/v1 if it exists. + SmallString<128> TargetDir(SysRoot); + llvm::sys::path::append(TargetDir, "usr", "include", "c++", "v1"); + if (D.getVFS().exists(TargetDir)) { + addSystemInclude(DriverArgs, CC1Args, TargetDir.str()); + break; + } + // Add generic path if nothing else succeeded so far. SmallString<128> Dir(SysRoot); llvm::sys::path::append(Dir, "include", "c++", "v1"); addSystemInclude(DriverArgs, CC1Args, Dir.str()); @@ -234,9 +243,8 @@ void BareMetal::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs, std::error_code EC; Generic_GCC::GCCVersion Version = {"", -1, -1, -1, "", "", ""}; // Walk the subdirs, and find the one with the newest gcc version: - for (llvm::vfs::directory_iterator - LI = getDriver().getVFS().dir_begin(Dir.str(), EC), - LE; + for (llvm::vfs::directory_iterator LI = D.getVFS().dir_begin(Dir.str(), EC), + LE; !EC && LI != LE; LI = LI.increment(EC)) { StringRef VersionText = llvm::sys::path::filename(LI->path()); auto CandidateVersion = Generic_GCC::GCCVersion::Parse(VersionText); diff --git a/clang/test/Driver/baremetal.cpp b/clang/test/Driver/baremetal.cpp index aded91915609..b5fc45586908 100644 --- a/clang/test/Driver/baremetal.cpp +++ b/clang/test/Driver/baremetal.cpp @@ -77,6 +77,22 @@ // CHECK-V6M-NDL-SAME: "-L{{[^"]*}}{{[/\\]+}}Inputs{{[/\\]+}}baremetal_arm{{[/\\]+}}lib" // CHECK-V6M-NDL-SAME: "-L[[RESOURCE_DIR]]{{[/\\]+}}lib{{[/\\]+}}baremetal" +// RUN: rm -rf %T/baremetal_cxx_sysroot +// RUN: mkdir -p %T/baremetal_cxx_sysroot/usr/include/c++/v1 +// RUN: %clangxx %s -### 2>&1 \ +// RUN: --target=armv6m-none-eabi \ +// RUN: --sysroot=%T/baremetal_cxx_sysroot \ +// RUN: -stdlib=libc++ \ +// RUN: | FileCheck --check-prefix=CHECK-V6M-LIBCXX-USR %s +// CHECK-V6M-LIBCXX-USR: "-resource-dir" "[[RESOURCE_DIR:[^"]+]]" +// CHECK-V6M-LIBCXX-USR-NOT: "-internal-isystem" "{{[^"]+}}baremetal_cxx_sysroot{{[/\\]+}}include{{[/\\]+}}c++{{[/\\]+}}{{[^v].*}}" +// CHECK-V6M-LIBCXX-USR-SAME: "-internal-isystem" "{{[^"]+}}baremetal_cxx_sysroot{{[/\\]+}}usr{{[/\\]+}}include{{[/\\]+}}c++{{[/\\]+}}v1" +// CHECK-V6M-LIBCXX-USR: "{{[^"]*}}-Bstatic" +// CHECK-V6M-LIBCXX-USR-SAME: "-L{{[^"]*}}{{[/\\]+}}baremetal_cxx_sysroot{{[/\\]+}}lib" +// CHECK-V6M-LIBCXX-USR-SAME: "-L[[RESOURCE_DIR]]{{[/\\]+}}lib{{[/\\]+}}baremetal" +// CHECK-V6M-LIBCXX-USR-SAME: "-lc++" "-lc++abi" "-lunwind" +// CHECK-V6M-LIBCXX-USR-SAME: "-lc" "-lm" "-lclang_rt.builtins-armv6m" + // RUN: %clangxx --target=arm-none-eabi -v 2>&1 \ // RUN: | FileCheck %s --check-prefix=CHECK-THREAD-MODEL // CHECK-THREAD-MODEL: Thread model: posix