Skip to content

Commit

Permalink
[clang][Driver][Darwin] Optionally use xcselect to find macOS SDK
Browse files Browse the repository at this point in the history
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 llvm#45225.
[^2]: https://developer.apple.com/documentation/xcselect?language=objc
  • Loading branch information
carlocab committed Dec 12, 2024
1 parent 0614c60 commit 9db768e
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 9 deletions.
33 changes: 33 additions & 0 deletions clang/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,39 @@ if(GCC_INSTALL_PREFIX AND NOT USE_DEPRECATED_GCC_INSTALL_PREFIX)
"See https://github.com/llvm/llvm-project/pull/77537 for detail.")
endif()

if(APPLE)
check_include_file(xcselect.h CLANG_HAVE_XCSELECT_H)
if(CLANG_HAVE_XCSELECT_H)
include(CheckSymbolExists)
list(APPEND CMAKE_REQUIRED_LIBRARIES xcselect)
check_symbol_exists(xcselect_host_sdk_path xcselect.h CLANG_HAVE_XCSELECT_HOST_SDK_PATH)
list(REMOVE_ITEM CMAKE_REQUIRED_LIBRARIES xcselect)
endif()
endif()

cmake_dependent_option(CLANG_USE_XCSELECT "Use libxcselect to find the macOS SDK." OFF
"APPLE;CLANG_HAVE_XCSELECT_HOST_SDK_PATH" OFF)

if(DEFAULT_SYSROOT AND CLANG_USE_XCSELECT)
message(FATAL_ERROR "Setting DEFAULT_SYSROOT is incompatible with CLANG_USE_XCSELECT.")
endif()

if(CLANG_USE_XCSELECT)
set(XCSELECT_VALID_POLICIES LATEST MATCHING_ONLY MATCHING_PREFERRED)
set(CLANG_XCSELECT_HOST_SDK_POLICY "LATEST" CACHE STRING
"Policy to use for xcselect. One of: ${XCSELECT_VALID_POLICIES}")
set_property(CACHE CLANG_XCSELECT_HOST_SDK_POLICY PROPERTY STRINGS ${XCSELECT_VALID_POLICIES})
string(TOUPPER ${CLANG_XCSELECT_HOST_SDK_POLICY} CLANG_XCSELECT_HOST_SDK_POLICY)
list(JOIN XCSELECT_VALID_POLICIES "|" XCSELECT_POLICY_REGEX)
if(NOT CLANG_XCSELECT_HOST_SDK_POLICY MATCHES "^XCSELECT_HOST_SDK_POLICY_(${XCSELECT_POLICY_REGEX})$")
if(NOT CLANG_XCSELECT_HOST_SDK_POLICY IN_LIST XCSELECT_VALID_POLICIES)
message(FATAL_ERROR
"CLANG_XCSELECT_HOST_SDK_POLICY (${CLANG_XCSELECT_HOST_SDK_POLICY}) must be one of: ${XCSELECT_VALID_POLICIES}")
endif()
set(CLANG_XCSELECT_HOST_SDK_POLICY "XCSELECT_HOST_SDK_POLICY_${CLANG_XCSELECT_HOST_SDK_POLICY}")
endif()
endif()

set(ENABLE_LINKER_BUILD_ID OFF CACHE BOOL "pass --build-id to ld")

set(ENABLE_X86_RELAX_RELOCATIONS ON CACHE BOOL
Expand Down
6 changes: 6 additions & 0 deletions clang/include/clang/Config/config.h.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,10 @@
/* Whether CIR is built into Clang */
#cmakedefine01 CLANG_ENABLE_CIR

/* Whether to use xcselect to find the macOS SDK */
#cmakedefine CLANG_USE_XCSELECT

/* Policy to use for xcselect */
#cmakedefine CLANG_XCSELECT_HOST_SDK_POLICY ${CLANG_XCSELECT_HOST_SDK_POLICY}

#endif
4 changes: 4 additions & 0 deletions clang/lib/Driver/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ if(WIN32)
set(system_libs version)
endif()

if(CLANG_USE_XCSELECT)
set(system_libs xcselect)
endif()

add_clang_library(clangDriver
Action.cpp
Compilation.cpp
Expand Down
31 changes: 22 additions & 9 deletions clang/lib/Driver/ToolChains/Darwin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
#include "llvm/TargetParser/Triple.h"
#include <cstdlib> // ::getenv

#ifdef CLANG_USE_XCSELECT
#include <xcselect.h> // ::xcselect_host_sdk_path
#endif

using namespace clang::driver;
using namespace clang::driver::tools;
using namespace clang::driver::toolchains;
Expand Down Expand Up @@ -2257,17 +2261,26 @@ void Darwin::AddDeploymentTarget(DerivedArgList &Args) const {
// Warn if the path does not exist.
if (!getVFS().exists(A->getValue()))
getDriver().Diag(clang::diag::warn_missing_sysroot) << A->getValue();
} 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));
}
} else if (const char *env = ::getenv("SDKROOT"); env && *env) {
// 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));
}
}
#ifdef CLANG_USE_XCSELECT
else if (const char *env = ::getenv("CLANG_NO_XCSELECT");
getTriple().isMacOSX() && (!env || !*env)) {
if (char *p;
!::xcselect_host_sdk_path(CLANG_XCSELECT_HOST_SDK_POLICY, &p)) {
Args.append(Args.MakeSeparateArg(
nullptr, Opts.getOption(options::OPT_isysroot), p));
::free(p);
}
}
#endif

// Read the SDKSettings.json file for more information, like the SDK version
// that we can pass down to the compiler.
Expand Down
4 changes: 4 additions & 0 deletions clang/test/lit.cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,3 +366,7 @@ def calculate_arch_features(arch_string):
# possibly be present in system and user configuration files, so disable
# default configs for the test runs.
config.environment["CLANG_NO_DEFAULT_CONFIG"] = "1"

# Configuring clang with CLANG_USE_XCSELECT=ON breaks some tests, so disable
# its behaviour while running tests.
config.environment["CLANG_NO_XCSELECT"] = "1"

0 comments on commit 9db768e

Please sign in to comment.