[clang] [Clang][Darwin] Centralize framework search paths for headers & libraries. (PR #118543)

via cfe-commits cfe-commits at lists.llvm.org
Tue Dec 3 12:50:25 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Cyndy Ishida (cyndyishida)

<details>
<summary>Changes</summary>

* Use the centralized API to add `SubFrameworks` for driverkit targets.

---
Full diff: https://github.com/llvm/llvm-project/pull/118543.diff


5 Files Affected:

- (modified) clang/include/clang/Basic/DarwinSDKInfo.h (+12-1) 
- (modified) clang/lib/Basic/DarwinSDKInfo.cpp (+20) 
- (modified) clang/lib/Driver/ToolChains/Darwin.cpp (+11-16) 
- (modified) clang/lib/Lex/InitHeaderSearch.cpp (+5-6) 
- (modified) clang/test/Driver/driverkit-path.c (+3) 


``````````diff
diff --git a/clang/include/clang/Basic/DarwinSDKInfo.h b/clang/include/clang/Basic/DarwinSDKInfo.h
index db20b968a898ea..87c0a2abb2432c 100644
--- a/clang/include/clang/Basic/DarwinSDKInfo.h
+++ b/clang/include/clang/Basic/DarwinSDKInfo.h
@@ -1,4 +1,4 @@
-//===--- DarwinSDKInfo.h - SDK Information parser for darwin ----*- C++ -*-===//
+//===--- DarwinSDKInfo.h - SDK Information for darwin -----------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -192,6 +192,17 @@ class DarwinSDKInfo {
 Expected<std::optional<DarwinSDKInfo>>
 parseDarwinSDKInfo(llvm::vfs::FileSystem &VFS, StringRef SDKRootPath);
 
+/// Get the system platform prefix for the active target triple.
+StringRef getSystemPrefix(const llvm::Triple &T);
+
+using KnownSystemPaths = std::array<std::string, 2>;
+
+/// Compute and get the common system search paths for header frontend and
+/// library linker searching.
+///
+/// \param T The active target triple to determine platform specific paths.
+KnownSystemPaths getCommonSystemPaths(llvm::Triple T);
+
 } // end namespace clang
 
 #endif // LLVM_CLANG_BASIC_DARWINSDKINFO_H
diff --git a/clang/lib/Basic/DarwinSDKInfo.cpp b/clang/lib/Basic/DarwinSDKInfo.cpp
index 00aa5f9e63cd3f..914ce0a554008a 100644
--- a/clang/lib/Basic/DarwinSDKInfo.cpp
+++ b/clang/lib/Basic/DarwinSDKInfo.cpp
@@ -150,3 +150,23 @@ clang::parseDarwinSDKInfo(llvm::vfs::FileSystem &VFS, StringRef SDKRootPath) {
   return llvm::make_error<llvm::StringError>("invalid SDKSettings.json",
                                              llvm::inconvertibleErrorCode());
 }
+
+// For certain platforms/environments almost all resources (e.g., headers) are
+// located in sub-directories, e.g., for DriverKit they live in
+// <SYSROOT>/System/DriverKit/usr/include (instead of <SYSROOT>/usr/include).
+StringRef clang::getSystemPrefix(const llvm::Triple &T) {
+  if (T.isDriverKit())
+    return "/System/DriverKit";
+  return "";
+}
+
+KnownSystemPaths clang::getCommonSystemPaths(llvm::Triple T) {
+  KnownSystemPaths CommonSysPaths = {"/System/Library/Frameworks",
+                                     "/System/Library/SubFrameworks"};
+
+  const StringRef Prefix = getSystemPrefix(T);
+  for (std::string &SysPath : CommonSysPaths)
+    SysPath = (Prefix + SysPath).str();
+
+  return CommonSysPaths;
+}
diff --git a/clang/lib/Driver/ToolChains/Darwin.cpp b/clang/lib/Driver/ToolChains/Darwin.cpp
index 87380869f6fdab..cadfbcba9afaa6 100644
--- a/clang/lib/Driver/ToolChains/Darwin.cpp
+++ b/clang/lib/Driver/ToolChains/Darwin.cpp
@@ -11,6 +11,7 @@
 #include "Arch/ARM.h"
 #include "CommonArgs.h"
 #include "clang/Basic/AlignedAllocation.h"
+#include "clang/Basic/DarwinSDKInfo.h"
 #include "clang/Basic/ObjCRuntime.h"
 #include "clang/Config/config.h"
 #include "clang/Driver/Compilation.h"
@@ -564,8 +565,6 @@ static void renderRemarksOptions(const ArgList &Args, ArgStringList &CmdArgs,
   }
 }
 
-static void AppendPlatformPrefix(SmallString<128> &Path, const llvm::Triple &T);
-
 void darwin::Linker::ConstructJob(Compilation &C, const JobAction &JA,
                                   const InputInfo &Output,
                                   const InputInfoList &Inputs,
@@ -811,16 +810,22 @@ void darwin::Linker::ConstructJob(Compilation &C, const JobAction &JA,
 
     if (NonStandardSearchPath) {
       if (auto *Sysroot = Args.getLastArg(options::OPT_isysroot)) {
-        auto AddSearchPath = [&](StringRef Flag, StringRef SearchPath) {
+        auto AddSearchPath = [&](StringRef Flag, StringRef SearchPath,
+                                 bool HasPrefix = false) {
           SmallString<128> P(Sysroot->getValue());
-          AppendPlatformPrefix(P, Triple);
+          if (!HasPrefix)
+            P.append(getSystemPrefix(Triple));
           llvm::sys::path::append(P, SearchPath);
           if (getToolChain().getVFS().exists(P)) {
             CmdArgs.push_back(Args.MakeArgString(Flag + P));
           }
         };
+
         AddSearchPath("-L", "/usr/lib");
-        AddSearchPath("-F", "/System/Library/Frameworks");
+        for (const StringRef Path : getCommonSystemPaths(Triple)) {
+          if (Path.contains("Framework"))
+            AddSearchPath("-F", Path, /*HasPrefix=*/true);
+        }
       }
     }
   }
@@ -2463,16 +2468,6 @@ void Darwin::AddDeploymentTarget(DerivedArgList &Args) const {
   }
 }
 
-// For certain platforms/environments almost all resources (e.g., headers) are
-// located in sub-directories, e.g., for DriverKit they live in
-// <SYSROOT>/System/DriverKit/usr/include (instead of <SYSROOT>/usr/include).
-static void AppendPlatformPrefix(SmallString<128> &Path,
-                                 const llvm::Triple &T) {
-  if (T.isDriverKit()) {
-    llvm::sys::path::append(Path, "System", "DriverKit");
-  }
-}
-
 // Returns the effective sysroot from either -isysroot or --sysroot, plus the
 // platform prefix (if any).
 llvm::SmallString<128>
@@ -2484,7 +2479,7 @@ DarwinClang::GetEffectiveSysroot(const llvm::opt::ArgList &DriverArgs) const {
     Path = getDriver().SysRoot;
 
   if (hasEffectiveTriple()) {
-    AppendPlatformPrefix(Path, getEffectiveTriple());
+    Path.append(getSystemPrefix(getEffectiveTriple()));
   }
   return Path;
 }
diff --git a/clang/lib/Lex/InitHeaderSearch.cpp b/clang/lib/Lex/InitHeaderSearch.cpp
index ea02f5dfb62644..d73eb47faf98bc 100644
--- a/clang/lib/Lex/InitHeaderSearch.cpp
+++ b/clang/lib/Lex/InitHeaderSearch.cpp
@@ -10,6 +10,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "clang/Basic/DarwinSDKInfo.h"
 #include "clang/Basic/FileManager.h"
 #include "clang/Basic/LangOptions.h"
 #include "clang/Config/config.h" // C_INCLUDE_DIRS
@@ -339,13 +340,11 @@ void InitHeaderSearch::AddDefaultIncludePaths(
   if (triple.isOSDarwin()) {
     if (HSOpts.UseStandardSystemIncludes) {
       // Add the default framework include paths on Darwin.
-      if (triple.isDriverKit()) {
-        AddPath("/System/DriverKit/System/Library/Frameworks", System, true);
-      } else {
-        AddPath("/System/Library/Frameworks", System, true);
-        AddPath("/System/Library/SubFrameworks", System, true);
+      for (const StringRef Path : getCommonSystemPaths(triple))
+        AddPath(Path, System, true);
+
+      if (!triple.isDriverKit())
         AddPath("/Library/Frameworks", System, true);
-      }
     }
     return;
   }
diff --git a/clang/test/Driver/driverkit-path.c b/clang/test/Driver/driverkit-path.c
index 3caae382d65bb3..8e87cd5cccf549 100644
--- a/clang/test/Driver/driverkit-path.c
+++ b/clang/test/Driver/driverkit-path.c
@@ -18,9 +18,11 @@ int main() { return 0; }
 // LD64-OLD: "-isysroot" "[[PATH:[^"]*]]Inputs/DriverKit19.0.sdk"
 // LD64-OLD: "-L[[PATH]]Inputs/DriverKit19.0.sdk/System/DriverKit/usr/lib"
 // LD64-OLD: "-F[[PATH]]Inputs/DriverKit19.0.sdk/System/DriverKit/System/Library/Frameworks"
+// LD64-OLD: "-F[[PATH]]Inputs/DriverKit19.0.sdk/System/DriverKit/System/Library/SubFrameworks"
 // LD64-NEW: "-isysroot" "[[PATH:[^"]*]]Inputs/DriverKit19.0.sdk"
 // LD64-NEW-NOT: "-L[[PATH]]Inputs/DriverKit19.0.sdk/System/DriverKit/usr/lib"
 // LD64-NEW-NOT: "-F[[PATH]]Inputs/DriverKit19.0.sdk/System/DriverKit/System/Library/Frameworks"
+// LD64-NEW-NOT: "-F[[PATH]]Inputs/DriverKit19.0.sdk/System/DriverKit/System/Library/SubFrameworks"
 
 
 // RUN: %clang %s -target x86_64-apple-driverkit19.0 -isysroot %S/Inputs/DriverKit19.0.sdk -E -v -x c++ 2>&1 | FileCheck %s --check-prefix=INC
@@ -31,3 +33,4 @@ int main() { return 0; }
 // INC:       /lib{{(64)?}}/clang/{{[^/ ]+}}/include
 // INC:       [[PATH]]/System/DriverKit/usr/include
 // INC:       [[PATH]]/System/DriverKit/System/Library/Frameworks (framework directory)
+// INC:       [[PATH]]/System/DriverKit/System/Library/SubFrameworks (framework directory)

``````````

</details>


https://github.com/llvm/llvm-project/pull/118543


More information about the cfe-commits mailing list