Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 9db768e

Browse files
committedDec 12, 2024··
[clang][Driver][Darwin] Optionally use xcselect to find macOS SDK
This is a scaled down version of https://reviews.llvm.org/D136315. The intent is largely the same as before[^1], but I've scaled down the scope to try to avoid the issues that the previous patch caused: - the changes are now opt-in based on enabling `CLANG_USE_XCSELECT` - this only works when targeting macOS on a macOS host (this is the only case supported by `libxcselect`[^2]) We also introduce an environment variable `CLANG_NO_XCSELECT` that disables this behaviour if Clang is configured with `CLANG_USE_XCSELECT=ON`. This is needed to avoid breaking tests. Another reason to leave this as opt-in for now is that there are some bugs in libxcselect that need fixing before it is safe to use by default for all users. This has been reported to Apple as FB16081077. [^1]: See also https://reviews.llvm.org/D109460 and #45225. [^2]: https://developer.apple.com/documentation/xcselect?language=objc
1 parent 0614c60 commit 9db768e

File tree

5 files changed

+69
-9
lines changed

5 files changed

+69
-9
lines changed
 

‎clang/CMakeLists.txt

+33
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,39 @@ if(GCC_INSTALL_PREFIX AND NOT USE_DEPRECATED_GCC_INSTALL_PREFIX)
214214
"See https://github.com/llvm/llvm-project/pull/77537 for detail.")
215215
endif()
216216

217+
if(APPLE)
218+
check_include_file(xcselect.h CLANG_HAVE_XCSELECT_H)
219+
if(CLANG_HAVE_XCSELECT_H)
220+
include(CheckSymbolExists)
221+
list(APPEND CMAKE_REQUIRED_LIBRARIES xcselect)
222+
check_symbol_exists(xcselect_host_sdk_path xcselect.h CLANG_HAVE_XCSELECT_HOST_SDK_PATH)
223+
list(REMOVE_ITEM CMAKE_REQUIRED_LIBRARIES xcselect)
224+
endif()
225+
endif()
226+
227+
cmake_dependent_option(CLANG_USE_XCSELECT "Use libxcselect to find the macOS SDK." OFF
228+
"APPLE;CLANG_HAVE_XCSELECT_HOST_SDK_PATH" OFF)
229+
230+
if(DEFAULT_SYSROOT AND CLANG_USE_XCSELECT)
231+
message(FATAL_ERROR "Setting DEFAULT_SYSROOT is incompatible with CLANG_USE_XCSELECT.")
232+
endif()
233+
234+
if(CLANG_USE_XCSELECT)
235+
set(XCSELECT_VALID_POLICIES LATEST MATCHING_ONLY MATCHING_PREFERRED)
236+
set(CLANG_XCSELECT_HOST_SDK_POLICY "LATEST" CACHE STRING
237+
"Policy to use for xcselect. One of: ${XCSELECT_VALID_POLICIES}")
238+
set_property(CACHE CLANG_XCSELECT_HOST_SDK_POLICY PROPERTY STRINGS ${XCSELECT_VALID_POLICIES})
239+
string(TOUPPER ${CLANG_XCSELECT_HOST_SDK_POLICY} CLANG_XCSELECT_HOST_SDK_POLICY)
240+
list(JOIN XCSELECT_VALID_POLICIES "|" XCSELECT_POLICY_REGEX)
241+
if(NOT CLANG_XCSELECT_HOST_SDK_POLICY MATCHES "^XCSELECT_HOST_SDK_POLICY_(${XCSELECT_POLICY_REGEX})$")
242+
if(NOT CLANG_XCSELECT_HOST_SDK_POLICY IN_LIST XCSELECT_VALID_POLICIES)
243+
message(FATAL_ERROR
244+
"CLANG_XCSELECT_HOST_SDK_POLICY (${CLANG_XCSELECT_HOST_SDK_POLICY}) must be one of: ${XCSELECT_VALID_POLICIES}")
245+
endif()
246+
set(CLANG_XCSELECT_HOST_SDK_POLICY "XCSELECT_HOST_SDK_POLICY_${CLANG_XCSELECT_HOST_SDK_POLICY}")
247+
endif()
248+
endif()
249+
217250
set(ENABLE_LINKER_BUILD_ID OFF CACHE BOOL "pass --build-id to ld")
218251

219252
set(ENABLE_X86_RELAX_RELOCATIONS ON CACHE BOOL

‎clang/include/clang/Config/config.h.cmake

+6
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,10 @@
8686
/* Whether CIR is built into Clang */
8787
#cmakedefine01 CLANG_ENABLE_CIR
8888

89+
/* Whether to use xcselect to find the macOS SDK */
90+
#cmakedefine CLANG_USE_XCSELECT
91+
92+
/* Policy to use for xcselect */
93+
#cmakedefine CLANG_XCSELECT_HOST_SDK_POLICY ${CLANG_XCSELECT_HOST_SDK_POLICY}
94+
8995
#endif

‎clang/lib/Driver/CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ if(WIN32)
1414
set(system_libs version)
1515
endif()
1616

17+
if(CLANG_USE_XCSELECT)
18+
set(system_libs xcselect)
19+
endif()
20+
1721
add_clang_library(clangDriver
1822
Action.cpp
1923
Compilation.cpp

‎clang/lib/Driver/ToolChains/Darwin.cpp

+22-9
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
#include "llvm/TargetParser/Triple.h"
3030
#include <cstdlib> // ::getenv
3131

32+
#ifdef CLANG_USE_XCSELECT
33+
#include <xcselect.h> // ::xcselect_host_sdk_path
34+
#endif
35+
3236
using namespace clang::driver;
3337
using namespace clang::driver::tools;
3438
using namespace clang::driver::toolchains;
@@ -2257,17 +2261,26 @@ void Darwin::AddDeploymentTarget(DerivedArgList &Args) const {
22572261
// Warn if the path does not exist.
22582262
if (!getVFS().exists(A->getValue()))
22592263
getDriver().Diag(clang::diag::warn_missing_sysroot) << A->getValue();
2260-
} else {
2261-
if (char *env = ::getenv("SDKROOT")) {
2262-
// We only use this value as the default if it is an absolute path,
2263-
// exists, and it is not the root path.
2264-
if (llvm::sys::path::is_absolute(env) && getVFS().exists(env) &&
2265-
StringRef(env) != "/") {
2266-
Args.append(Args.MakeSeparateArg(
2267-
nullptr, Opts.getOption(options::OPT_isysroot), env));
2268-
}
2264+
} else if (const char *env = ::getenv("SDKROOT"); env && *env) {
2265+
// We only use this value as the default if it is an absolute path,
2266+
// exists, and it is not the root path.
2267+
if (llvm::sys::path::is_absolute(env) && getVFS().exists(env) &&
2268+
StringRef(env) != "/") {
2269+
Args.append(Args.MakeSeparateArg(
2270+
nullptr, Opts.getOption(options::OPT_isysroot), env));
22692271
}
22702272
}
2273+
#ifdef CLANG_USE_XCSELECT
2274+
else if (const char *env = ::getenv("CLANG_NO_XCSELECT");
2275+
getTriple().isMacOSX() && (!env || !*env)) {
2276+
if (char *p;
2277+
!::xcselect_host_sdk_path(CLANG_XCSELECT_HOST_SDK_POLICY, &p)) {
2278+
Args.append(Args.MakeSeparateArg(
2279+
nullptr, Opts.getOption(options::OPT_isysroot), p));
2280+
::free(p);
2281+
}
2282+
}
2283+
#endif
22712284

22722285
// Read the SDKSettings.json file for more information, like the SDK version
22732286
// that we can pass down to the compiler.

‎clang/test/lit.cfg.py

+4
Original file line numberDiff line numberDiff line change
@@ -366,3 +366,7 @@ def calculate_arch_features(arch_string):
366366
# possibly be present in system and user configuration files, so disable
367367
# default configs for the test runs.
368368
config.environment["CLANG_NO_DEFAULT_CONFIG"] = "1"
369+
370+
# Configuring clang with CLANG_USE_XCSELECT=ON breaks some tests, so disable
371+
# its behaviour while running tests.
372+
config.environment["CLANG_NO_XCSELECT"] = "1"

0 commit comments

Comments
 (0)
Please sign in to comment.