Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[clang][Driver][Darwin] Optionally use xcselect to find macOS SDK #119670

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions clang/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,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
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
30 changes: 21 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,25 @@ 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
else if (getTriple().isMacOSX()) {
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.
Expand Down
1 change: 1 addition & 0 deletions clang/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ llvm_canonicalize_cmake_booleans(
CLANG_PLUGIN_SUPPORT
CLANG_SPAWN_CC1
CLANG_ENABLE_CIR
CLANG_USE_XCSELECT
ENABLE_BACKTRACES
LLVM_BUILD_EXAMPLES
LLVM_BYE_LINK_INTO_TOOLS
Expand Down
3 changes: 3 additions & 0 deletions clang/test/Driver/arc.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// XFAIL: xcselect
// FIXME: There's no reason why this should fail with CLANG_USE_XCSELECT.

// RUN: not %clang -ObjC -target i386-apple-darwin10 -stdlib=libstdc++ -m32 -fobjc-arc %s -fsyntax-only 2>&1 | FileCheck %s
// RUN: not %clang -x objective-c -target i386-apple-darwin10 -stdlib=libstdc++ -m32 -fobjc-arc %s -fsyntax-only 2>&1 | FileCheck %s
// RUN: not %clang -x objective-c++ -target i386-apple-darwin10 -stdlib=libstdc++ -m32 -fobjc-arc %s -fsyntax-only 2>&1 | FileCheck %s
Expand Down
3 changes: 3 additions & 0 deletions clang/test/Driver/clang-g-opts.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// XFAIL: xcselect
// FIXME: There's no reason why this should fail with CLANG_USE_XCSELECT.

// RUN: %clang -### -S %s 2>&1 | FileCheck --check-prefix=CHECK-WITHOUT-G %s
// RUN: %clang -### -S %s -g -target x86_64-linux-gnu 2>&1 \
// RUN: | FileCheck --check-prefix=CHECK-WITH-G %s
Expand Down
3 changes: 3 additions & 0 deletions clang/test/Driver/clang-translation.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// XFAIL: xcselect
// FIXME: There's no reason why this should fail with CLANG_USE_XCSELECT.

// RUN: %clang -target i386-unknown-unknown -### -S -O0 -Os %s -o %t.s -fverbose-asm -fvisibility=hidden 2>&1 | FileCheck -check-prefix=I386 %s
// I386: "-triple" "i386-unknown-unknown"
// I386: "-S"
Expand Down
3 changes: 3 additions & 0 deletions clang/test/Driver/darwin-builtin-modules.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// XFAIL: xcselect
// FIXME: There's no reason why this should fail with CLANG_USE_XCSELECT.

// Check that darwin passes -fbuiltin-headers-in-system-modules
// when expected.

Expand Down
3 changes: 3 additions & 0 deletions clang/test/Driver/darwin-debug-flags.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// XFAIL: xcselect
// FIXME: There's no reason why this should fail with CLANG_USE_XCSELECT.

// RUN: env RC_DEBUG_OPTIONS=1 %clang -target i386-apple-darwin11 -I "path with \spaces" -g -Os %s -emit-llvm -S -o - | FileCheck %s
// RUN: touch %t.s
// RUN: env RC_DEBUG_OPTIONS=1 %clang -### -target i386-apple-darwin11 -c -g %t.s 2>&1 | FileCheck -check-prefix=S %s
Expand Down
2 changes: 2 additions & 0 deletions clang/test/Driver/darwin-header-search-system.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// UNSUPPORTED: system-windows
// XFAIL: xcselect
// FIXME: There's no reason why this should fail with CLANG_USE_XCSELECT.

// General tests that the system header search paths detected by the driver
// and passed to CC1 are correct on Darwin platforms.
Expand Down
3 changes: 3 additions & 0 deletions clang/test/Driver/darwin-ld-platform-version-macos.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// XFAIL: xcselect
// FIXME: There's no reason why this should fail with CLANG_USE_XCSELECT.

// RUN: touch %t.o

// RUN: %clang -target x86_64-apple-macos10.13 -fuse-ld=lld \
Expand Down
3 changes: 3 additions & 0 deletions clang/test/Driver/darwin-ld.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// XFAIL: xcselect
// FIXME: There's no reason why this should fail with CLANG_USE_XCSELECT.

// Check that ld gets arch_multiple.

// RUN: %clang -target i386-apple-darwin9 -arch i386 -arch x86_64 %s -### -o foo 2> %t.log
Expand Down
3 changes: 3 additions & 0 deletions clang/test/Driver/darwin-multiarch-arm.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// XFAIL: xcselect
// FIXME: There's no reason why this should fail with CLANG_USE_XCSELECT.

// Check that we compile correctly with multiple ARM -arch options.
//
// RUN: %clang -target arm7-apple-darwin10 -### \
Expand Down
3 changes: 3 additions & 0 deletions clang/test/Driver/darwin-objc-options.m
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// XFAIL: xcselect
// FIXME: There's no reason why this should fail with CLANG_USE_XCSELECT.

// Check miscellaneous Objective-C options.

// RUN: %clang -target x86_64-apple-darwin10 -S -### %s \
Expand Down
3 changes: 3 additions & 0 deletions clang/test/Driver/darwin-version.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// XFAIL: xcselect
// FIXME: There's no reason why this should fail with CLANG_USE_XCSELECT.

// RUN: %clang -target armv6-apple-darwin9 -c %s -### 2>&1 | \
// RUN: FileCheck --check-prefix=CHECK-VERSION-OSX %s
// CHECK-VERSION-OSX: "armv6k-apple-macosx10.5.0"
Expand Down
3 changes: 3 additions & 0 deletions clang/test/Driver/debug-options.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// XFAIL: xcselect
// FIXME: There's no reason why this should fail with CLANG_USE_XCSELECT.

// Check to make sure clang is somewhat picky about -g options.

// Linux.
Expand Down
3 changes: 3 additions & 0 deletions clang/test/Driver/fsanitize.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// XFAIL: xcselect
// FIXME: There's no reason why this should fail with CLANG_USE_XCSELECT.

// RUN: %clang --target=x86_64-linux-gnu -fsanitize=undefined -fsanitize-trap=undefined %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-UNDEFINED-TRAP
// RUN: %clang --target=x86_64-linux-gnu -fsanitize=undefined -fsanitize-trap=undefined -fno-sanitize-trap=signed-integer-overflow %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-UNDEFINED-TRAP2
// RUN: %clang --target=x86_64-linux-gnu -fsanitize=undefined -fsanitize-undefined-trap-on-error %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-UNDEFINED-TRAP
Expand Down
3 changes: 3 additions & 0 deletions clang/test/Driver/macos-apple-silicon-slice-link-libs.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// XFAIL: xcselect
// FIXME: There's no reason why this should fail with CLANG_USE_XCSELECT.

// RUN: %clang -### -target arm64-apple-macos10.7 %s 2>&1 | FileCheck -check-prefix=ARM64-10_7 %s
// RUN: %clang -### -target x86_64-apple-macos10.7 %s 2>&1 | FileCheck -check-prefix=x86_64-10_7 %s
// RUN: %clang -### -target arm64-apple-darwin6 %s 2>&1 | FileCheck -check-prefix=ARM64-10_7 %s
Expand Down
3 changes: 3 additions & 0 deletions clang/test/Driver/target-triple-deployment.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// XFAIL: xcselect
// FIXME: There's no reason why this should fail with CLANG_USE_XCSELECT.

// RUN: touch %t.o
// RUN: %clang -fuse-ld= -target x86_64-apple-macosx10.4 -mlinker-version=400 -### %t.o 2> %t.log
// RUN: %clang -fuse-ld= -target x86_64-apple-darwin9 -mlinker-version=400 -### %t.o 2>> %t.log
Expand Down
5 changes: 5 additions & 0 deletions clang/test/Driver/xcselect.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// REQUIRES: xcselect
// RUN: %clang -target arm64-apple-darwin -c -### %s 2> %t.log
// RUN: FileCheck %s <%t.log

// CHECK: "-isysroot" "{{.*}}/SDKs/MacOSX{{([0-9]+(\.[0-9]+)?)?}}.sdk"
2 changes: 2 additions & 0 deletions clang/test/lit.cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,8 @@ def calculate_arch_features(arch_string):
if config.have_llvm_driver:
config.available_features.add("llvm-driver")

if config.use_xcselect:
config.available_features.add("xcselect")

# Some tests perform deep recursion, which requires a larger pthread stack size
# than the relatively low default of 192 KiB for 64-bit processes on AIX. The
Expand Down
1 change: 1 addition & 0 deletions clang/test/lit.site.cfg.py.in
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ config.standalone_build = @CLANG_BUILT_STANDALONE@
config.ppc_linux_default_ieeelongdouble = @PPC_LINUX_DEFAULT_IEEELONGDOUBLE@
config.have_llvm_driver = @LLVM_TOOL_LLVM_DRIVER_BUILD@
config.substitutions.append(("%llvm-version-major", "@LLVM_VERSION_MAJOR@"))
config.use_xcselect = @CLANG_USE_XCSELECT@

import lit.llvm
lit.llvm.initialize(lit_config, config)
Expand Down
Loading