-
Notifications
You must be signed in to change notification settings - Fork 10.6k
[Runtime] Dynamically select the is-Swift bit at runtime on Apple platforms. #23064
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
//===--- BackDeployment.h - Support for running on older OS versions. -----===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef SWIFT_STDLIB_BACKDEPLOYMENT_H | ||
#define SWIFT_STDLIB_BACKDEPLOYMENT_H | ||
|
||
#if defined(__APPLE__) && defined(__MACH__) | ||
|
||
#include "swift/Runtime/Config.h" | ||
#include "../../../stdlib/public/SwiftShims/Visibility.h" | ||
|
||
#ifdef __cplusplus | ||
namespace swift { extern "C" { | ||
#endif | ||
|
||
#if SWIFT_CLASS_IS_SWIFT_MASK_GLOBAL_VARIABLE | ||
# ifndef __cplusplus | ||
// This file gets included from some C/ObjC files and | ||
// SWIFT_RUNTIME_STDLIB_SPI doesn't imply extern in C. | ||
extern | ||
# endif | ||
SWIFT_RUNTIME_STDLIB_SPI unsigned long long _swift_classIsSwiftMask; | ||
#endif | ||
|
||
/// Returns true if the current OS version, at runtime, is a back-deployment | ||
/// version. | ||
SWIFT_RUNTIME_STDLIB_INTERNAL | ||
int _swift_isBackDeploying(); | ||
|
||
#ifdef __cplusplus | ||
}} // extern "C", namespace swift | ||
#endif | ||
|
||
#endif // defined(__APPLE__) && defined(__MACH__) | ||
|
||
#endif // SWIFT_STDLIB_BACKDEPLOYMENT_H |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,13 @@ | ||
# Detect B&I builds. | ||
set(SWIFT_BNI_OS_BUILD FALSE) | ||
set(SWIFT_BNI_XCODE_BUILD FALSE) | ||
if(DEFINED ENV{RC_XBS}) | ||
if(NOT DEFINED ENV{RC_XCODE} OR NOT "$ENV{RC_XCODE}") | ||
set(SWIFT_BNI_OS_BUILD TRUE) | ||
else() | ||
set(SWIFT_BNI_XCODE_BUILD TRUE) | ||
endif() | ||
endif() | ||
|
||
configure_file(CMakeConfig.h.in ${CMAKE_CURRENT_BINARY_DIR}/CMakeConfig.h | ||
ESCAPE_QUOTES @ONLY) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
//===--- BackDeployment.cpp - Support for running on older OS versions. ---===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "swift/Runtime/BackDeployment.h" | ||
#include "swift/Runtime/Config.h" | ||
#include "../SwiftShims/FoundationShims.h" | ||
#include <stdlib.h> | ||
|
||
#if defined(__APPLE__) && defined(__MACH__) | ||
|
||
#if SWIFT_CLASS_IS_SWIFT_MASK_GLOBAL_VARIABLE | ||
static unsigned long long computeIsSwiftMask() { | ||
if (swift::_swift_isBackDeploying()) | ||
return 1ULL; | ||
return 2ULL; | ||
} | ||
|
||
SWIFT_ALLOWED_RUNTIME_GLOBAL_CTOR_BEGIN | ||
extern "C" unsigned long long | ||
_swift_classIsSwiftMask = computeIsSwiftMask(); | ||
SWIFT_ALLOWED_RUNTIME_GLOBAL_CTOR_END | ||
#endif // SWIFT_CLASS_IS_SWIFT_MASK_GLOBAL_VARIABLE | ||
|
||
static swift::_SwiftNSOperatingSystemVersion swiftInOSVersion = { | ||
#if __MAC_OS_X_VERSION_MIN_REQUIRED | ||
10, 14, 4 | ||
|
||
// WatchOS also pretends to be iOS, so check it first. | ||
#elif __WATCH_OS_VERSION_MIN_REQUIRED | ||
5, 2, 0 | ||
#elif __IPHONE_OS_VERSION_MIN_REQUIRED || __TV_OS_VERSION_MIN_REQUIRED | ||
12, 2, 0 | ||
#else | ||
9999, 0, 0 | ||
#endif | ||
|
||
}; | ||
|
||
static bool versionLessThan(swift::_SwiftNSOperatingSystemVersion lhs, | ||
swift::_SwiftNSOperatingSystemVersion rhs) { | ||
if (lhs.majorVersion < rhs.majorVersion) return true; | ||
if (lhs.majorVersion > rhs.majorVersion) return false; | ||
|
||
if (lhs.minorVersion < rhs.minorVersion) return true; | ||
if (lhs.minorVersion > rhs.minorVersion) return false; | ||
|
||
if (lhs.patchVersion < rhs.patchVersion) return true; | ||
|
||
return false; | ||
} | ||
|
||
SWIFT_RUNTIME_STDLIB_INTERNAL | ||
int _swift_isBackDeploying() { | ||
auto version = swift::_swift_stdlib_operatingSystemVersion(); | ||
return versionLessThan(version, swiftInOSVersion); | ||
} | ||
#endif |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// RUN: %empty-directory(%t) | ||
// -- Deployment target is set to pre-10.14.4 so that we use the "old" | ||
// Swift runtime bit in compiler-emitted classes | ||
// RUN: %target-build-swift -target x86_64-apple-macosx10.9 %s -module-name main -o %t/a.out | ||
// RUN: %target-run %t/a.out | %FileCheck %s | ||
|
||
// REQUIRES: executable_test | ||
// REQUIRES: OS=macosx | ||
|
||
import Foundation | ||
|
||
// A fixed-layout class should be considered Swift metadata by the OS runtime. | ||
class FixedLayout { } | ||
|
||
debugPrint(FixedLayout.self) // CHECK: main.FixedLayout | ||
|
||
// A generic class | ||
class GenericBase<T> { } | ||
debugPrint(GenericBase<Int>.self) // CHECK-NEXT: main.GenericBase<Swift.Int> | ||
|
||
// A singleton-initialized class | ||
class SingletonInit: GenericBase<Int> { } | ||
debugPrint(SingletonInit.self) // CHECK-NEXT: main.SingletonInit | ||
|
||
// A resilient-heritage class | ||
class ResilientSubInit: JSONEncoder {} | ||
debugPrint(ResilientSubInit.self) // CHECK-NEXT: main.ResilientSubInit | ||
|
||
print("nailed it") // CHECK-NEXT: nailed it |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any point in leaving this option behind? I think that we could just remove it right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not much point other than just being incremental. I think I'd like to rip out the option as part of a separate task.