diff --git a/clang/lib/Driver/ToolChains/Darwin.cpp b/clang/lib/Driver/ToolChains/Darwin.cpp --- a/clang/lib/Driver/ToolChains/Darwin.cpp +++ b/clang/lib/Driver/ToolChains/Darwin.cpp @@ -18,7 +18,9 @@ #include "clang/Driver/DriverDiagnostic.h" #include "clang/Driver/Options.h" #include "clang/Driver/SanitizerArgs.h" +#include "llvm/ADT/StringRef.h" #include "llvm/ADT/StringSwitch.h" +#include "llvm/ADT/Triple.h" #include "llvm/Option/ArgList.h" #include "llvm/ProfileData/InstrProf.h" #include "llvm/Support/Path.h" @@ -2039,6 +2041,30 @@ } // namespace +static const std::string guessSDKPath(const llvm::Triple::OSType OSType) { + std::string Platform; + + if (OSType == llvm::Triple::OSType::MacOSX || OSType == llvm::Triple::OSType::Darwin) { + // Assume macOS when nothing specific is specified. + Platform = "MacOSX"; + } else if (OSType == llvm::Triple::OSType::IOS) { + Platform = "IPhoneOS"; + } else if (OSType == llvm::Triple::OSType::TvOS) { + Platform = "AppleTVOS"; + } else if (OSType == llvm::Triple::OSType::WatchOS) { + Platform = "WatchOS"; + } + + std::string GuessPath = ( + "/Applications/Xcode.app/Contents/Developer/Platforms/" + + Platform + + ".platform/Developer/SDKs/" + + Platform + + ".sdk"); + + return GuessPath; +} + void Darwin::AddDeploymentTarget(DerivedArgList &Args) const { const OptTable &Opts = getDriver().getOpts(); @@ -2058,6 +2084,14 @@ Args.append(Args.MakeSeparateArg( nullptr, Opts.getOption(options::OPT_isysroot), env)); } + } else { + // If the user doesn't pass -isysroot nor SDKROOT we will try to guess + // the standard path of the SDK (in /Applications/Xcode.app ...) + const std::string GuessPath = guessSDKPath(getTriple().getOS()); + if (getVFS().exists(GuessPath)) { + Args.append(Args.MakeSeparateArg( + nullptr, Opts.getOption(options::OPT_isysroot), GuessPath)); + } } } @@ -2251,6 +2285,7 @@ return DriverArgs.getLastArgValue(options::OPT_isysroot); if (!getDriver().SysRoot.empty()) return getDriver().SysRoot; + return "/"; }