Skip to content

Commit 653a547

Browse files
[Darwin][Driver][clang] apple-none-macho orders the resource directory after internal-externc-isystem when nostdlibinc is used (#120507)
Embedded development often needs to use a different C standard library, replacing the existing one normally passed as -internal-externc-isystem. This works fine for an apple-macos target, but apple-none-macho doesn't work because the MachO driver doesn't implement AddClangSystemIncludeArgs to add the resource directory as -internal-isystem like most other drivers do. Move most of the search path logic from Darwin and DarwinClang down into an AppleMachO toolchain between the MachO and Darwin toolchains. Also define \_\_MACH__ for apple-none-macho, as Swift expects all MachO targets to have that defined.
1 parent 8c0483b commit 653a547

File tree

13 files changed

+175
-90
lines changed

13 files changed

+175
-90
lines changed

clang/lib/Basic/Targets/OSTargets.cpp

-3
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,6 @@ void getDarwinDefines(MacroBuilder &Builder, const LangOptions &Opts,
114114
assert(OsVersion.getMinor().value_or(0) < 100 &&
115115
OsVersion.getSubminor().value_or(0) < 100 && "Invalid version!");
116116
Builder.defineMacro("__ENVIRONMENT_OS_VERSION_MIN_REQUIRED__", Str);
117-
118-
// Tell users about the kernel if there is one.
119-
Builder.defineMacro("__MACH__");
120117
}
121118

122119
PlatformMinVersion = OsVersion;

clang/lib/Driver/Driver.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -6686,6 +6686,8 @@ const ToolChain &Driver::getToolChain(const ArgList &Args,
66866686
TC = std::make_unique<toolchains::BareMetal>(*this, Target, Args);
66876687
else if (Target.isOSBinFormatELF())
66886688
TC = std::make_unique<toolchains::Generic_ELF>(*this, Target, Args);
6689+
else if (Target.isAppleMachO())
6690+
TC = std::make_unique<toolchains::AppleMachO>(*this, Target, Args);
66896691
else if (Target.isOSBinFormatMachO())
66906692
TC = std::make_unique<toolchains::MachO>(*this, Target, Args);
66916693
else

clang/lib/Driver/ToolChains/Darwin.cpp

+66-55
Original file line numberDiff line numberDiff line change
@@ -966,11 +966,14 @@ MachO::MachO(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
966966
getProgramPaths().push_back(getDriver().Dir);
967967
}
968968

969+
AppleMachO::AppleMachO(const Driver &D, const llvm::Triple &Triple,
970+
const ArgList &Args)
971+
: MachO(D, Triple, Args), CudaInstallation(D, Triple, Args),
972+
RocmInstallation(D, Triple, Args), SYCLInstallation(D, Triple, Args) {}
973+
969974
/// Darwin - Darwin tool chain for i386 and x86_64.
970975
Darwin::Darwin(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
971-
: MachO(D, Triple, Args), TargetInitialized(false),
972-
CudaInstallation(D, Triple, Args), RocmInstallation(D, Triple, Args),
973-
SYCLInstallation(D, Triple, Args) {}
976+
: AppleMachO(D, Triple, Args), TargetInitialized(false) {}
974977

975978
types::ID MachO::LookupTypeForExtension(StringRef Ext) const {
976979
types::ID Ty = ToolChain::LookupTypeForExtension(Ext);
@@ -1019,18 +1022,18 @@ bool Darwin::hasBlocksRuntime() const {
10191022
}
10201023
}
10211024

1022-
void Darwin::AddCudaIncludeArgs(const ArgList &DriverArgs,
1023-
ArgStringList &CC1Args) const {
1025+
void AppleMachO::AddCudaIncludeArgs(const ArgList &DriverArgs,
1026+
ArgStringList &CC1Args) const {
10241027
CudaInstallation->AddCudaIncludeArgs(DriverArgs, CC1Args);
10251028
}
10261029

1027-
void Darwin::AddHIPIncludeArgs(const ArgList &DriverArgs,
1028-
ArgStringList &CC1Args) const {
1030+
void AppleMachO::AddHIPIncludeArgs(const ArgList &DriverArgs,
1031+
ArgStringList &CC1Args) const {
10291032
RocmInstallation->AddHIPIncludeArgs(DriverArgs, CC1Args);
10301033
}
10311034

1032-
void Darwin::addSYCLIncludeArgs(const ArgList &DriverArgs,
1033-
ArgStringList &CC1Args) const {
1035+
void AppleMachO::addSYCLIncludeArgs(const ArgList &DriverArgs,
1036+
ArgStringList &CC1Args) const {
10341037
SYCLInstallation->addSYCLIncludeArgs(DriverArgs, CC1Args);
10351038
}
10361039

@@ -1125,6 +1128,8 @@ VersionTuple MachO::getLinkerVersion(const llvm::opt::ArgList &Args) const {
11251128

11261129
Darwin::~Darwin() {}
11271130

1131+
AppleMachO::~AppleMachO() {}
1132+
11281133
MachO::~MachO() {}
11291134

11301135
std::string Darwin::ComputeEffectiveClangTriple(const ArgList &Args,
@@ -2488,7 +2493,7 @@ static void AppendPlatformPrefix(SmallString<128> &Path,
24882493
// Returns the effective sysroot from either -isysroot or --sysroot, plus the
24892494
// platform prefix (if any).
24902495
llvm::SmallString<128>
2491-
DarwinClang::GetEffectiveSysroot(const llvm::opt::ArgList &DriverArgs) const {
2496+
AppleMachO::GetEffectiveSysroot(const llvm::opt::ArgList &DriverArgs) const {
24922497
llvm::SmallString<128> Path("/");
24932498
if (DriverArgs.hasArg(options::OPT_isysroot))
24942499
Path = DriverArgs.getLastArgValue(options::OPT_isysroot);
@@ -2501,8 +2506,9 @@ DarwinClang::GetEffectiveSysroot(const llvm::opt::ArgList &DriverArgs) const {
25012506
return Path;
25022507
}
25032508

2504-
void DarwinClang::AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs,
2505-
llvm::opt::ArgStringList &CC1Args) const {
2509+
void AppleMachO::AddClangSystemIncludeArgs(
2510+
const llvm::opt::ArgList &DriverArgs,
2511+
llvm::opt::ArgStringList &CC1Args) const {
25062512
const Driver &D = getDriver();
25072513

25082514
llvm::SmallString<128> Sysroot = GetEffectiveSysroot(DriverArgs);
@@ -2580,7 +2586,7 @@ bool DarwinClang::AddGnuCPlusPlusIncludePaths(const llvm::opt::ArgList &DriverAr
25802586
return getVFS().exists(Base);
25812587
}
25822588

2583-
void DarwinClang::AddClangCXXStdlibIncludeArgs(
2589+
void AppleMachO::AddClangCXXStdlibIncludeArgs(
25842590
const llvm::opt::ArgList &DriverArgs,
25852591
llvm::opt::ArgStringList &CC1Args) const {
25862592
// The implementation from a base class will pass through the -stdlib to
@@ -2637,55 +2643,60 @@ void DarwinClang::AddClangCXXStdlibIncludeArgs(
26372643
}
26382644

26392645
case ToolChain::CST_Libstdcxx:
2640-
llvm::SmallString<128> UsrIncludeCxx = Sysroot;
2641-
llvm::sys::path::append(UsrIncludeCxx, "usr", "include", "c++");
2642-
2643-
llvm::Triple::ArchType arch = getTriple().getArch();
2644-
bool IsBaseFound = true;
2645-
switch (arch) {
2646-
default: break;
2647-
2648-
case llvm::Triple::x86:
2649-
case llvm::Triple::x86_64:
2650-
IsBaseFound = AddGnuCPlusPlusIncludePaths(DriverArgs, CC1Args, UsrIncludeCxx,
2651-
"4.2.1",
2652-
"i686-apple-darwin10",
2653-
arch == llvm::Triple::x86_64 ? "x86_64" : "");
2654-
IsBaseFound |= AddGnuCPlusPlusIncludePaths(DriverArgs, CC1Args, UsrIncludeCxx,
2655-
"4.0.0", "i686-apple-darwin8",
2656-
"");
2657-
break;
2646+
AddGnuCPlusPlusIncludePaths(DriverArgs, CC1Args);
2647+
break;
2648+
}
2649+
}
26582650

2659-
case llvm::Triple::arm:
2660-
case llvm::Triple::thumb:
2661-
IsBaseFound = AddGnuCPlusPlusIncludePaths(DriverArgs, CC1Args, UsrIncludeCxx,
2662-
"4.2.1",
2663-
"arm-apple-darwin10",
2664-
"v7");
2665-
IsBaseFound |= AddGnuCPlusPlusIncludePaths(DriverArgs, CC1Args, UsrIncludeCxx,
2666-
"4.2.1",
2667-
"arm-apple-darwin10",
2668-
"v6");
2669-
break;
2651+
void AppleMachO::AddGnuCPlusPlusIncludePaths(
2652+
const llvm::opt::ArgList &DriverArgs,
2653+
llvm::opt::ArgStringList &CC1Args) const {}
26702654

2671-
case llvm::Triple::aarch64:
2672-
IsBaseFound = AddGnuCPlusPlusIncludePaths(DriverArgs, CC1Args, UsrIncludeCxx,
2673-
"4.2.1",
2674-
"arm64-apple-darwin10",
2675-
"");
2676-
break;
2677-
}
2655+
void DarwinClang::AddGnuCPlusPlusIncludePaths(
2656+
const llvm::opt::ArgList &DriverArgs,
2657+
llvm::opt::ArgStringList &CC1Args) const {
2658+
llvm::SmallString<128> UsrIncludeCxx = GetEffectiveSysroot(DriverArgs);
2659+
llvm::sys::path::append(UsrIncludeCxx, "usr", "include", "c++");
26782660

2679-
if (!IsBaseFound) {
2680-
getDriver().Diag(diag::warn_drv_libstdcxx_not_found);
2681-
}
2661+
llvm::Triple::ArchType arch = getTriple().getArch();
2662+
bool IsBaseFound = true;
2663+
switch (arch) {
2664+
default:
2665+
break;
26822666

2667+
case llvm::Triple::x86:
2668+
case llvm::Triple::x86_64:
2669+
IsBaseFound = AddGnuCPlusPlusIncludePaths(
2670+
DriverArgs, CC1Args, UsrIncludeCxx, "4.2.1", "i686-apple-darwin10",
2671+
arch == llvm::Triple::x86_64 ? "x86_64" : "");
2672+
IsBaseFound |= AddGnuCPlusPlusIncludePaths(
2673+
DriverArgs, CC1Args, UsrIncludeCxx, "4.0.0", "i686-apple-darwin8", "");
2674+
break;
2675+
2676+
case llvm::Triple::arm:
2677+
case llvm::Triple::thumb:
2678+
IsBaseFound =
2679+
AddGnuCPlusPlusIncludePaths(DriverArgs, CC1Args, UsrIncludeCxx, "4.2.1",
2680+
"arm-apple-darwin10", "v7");
2681+
IsBaseFound |=
2682+
AddGnuCPlusPlusIncludePaths(DriverArgs, CC1Args, UsrIncludeCxx, "4.2.1",
2683+
"arm-apple-darwin10", "v6");
2684+
break;
2685+
2686+
case llvm::Triple::aarch64:
2687+
IsBaseFound =
2688+
AddGnuCPlusPlusIncludePaths(DriverArgs, CC1Args, UsrIncludeCxx, "4.2.1",
2689+
"arm64-apple-darwin10", "");
26832690
break;
26842691
}
2692+
2693+
if (!IsBaseFound) {
2694+
getDriver().Diag(diag::warn_drv_libstdcxx_not_found);
2695+
}
26852696
}
26862697

2687-
void DarwinClang::AddCXXStdlibLibArgs(const ArgList &Args,
2688-
ArgStringList &CmdArgs) const {
2698+
void AppleMachO::AddCXXStdlibLibArgs(const ArgList &Args,
2699+
ArgStringList &CmdArgs) const {
26892700
CXXStdlibType Type = GetCXXStdlibType(Args);
26902701

26912702
switch (Type) {
@@ -3621,7 +3632,7 @@ SanitizerMask Darwin::getSupportedSanitizers() const {
36213632
return Res;
36223633
}
36233634

3624-
void Darwin::printVerboseInfo(raw_ostream &OS) const {
3635+
void AppleMachO::printVerboseInfo(raw_ostream &OS) const {
36253636
CudaInstallation->print(OS);
36263637
RocmInstallation->print(OS);
36273638
}

clang/lib/Driver/ToolChains/Darwin.h

+50-28
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,52 @@ class LLVM_LIBRARY_VISIBILITY MachO : public ToolChain {
291291
/// }
292292
};
293293

294+
/// Apple specific MachO extensions
295+
class LLVM_LIBRARY_VISIBILITY AppleMachO : public MachO {
296+
public:
297+
AppleMachO(const Driver &D, const llvm::Triple &Triple,
298+
const llvm::opt::ArgList &Args);
299+
~AppleMachO() override;
300+
301+
/// }
302+
/// @name Apple Specific ToolChain Implementation
303+
/// {
304+
void
305+
AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs,
306+
llvm::opt::ArgStringList &CC1Args) const override;
307+
308+
void AddCudaIncludeArgs(const llvm::opt::ArgList &DriverArgs,
309+
llvm::opt::ArgStringList &CC1Args) const override;
310+
void AddHIPIncludeArgs(const llvm::opt::ArgList &DriverArgs,
311+
llvm::opt::ArgStringList &CC1Args) const override;
312+
void addSYCLIncludeArgs(const llvm::opt::ArgList &DriverArgs,
313+
llvm::opt::ArgStringList &CC1Args) const override;
314+
315+
void AddClangCXXStdlibIncludeArgs(
316+
const llvm::opt::ArgList &DriverArgs,
317+
llvm::opt::ArgStringList &CC1Args) const override;
318+
void AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args,
319+
llvm::opt::ArgStringList &CmdArgs) const override;
320+
321+
void printVerboseInfo(raw_ostream &OS) const override;
322+
/// }
323+
324+
LazyDetector<CudaInstallationDetector> CudaInstallation;
325+
LazyDetector<RocmInstallationDetector> RocmInstallation;
326+
LazyDetector<SYCLInstallationDetector> SYCLInstallation;
327+
328+
protected:
329+
llvm::SmallString<128>
330+
GetEffectiveSysroot(const llvm::opt::ArgList &DriverArgs) const;
331+
332+
private:
333+
virtual void
334+
AddGnuCPlusPlusIncludePaths(const llvm::opt::ArgList &DriverArgs,
335+
llvm::opt::ArgStringList &CC1Args) const;
336+
};
337+
294338
/// Darwin - The base Darwin tool chain.
295-
class LLVM_LIBRARY_VISIBILITY Darwin : public MachO {
339+
class LLVM_LIBRARY_VISIBILITY Darwin : public AppleMachO {
296340
public:
297341
/// Whether the information on the target has been initialized.
298342
//
@@ -330,10 +374,6 @@ class LLVM_LIBRARY_VISIBILITY Darwin : public MachO {
330374
/// The target variant triple that was specified (if any).
331375
mutable std::optional<llvm::Triple> TargetVariantTriple;
332376

333-
LazyDetector<CudaInstallationDetector> CudaInstallation;
334-
LazyDetector<RocmInstallationDetector> RocmInstallation;
335-
LazyDetector<SYCLInstallationDetector> SYCLInstallation;
336-
337377
private:
338378
void AddDeploymentTarget(llvm::opt::DerivedArgList &Args) const;
339379

@@ -345,7 +385,7 @@ class LLVM_LIBRARY_VISIBILITY Darwin : public MachO {
345385
std::string ComputeEffectiveClangTriple(const llvm::opt::ArgList &Args,
346386
types::ID InputType) const override;
347387

348-
/// @name Apple Specific Toolchain Implementation
388+
/// @name Darwin Specific Toolchain Implementation
349389
/// {
350390

351391
void addMinVersionArgs(const llvm::opt::ArgList &Args,
@@ -561,13 +601,6 @@ class LLVM_LIBRARY_VISIBILITY Darwin : public MachO {
561601
ObjCRuntime getDefaultObjCRuntime(bool isNonFragile) const override;
562602
bool hasBlocksRuntime() const override;
563603

564-
void AddCudaIncludeArgs(const llvm::opt::ArgList &DriverArgs,
565-
llvm::opt::ArgStringList &CC1Args) const override;
566-
void AddHIPIncludeArgs(const llvm::opt::ArgList &DriverArgs,
567-
llvm::opt::ArgStringList &CC1Args) const override;
568-
void addSYCLIncludeArgs(const llvm::opt::ArgList &DriverArgs,
569-
llvm::opt::ArgStringList &CC1Args) const override;
570-
571604
bool UseObjCMixedDispatch() const override {
572605
// This is only used with the non-fragile ABI and non-legacy dispatch.
573606

@@ -598,8 +631,6 @@ class LLVM_LIBRARY_VISIBILITY Darwin : public MachO {
598631
bool SupportsEmbeddedBitcode() const override;
599632

600633
SanitizerMask getSupportedSanitizers() const override;
601-
602-
void printVerboseInfo(raw_ostream &OS) const override;
603634
};
604635

605636
/// DarwinClang - The Darwin toolchain used by Clang.
@@ -617,16 +648,6 @@ class LLVM_LIBRARY_VISIBILITY DarwinClang : public Darwin {
617648
llvm::opt::ArgStringList &CmdArgs,
618649
bool ForceLinkBuiltinRT = false) const override;
619650

620-
void AddClangCXXStdlibIncludeArgs(
621-
const llvm::opt::ArgList &DriverArgs,
622-
llvm::opt::ArgStringList &CC1Args) const override;
623-
624-
void AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs,
625-
llvm::opt::ArgStringList &CC1Args) const override;
626-
627-
void AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args,
628-
llvm::opt::ArgStringList &CmdArgs) const override;
629-
630651
void AddCCKextLibArgs(const llvm::opt::ArgList &Args,
631652
llvm::opt::ArgStringList &CmdArgs) const override;
632653

@@ -651,15 +672,16 @@ class LLVM_LIBRARY_VISIBILITY DarwinClang : public Darwin {
651672
StringRef Sanitizer,
652673
bool shared = true) const;
653674

675+
void
676+
AddGnuCPlusPlusIncludePaths(const llvm::opt::ArgList &DriverArgs,
677+
llvm::opt::ArgStringList &CC1Args) const override;
678+
654679
bool AddGnuCPlusPlusIncludePaths(const llvm::opt::ArgList &DriverArgs,
655680
llvm::opt::ArgStringList &CC1Args,
656681
llvm::SmallString<128> Base,
657682
llvm::StringRef Version,
658683
llvm::StringRef ArchDir,
659684
llvm::StringRef BitDir) const;
660-
661-
llvm::SmallString<128>
662-
GetEffectiveSysroot(const llvm::opt::ArgList &DriverArgs) const;
663685
};
664686

665687
} // end namespace toolchains

clang/lib/Frontend/InitPreprocessor.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -1507,6 +1507,11 @@ static void InitializePredefinedMacros(const TargetInfo &TI,
15071507
// ELF targets define __ELF__
15081508
if (TI.getTriple().isOSBinFormatELF())
15091509
Builder.defineMacro("__ELF__");
1510+
else if (TI.getTriple().isAppleMachO())
1511+
// Apple MachO targets define __MACH__ even when not using DarwinTargetInfo.
1512+
// Hurd will also define this in some circumstances, but that's done in
1513+
// HurdTargetInfo. Windows targets don't define this.
1514+
Builder.defineMacro("__MACH__");
15101515

15111516
// Target OS macro definitions.
15121517
if (PPOpts.DefineTargetOSMacros) {

clang/lib/Lex/InitHeaderSearch.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ bool InitHeaderSearch::ShouldAddDefaultIncludePaths(
313313
break;
314314

315315
case llvm::Triple::UnknownOS:
316-
if (triple.isWasm())
316+
if (triple.isWasm() || triple.isAppleMachO())
317317
return false;
318318
break;
319319

clang/test/Driver/Inputs/MacOSX15.1.sdk/embedded/usr/include/.keep

Whitespace-only changes.

clang/test/Driver/Inputs/MacOSX15.1.sdk/embedded/usr/local/include/.keep

Whitespace-only changes.

clang/test/Driver/Inputs/MacOSX15.1.sdk/usr/include/c++/v1/.keep

Whitespace-only changes.

clang/test/Driver/Inputs/MacOSX15.1.sdk/usr/local/include/.keep

Whitespace-only changes.

0 commit comments

Comments
 (0)