From 1a2f4824cb2d472649e65f845510ac0e47ca98c1 Mon Sep 17 00:00:00 2001 From: Shuhong Liu Date: Tue, 7 Jul 2020 11:10:15 -0400 Subject: [PATCH] [Clang] Handle AIX Include management in the driver Summary: Modify the AIX clang toolchain to include AIX dependencies in the search path Reviewers: daltenty, stevewan, hubert.reinterpretcast Reviewed By: daltenty, stevewan, hubert.reinterpretcast Subscribers: ormris, hubert.reinterpretcast, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D82677 --- clang/lib/Driver/ToolChains/AIX.cpp | 40 ++++++ clang/lib/Driver/ToolChains/AIX.h | 7 + clang/lib/Frontend/InitHeaderSearch.cpp | 2 + clang/test/Driver/aix-toolchain-include.cpp | 136 ++++++++++++++++++++ 4 files changed, 185 insertions(+) create mode 100644 clang/test/Driver/aix-toolchain-include.cpp diff --git a/clang/lib/Driver/ToolChains/AIX.cpp b/clang/lib/Driver/ToolChains/AIX.cpp index 0194f452111a..ac5544eedb00 100644 --- a/clang/lib/Driver/ToolChains/AIX.cpp +++ b/clang/lib/Driver/ToolChains/AIX.cpp @@ -13,12 +13,15 @@ #include "clang/Driver/Options.h" #include "clang/Driver/SanitizerArgs.h" #include "llvm/Option/ArgList.h" +#include "llvm/Support/Path.h" using AIX = clang::driver::toolchains::AIX; using namespace clang::driver; using namespace clang::driver::tools; +using namespace clang::driver::toolchains; using namespace llvm::opt; +using namespace llvm::sys; void aix::Assembler::ConstructJob(Compilation &C, const JobAction &JA, const InputInfo &Output, @@ -163,6 +166,43 @@ AIX::AIX(const Driver &D, const llvm::Triple &Triple, const ArgList &Args) getFilePaths().push_back(getDriver().SysRoot + "/usr/lib"); } +// Returns the effective header sysroot path to use. +// This comes from either -isysroot or --sysroot. +llvm::StringRef +AIX::GetHeaderSysroot(const llvm::opt::ArgList &DriverArgs) const { + if (DriverArgs.hasArg(options::OPT_isysroot)) + return DriverArgs.getLastArgValue(options::OPT_isysroot); + if (!getDriver().SysRoot.empty()) + return getDriver().SysRoot; + return "/"; +} + +void AIX::AddClangSystemIncludeArgs(const ArgList &DriverArgs, + ArgStringList &CC1Args) const { + // Return if -nostdinc is specified as a driver option. + if (DriverArgs.hasArg(options::OPT_nostdinc)) + return; + + llvm::StringRef Sysroot = GetHeaderSysroot(DriverArgs); + const Driver &D = getDriver(); + + // Add the Clang builtin headers (/include). + if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) { + SmallString<128> P(D.ResourceDir); + path::append(P, "/include"); + addSystemInclude(DriverArgs, CC1Args, P.str()); + } + + // Return if -nostdlibinc is specified as a driver option. + if (DriverArgs.hasArg(options::OPT_nostdlibinc)) + return; + + // Add /usr/include. + SmallString<128> UP(Sysroot); + path::append(UP, "/usr/include"); + addSystemInclude(DriverArgs, CC1Args, UP.str()); +} + auto AIX::buildAssembler() const -> Tool * { return new aix::Assembler(*this); } auto AIX::buildLinker() const -> Tool * { return new aix::Linker(*this); } diff --git a/clang/lib/Driver/ToolChains/AIX.h b/clang/lib/Driver/ToolChains/AIX.h index 69b948bc0ea8..942bb3cceb8a 100644 --- a/clang/lib/Driver/ToolChains/AIX.h +++ b/clang/lib/Driver/ToolChains/AIX.h @@ -63,9 +63,16 @@ public: bool isPIEDefault() const override { return false; } bool isPICDefaultForced() const override { return true; } + void + AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs, + llvm::opt::ArgStringList &CC1Args) const override; + protected: Tool *buildAssembler() const override; Tool *buildLinker() const override; + +private: + llvm::StringRef GetHeaderSysroot(const llvm::opt::ArgList &DriverArgs) const; }; } // end namespace toolchains diff --git a/clang/lib/Frontend/InitHeaderSearch.cpp b/clang/lib/Frontend/InitHeaderSearch.cpp index 159cd47f7831..16f1f1670e8d 100644 --- a/clang/lib/Frontend/InitHeaderSearch.cpp +++ b/clang/lib/Frontend/InitHeaderSearch.cpp @@ -381,6 +381,7 @@ void InitHeaderSearch::AddDefaultCPlusPlusIncludePaths( case llvm::Triple::Linux: case llvm::Triple::Hurd: case llvm::Triple::Solaris: + case llvm::Triple::AIX: llvm_unreachable("Include management is handled in the driver."); break; case llvm::Triple::Win32: @@ -424,6 +425,7 @@ void InitHeaderSearch::AddDefaultIncludePaths(const LangOptions &Lang, case llvm::Triple::Hurd: case llvm::Triple::Solaris: case llvm::Triple::WASI: + case llvm::Triple::AIX: return; case llvm::Triple::Win32: diff --git a/clang/test/Driver/aix-toolchain-include.cpp b/clang/test/Driver/aix-toolchain-include.cpp new file mode 100644 index 000000000000..dc8b272936a4 --- /dev/null +++ b/clang/test/Driver/aix-toolchain-include.cpp @@ -0,0 +1,136 @@ +// Tests that the AIX toolchain adds system includes to its search path. + +// Check powerpc-ibm-aix, 32-bit/64-bit. +// RUN: %clangxx -### -no-canonical-prefixes %s 2>&1 \ +// RUN: -target powerpc-ibm-aix \ +// RUN: -resource-dir=%S/Inputs/resource_dir \ +// RUN: --sysroot=%S/Inputs/basic_aix_tree \ +// RUN: | FileCheck -check-prefix=CHECK-INTERNAL-INCLUDE %s + +// RUN: %clangxx -### -no-canonical-prefixes %s 2>&1 \ +// RUN: -target powerpc64-ibm-aix \ +// RUN: -resource-dir=%S/Inputs/resource_dir \ +// RUN: --sysroot=%S/Inputs/basic_aix_tree \ +// RUN: | FileCheck -check-prefix=CHECK-INTERNAL-INCLUDE %s + +// RUN: %clang -### -xc -no-canonical-prefixes %s 2>&1 \ +// RUN: -target powerpc-ibm-aix \ +// RUN: -resource-dir=%S/Inputs/resource_dir \ +// RUN: --sysroot=%S/Inputs/basic_aix_tree \ +// RUN: | FileCheck -check-prefix=CHECK-INTERNAL-INCLUDE %s + +// RUN: %clang -### -xc -no-canonical-prefixes %s 2>&1 \ +// RUN: -target powerpc64-ibm-aix \ +// RUN: -resource-dir=%S/Inputs/resource_dir \ +// RUN: --sysroot=%S/Inputs/basic_aix_tree \ +// RUN: | FileCheck -check-prefix=CHECK-INTERNAL-INCLUDE %s + +// CHECK-INTERNAL-INCLUDE: {{.*}}clang{{.*}}" "-cc1" +// CHECK-INTERNAL-INCLUDE: "-resource-dir" "[[RESOURCE_DIR:[^"]+]]" +// CHECK-INTERNAL-INCLUDE: "-isysroot" "[[SYSROOT:[^"]+]]" +// CHECK-INTERNAL-INCLUDE: "-internal-isystem" "[[RESOURCE_DIR]]{{(/|\\\\)}}include" +// CHECK-INTERNAL-INCLUDE: "-internal-isystem" "[[SYSROOT]]/usr/include" + +// Check powerpc-ibm-aix, 32-bit/64-bit. -nostdinc option. +// RUN: %clangxx -### -no-canonical-prefixes %s 2>&1 \ +// RUN: -target powerpc-ibm-aix \ +// RUN: -resource-dir=%S/Inputs/resource_dir \ +// RUN: --sysroot=%S/Inputs/basic_aix_tree \ +// RUN: -nostdinc \ +// RUN: | FileCheck -check-prefix=CHECK-NOSTDINC-INCLUDE %s + +// RUN: %clangxx -### -no-canonical-prefixes %s 2>&1 \ +// RUN: -target powerpc64-ibm-aix \ +// RUN: -resource-dir=%S/Inputs/resource_dir \ +// RUN: --sysroot=%S/Inputs/basic_aix_tree \ +// RUN: -nostdinc \ +// RUN: | FileCheck -check-prefix=CHECK-NOSTDINC-INCLUDE %s + +// RUN: %clang -### -xc -no-canonical-prefixes %s 2>&1 \ +// RUN: -target powerpc-ibm-aix \ +// RUN: -resource-dir=%S/Inputs/resource_dir \ +// RUN: --sysroot=%S/Inputs/basic_aix_tree \ +// RUN: -nostdinc \ +// RUN: | FileCheck -check-prefix=CHECK-NOSTDINC-INCLUDE %s + +// RUN: %clang -### -xc -no-canonical-prefixes %s 2>&1 \ +// RUN: -target powerpc64-ibm-aix \ +// RUN: -resource-dir=%S/Inputs/resource_dir \ +// RUN: --sysroot=%S/Inputs/basic_aix_tree \ +// RUN: -nostdinc \ +// RUN: | FileCheck -check-prefix=CHECK-NOSTDINC-INCLUDE %s + +// CHECK-NOSTDINC-INCLUDE: {{.*}}clang{{.*}}" "-cc1" +// CHECK-NOSTDINC-INCLUDE: "-resource-dir" "[[RESOURCE_DIR:[^"]+]]" +// CHECK-NOSTDINC-INCLUDE: "-isysroot" "[[SYSROOT:[^"]+]]" +// CHECK-NOSTDINC-INCLUDE-NOT: "-internal-isystem" + +// Check powerpc-ibm-aix, 32-bit/64-bit. -nostdlibinc option. +// RUN: %clangxx -### -no-canonical-prefixes %s 2>&1 \ +// RUN: -target powerpc-ibm-aix \ +// RUN: -resource-dir=%S/Inputs/resource_dir \ +// RUN: --sysroot=%S/Inputs/basic_aix_tree \ +// RUN: -nostdlibinc \ +// RUN: | FileCheck -check-prefix=CHECK-NOSTDLIBINC-INCLUDE %s + +// RUN: %clangxx -### -no-canonical-prefixes %s 2>&1 \ +// RUN: -target powerpc64-ibm-aix \ +// RUN: -resource-dir=%S/Inputs/resource_dir \ +// RUN: --sysroot=%S/Inputs/basic_aix_tree \ +// RUN: -nostdlibinc \ +// RUN: | FileCheck -check-prefix=CHECK-NOSTDLIBINC-INCLUDE %s + +// RUN: %clang -### -xc -no-canonical-prefixes %s 2>&1 \ +// RUN: -target powerpc-ibm-aix \ +// RUN: -resource-dir=%S/Inputs/resource_dir \ +// RUN: --sysroot=%S/Inputs/basic_aix_tree \ +// RUN: -nostdlibinc \ +// RUN: | FileCheck -check-prefix=CHECK-NOSTDLIBINC-INCLUDE %s + +// RUN: %clang -### -xc -no-canonical-prefixes %s 2>&1 \ +// RUN: -target powerpc64-ibm-aix \ +// RUN: -resource-dir=%S/Inputs/resource_dir \ +// RUN: --sysroot=%S/Inputs/basic_aix_tree \ +// RUN: -nostdlibinc \ +// RUN: | FileCheck -check-prefix=CHECK-NOSTDLIBINC-INCLUDE %s + +// CHECK-NOSTDLIBINC-INCLUDE: {{.*}}clang{{.*}}" "-cc1" +// CHECK-NOSTDLIBINC-INCLUDE: "-resource-dir" "[[RESOURCE_DIR:[^"]+]]" +// CHECK-NOSTDLIBINC-INCLUDE: "-isysroot" "[[SYSROOT:[^"]+]]" +// CHECK-NOSTDLIBINC-INCLUDE: "-internal-isystem" "[[RESOURCE_DIR]]{{(/|\\\\)}}include" +// CHECK-NOSTDLIBINC-INCLUDE-NOT: "-internal-isystem" "[[SYSROOT]]/usr/include" + +// Check powerpc-ibm-aix, 32-bit/64-bit. -nobuiltininc option. +// RUN: %clangxx -### -no-canonical-prefixes %s 2>&1 \ +// RUN: -target powerpc-ibm-aix \ +// RUN: -resource-dir=%S/Inputs/resource_dir \ +// RUN: --sysroot=%S/Inputs/basic_aix_tree \ +// RUN: -nobuiltininc \ +// RUN: | FileCheck -check-prefix=CHECK-NOBUILTININC-INCLUDE %s + +// RUN: %clangxx -### -no-canonical-prefixes %s 2>&1 \ +// RUN: -target powerpc64-ibm-aix \ +// RUN: -resource-dir=%S/Inputs/resource_dir \ +// RUN: --sysroot=%S/Inputs/basic_aix_tree \ +// RUN: -nobuiltininc \ +// RUN: | FileCheck -check-prefix=CHECK-NOBUILTININC-INCLUDE %s + +// RUN: %clang -### -xc -no-canonical-prefixes %s 2>&1 \ +// RUN: -target powerpc-ibm-aix \ +// RUN: -resource-dir=%S/Inputs/resource_dir \ +// RUN: --sysroot=%S/Inputs/basic_aix_tree \ +// RUN: -nobuiltininc \ +// RUN: | FileCheck -check-prefix=CHECK-NOBUILTININC-INCLUDE %s + +// RUN: %clang -### -xc -no-canonical-prefixes %s 2>&1 \ +// RUN: -target powerpc64-ibm-aix \ +// RUN: -resource-dir=%S/Inputs/resource_dir \ +// RUN: --sysroot=%S/Inputs/basic_aix_tree \ +// RUN: -nobuiltininc \ +// RUN: | FileCheck -check-prefix=CHECK-NOBUILTININC-INCLUDE %s + +// CHECK-NOBUILTININC-INCLUDE: {{.*}}clang{{.*}}" "-cc1" +// CHECK-NOBUILTININC-INCLUDE: "-resource-dir" "[[RESOURCE_DIR:[^"]+]]" +// CHECK-NOBUILTININC-INCLUDE: "-isysroot" "[[SYSROOT:[^"]+]]" +// CHECK-NOBUILTININC-INCLUDE-NOT: "-internal-isystem" "[[RESOURCE_DIR]]{{(/|\\\\)}}include" +// CHECK-NOBUILTININC-INCLUDE: "-internal-isystem" "[[SYSROOT]]/usr/include"