Skip to content

Commit 764bf57

Browse files
committed
[Clang][Darwin] Centralize framework search paths for headers &
libraries. * Use the centralized API to add `SubFrameworks` for driverkit targets.
1 parent 3845624 commit 764bf57

File tree

5 files changed

+51
-23
lines changed

5 files changed

+51
-23
lines changed

clang/include/clang/Basic/DarwinSDKInfo.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===--- DarwinSDKInfo.h - SDK Information parser for darwin ----*- C++ -*-===//
1+
//===--- DarwinSDKInfo.h - SDK Information for darwin -----------*- C++ -*-===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
@@ -192,6 +192,17 @@ class DarwinSDKInfo {
192192
Expected<std::optional<DarwinSDKInfo>>
193193
parseDarwinSDKInfo(llvm::vfs::FileSystem &VFS, StringRef SDKRootPath);
194194

195+
/// Get the system platform prefix for the active target triple.
196+
StringRef getSystemPrefix(const llvm::Triple &T);
197+
198+
using KnownSystemPaths = std::array<std::string, 2>;
199+
200+
/// Compute and get the common system search paths for header frontend and
201+
/// library linker searching.
202+
///
203+
/// \param T The active target triple to determine platform specific paths.
204+
KnownSystemPaths getCommonSystemPaths(llvm::Triple T);
205+
195206
} // end namespace clang
196207

197208
#endif // LLVM_CLANG_BASIC_DARWINSDKINFO_H

clang/lib/Basic/DarwinSDKInfo.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,23 @@ clang::parseDarwinSDKInfo(llvm::vfs::FileSystem &VFS, StringRef SDKRootPath) {
150150
return llvm::make_error<llvm::StringError>("invalid SDKSettings.json",
151151
llvm::inconvertibleErrorCode());
152152
}
153+
154+
// For certain platforms/environments almost all resources (e.g., headers) are
155+
// located in sub-directories, e.g., for DriverKit they live in
156+
// <SYSROOT>/System/DriverKit/usr/include (instead of <SYSROOT>/usr/include).
157+
StringRef clang::getSystemPrefix(const llvm::Triple &T) {
158+
if (T.isDriverKit())
159+
return "/System/DriverKit";
160+
return "";
161+
}
162+
163+
KnownSystemPaths clang::getCommonSystemPaths(llvm::Triple T) {
164+
KnownSystemPaths CommonSysPaths = {"/System/Library/Frameworks",
165+
"/System/Library/SubFrameworks"};
166+
167+
const StringRef Prefix = getSystemPrefix(T);
168+
for (std::string &SysPath : CommonSysPaths)
169+
SysPath = (Prefix + SysPath).str();
170+
171+
return CommonSysPaths;
172+
}

clang/lib/Driver/ToolChains/Darwin.cpp

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "Arch/ARM.h"
1212
#include "CommonArgs.h"
1313
#include "clang/Basic/AlignedAllocation.h"
14+
#include "clang/Basic/DarwinSDKInfo.h"
1415
#include "clang/Basic/ObjCRuntime.h"
1516
#include "clang/Config/config.h"
1617
#include "clang/Driver/Compilation.h"
@@ -564,8 +565,6 @@ static void renderRemarksOptions(const ArgList &Args, ArgStringList &CmdArgs,
564565
}
565566
}
566567

567-
static void AppendPlatformPrefix(SmallString<128> &Path, const llvm::Triple &T);
568-
569568
void darwin::Linker::ConstructJob(Compilation &C, const JobAction &JA,
570569
const InputInfo &Output,
571570
const InputInfoList &Inputs,
@@ -811,16 +810,22 @@ void darwin::Linker::ConstructJob(Compilation &C, const JobAction &JA,
811810

812811
if (NonStandardSearchPath) {
813812
if (auto *Sysroot = Args.getLastArg(options::OPT_isysroot)) {
814-
auto AddSearchPath = [&](StringRef Flag, StringRef SearchPath) {
813+
auto AddSearchPath = [&](StringRef Flag, StringRef SearchPath,
814+
bool HasPrefix = false) {
815815
SmallString<128> P(Sysroot->getValue());
816-
AppendPlatformPrefix(P, Triple);
816+
if (!HasPrefix)
817+
P.append(getSystemPrefix(Triple));
817818
llvm::sys::path::append(P, SearchPath);
818819
if (getToolChain().getVFS().exists(P)) {
819820
CmdArgs.push_back(Args.MakeArgString(Flag + P));
820821
}
821822
};
823+
822824
AddSearchPath("-L", "/usr/lib");
823-
AddSearchPath("-F", "/System/Library/Frameworks");
825+
for (const StringRef Path : getCommonSystemPaths(Triple)) {
826+
if (Path.contains("Framework"))
827+
AddSearchPath("-F", Path, /*HasPrefix=*/true);
828+
}
824829
}
825830
}
826831
}
@@ -2463,16 +2468,6 @@ void Darwin::AddDeploymentTarget(DerivedArgList &Args) const {
24632468
}
24642469
}
24652470

2466-
// For certain platforms/environments almost all resources (e.g., headers) are
2467-
// located in sub-directories, e.g., for DriverKit they live in
2468-
// <SYSROOT>/System/DriverKit/usr/include (instead of <SYSROOT>/usr/include).
2469-
static void AppendPlatformPrefix(SmallString<128> &Path,
2470-
const llvm::Triple &T) {
2471-
if (T.isDriverKit()) {
2472-
llvm::sys::path::append(Path, "System", "DriverKit");
2473-
}
2474-
}
2475-
24762471
// Returns the effective sysroot from either -isysroot or --sysroot, plus the
24772472
// platform prefix (if any).
24782473
llvm::SmallString<128>
@@ -2484,7 +2479,7 @@ DarwinClang::GetEffectiveSysroot(const llvm::opt::ArgList &DriverArgs) const {
24842479
Path = getDriver().SysRoot;
24852480

24862481
if (hasEffectiveTriple()) {
2487-
AppendPlatformPrefix(Path, getEffectiveTriple());
2482+
Path.append(getSystemPrefix(getEffectiveTriple()));
24882483
}
24892484
return Path;
24902485
}

clang/lib/Lex/InitHeaderSearch.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
#include "clang/Basic/DarwinSDKInfo.h"
1314
#include "clang/Basic/FileManager.h"
1415
#include "clang/Basic/LangOptions.h"
1516
#include "clang/Config/config.h" // C_INCLUDE_DIRS
@@ -339,13 +340,11 @@ void InitHeaderSearch::AddDefaultIncludePaths(
339340
if (triple.isOSDarwin()) {
340341
if (HSOpts.UseStandardSystemIncludes) {
341342
// Add the default framework include paths on Darwin.
342-
if (triple.isDriverKit()) {
343-
AddPath("/System/DriverKit/System/Library/Frameworks", System, true);
344-
} else {
345-
AddPath("/System/Library/Frameworks", System, true);
346-
AddPath("/System/Library/SubFrameworks", System, true);
343+
for (const StringRef Path : getCommonSystemPaths(triple))
344+
AddPath(Path, System, true);
345+
346+
if (!triple.isDriverKit())
347347
AddPath("/Library/Frameworks", System, true);
348-
}
349348
}
350349
return;
351350
}

clang/test/Driver/driverkit-path.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ int main() { return 0; }
1818
// LD64-OLD: "-isysroot" "[[PATH:[^"]*]]Inputs/DriverKit19.0.sdk"
1919
// LD64-OLD: "-L[[PATH]]Inputs/DriverKit19.0.sdk/System/DriverKit/usr/lib"
2020
// LD64-OLD: "-F[[PATH]]Inputs/DriverKit19.0.sdk/System/DriverKit/System/Library/Frameworks"
21+
// LD64-OLD: "-F[[PATH]]Inputs/DriverKit19.0.sdk/System/DriverKit/System/Library/SubFrameworks"
2122
// LD64-NEW: "-isysroot" "[[PATH:[^"]*]]Inputs/DriverKit19.0.sdk"
2223
// LD64-NEW-NOT: "-L[[PATH]]Inputs/DriverKit19.0.sdk/System/DriverKit/usr/lib"
2324
// LD64-NEW-NOT: "-F[[PATH]]Inputs/DriverKit19.0.sdk/System/DriverKit/System/Library/Frameworks"
25+
// LD64-NEW-NOT: "-F[[PATH]]Inputs/DriverKit19.0.sdk/System/DriverKit/System/Library/SubFrameworks"
2426

2527

2628
// 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; }
3133
// INC: /lib{{(64)?}}/clang/{{[^/ ]+}}/include
3234
// INC: [[PATH]]/System/DriverKit/usr/include
3335
// INC: [[PATH]]/System/DriverKit/System/Library/Frameworks (framework directory)
36+
// INC: [[PATH]]/System/DriverKit/System/Library/SubFrameworks (framework directory)

0 commit comments

Comments
 (0)