|
18 | 18 | #include "clang/Driver/DriverDiagnostic.h" |
19 | 19 | #include "clang/Driver/Options.h" |
20 | 20 | #include "clang/Driver/SanitizerArgs.h" |
21 | | -#include "llvm/ADT/ArrayRef.h" |
22 | | -#include "llvm/ADT/Optional.h" |
23 | | -#include "llvm/ADT/SmallString.h" |
24 | 21 | #include "llvm/ADT/StringSwitch.h" |
25 | 22 | #include "llvm/Option/ArgList.h" |
26 | 23 | #include "llvm/ProfileData/InstrProf.h" |
27 | | -#include "llvm/Support/FileSystem.h" |
28 | | -#include "llvm/Support/FileUtilities.h" |
29 | 24 | #include "llvm/Support/Path.h" |
30 | | -#include "llvm/Support/Program.h" |
31 | 25 | #include "llvm/Support/ScopedPrinter.h" |
32 | 26 | #include "llvm/Support/TargetParser.h" |
33 | 27 | #include "llvm/Support/Threading.h" |
34 | 28 | #include "llvm/Support/VirtualFileSystem.h" |
35 | 29 | #include <cstdlib> // ::getenv |
36 | | -#include <memory> // std::unique_ptr |
37 | 30 |
|
38 | 31 | using namespace clang::driver; |
39 | 32 | using namespace clang::driver::tools; |
@@ -2085,89 +2078,21 @@ std::optional<DarwinSDKInfo> parseSDKSettings(llvm::vfs::FileSystem &VFS, |
2085 | 2078 | void Darwin::AddDeploymentTarget(DerivedArgList &Args) const { |
2086 | 2079 | const OptTable &Opts = getDriver().getOpts(); |
2087 | 2080 |
|
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. |
2094 | 2084 | if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) { |
2095 | 2085 | // Warn if the path does not exist. |
2096 | 2086 | if (!getVFS().exists(A->getValue())) |
2097 | 2087 | 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)); |
2171 | 2096 | } |
2172 | 2097 | } |
2173 | 2098 | } |
|
0 commit comments