[clang][Driver][Darwin] Optionally use xcselect to find macOS SDK (#119670)
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]) - calling `libxcselect` is done only when the target is `*-apple-macos*` to avoid breaking many 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
This commit is contained in:
parent
5f479438dd
commit
629edaf678
@ -229,6 +229,44 @@ if(GCC_INSTALL_PREFIX AND NOT USE_DEPRECATED_GCC_INSTALL_PREFIX)
|
||||
"See https://github.com/llvm/llvm-project/pull/77537 for detail.")
|
||||
endif()
|
||||
|
||||
cmake_dependent_option(CLANG_USE_XCSELECT "Use libxcselect to find the macOS SDK." OFF "APPLE" OFF)
|
||||
|
||||
if(CLANG_USE_XCSELECT)
|
||||
if(DEFAULT_SYSROOT)
|
||||
message(FATAL_ERROR "Setting DEFAULT_SYSROOT is incompatible with CLANG_USE_XCSELECT.")
|
||||
endif()
|
||||
|
||||
check_include_file(xcselect.h CLANG_HAVE_XCSELECT_H)
|
||||
if(NOT CLANG_HAVE_XCSELECT_H)
|
||||
message(FATAL_ERROR "CLANG_USE_XCSELECT is enabled but xcselect.h was not found.")
|
||||
endif()
|
||||
|
||||
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)
|
||||
|
||||
if(NOT CLANG_HAVE_XCSELECT_HOST_SDK_PATH)
|
||||
message(FATAL_ERROR "CLANG_USE_XCSELECT is enabled but either libxcselect is not available "
|
||||
"or it is missing xcselect_host_sdk_path.")
|
||||
endif()
|
||||
|
||||
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
|
||||
|
||||
@ -85,4 +85,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
|
||||
|
||||
@ -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
|
||||
|
||||
@ -27,6 +27,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;
|
||||
@ -2488,17 +2492,27 @@ 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")) {
|
||||
// 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
|
||||
// FIXME: This should check for `getTriple().isMacOSX()`, but this breaks
|
||||
// many tests. See https://github.com/llvm/llvm-project/pull/119670.
|
||||
else if (getTriple().getOS() == llvm::Triple::MacOSX) {
|
||||
char *p;
|
||||
if (!::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.
|
||||
|
||||
@ -12,6 +12,7 @@ llvm_canonicalize_cmake_booleans(
|
||||
CLANG_ENABLE_CIR
|
||||
CLANG_ENABLE_OBJC_REWRITER
|
||||
CLANG_LINK_CLANG_DYLIB
|
||||
CLANG_USE_XCSELECT
|
||||
ENABLE_BACKTRACES
|
||||
LLVM_BYE_LINK_INTO_TOOLS
|
||||
LLVM_ENABLE_PLUGINS
|
||||
|
||||
17
clang/test/Driver/darwin-ld-platform-version-macos-nosdk.c
Normal file
17
clang/test/Driver/darwin-ld-platform-version-macos-nosdk.c
Normal file
@ -0,0 +1,17 @@
|
||||
// UNSUPPORTED: xcselect
|
||||
// CLANG_USE_XCSELECT will always have an SDK inferred.
|
||||
|
||||
// RUN: touch %t.o
|
||||
|
||||
// RUN: %clang -target x86_64-apple-macos10.13 -mlinker-version=520 \
|
||||
// RUN: -### %t.o 2>&1 \
|
||||
// RUN: | FileCheck --check-prefix=NOSDK %s
|
||||
// RUN: %clang -target x86_64-apple-darwin17 -mlinker-version=520 \
|
||||
// RUN: -### %t.o 2>&1 \
|
||||
// RUN: | FileCheck --check-prefix=NOSDK %s
|
||||
// NOSDK: "-platform_version" "macos" "10.13.0" "10.13.0"
|
||||
|
||||
// RUN: %clang -target arm64-apple-macos26 -mlinker-version=520 \
|
||||
// RUN: -### %t.o 2>&1 \
|
||||
// RUN: | FileCheck --check-prefix=VERSION_BUMP %s
|
||||
// VERSION_BUMP: "-platform_version" "macos" "26.0.0" "26.0.0"
|
||||
@ -40,16 +40,3 @@
|
||||
// ARM64_NEW: "-platform_version" "macos" "11.0.0" "10.15"
|
||||
// ARM64_NEW_1: "-platform_version" "macos" "11.1.0" "10.15"
|
||||
// ARM64_OLD: "-macosx_version_min" "11.0.0"
|
||||
|
||||
// RUN: %clang -target x86_64-apple-macos10.13 -mlinker-version=520 \
|
||||
// RUN: -### %t.o 2>&1 \
|
||||
// RUN: | FileCheck --check-prefix=NOSDK %s
|
||||
// RUN: %clang -target x86_64-apple-darwin17 -mlinker-version=520 \
|
||||
// RUN: -### %t.o 2>&1 \
|
||||
// RUN: | FileCheck --check-prefix=NOSDK %s
|
||||
// NOSDK: "-platform_version" "macos" "10.13.0" "10.13.0"
|
||||
|
||||
// RUN: %clang -target arm64-apple-macos26 -mlinker-version=520 \
|
||||
// RUN: -### %t.o 2>&1 \
|
||||
// RUN: | FileCheck --check-prefix=VERSION_BUMP %s
|
||||
// VERSION_BUMP: "-platform_version" "macos" "26.0.0" "26.0.0"
|
||||
|
||||
5
clang/test/Driver/xcselect.c
Normal file
5
clang/test/Driver/xcselect.c
Normal file
@ -0,0 +1,5 @@
|
||||
// REQUIRES: xcselect
|
||||
// RUN: %clang -target arm64-apple-macosx -c -### %s 2> %t.log
|
||||
// RUN: FileCheck %s <%t.log
|
||||
|
||||
// CHECK: "-isysroot" "{{.*}}/SDKs/MacOSX{{([0-9]+(\.[0-9]+)?)?}}.sdk"
|
||||
@ -429,6 +429,8 @@ if config.have_llvm_driver:
|
||||
if config.clang_enable_cir:
|
||||
config.available_features.add("cir-enabled")
|
||||
|
||||
if config.use_xcselect:
|
||||
config.available_features.add("xcselect")
|
||||
|
||||
# Tests that rely on chmod to restrict file permissions (e.g. write-permission
|
||||
# checks) are unreliable when run as root, since root bypasses file permissions.
|
||||
|
||||
@ -47,6 +47,7 @@ config.ppc_linux_default_ieeelongdouble = @PPC_LINUX_DEFAULT_IEEELONGDOUBLE@
|
||||
config.have_llvm_driver = @LLVM_TOOL_LLVM_DRIVER_BUILD@
|
||||
config.spirv_tools_tests = @LLVM_INCLUDE_SPIRV_TOOLS_TESTS@
|
||||
config.substitutions.append(("%llvm-version-major", "@LLVM_VERSION_MAJOR@"))
|
||||
config.use_xcselect = @CLANG_USE_XCSELECT@
|
||||
|
||||
import lit.llvm
|
||||
lit.llvm.initialize(lit_config, config)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user