Skip to content

Commit a5f446b

Browse files
committed
Revert "[clang][Darwin] Try to guess the SDK root with xcrun when unspecified"
This reverts commit ecade80. Breaks tests on macOS and tries to run xcrun on non-mac platforms, see comments on https://reviews.llvm.org/D136315
1 parent c5ea42b commit a5f446b

File tree

3 files changed

+11
-120
lines changed

3 files changed

+11
-120
lines changed

clang/docs/UsersManual.rst

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3824,20 +3824,6 @@ backend.
38243824
Operating System Features and Limitations
38253825
-----------------------------------------
38263826

3827-
Apple
3828-
^^^^^
3829-
3830-
On Apple platforms, the standard headers and libraries are not provided by
3831-
the base system and are instead part of the Xcode SDK application. The location
3832-
of the SDK is determined any of the following ways:
3833-
3834-
- If passed to Clang, the ``-isysroot`` option specifies the path to the SDK.
3835-
3836-
- If the sysroot isn't provided, the ``SDKROOT`` environment variable is checked.
3837-
This variable is set by various Xcode tools.
3838-
3839-
- Otherwise, Clang uses Xcode's ``xcrun`` tool to find the SDK.
3840-
38413827
Windows
38423828
^^^^^^^
38433829

clang/lib/Driver/ToolChains/Darwin.cpp

Lines changed: 11 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,15 @@
1818
#include "clang/Driver/DriverDiagnostic.h"
1919
#include "clang/Driver/Options.h"
2020
#include "clang/Driver/SanitizerArgs.h"
21-
#include "llvm/ADT/ArrayRef.h"
22-
#include "llvm/ADT/Optional.h"
23-
#include "llvm/ADT/SmallString.h"
2421
#include "llvm/ADT/StringSwitch.h"
2522
#include "llvm/Option/ArgList.h"
2623
#include "llvm/ProfileData/InstrProf.h"
27-
#include "llvm/Support/FileSystem.h"
28-
#include "llvm/Support/FileUtilities.h"
2924
#include "llvm/Support/Path.h"
30-
#include "llvm/Support/Program.h"
3125
#include "llvm/Support/ScopedPrinter.h"
3226
#include "llvm/Support/TargetParser.h"
3327
#include "llvm/Support/Threading.h"
3428
#include "llvm/Support/VirtualFileSystem.h"
3529
#include <cstdlib> // ::getenv
36-
#include <memory> // std::unique_ptr
3730

3831
using namespace clang::driver;
3932
using namespace clang::driver::tools;
@@ -2085,89 +2078,21 @@ std::optional<DarwinSDKInfo> parseSDKSettings(llvm::vfs::FileSystem &VFS,
20852078
void Darwin::AddDeploymentTarget(DerivedArgList &Args) const {
20862079
const OptTable &Opts = getDriver().getOpts();
20872080

2088-
// On Apple platforms, standard headers and libraries are not provided with
2089-
// the base system (e.g. in /usr/{include,lib}). Instead, they are provided
2090-
// in various SDKs for the different Apple platforms. Clang needs to know
2091-
// where that SDK lives, and there are a couple ways this can be achieved:
2092-
//
2093-
// (1) If `-isysroot <path-to-SDK>` is passed explicitly, use that.
2081+
// Support allowing the SDKROOT environment variable used by xcrun and other
2082+
// Xcode tools to define the default sysroot, by making it the default for
2083+
// isysroot.
20942084
if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
20952085
// Warn if the path does not exist.
20962086
if (!getVFS().exists(A->getValue()))
20972087
getDriver().Diag(clang::diag::warn_missing_sysroot) << A->getValue();
2098-
}
2099-
2100-
// (2) If the SDKROOT environment variable is defined and points to a valid
2101-
// path, use that. $SDKROOT is set by `xcrun` and other Xcode tools, so
2102-
// running `xcrun clang` will work by going through this path.
2103-
else if (char *env = ::getenv("SDKROOT")) {
2104-
// We only use this value as the default if it is an absolute path,
2105-
// exists, and it is not the root path.
2106-
if (llvm::sys::path::is_absolute(env) && getVFS().exists(env) &&
2107-
StringRef(env) != "/") {
2108-
Args.append(Args.MakeSeparateArg(
2109-
nullptr, Opts.getOption(options::OPT_isysroot), env));
2110-
}
2111-
}
2112-
2113-
// (3) Otherwise, we try to guess the path of the default SDK by running
2114-
// `xcrun --show-sdk-path`. This won't always be correct, but if the
2115-
// user wants to use the non-default SDK, they should specify it
2116-
// explicitly with methods (1) or (2) above.
2117-
else {
2118-
llvm::SmallString<64> OutputFile;
2119-
llvm::sys::fs::createTemporaryFile("print-sdk-path", "" /* No Suffix */,
2120-
OutputFile);
2121-
llvm::FileRemover OutputRemover(OutputFile.c_str());
2122-
std::optional<llvm::StringRef> Redirects[] = {{""}, OutputFile.str(), {""}};
2123-
2124-
Optional<StringRef> SDKName = std::nullopt;
2125-
switch (getTriple().getOS()) {
2126-
case llvm::Triple::OSType::WatchOS:
2127-
if (getTriple().isSimulatorEnvironment())
2128-
SDKName = "watchsimulator";
2129-
else
2130-
SDKName = "watchos";
2131-
break;
2132-
case llvm::Triple::OSType::TvOS:
2133-
if (getTriple().isSimulatorEnvironment())
2134-
SDKName = "appletvsimulator";
2135-
else
2136-
SDKName = "appletvos";
2137-
break;
2138-
case llvm::Triple::OSType::IOS:
2139-
if (getTriple().isSimulatorEnvironment())
2140-
SDKName = "iphonesimulator";
2141-
else
2142-
SDKName = "iphoneos";
2143-
break;
2144-
case llvm::Triple::OSType::Darwin:
2145-
case llvm::Triple::OSType::MacOSX:
2146-
SDKName = "macosx";
2147-
break;
2148-
case llvm::Triple::OSType::DriverKit:
2149-
SDKName = "driverkit";
2150-
break;
2151-
default:
2152-
llvm_unreachable("unknown kind of Darwin platform");
2153-
}
2154-
2155-
if (SDKName) {
2156-
int Result = llvm::sys::ExecuteAndWait(
2157-
"/usr/bin/xcrun",
2158-
{"/usr/bin/xcrun", "--sdk", *SDKName, "--show-sdk-path"},
2159-
/* Inherit environment from parent process */ std::nullopt, Redirects,
2160-
/* SecondsToWait */ 0, /*MemoryLimit*/ 0);
2161-
if (Result == 0) {
2162-
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> OutputBuf =
2163-
llvm::MemoryBuffer::getFile(OutputFile.c_str(), /* IsText */ true);
2164-
if (OutputBuf) {
2165-
llvm::StringRef SdkPath = (*OutputBuf)->getBuffer().trim();
2166-
if (getVFS().exists(SdkPath)) {
2167-
Args.append(Args.MakeSeparateArg(
2168-
nullptr, Opts.getOption(options::OPT_isysroot), SdkPath));
2169-
}
2170-
}
2088+
} else {
2089+
if (char *env = ::getenv("SDKROOT")) {
2090+
// We only use this value as the default if it is an absolute path,
2091+
// exists, and it is not the root path.
2092+
if (llvm::sys::path::is_absolute(env) && getVFS().exists(env) &&
2093+
StringRef(env) != "/") {
2094+
Args.append(Args.MakeSeparateArg(
2095+
nullptr, Opts.getOption(options::OPT_isysroot), env));
21712096
}
21722097
}
21732098
}

clang/test/Driver/darwin-sdk-detect.c

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)