Skip to content

Commit

Permalink
[Darwin][Driver][clang] apple-none-macho orders the resource director…
Browse files Browse the repository at this point in the history
…y after internal-externc-isystem when nostdlibinc is used (llvm#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.
  • Loading branch information
ian-twilightcoder authored Jan 7, 2025
1 parent 8c0483b commit 653a547
Show file tree
Hide file tree
Showing 13 changed files with 175 additions and 90 deletions.
3 changes: 0 additions & 3 deletions clang/lib/Basic/Targets/OSTargets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,6 @@ void getDarwinDefines(MacroBuilder &Builder, const LangOptions &Opts,
assert(OsVersion.getMinor().value_or(0) < 100 &&
OsVersion.getSubminor().value_or(0) < 100 && "Invalid version!");
Builder.defineMacro("__ENVIRONMENT_OS_VERSION_MIN_REQUIRED__", Str);

// Tell users about the kernel if there is one.
Builder.defineMacro("__MACH__");
}

PlatformMinVersion = OsVersion;
Expand Down
2 changes: 2 additions & 0 deletions clang/lib/Driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6686,6 +6686,8 @@ const ToolChain &Driver::getToolChain(const ArgList &Args,
TC = std::make_unique<toolchains::BareMetal>(*this, Target, Args);
else if (Target.isOSBinFormatELF())
TC = std::make_unique<toolchains::Generic_ELF>(*this, Target, Args);
else if (Target.isAppleMachO())
TC = std::make_unique<toolchains::AppleMachO>(*this, Target, Args);
else if (Target.isOSBinFormatMachO())
TC = std::make_unique<toolchains::MachO>(*this, Target, Args);
else
Expand Down
121 changes: 66 additions & 55 deletions clang/lib/Driver/ToolChains/Darwin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -966,11 +966,14 @@ MachO::MachO(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
getProgramPaths().push_back(getDriver().Dir);
}

AppleMachO::AppleMachO(const Driver &D, const llvm::Triple &Triple,
const ArgList &Args)
: MachO(D, Triple, Args), CudaInstallation(D, Triple, Args),
RocmInstallation(D, Triple, Args), SYCLInstallation(D, Triple, Args) {}

/// Darwin - Darwin tool chain for i386 and x86_64.
Darwin::Darwin(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
: MachO(D, Triple, Args), TargetInitialized(false),
CudaInstallation(D, Triple, Args), RocmInstallation(D, Triple, Args),
SYCLInstallation(D, Triple, Args) {}
: AppleMachO(D, Triple, Args), TargetInitialized(false) {}

types::ID MachO::LookupTypeForExtension(StringRef Ext) const {
types::ID Ty = ToolChain::LookupTypeForExtension(Ext);
Expand Down Expand Up @@ -1019,18 +1022,18 @@ bool Darwin::hasBlocksRuntime() const {
}
}

void Darwin::AddCudaIncludeArgs(const ArgList &DriverArgs,
ArgStringList &CC1Args) const {
void AppleMachO::AddCudaIncludeArgs(const ArgList &DriverArgs,
ArgStringList &CC1Args) const {
CudaInstallation->AddCudaIncludeArgs(DriverArgs, CC1Args);
}

void Darwin::AddHIPIncludeArgs(const ArgList &DriverArgs,
ArgStringList &CC1Args) const {
void AppleMachO::AddHIPIncludeArgs(const ArgList &DriverArgs,
ArgStringList &CC1Args) const {
RocmInstallation->AddHIPIncludeArgs(DriverArgs, CC1Args);
}

void Darwin::addSYCLIncludeArgs(const ArgList &DriverArgs,
ArgStringList &CC1Args) const {
void AppleMachO::addSYCLIncludeArgs(const ArgList &DriverArgs,
ArgStringList &CC1Args) const {
SYCLInstallation->addSYCLIncludeArgs(DriverArgs, CC1Args);
}

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

Darwin::~Darwin() {}

AppleMachO::~AppleMachO() {}

MachO::~MachO() {}

std::string Darwin::ComputeEffectiveClangTriple(const ArgList &Args,
Expand Down Expand Up @@ -2488,7 +2493,7 @@ static void AppendPlatformPrefix(SmallString<128> &Path,
// Returns the effective sysroot from either -isysroot or --sysroot, plus the
// platform prefix (if any).
llvm::SmallString<128>
DarwinClang::GetEffectiveSysroot(const llvm::opt::ArgList &DriverArgs) const {
AppleMachO::GetEffectiveSysroot(const llvm::opt::ArgList &DriverArgs) const {
llvm::SmallString<128> Path("/");
if (DriverArgs.hasArg(options::OPT_isysroot))
Path = DriverArgs.getLastArgValue(options::OPT_isysroot);
Expand All @@ -2501,8 +2506,9 @@ DarwinClang::GetEffectiveSysroot(const llvm::opt::ArgList &DriverArgs) const {
return Path;
}

void DarwinClang::AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs,
llvm::opt::ArgStringList &CC1Args) const {
void AppleMachO::AddClangSystemIncludeArgs(
const llvm::opt::ArgList &DriverArgs,
llvm::opt::ArgStringList &CC1Args) const {
const Driver &D = getDriver();

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

void DarwinClang::AddClangCXXStdlibIncludeArgs(
void AppleMachO::AddClangCXXStdlibIncludeArgs(
const llvm::opt::ArgList &DriverArgs,
llvm::opt::ArgStringList &CC1Args) const {
// The implementation from a base class will pass through the -stdlib to
Expand Down Expand Up @@ -2637,55 +2643,60 @@ void DarwinClang::AddClangCXXStdlibIncludeArgs(
}

case ToolChain::CST_Libstdcxx:
llvm::SmallString<128> UsrIncludeCxx = Sysroot;
llvm::sys::path::append(UsrIncludeCxx, "usr", "include", "c++");

llvm::Triple::ArchType arch = getTriple().getArch();
bool IsBaseFound = true;
switch (arch) {
default: break;

case llvm::Triple::x86:
case llvm::Triple::x86_64:
IsBaseFound = AddGnuCPlusPlusIncludePaths(DriverArgs, CC1Args, UsrIncludeCxx,
"4.2.1",
"i686-apple-darwin10",
arch == llvm::Triple::x86_64 ? "x86_64" : "");
IsBaseFound |= AddGnuCPlusPlusIncludePaths(DriverArgs, CC1Args, UsrIncludeCxx,
"4.0.0", "i686-apple-darwin8",
"");
break;
AddGnuCPlusPlusIncludePaths(DriverArgs, CC1Args);
break;
}
}

case llvm::Triple::arm:
case llvm::Triple::thumb:
IsBaseFound = AddGnuCPlusPlusIncludePaths(DriverArgs, CC1Args, UsrIncludeCxx,
"4.2.1",
"arm-apple-darwin10",
"v7");
IsBaseFound |= AddGnuCPlusPlusIncludePaths(DriverArgs, CC1Args, UsrIncludeCxx,
"4.2.1",
"arm-apple-darwin10",
"v6");
break;
void AppleMachO::AddGnuCPlusPlusIncludePaths(
const llvm::opt::ArgList &DriverArgs,
llvm::opt::ArgStringList &CC1Args) const {}

case llvm::Triple::aarch64:
IsBaseFound = AddGnuCPlusPlusIncludePaths(DriverArgs, CC1Args, UsrIncludeCxx,
"4.2.1",
"arm64-apple-darwin10",
"");
break;
}
void DarwinClang::AddGnuCPlusPlusIncludePaths(
const llvm::opt::ArgList &DriverArgs,
llvm::opt::ArgStringList &CC1Args) const {
llvm::SmallString<128> UsrIncludeCxx = GetEffectiveSysroot(DriverArgs);
llvm::sys::path::append(UsrIncludeCxx, "usr", "include", "c++");

if (!IsBaseFound) {
getDriver().Diag(diag::warn_drv_libstdcxx_not_found);
}
llvm::Triple::ArchType arch = getTriple().getArch();
bool IsBaseFound = true;
switch (arch) {
default:
break;

case llvm::Triple::x86:
case llvm::Triple::x86_64:
IsBaseFound = AddGnuCPlusPlusIncludePaths(
DriverArgs, CC1Args, UsrIncludeCxx, "4.2.1", "i686-apple-darwin10",
arch == llvm::Triple::x86_64 ? "x86_64" : "");
IsBaseFound |= AddGnuCPlusPlusIncludePaths(
DriverArgs, CC1Args, UsrIncludeCxx, "4.0.0", "i686-apple-darwin8", "");
break;

case llvm::Triple::arm:
case llvm::Triple::thumb:
IsBaseFound =
AddGnuCPlusPlusIncludePaths(DriverArgs, CC1Args, UsrIncludeCxx, "4.2.1",
"arm-apple-darwin10", "v7");
IsBaseFound |=
AddGnuCPlusPlusIncludePaths(DriverArgs, CC1Args, UsrIncludeCxx, "4.2.1",
"arm-apple-darwin10", "v6");
break;

case llvm::Triple::aarch64:
IsBaseFound =
AddGnuCPlusPlusIncludePaths(DriverArgs, CC1Args, UsrIncludeCxx, "4.2.1",
"arm64-apple-darwin10", "");
break;
}

if (!IsBaseFound) {
getDriver().Diag(diag::warn_drv_libstdcxx_not_found);
}
}

void DarwinClang::AddCXXStdlibLibArgs(const ArgList &Args,
ArgStringList &CmdArgs) const {
void AppleMachO::AddCXXStdlibLibArgs(const ArgList &Args,
ArgStringList &CmdArgs) const {
CXXStdlibType Type = GetCXXStdlibType(Args);

switch (Type) {
Expand Down Expand Up @@ -3621,7 +3632,7 @@ SanitizerMask Darwin::getSupportedSanitizers() const {
return Res;
}

void Darwin::printVerboseInfo(raw_ostream &OS) const {
void AppleMachO::printVerboseInfo(raw_ostream &OS) const {
CudaInstallation->print(OS);
RocmInstallation->print(OS);
}
78 changes: 50 additions & 28 deletions clang/lib/Driver/ToolChains/Darwin.h
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,52 @@ class LLVM_LIBRARY_VISIBILITY MachO : public ToolChain {
/// }
};

/// Apple specific MachO extensions
class LLVM_LIBRARY_VISIBILITY AppleMachO : public MachO {
public:
AppleMachO(const Driver &D, const llvm::Triple &Triple,
const llvm::opt::ArgList &Args);
~AppleMachO() override;

/// }
/// @name Apple Specific ToolChain Implementation
/// {
void
AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs,
llvm::opt::ArgStringList &CC1Args) const override;

void AddCudaIncludeArgs(const llvm::opt::ArgList &DriverArgs,
llvm::opt::ArgStringList &CC1Args) const override;
void AddHIPIncludeArgs(const llvm::opt::ArgList &DriverArgs,
llvm::opt::ArgStringList &CC1Args) const override;
void addSYCLIncludeArgs(const llvm::opt::ArgList &DriverArgs,
llvm::opt::ArgStringList &CC1Args) const override;

void AddClangCXXStdlibIncludeArgs(
const llvm::opt::ArgList &DriverArgs,
llvm::opt::ArgStringList &CC1Args) const override;
void AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args,
llvm::opt::ArgStringList &CmdArgs) const override;

void printVerboseInfo(raw_ostream &OS) const override;
/// }

LazyDetector<CudaInstallationDetector> CudaInstallation;
LazyDetector<RocmInstallationDetector> RocmInstallation;
LazyDetector<SYCLInstallationDetector> SYCLInstallation;

protected:
llvm::SmallString<128>
GetEffectiveSysroot(const llvm::opt::ArgList &DriverArgs) const;

private:
virtual void
AddGnuCPlusPlusIncludePaths(const llvm::opt::ArgList &DriverArgs,
llvm::opt::ArgStringList &CC1Args) const;
};

/// Darwin - The base Darwin tool chain.
class LLVM_LIBRARY_VISIBILITY Darwin : public MachO {
class LLVM_LIBRARY_VISIBILITY Darwin : public AppleMachO {
public:
/// Whether the information on the target has been initialized.
//
Expand Down Expand Up @@ -330,10 +374,6 @@ class LLVM_LIBRARY_VISIBILITY Darwin : public MachO {
/// The target variant triple that was specified (if any).
mutable std::optional<llvm::Triple> TargetVariantTriple;

LazyDetector<CudaInstallationDetector> CudaInstallation;
LazyDetector<RocmInstallationDetector> RocmInstallation;
LazyDetector<SYCLInstallationDetector> SYCLInstallation;

private:
void AddDeploymentTarget(llvm::opt::DerivedArgList &Args) const;

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

/// @name Apple Specific Toolchain Implementation
/// @name Darwin Specific Toolchain Implementation
/// {

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

void AddCudaIncludeArgs(const llvm::opt::ArgList &DriverArgs,
llvm::opt::ArgStringList &CC1Args) const override;
void AddHIPIncludeArgs(const llvm::opt::ArgList &DriverArgs,
llvm::opt::ArgStringList &CC1Args) const override;
void addSYCLIncludeArgs(const llvm::opt::ArgList &DriverArgs,
llvm::opt::ArgStringList &CC1Args) const override;

bool UseObjCMixedDispatch() const override {
// This is only used with the non-fragile ABI and non-legacy dispatch.

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

SanitizerMask getSupportedSanitizers() const override;

void printVerboseInfo(raw_ostream &OS) const override;
};

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

void AddClangCXXStdlibIncludeArgs(
const llvm::opt::ArgList &DriverArgs,
llvm::opt::ArgStringList &CC1Args) const override;

void AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs,
llvm::opt::ArgStringList &CC1Args) const override;

void AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args,
llvm::opt::ArgStringList &CmdArgs) const override;

void AddCCKextLibArgs(const llvm::opt::ArgList &Args,
llvm::opt::ArgStringList &CmdArgs) const override;

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

void
AddGnuCPlusPlusIncludePaths(const llvm::opt::ArgList &DriverArgs,
llvm::opt::ArgStringList &CC1Args) const override;

bool AddGnuCPlusPlusIncludePaths(const llvm::opt::ArgList &DriverArgs,
llvm::opt::ArgStringList &CC1Args,
llvm::SmallString<128> Base,
llvm::StringRef Version,
llvm::StringRef ArchDir,
llvm::StringRef BitDir) const;

llvm::SmallString<128>
GetEffectiveSysroot(const llvm::opt::ArgList &DriverArgs) const;
};

} // end namespace toolchains
Expand Down
5 changes: 5 additions & 0 deletions clang/lib/Frontend/InitPreprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1507,6 +1507,11 @@ static void InitializePredefinedMacros(const TargetInfo &TI,
// ELF targets define __ELF__
if (TI.getTriple().isOSBinFormatELF())
Builder.defineMacro("__ELF__");
else if (TI.getTriple().isAppleMachO())
// Apple MachO targets define __MACH__ even when not using DarwinTargetInfo.
// Hurd will also define this in some circumstances, but that's done in
// HurdTargetInfo. Windows targets don't define this.
Builder.defineMacro("__MACH__");

// Target OS macro definitions.
if (PPOpts.DefineTargetOSMacros) {
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Lex/InitHeaderSearch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ bool InitHeaderSearch::ShouldAddDefaultIncludePaths(
break;

case llvm::Triple::UnknownOS:
if (triple.isWasm())
if (triple.isWasm() || triple.isAppleMachO())
return false;
break;

Expand Down
Empty file.
Empty file.
Empty file.
Empty file.
Loading

0 comments on commit 653a547

Please sign in to comment.