Skip to content

Commit 59f8d68

Browse files
[clang][Darwin] Remove legacy framework search path logic in the frontend
Move the Darwin framework search path logic from InitHeaderSearch::AddDefaultIncludePaths to DarwinClang::AddClangSystemIncludeArgs. Add a new -internal-iframework cc1 argument to support the tool chain adding these paths. Now that the tool chain is adding search paths via cc1 flag, they're only added if they exist, so the Preprocessor/cuda-macos-includes.cu test is no longer relevant. Change Driver/driverkit-path.c and Driver/darwin-subframeworks.c to do -### style testing similar to the darwin-header-search and darwin-embedded-search-paths tests. Rename darwin-subframeworks.c to darwin-framework-search-paths.c and have it test all framework search paths, not just SubFrameworks. Add a unit test to validate that the myriad of search path flags result in the expected search path list. Fixes #75638
1 parent ae34440 commit 59f8d68

File tree

17 files changed

+262
-62
lines changed

17 files changed

+262
-62
lines changed

clang/include/clang/Driver/Options.td

+5
Original file line numberDiff line numberDiff line change
@@ -8413,6 +8413,11 @@ def objc_isystem : Separate<["-"], "objc-isystem">,
84138413
def objcxx_isystem : Separate<["-"], "objcxx-isystem">,
84148414
MetaVarName<"<directory>">,
84158415
HelpText<"Add directory to the ObjC++ SYSTEM include search path">;
8416+
def internal_iframework : Separate<["-"], "internal-iframework">,
8417+
MetaVarName<"<directory>">,
8418+
HelpText<"Add directory to the internal system framework search path; these "
8419+
"are assumed to not be user-provided and are used to model system "
8420+
"and standard frameworks' paths.">;
84168421
def internal_isystem : Separate<["-"], "internal-isystem">,
84178422
MetaVarName<"<directory>">,
84188423
HelpText<"Add directory to the internal system include search path; these "

clang/include/clang/Driver/ToolChain.h

+6
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,9 @@ class ToolChain {
226226

227227
/// \name Utilities for implementing subclasses.
228228
///@{
229+
static void addSystemFrameworkInclude(const llvm::opt::ArgList &DriverArgs,
230+
llvm::opt::ArgStringList &CC1Args,
231+
const Twine &Path);
229232
static void addSystemInclude(const llvm::opt::ArgList &DriverArgs,
230233
llvm::opt::ArgStringList &CC1Args,
231234
const Twine &Path);
@@ -236,6 +239,9 @@ class ToolChain {
236239
addExternCSystemIncludeIfExists(const llvm::opt::ArgList &DriverArgs,
237240
llvm::opt::ArgStringList &CC1Args,
238241
const Twine &Path);
242+
static void addSystemFrameworkIncludes(const llvm::opt::ArgList &DriverArgs,
243+
llvm::opt::ArgStringList &CC1Args,
244+
ArrayRef<StringRef> Paths);
239245
static void addSystemIncludes(const llvm::opt::ArgList &DriverArgs,
240246
llvm::opt::ArgStringList &CC1Args,
241247
ArrayRef<StringRef> Paths);

clang/lib/Driver/Job.cpp

+9-8
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,15 @@ static bool skipArgs(const char *Flag, bool HaveCrashVFS, int &SkipNum,
6767
return true;
6868

6969
// Some include flags shouldn't be skipped if we have a crash VFS
70-
IsInclude = llvm::StringSwitch<bool>(Flag)
71-
.Cases("-include", "-header-include-file", true)
72-
.Cases("-idirafter", "-internal-isystem", "-iwithprefix", true)
73-
.Cases("-internal-externc-isystem", "-iprefix", true)
74-
.Cases("-iwithprefixbefore", "-isystem", "-iquote", true)
75-
.Cases("-isysroot", "-I", "-F", "-resource-dir", true)
76-
.Cases("-iframework", "-include-pch", true)
77-
.Default(false);
70+
IsInclude =
71+
llvm::StringSwitch<bool>(Flag)
72+
.Cases("-include", "-header-include-file", true)
73+
.Cases("-idirafter", "-internal-isystem", "-iwithprefix", true)
74+
.Cases("-internal-externc-isystem", "-iprefix", true)
75+
.Cases("-iwithprefixbefore", "-isystem", "-iquote", true)
76+
.Cases("-isysroot", "-I", "-F", "-resource-dir", true)
77+
.Cases("-internal-iframework", "-iframework", "-include-pch", true)
78+
.Default(false);
7879
if (IsInclude)
7980
return !HaveCrashVFS;
8081

clang/lib/Driver/ToolChain.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -1366,6 +1366,15 @@ ToolChain::CXXStdlibType ToolChain::GetCXXStdlibType(const ArgList &Args) const{
13661366
return *cxxStdlibType;
13671367
}
13681368

1369+
/// Utility function to add a system framework directory to CC1 arguments.
1370+
/*static*/ void
1371+
ToolChain::addSystemFrameworkInclude(const llvm::opt::ArgList &DriverArgs,
1372+
llvm::opt::ArgStringList &CC1Args,
1373+
const Twine &Path) {
1374+
CC1Args.push_back("-internal-iframework");
1375+
CC1Args.push_back(DriverArgs.MakeArgString(Path));
1376+
}
1377+
13691378
/// Utility function to add a system include directory to CC1 arguments.
13701379
/*static*/ void ToolChain::addSystemInclude(const ArgList &DriverArgs,
13711380
ArgStringList &CC1Args,
@@ -1396,6 +1405,17 @@ void ToolChain::addExternCSystemIncludeIfExists(const ArgList &DriverArgs,
13961405
addExternCSystemInclude(DriverArgs, CC1Args, Path);
13971406
}
13981407

1408+
/// Utility function to add a list of system framework directories to CC1.
1409+
/*static*/ void
1410+
ToolChain::addSystemFrameworkIncludes(const ArgList &DriverArgs,
1411+
ArgStringList &CC1Args,
1412+
ArrayRef<StringRef> Paths) {
1413+
for (const auto &Path : Paths) {
1414+
CC1Args.push_back("-internal-iframework");
1415+
CC1Args.push_back(DriverArgs.MakeArgString(Path));
1416+
}
1417+
}
1418+
13991419
/// Utility function to add a list of system include directories to CC1.
14001420
/*static*/ void ToolChain::addSystemIncludes(const ArgList &DriverArgs,
14011421
ArgStringList &CC1Args,

clang/lib/Driver/ToolChains/Darwin.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -2577,6 +2577,27 @@ void AppleMachO::AddClangSystemIncludeArgs(
25772577
}
25782578
}
25792579

2580+
void DarwinClang::AddClangSystemIncludeArgs(
2581+
const llvm::opt::ArgList &DriverArgs,
2582+
llvm::opt::ArgStringList &CC1Args) const {
2583+
AppleMachO::AddClangSystemIncludeArgs(DriverArgs, CC1Args);
2584+
2585+
if (DriverArgs.hasArg(options::OPT_nostdinc) ||
2586+
DriverArgs.hasArg(options::OPT_nostdlibinc))
2587+
return;
2588+
2589+
llvm::SmallString<128> Sysroot = GetEffectiveSysroot(DriverArgs);
2590+
2591+
// Add <sysroot>/System/Library/Frameworks
2592+
// Add <sysroot>/System/Library/SubFrameworks
2593+
// Add <sysroot>/Library/Frameworks
2594+
SmallString<128> P1(Sysroot), P2(Sysroot), P3(Sysroot);
2595+
llvm::sys::path::append(P1, "System", "Library", "Frameworks");
2596+
llvm::sys::path::append(P2, "System", "Library", "SubFrameworks");
2597+
llvm::sys::path::append(P3, "Library", "Frameworks");
2598+
addSystemFrameworkIncludes(DriverArgs, CC1Args, {P1, P2, P3});
2599+
}
2600+
25802601
bool DarwinClang::AddGnuCPlusPlusIncludePaths(const llvm::opt::ArgList &DriverArgs,
25812602
llvm::opt::ArgStringList &CC1Args,
25822603
llvm::SmallString<128> Base,

clang/lib/Driver/ToolChains/Darwin.h

+4
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,10 @@ class LLVM_LIBRARY_VISIBILITY DarwinClang : public Darwin {
647647
/// @name Apple ToolChain Implementation
648648
/// {
649649

650+
void
651+
AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs,
652+
llvm::opt::ArgStringList &CC1Args) const override;
653+
650654
RuntimeLibType GetRuntimeLibType(const llvm::opt::ArgList &Args) const override;
651655

652656
void AddLinkRuntimeLibArgs(const llvm::opt::ArgList &Args,

clang/lib/Frontend/CompilerInvocation.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -3364,6 +3364,8 @@ static void GenerateHeaderSearchArgs(const HeaderSearchOptions &Opts,
33643364
: OPT_internal_externc_isystem;
33653365
GenerateArg(Consumer, Opt, It->Path);
33663366
}
3367+
for (; It < End && Matches(*It, {frontend::System}, true, true); ++It)
3368+
GenerateArg(Consumer, OPT_internal_iframework, It->Path);
33673369

33683370
assert(It == End && "Unhandled HeaderSearchOption::Entry.");
33693371

@@ -3496,6 +3498,8 @@ static bool ParseHeaderSearchArgs(HeaderSearchOptions &Opts, ArgList &Args,
34963498
Group = frontend::ExternCSystem;
34973499
Opts.AddPath(A->getValue(), Group, false, true);
34983500
}
3501+
for (const auto *A : Args.filtered(OPT_internal_iframework))
3502+
Opts.AddPath(A->getValue(), frontend::System, true, true);
34993503

35003504
// Add the path prefixes which are implicitly treated as being system headers.
35013505
for (const auto *A :

clang/lib/Lex/InitHeaderSearch.cpp

+3-16
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,9 @@ bool InitHeaderSearch::ShouldAddDefaultIncludePaths(
321321
break;
322322
}
323323

324+
if (triple.isOSDarwin())
325+
return false;
326+
324327
return true; // Everything else uses AddDefaultIncludePaths().
325328
}
326329

@@ -335,22 +338,6 @@ void InitHeaderSearch::AddDefaultIncludePaths(
335338
if (!ShouldAddDefaultIncludePaths(triple))
336339
return;
337340

338-
// NOTE: some additional header search logic is handled in the driver for
339-
// Darwin.
340-
if (triple.isOSDarwin()) {
341-
if (HSOpts.UseStandardSystemIncludes) {
342-
// Add the default framework include paths on Darwin.
343-
if (triple.isDriverKit()) {
344-
AddPath("/System/DriverKit/System/Library/Frameworks", System, true);
345-
} else {
346-
AddPath("/System/Library/Frameworks", System, true);
347-
AddPath("/System/Library/SubFrameworks", System, true);
348-
AddPath("/Library/Frameworks", System, true);
349-
}
350-
}
351-
return;
352-
}
353-
354341
if (Lang.CPlusPlus && !Lang.AsmPreprocessor &&
355342
HSOpts.UseStandardCXXIncludes && HSOpts.UseStandardSystemIncludes) {
356343
if (HSOpts.UseLibcxx) {

clang/test/Driver/Inputs/DriverKit19.0.sdk/System/DriverKit/System/Library/SubFrameworks/.keep

Whitespace-only changes.

clang/test/Driver/Inputs/MacOSX15.1.sdk/System/LibraryFrameworks/.keep

Whitespace-only changes.

clang/test/Driver/Inputs/MacOSX15.1.sdk/System/LibrarySubFrameworks/.keep

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// UNSUPPORTED: system-windows
2+
// Windows is unsupported because we use the Unix path separator `/` in the test.
3+
4+
// RUN: %clang %s -target arm64-apple-macosx15.1 -isysroot %S/Inputs/MacOSX15.1.sdk -c %s -### 2>&1 \
5+
// RUN: | FileCheck -DSDKROOT=%S/Inputs/MacOSX15.1.sdk %s
6+
//
7+
// CHECK: "-cc1"
8+
// CHECK: "-resource-dir" "[[RESOURCE_DIR:[^"]*]]"
9+
// CHECK-SAME: "-internal-iframework" "[[SDKROOT]]/System/Library/Frameworks"
10+
// CHECK-SAME: "-internal-iframework" "[[SDKROOT]]/System/Library/SubFrameworks"
11+
// CHECK-SAME: "-internal-iframework" "[[SDKROOT]]/Library/Frameworks"
12+
13+
// Verify that -nostdlibinc and -nostdinc removes the default search paths.
14+
//
15+
// RUN: %clang %s -target arm64-apple-macosx15.1 -isysroot %S/Inputs/MacOSX15.1.sdk -nostdinc -c %s -### 2>&1 \
16+
// RUN: | FileCheck --check-prefix=CHECK-NOSTD -DSDKROOT=%S/Inputs/MacOSX15.1.sdk %s
17+
//
18+
// RUN: %clang %s -target arm64-apple-macosx15.1 -isysroot %S/Inputs/MacOSX15.1.sdk -nostdlibinc -c %s -### 2>&1 \
19+
// RUN: | FileCheck --check-prefix=CHECK-NOSTD -DSDKROOT=%S/Inputs/MacOSX15.1.sdk %s
20+
//
21+
// CHECK-NOSTD: "-cc1"
22+
// CHECK-NOSTD: "-resource-dir" "[[RESOURCE_DIR:[^"]*]]"
23+
// CHECK-NOSTD-NOT: "-internal-iframework"

clang/test/Driver/darwin-subframeworks.c

-18
This file was deleted.

clang/test/Driver/driverkit-path.c

+9-7
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ int main() { return 0; }
2121
// LD64-NEW: "-isysroot" "[[PATH:[^"]*]]Inputs/DriverKit19.0.sdk"
2222
// LD64-NEW-NOT: "-L[[PATH]]Inputs/DriverKit19.0.sdk/System/DriverKit/usr/lib"
2323
// LD64-NEW-NOT: "-F[[PATH]]Inputs/DriverKit19.0.sdk/System/DriverKit/System/Library/Frameworks"
24+
// LD64-NEW-NOT: "-F[[PATH]]Inputs/DriverKit19.0.sdk/System/DriverKit/System/Library/SubFrameworks"
2425

2526

26-
// 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
27+
// RUN: %clang %s -target x86_64-apple-driverkit19.0 -isysroot %S/Inputs/DriverKit19.0.sdk -x c++ -### 2>&1 \
28+
// RUN: | FileCheck %s -DSDKROOT=%S/Inputs/DriverKit19.0.sdk --check-prefix=INC
2729
//
28-
// INC: -isysroot [[PATH:[^ ]*/Inputs/DriverKit19.0.sdk]]
29-
// INC-LABEL: #include <...> search starts here:
30-
// INC: [[PATH]]/System/DriverKit/usr/local/include
31-
// INC: /lib{{(64)?}}/clang/{{[^/ ]+}}/include
32-
// INC: [[PATH]]/System/DriverKit/usr/include
33-
// INC: [[PATH]]/System/DriverKit/System/Library/Frameworks (framework directory)
30+
// INC: "-isysroot" "[[SDKROOT]]"
31+
// INC: "-internal-isystem" "[[SDKROOT]]/System/DriverKit/usr/local/include"
32+
// INC: "-internal-isystem" "{{.+}}/lib{{(64)?}}/clang/{{[^/ ]+}}/include"
33+
// INC: "-internal-externc-isystem" "[[SDKROOT]]/System/DriverKit/usr/include"
34+
// INC: "-internal-iframework" "[[SDKROOT]]/System/DriverKit/System/Library/Frameworks"
35+
// INC: "-internal-iframework" "[[SDKROOT]]/System/DriverKit/System/Library/SubFrameworks"

clang/test/Preprocessor/cuda-macos-includes.cu

-13
This file was deleted.

clang/unittests/Frontend/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ add_clang_unittest(FrontendTests
1010
PCHPreambleTest.cpp
1111
ReparseWorkingDirTest.cpp
1212
OutputStreamTest.cpp
13+
SearchPathTest.cpp
1314
TextDiagnosticTest.cpp
1415
UtilsTest.cpp
1516
CLANG_LIBS

0 commit comments

Comments
 (0)