Skip to content

Commit 7a1372c

Browse files
Update darwin SDK version parsing to support OSX and simulator
This re-commits r226005 with a tweak. The origin attempt failed because Darwin bot sets up SDKROOT and clang can deduce SDK version from them after this patch. That broke many driver tests due to the change of deployment target version. Now the tests should not complain after r240574. llvm-svn: 240619
1 parent 1d4cff2 commit 7a1372c

File tree

2 files changed

+59
-8
lines changed

2 files changed

+59
-8
lines changed

clang/lib/Driver/ToolChains.cpp

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -463,16 +463,31 @@ void Darwin::AddDeploymentTarget(DerivedArgList &Args) const {
463463
if (char *env = ::getenv("IPHONEOS_DEPLOYMENT_TARGET"))
464464
iOSTarget = env;
465465

466-
// If no '-miphoneos-version-min' specified on the command line and
467-
// IPHONEOS_DEPLOYMENT_TARGET is not defined, see if we can set the default
468-
// based on -isysroot.
469-
if (iOSTarget.empty()) {
466+
// If there is no command-line argument to specify the Target version and
467+
// no environment variable defined, see if we can set the default based
468+
// on -isysroot.
469+
if (iOSTarget.empty() && OSXTarget.empty() &&
470+
Args.hasArg(options::OPT_isysroot)) {
470471
if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
471-
StringRef first, second;
472472
StringRef isysroot = A->getValue();
473-
std::tie(first, second) = isysroot.split(StringRef("SDKs/iPhoneOS"));
474-
if (second != "")
475-
iOSTarget = second.substr(0,3);
473+
// Assume SDK has path: SOME_PATH/SDKs/PlatformXX.YY.sdk
474+
size_t BeginSDK = isysroot.rfind("SDKs/");
475+
size_t EndSDK = isysroot.rfind(".sdk");
476+
if (BeginSDK != StringRef::npos && EndSDK != StringRef::npos) {
477+
StringRef SDK = isysroot.slice(BeginSDK + 5, EndSDK);
478+
// Slice the version number out.
479+
// Version number is between the first and the last number.
480+
size_t StartVer = SDK.find_first_of("0123456789");
481+
size_t EndVer = SDK.find_last_of("0123456789");
482+
if (StartVer != StringRef::npos && EndVer > StartVer) {
483+
StringRef Version = SDK.slice(StartVer, EndVer + 1);
484+
if (SDK.startswith("iPhoneOS") ||
485+
SDK.startswith("iPhoneSimulator"))
486+
iOSTarget = Version;
487+
else if (SDK.startswith("MacOSX"))
488+
OSXTarget = Version;
489+
}
490+
}
476491
}
477492
}
478493

clang/test/Driver/darwin-sdkroot.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,39 @@
4040
// env SDKROOT=/ cmd //c echo %SDKROOT%
4141
//
4242
// This test passes using env.exe from GnuWin32.
43+
44+
// Check if clang set the correct deployment target from -sysroot
45+
//
46+
// RUN: rm -rf %t/SDKs/iPhoneOS8.0.0.sdk
47+
// RUN: mkdir -p %t/SDKs/iPhoneOS8.0.0.sdk
48+
// RUN: env SDKROOT=%t/SDKs/iPhoneOS8.0.0.sdk %clang -target arm64-apple-darwin %s -### 2>&1 \
49+
// RUN: | FileCheck --check-prefix=CHECK-IPHONE %s
50+
//
51+
// CHECK-IPHONE: clang
52+
// CHECK-IPHONE: "-cc1"
53+
// CHECK-IPHONE: "-triple" "arm64-apple-ios8.0.0"
54+
// CHECK-IPHONE: ld
55+
// CHECK-IPHONE: "-iphoneos_version_min" "8.0.0"
56+
//
57+
//
58+
// RUN: rm -rf %t/SDKs/iPhoneSimulator8.0.sdk
59+
// RUN: mkdir -p %t/SDKs/iPhoneSimulator8.0.sdk
60+
// RUN: env SDKROOT=%t/SDKs/iPhoneSimulator8.0.sdk %clang -target x86_64-apple-darwin %s -### 2>&1 \
61+
// RUN: | FileCheck --check-prefix=CHECK-SIMULATOR %s
62+
//
63+
// CHECK-SIMULATOR: clang
64+
// CHECK-SIMULATOR: "-cc1"
65+
// CHECK-SIMULATOR: "-triple" "x86_64-apple-ios8.0.0"
66+
// CHECK-SIMULATOR: ld
67+
// CHECK-SIMULATOR: "-ios_simulator_version_min" "8.0.0"
68+
//
69+
// RUN: rm -rf %t/SDKs/MacOSX10.10.0.sdk
70+
// RUN: mkdir -p %t/SDKs/MacOSX10.10.0.sdk
71+
// RUN: env SDKROOT=%t/SDKs/MacOSX10.10.0.sdk %clang -target x86_64-apple-darwin %s -### 2>&1 \
72+
// RUN: | FileCheck --check-prefix=CHECK-MACOSX %s
73+
//
74+
// CHECK-MACOSX: clang
75+
// CHECK-MACOSX: "-cc1"
76+
// CHECK-MACOSX: "-triple" "x86_64-apple-macosx10.10.0"
77+
// CHECK-MACOSX: ld
78+
// CHECK-MACOSX: "-macosx_version_min" "10.10.0"

0 commit comments

Comments
 (0)