[Driver] Move PS4/PS5 header search path management to the driver
authorPaul Robinson <paul.robinson@sony.com>
Tue, 31 Jan 2023 23:22:25 +0000 (15:22 -0800)
committerPaul Robinson <paul.robinson@sony.com>
Wed, 1 Feb 2023 15:40:30 +0000 (07:40 -0800)
This follows how OpenBSD, FreeBSD, and NetBSD now work. (See
D138183 and D140817 for those cases.)

It also tidies up some code duplication that wasn't exactly right.

clang/lib/Driver/ToolChains/PS4CPU.cpp
clang/lib/Driver/ToolChains/PS4CPU.h
clang/lib/Lex/InitHeaderSearch.cpp
clang/test/Driver/ps4-ps5-header-search.c

index 643f815..fc5d46b 100644 (file)
@@ -8,6 +8,7 @@
 
 #include "PS4CPU.h"
 #include "CommonArgs.h"
+#include "clang/Config/config.h"
 #include "clang/Driver/Compilation.h"
 #include "clang/Driver/Driver.h"
 #include "clang/Driver/DriverDiagnostic.h"
@@ -253,15 +254,14 @@ toolchains::PS4PS5Base::PS4PS5Base(const Driver &D, const llvm::Triple &Triple,
   // SDK include or lib directories. This behavior could be changed if
   // -Weverything or -Winvalid-or-nonexistent-directory options are passed.
   // If -isysroot was passed, use that as the SDK base path.
-  std::string PrefixDir;
   if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
-    PrefixDir = A->getValue();
-    if (!llvm::sys::fs::exists(PrefixDir))
-      D.Diag(clang::diag::warn_missing_sysroot) << PrefixDir;
+    SDKRootDir = A->getValue();
+    if (!llvm::sys::fs::exists(SDKRootDir))
+      D.Diag(clang::diag::warn_missing_sysroot) << SDKRootDir;
   } else
-    PrefixDir = std::string(SDKDir.str());
+    SDKRootDir = std::string(SDKDir.str());
 
-  SmallString<512> SDKIncludeDir(PrefixDir);
+  SmallString<512> SDKIncludeDir(SDKRootDir);
   llvm::sys::path::append(SDKIncludeDir, "target/include");
   if (!Args.hasArg(options::OPT_nostdinc) &&
       !Args.hasArg(options::OPT_nostdlibinc) &&
@@ -272,7 +272,7 @@ toolchains::PS4PS5Base::PS4PS5Base(const Driver &D, const llvm::Triple &Triple,
         << Twine(Platform, " system headers").str() << SDKIncludeDir;
   }
 
-  SmallString<512> SDKLibDir(SDKDir);
+  SmallString<512> SDKLibDir(SDKRootDir);
   llvm::sys::path::append(SDKLibDir, "target/lib");
   if (!Args.hasArg(options::OPT_nostdlib) &&
       !Args.hasArg(options::OPT_nodefaultlibs) &&
@@ -287,6 +287,42 @@ toolchains::PS4PS5Base::PS4PS5Base(const Driver &D, const llvm::Triple &Triple,
   getFilePaths().push_back(std::string(SDKLibDir.str()));
 }
 
+void toolchains::PS4PS5Base::AddClangSystemIncludeArgs(
+    const ArgList &DriverArgs,
+    ArgStringList &CC1Args) const {
+  const Driver &D = getDriver();
+
+  if (DriverArgs.hasArg(options::OPT_nostdinc))
+    return;
+
+  if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
+    SmallString<128> Dir(D.ResourceDir);
+    llvm::sys::path::append(Dir, "include");
+    addSystemInclude(DriverArgs, CC1Args, Dir.str());
+  }
+
+  if (DriverArgs.hasArg(options::OPT_nostdlibinc))
+    return;
+
+  // Add dirs specified via 'configure --with-c-include-dirs'.
+  StringRef CIncludeDirs(C_INCLUDE_DIRS);
+  if (!CIncludeDirs.empty()) {
+    SmallVector<StringRef, 5> dirs;
+    CIncludeDirs.split(dirs, ":");
+    for (StringRef dir : dirs) {
+      StringRef Prefix =
+        llvm::sys::path::is_absolute(dir) ? StringRef(D.SysRoot) : "";
+      addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir);
+    }
+    return;
+  }
+
+  addExternCSystemInclude(DriverArgs, CC1Args,
+                          SDKRootDir + "/target/include");
+  addExternCSystemInclude(DriverArgs, CC1Args,
+                          SDKRootDir + "/target/include_common");
+}
+
 Tool *toolchains::PS4CPU::buildAssembler() const {
   return new tools::PScpu::Assembler(*this);
 }
index 954e7d8..0866a5d 100644 (file)
@@ -63,6 +63,9 @@ public:
              const llvm::opt::ArgList &Args, StringRef Platform,
              const char *EnvVar);
 
+  void
+  AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs,
+                            llvm::opt::ArgStringList &CC1Args) const override;
   // No support for finding a C++ standard library yet.
   void addLibCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
                              llvm::opt::ArgStringList &CC1Args) const override {
@@ -111,6 +114,10 @@ public:
 
 protected:
   Tool *buildLinker() const override;
+
+private:
+  // We compute the SDK root dir in the ctor, and use it later.
+  std::string SDKRootDir;
 };
 
 // PS4-specific Toolchain class.
index d446556..4834559 100644 (file)
@@ -233,8 +233,6 @@ void InitHeaderSearch::AddDefaultCIncludePaths(const llvm::Triple &triple,
     switch (os) {
     case llvm::Triple::CloudABI:
     case llvm::Triple::NaCl:
-    case llvm::Triple::PS4:
-    case llvm::Triple::PS5:
     case llvm::Triple::ELFIAMCU:
       break;
     case llvm::Triple::Win32:
@@ -339,31 +337,6 @@ void InitHeaderSearch::AddDefaultCIncludePaths(const llvm::Triple &triple,
   case llvm::Triple::NaCl:
   case llvm::Triple::ELFIAMCU:
     break;
-  case llvm::Triple::PS4:
-  case llvm::Triple::PS5: {
-    // <isysroot> gets prepended later in AddPath().
-    std::string BaseSDKPath;
-    if (!HasSysroot) {
-      const char *EnvVar = (os == llvm::Triple::PS4) ? "SCE_ORBIS_SDK_DIR"
-                                                     : "SCE_PROSPERO_SDK_DIR";
-      const char *envValue = getenv(EnvVar);
-      if (envValue)
-        BaseSDKPath = envValue;
-      else {
-        // HSOpts.ResourceDir variable contains the location of Clang's
-        // resource files.
-        // Assuming that Clang is configured for PS4 without
-        // --with-clang-resource-dir option, the location of Clang's resource
-        // files is <SDK_DIR>/host_tools/lib/clang
-        SmallString<128> P = StringRef(HSOpts.ResourceDir);
-        llvm::sys::path::append(P, "../../..");
-        BaseSDKPath = std::string(P.str());
-      }
-    }
-    AddPath(BaseSDKPath + "/target/include", System, false);
-    AddPath(BaseSDKPath + "/target/include_common", System, false);
-    break;
-  }
   default:
     AddPath("/usr/include", ExternCSystem, false);
     break;
@@ -412,6 +385,8 @@ bool InitHeaderSearch::ShouldAddDefaultIncludePaths(
   case llvm::Triple::FreeBSD:
   case llvm::Triple::NetBSD:
   case llvm::Triple::OpenBSD:
+  case llvm::Triple::PS4:
+  case llvm::Triple::PS5:
   case llvm::Triple::Fuchsia:
   case llvm::Triple::Hurd:
   case llvm::Triple::Linux:
index 59d3f2a..066d284 100644 (file)
@@ -1,13 +1,13 @@
 /// PS4 and PS5 use the same SDK layout, so use the same tree for both.
 // RUN: env SCE_ORBIS_SDK_DIR=%S/Inputs/scei-ps4_tree %clang -target x86_64-scei-ps4 --sysroot="" -E -v %s 2>&1 | FileCheck %s --check-prefix=ENVPS4
 // RUN: env SCE_PROSPERO_SDK_DIR=%S/Inputs/scei-ps4_tree %clang -target x86_64-sie-ps5 --sysroot="" -E -v %s 2>&1 | FileCheck %s --check-prefix=ENVPS4
-// ENVPS4: Inputs/scei-ps4_tree/target/include{{$}}
-// ENVPS4: Inputs/scei-ps4_tree/target/include_common{{$}}
+// ENVPS4: Inputs/scei-ps4_tree/target/include
+// ENVPS4: Inputs/scei-ps4_tree/target/include_common
 // ENVPS4-NOT: /usr/include
 
 // RUN: %clang -isysroot %S/Inputs/scei-ps4_tree -target x86_64-scei-ps4 -E -v %s 2>&1 | FileCheck %s --check-prefix=SYSROOTPS4
 // RUN: %clang -isysroot %S/Inputs/scei-ps4_tree -target x86_64-sie-ps5 -E -v %s 2>&1 | FileCheck %s --check-prefix=SYSROOTPS4
 // SYSROOTPS4: "{{[^"]*}}clang{{[^"]*}}"
-// SYSROOTPS4: Inputs/scei-ps4_tree/target/include{{$}}
-// SYSROOTPS4: Inputs/scei-ps4_tree/target/include_common{{$}}
+// SYSROOTPS4: Inputs/scei-ps4_tree/target/include
+// SYSROOTPS4: Inputs/scei-ps4_tree/target/include_common
 // SYSROOTPS4-NOT: /usr/include