From a5f446bc4bb1ac78d6852cc8e251a1229899b783 Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Thu, 19 Jan 2023 20:04:38 -0500 Subject: [PATCH] Revert "[clang][Darwin] Try to guess the SDK root with xcrun when unspecified" This reverts commit ecade80d93960ad01d8665db02c23841e055a80f. Breaks tests on macOS and tries to run xcrun on non-mac platforms, see comments on https://reviews.llvm.org/D136315 --- clang/docs/UsersManual.rst | 14 ----- clang/lib/Driver/ToolChains/Darwin.cpp | 97 ++++------------------------------ clang/test/Driver/darwin-sdk-detect.c | 20 ------- 3 files changed, 11 insertions(+), 120 deletions(-) delete mode 100644 clang/test/Driver/darwin-sdk-detect.c diff --git a/clang/docs/UsersManual.rst b/clang/docs/UsersManual.rst index e0030d2..7dd8ecb 100644 --- a/clang/docs/UsersManual.rst +++ b/clang/docs/UsersManual.rst @@ -3824,20 +3824,6 @@ backend. Operating System Features and Limitations ----------------------------------------- -Apple -^^^^^ - -On Apple platforms, the standard headers and libraries are not provided by -the base system and are instead part of the Xcode SDK application. The location -of the SDK is determined any of the following ways: - -- If passed to Clang, the ``-isysroot`` option specifies the path to the SDK. - -- If the sysroot isn't provided, the ``SDKROOT`` environment variable is checked. - This variable is set by various Xcode tools. - -- Otherwise, Clang uses Xcode's ``xcrun`` tool to find the SDK. - Windows ^^^^^^^ diff --git a/clang/lib/Driver/ToolChains/Darwin.cpp b/clang/lib/Driver/ToolChains/Darwin.cpp index 03c28c1..9f95c96 100644 --- a/clang/lib/Driver/ToolChains/Darwin.cpp +++ b/clang/lib/Driver/ToolChains/Darwin.cpp @@ -18,22 +18,15 @@ #include "clang/Driver/DriverDiagnostic.h" #include "clang/Driver/Options.h" #include "clang/Driver/SanitizerArgs.h" -#include "llvm/ADT/ArrayRef.h" -#include "llvm/ADT/Optional.h" -#include "llvm/ADT/SmallString.h" #include "llvm/ADT/StringSwitch.h" #include "llvm/Option/ArgList.h" #include "llvm/ProfileData/InstrProf.h" -#include "llvm/Support/FileSystem.h" -#include "llvm/Support/FileUtilities.h" #include "llvm/Support/Path.h" -#include "llvm/Support/Program.h" #include "llvm/Support/ScopedPrinter.h" #include "llvm/Support/TargetParser.h" #include "llvm/Support/Threading.h" #include "llvm/Support/VirtualFileSystem.h" #include // ::getenv -#include // std::unique_ptr using namespace clang::driver; using namespace clang::driver::tools; @@ -2085,89 +2078,21 @@ std::optional parseSDKSettings(llvm::vfs::FileSystem &VFS, void Darwin::AddDeploymentTarget(DerivedArgList &Args) const { const OptTable &Opts = getDriver().getOpts(); - // On Apple platforms, standard headers and libraries are not provided with - // the base system (e.g. in /usr/{include,lib}). Instead, they are provided - // in various SDKs for the different Apple platforms. Clang needs to know - // where that SDK lives, and there are a couple ways this can be achieved: - // - // (1) If `-isysroot ` is passed explicitly, use that. + // Support allowing the SDKROOT environment variable used by xcrun and other + // Xcode tools to define the default sysroot, by making it the default for + // isysroot. if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) { // Warn if the path does not exist. if (!getVFS().exists(A->getValue())) getDriver().Diag(clang::diag::warn_missing_sysroot) << A->getValue(); - } - - // (2) If the SDKROOT environment variable is defined and points to a valid - // path, use that. $SDKROOT is set by `xcrun` and other Xcode tools, so - // running `xcrun clang` will work by going through this path. - else if (char *env = ::getenv("SDKROOT")) { - // We only use this value as the default if it is an absolute path, - // exists, and it is not the root path. - if (llvm::sys::path::is_absolute(env) && getVFS().exists(env) && - StringRef(env) != "/") { - Args.append(Args.MakeSeparateArg( - nullptr, Opts.getOption(options::OPT_isysroot), env)); - } - } - - // (3) Otherwise, we try to guess the path of the default SDK by running - // `xcrun --show-sdk-path`. This won't always be correct, but if the - // user wants to use the non-default SDK, they should specify it - // explicitly with methods (1) or (2) above. - else { - llvm::SmallString<64> OutputFile; - llvm::sys::fs::createTemporaryFile("print-sdk-path", "" /* No Suffix */, - OutputFile); - llvm::FileRemover OutputRemover(OutputFile.c_str()); - std::optional Redirects[] = {{""}, OutputFile.str(), {""}}; - - Optional SDKName = std::nullopt; - switch (getTriple().getOS()) { - case llvm::Triple::OSType::WatchOS: - if (getTriple().isSimulatorEnvironment()) - SDKName = "watchsimulator"; - else - SDKName = "watchos"; - break; - case llvm::Triple::OSType::TvOS: - if (getTriple().isSimulatorEnvironment()) - SDKName = "appletvsimulator"; - else - SDKName = "appletvos"; - break; - case llvm::Triple::OSType::IOS: - if (getTriple().isSimulatorEnvironment()) - SDKName = "iphonesimulator"; - else - SDKName = "iphoneos"; - break; - case llvm::Triple::OSType::Darwin: - case llvm::Triple::OSType::MacOSX: - SDKName = "macosx"; - break; - case llvm::Triple::OSType::DriverKit: - SDKName = "driverkit"; - break; - default: - llvm_unreachable("unknown kind of Darwin platform"); - } - - if (SDKName) { - int Result = llvm::sys::ExecuteAndWait( - "/usr/bin/xcrun", - {"/usr/bin/xcrun", "--sdk", *SDKName, "--show-sdk-path"}, - /* Inherit environment from parent process */ std::nullopt, Redirects, - /* SecondsToWait */ 0, /*MemoryLimit*/ 0); - if (Result == 0) { - llvm::ErrorOr> OutputBuf = - llvm::MemoryBuffer::getFile(OutputFile.c_str(), /* IsText */ true); - if (OutputBuf) { - llvm::StringRef SdkPath = (*OutputBuf)->getBuffer().trim(); - if (getVFS().exists(SdkPath)) { - Args.append(Args.MakeSeparateArg( - nullptr, Opts.getOption(options::OPT_isysroot), SdkPath)); - } - } + } else { + if (char *env = ::getenv("SDKROOT")) { + // We only use this value as the default if it is an absolute path, + // exists, and it is not the root path. + if (llvm::sys::path::is_absolute(env) && getVFS().exists(env) && + StringRef(env) != "/") { + Args.append(Args.MakeSeparateArg( + nullptr, Opts.getOption(options::OPT_isysroot), env)); } } } diff --git a/clang/test/Driver/darwin-sdk-detect.c b/clang/test/Driver/darwin-sdk-detect.c deleted file mode 100644 index dff4def..0000000 --- a/clang/test/Driver/darwin-sdk-detect.c +++ /dev/null @@ -1,20 +0,0 @@ -// REQUIRES: system-darwin - -// Check that we default to running `xcrun --show-sdk-path` if there is no -// SDKROOT defined in the environment. -// -// RUN: env -u SDKROOT %clang -target x86_64-apple-darwin -c %s -### 2> %t.log -// RUN: FileCheck --check-prefix=CHECK-XC < %t.log %s -// -// CHECK-XC: clang -// CHECK-XC: "-cc1" -// CHECK-XC: "-isysroot" "{{.*MacOSX[0-9\.]*\.sdk}}" - -// Check once again that we default to running `xcrun`, this time with another target. -// -// RUN: env -u SDKROOT %clang -target arm64-apple-ios -c %s -### 2> %t.log -// RUN: FileCheck --check-prefix=CHECK-XC-IOS < %t.log %s -// -// CHECK-XC-IOS: clang -// CHECK-XC-IOS: "-cc1" -// CHECK-XC-IOS: "-isysroot" "{{.*iPhoneOS[0-9\.]*\.sdk}}" -- 2.7.4