Skip to content

Commit d36db74

Browse files
committed
[Concurrency][Stdlib] Add SwiftStdlibCurrentOS availability, use it.
If you use SwiftStdlibCurrentOS availability, you will be able to use new types and functions from within the implementation. This works by, when appropriate, building with the CurrentOS availability set to the current deployment target. rdar://148275746
1 parent 0e6f3a8 commit d36db74

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+416
-266
lines changed

CMakeLists.txt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,10 @@ option(SWIFT_STDLIB_ASSERTIONS
462462
"Enable internal checks for the Swift standard library (useful for debugging the library itself, does not affect checks required for safety)"
463463
"${SWIFT_STDLIB_ASSERTIONS_default}")
464464

465+
option(SWIFT_STDLIB_ENABLE_STRICT_AVAILABILITY
466+
"Enable strict availability; this will cause things to break at desk or in CI if the host OS is a lower version than some `@availability` annotations in the runtime code"
467+
FALSE)
468+
465469
option(SWIFT_BUILD_RUNTIME_WITH_HOST_COMPILER
466470
"Use the host compiler and not the internal clang to build the swift runtime"
467471
FALSE)
@@ -1404,8 +1408,9 @@ endif()
14041408

14051409
if(SWIFT_BUILD_STDLIB OR SWIFT_BUILD_SDK_OVERLAY)
14061410
message(STATUS "Building Swift standard library and overlays for SDKs: ${SWIFT_SDKS}")
1407-
message(STATUS " Build type: ${SWIFT_STDLIB_BUILD_TYPE}")
1408-
message(STATUS " Assertions: ${SWIFT_STDLIB_ASSERTIONS}")
1411+
message(STATUS " Build type: ${SWIFT_STDLIB_BUILD_TYPE}")
1412+
message(STATUS " Assertions: ${SWIFT_STDLIB_ASSERTIONS}")
1413+
message(STATUS " Strict availability: ${SWIFT_STDLIB_ENABLE_STRICT_AVAILABILITY}")
14091414
message(STATUS "")
14101415

14111416
message(STATUS "Building Swift runtime with:")

Runtimes/Core/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ defaulted_option(SwiftCore_ENABLE_BACKTRACING "Enable backtracing runtime suppor
125125
defaulted_set(SwiftCore_BACKTRACER_PATH STRING "Set a fixed path to the Swift backtracer")
126126
defaulted_option(SwiftCore_ENABLE_FATALERROR_BACKTRACE "Build stdlib fatalError with backtrace output")
127127
defaulted_option(SwiftCore_ENABLE_PRESPECIALIZATION "Enable generic metadata prespecialization")
128+
defaulted_option(SwiftCore_ENABLE_STRICT_AVAILABILITY "Enable strict availability; this will cause things to break at desk or in CI if the host OS is a lower version than some `@availability` annotations in the runtime code")
128129

129130
option(SwiftCore_ENABLE_CLOBBER_FREED_OBJECTS "" OFF)
130131
option(SwiftCore_ENABLE_RUNTIME_LEAK_CHECKER "" OFF)

Runtimes/Core/cmake/caches/Vendors/Apple/apple-common.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ set(SwiftCore_ENABLE_VECTOR_TYPES ON CACHE BOOL "")
1212
set(SwiftCore_ENABLE_RUNTIME_FUNCTION_COUNTERS ON CACHE BOOL "")
1313
set(SwiftCore_ENABLE_BACKDEPLOYMENT_SUPPORT ON CACHE BOOL "")
1414
set(SwiftCore_ENABLE_FILESYSTEM_SUPPORT ON CACHE BOOL "")
15+
set(SwiftCore_ENABLE_STRICT_AVAILABILITY ON CACHE BOOL "")
16+
1517
set(SwiftCore_OPTIMIZATION_REMARKS "bitstream" CACHE STRING "")
1618

1719
set(SwiftCore_INSTALL_NESTED_SUBDIR OFF CACHE BOOL "")

Runtimes/Core/cmake/modules/AvailabilityMacros.cmake

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,22 @@ file(STRINGS "${CMAKE_CURRENT_BINARY_DIR}/availability-macros.def" availability_
55
list(FILTER availability_defs EXCLUDE REGEX "^\\s*(#.*)?$")
66
foreach(def ${availability_defs})
77
add_compile_options("$<$<COMPILE_LANGUAGE:Swift>:SHELL:-Xfrontend -define-availability -Xfrontend \"${def}\">")
8+
9+
if("${def}" MATCHES "SwiftStdlib .*")
10+
# For each SwiftStdlib x.y, also define SwiftStdlibTargetOS x.y, which,
11+
# will expand to the current `-target` platform if the macro defines a
12+
# newer platform as its availability.
13+
#
14+
# There is a setting, SwiftCore_ENABLE_STRICT_AVAILABILITY, which if set
15+
# ON will cause us to use the "proper" availability instead.
16+
string(REPLACE "SwiftStdlib" "SwiftStdlibCurrentOS" current "${def}")
17+
if(NOT SwiftCore_ENABLE_STRICT_AVAILABILITY AND SwiftCore_SWIFT_AVAILABILITY_PLATFORM)
18+
string(REGEX MATCH "${SwiftCore_SWIFT_AVAILABILITY_PLATFORM} ([0-9]+(\.[0-9]+)+)" platform_version "${def}")
19+
string(REGEX MATCH "[0-9]+(\.[0-9]+)+" version "${platform_version}")
20+
if(NOT version STREQUAL "9999" AND version VERSION_GREATER CMAKE_OSX_DEPLOYMENT_TARGET)
21+
string(REGEX REPLACE ":.*" ":${SwiftCore_SWIFT_AVAILABILITY_PLATFORM} ${CMAKE_OSX_DEPLOYMENT_TARGET}" current "${current}")
22+
endif()
23+
endif()
24+
add_compile_options("$<$<COMPILE_LANGUAGE:Swift>:SHELL:-Xfrontend -define-availability -Xfrontend \"${current}\">")
25+
endif()
826
endforeach()

Runtimes/Core/cmake/modules/DefaultSettings.cmake

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ set(SwiftCore_ENABLE_COMMANDLINE_SUPPORT_default OFF) # TODO: enable this by def
1111
set(SwiftCore_ENABLE_STDIN_default ON)
1212
set(SwiftCore_ENABLE_TYPE_PRINTING_default ON)
1313

14+
set(SwiftCore_ENABLE_STRICT_AVAILABILITY_default OFF)
15+
1416
set(SwiftCore_BACKTRACER_PATH_default "")
1517

1618
# Provide a boolean option that a user can optionally enable.
@@ -20,7 +22,7 @@ macro(defaulted_option variable helptext)
2022
if(NOT DEFINED ${variable}_default)
2123
set(${variable}_default OFF)
2224
endif()
23-
option(${variable} ${helptext} ${${variable}_default})
25+
option(${variable} "${helptext}" ${${variable}_default})
2426
endmacro()
2527

2628
# Create a defaulted cache entry

Runtimes/Core/cmake/modules/PlatformInfo.cmake

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,26 @@ if(NOT SwiftCore_ARCH_SUBDIR)
3636

3737
message(CONFIGURE_LOG "Swift Arch: ${arch}")
3838
endif()
39+
40+
if(NOT SwiftCore_SWIFT_AVAILABILITY_PLATFORM)
41+
set(availability_platform_macosx "macOS")
42+
set(availability_platform_ios "iOS")
43+
set(availability_platform_watchos "watchOS")
44+
set(availability_platform_tvos "tvOS")
45+
set(availability_platform_xros "visionOS")
46+
set(availability_platform_bridgeos "bridgeOS")
47+
48+
if(SwiftCore_SWIFT_MODULE_TRIPLE MATCHES ".*-([^-]+)-simulator$")
49+
set(platform "${CMAKE_MATCH_1}")
50+
elseif(SwiftCore_SWIFT_MODULE_TRIPLE MATCHES ".*-([^-]+)$")
51+
set(platform "${CMAKE_MATCH_1}")
52+
else()
53+
message(ERROR "Unable to extract platform name from triple ${SwiftCore_SWIFT_MODULE_TRIPLE}")
54+
endif()
55+
56+
if(availability_platform_${platform})
57+
set(SwiftCore_SWIFT_AVAILABILITY_PLATFORM "${availability_platform_${platform}}")
58+
else()
59+
message(ERROR "Unknown platform ${platform} for availability")
60+
endif()
61+
endif()

cmake/modules/DarwinSDKs.cmake

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ is_sdk_requested(OSX swift_build_osx)
1212
if(swift_build_osx)
1313
configure_sdk_darwin(
1414
OSX "OS X" "${SWIFT_DARWIN_DEPLOYMENT_VERSION_OSX}"
15-
macosx macosx macos "${SUPPORTED_OSX_ARCHS}")
15+
macosx macosx macos macOS "${SUPPORTED_OSX_ARCHS}")
1616
configure_target_variant(OSX-DA "OS X Debug+Asserts" OSX DA "Debug+Asserts")
1717
configure_target_variant(OSX-RA "OS X Release+Asserts" OSX RA "Release+Asserts")
1818
configure_target_variant(OSX-R "OS X Release" OSX R "Release")
@@ -26,12 +26,15 @@ if(swift_build_freestanding AND (SWIFT_FREESTANDING_FLAVOR STREQUAL "apple"))
2626
"Which triple name (e.g. 'none-macho') to use when building the FREESTANDING stdlib")
2727
set(SWIFT_FREESTANDING_MODULE_NAME "" CACHE STRING
2828
"Which .swiftmodule name (e.g. 'freestanding') to use when building the FREESTANDING stdlib")
29+
set(SWIFT_FREESTANDING_AVAILABILITY_NAME "" CACHE STRING
30+
"Which @availability name (e.g. 'macOS') to use when building the FREESTANDING stdlib")
2931
set(SWIFT_FREESTANDING_ARCHS "" CACHE STRING
3032
"Which architectures to build when building the FREESTANDING stdlib")
3133
configure_sdk_darwin(
3234
FREESTANDING "FREESTANDING" ""
3335
"${SWIFT_FREESTANDING_SDK}"
34-
"${SWIFT_FREESTANDING_TRIPLE_NAME}" "${SWIFT_FREESTANDING_MODULE_NAME}" "${SWIFT_FREESTANDING_ARCHS}")
36+
"${SWIFT_FREESTANDING_TRIPLE_NAME}" "${SWIFT_FREESTANDING_MODULE_NAME}"
37+
"${SWIFT_FREESTANDING_AVAILABILITY_NAME}" "${SWIFT_FREESTANDING_ARCHS}")
3538
set(SWIFT_SDK_FREESTANDING_LIB_SUBDIR "freestanding")
3639
configure_target_variant(FREESTANDING-DA "FREESTANDING Debug+Asserts" FREESTANDING DA "Debug+Asserts")
3740
configure_target_variant(FREESTANDING-RA "FREESTANDING Release+Asserts" FREESTANDING RA "Release+Asserts")
@@ -53,7 +56,7 @@ is_sdk_requested(IOS swift_build_ios)
5356
if(swift_build_ios)
5457
configure_sdk_darwin(
5558
IOS "iOS" "${SWIFT_DARWIN_DEPLOYMENT_VERSION_IOS}"
56-
iphoneos ios ios "${SUPPORTED_IOS_ARCHS}")
59+
iphoneos ios ios iOS "${SUPPORTED_IOS_ARCHS}")
5760
configure_target_variant(IOS-DA "iOS Debug+Asserts" IOS DA "Debug+Asserts")
5861
configure_target_variant(IOS-RA "iOS Release+Asserts" IOS RA "Release+Asserts")
5962
configure_target_variant(IOS-R "iOS Release" IOS R "Release")
@@ -63,7 +66,7 @@ is_sdk_requested(IOS_SIMULATOR swift_build_ios_simulator)
6366
if(swift_build_ios_simulator)
6467
configure_sdk_darwin(
6568
IOS_SIMULATOR "iOS Simulator" "${SWIFT_DARWIN_DEPLOYMENT_VERSION_IOS}"
66-
iphonesimulator ios ios-simulator
69+
iphonesimulator ios ios-simulator iOS
6770
"${SUPPORTED_IOS_SIMULATOR_ARCHS}")
6871
configure_target_variant(
6972
IOS_SIMULATOR-DA "iOS Debug+Asserts" IOS_SIMULATOR DA "Debug+Asserts")
@@ -77,7 +80,7 @@ is_sdk_requested(TVOS swift_build_tvos)
7780
if(swift_build_tvos)
7881
configure_sdk_darwin(
7982
TVOS "tvOS" "${SWIFT_DARWIN_DEPLOYMENT_VERSION_TVOS}"
80-
appletvos tvos tvos "${SUPPORTED_TVOS_ARCHS}")
83+
appletvos tvos tvos tvOS "${SUPPORTED_TVOS_ARCHS}")
8184
configure_target_variant(TVOS-DA "tvOS Debug+Asserts" TVOS DA "Debug+Asserts")
8285
configure_target_variant(TVOS-RA "tvOS Release+Asserts" TVOS RA "Release+Asserts")
8386
configure_target_variant(TVOS-R "tvOS Release" TVOS R "Release")
@@ -87,7 +90,7 @@ is_sdk_requested(TVOS_SIMULATOR swift_build_tvos_simulator)
8790
if(swift_build_tvos_simulator)
8891
configure_sdk_darwin(
8992
TVOS_SIMULATOR "tvOS Simulator" "${SWIFT_DARWIN_DEPLOYMENT_VERSION_TVOS}"
90-
appletvsimulator tvos tvos-simulator
93+
appletvsimulator tvos tvos-simulator tvOS
9194
"${SUPPORTED_TVOS_SIMULATOR_ARCHS}")
9295
configure_target_variant(
9396
TVOS_SIMULATOR-DA "tvOS Debug+Asserts" TVOS_SIMULATOR DA "Debug+Asserts")
@@ -101,7 +104,7 @@ is_sdk_requested(WATCHOS swift_build_watchos)
101104
if(swift_build_watchos)
102105
configure_sdk_darwin(
103106
WATCHOS "watchOS" "${SWIFT_DARWIN_DEPLOYMENT_VERSION_WATCHOS}"
104-
watchos watchos watchos "${SUPPORTED_WATCHOS_ARCHS}")
107+
watchos watchos watchos watchOS "${SUPPORTED_WATCHOS_ARCHS}")
105108
configure_target_variant(WATCHOS-DA "watchOS Debug+Asserts" WATCHOS DA "Debug+Asserts")
106109
configure_target_variant(WATCHOS-RA "watchOS Release+Asserts" WATCHOS RA "Release+Asserts")
107110
configure_target_variant(WATCHOS-R "watchOS Release" WATCHOS R "Release")
@@ -111,7 +114,7 @@ is_sdk_requested(WATCHOS_SIMULATOR swift_build_watchos_simulator)
111114
if(swift_build_watchos_simulator)
112115
configure_sdk_darwin(
113116
WATCHOS_SIMULATOR "watchOS Simulator" "${SWIFT_DARWIN_DEPLOYMENT_VERSION_WATCHOS}"
114-
watchsimulator watchos watchos-simulator
117+
watchsimulator watchos watchos-simulator watchOS
115118
"${SUPPORTED_WATCHOS_SIMULATOR_ARCHS}")
116119
configure_target_variant(WATCHOS_SIMULATOR-DA "watchOS Debug+Asserts" WATCHOS_SIMULATOR DA "Debug+Asserts")
117120
configure_target_variant(WATCHOS_SIMULATOR-RA "watchOS Release+Asserts" WATCHOS_SIMULATOR RA "Release+Asserts")
@@ -122,7 +125,7 @@ is_sdk_requested(XROS swift_build_xros)
122125
if(swift_build_xros)
123126
configure_sdk_darwin(
124127
XROS "xrOS" "${SWIFT_DARWIN_DEPLOYMENT_VERSION_XROS}"
125-
xros xros xros "${SUPPORTED_XROS_ARCHS}")
128+
xros xros xros visionOS "${SUPPORTED_XROS_ARCHS}")
126129
configure_target_variant(XROS-DA "xrOS Debug+Asserts" XROS DA "Debug+Asserts")
127130
configure_target_variant(XROS-RA "xrOS Release+Asserts" XROS RA "Release+Asserts")
128131
configure_target_variant(XROS-R "xrOS Release" XROS R "Release")
@@ -132,7 +135,7 @@ is_sdk_requested(XROS_SIMULATOR swift_build_xros_simulator)
132135
if(swift_build_xros_simulator)
133136
configure_sdk_darwin(
134137
XROS_SIMULATOR "xrOS Simulator" "${SWIFT_DARWIN_DEPLOYMENT_VERSION_XROS}"
135-
xrsimulator xros xros-simulator
138+
xrsimulator xros xros-simulator visionOS
136139
"${SUPPORTED_XROS_SIMULATOR_ARCHS}")
137140

138141
configure_target_variant(

cmake/modules/SwiftConfigureSDK.cmake

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ endfunction()
136136
# deployment_version # Deployment version
137137
# xcrun_name # SDK name to use with xcrun
138138
# triple_name # The name used in Swift's -triple
139+
# availability_name # The name used in Swift's @availability
139140
# architectures # A list of architectures this SDK supports
140141
# )
141142
#
@@ -165,9 +166,11 @@ endfunction()
165166
# SWIFT_SDK_${prefix}_ARCH_${ARCH}_TRIPLE Triple name
166167
# SWIFT_SDK_${prefix}_ARCH_${ARCH}_MODULE Module triple name for this SDK
167168
# SWIFT_SDK_${prefix}_USE_BUILD_ID Whether to pass --build-id to the linker
169+
# SWIFT_SDK_${prefix}_AVAILABILITY_NAME Name to use in @availability
170+
#
168171
macro(configure_sdk_darwin
169172
prefix name deployment_version xcrun_name
170-
triple_name module_name architectures)
173+
triple_name module_name availability_name architectures)
171174
# Note: this has to be implemented as a macro because it sets global
172175
# variables.
173176

@@ -201,6 +204,7 @@ macro(configure_sdk_darwin
201204
set(SWIFT_SDK_${prefix}_DEPLOYMENT_VERSION "${deployment_version}")
202205
set(SWIFT_SDK_${prefix}_LIB_SUBDIR "${xcrun_name}")
203206
set(SWIFT_SDK_${prefix}_TRIPLE_NAME "${triple_name}")
207+
set(SWIFT_SDK_${prefix}_AVAILABILITY_NAME "${availability_name}")
204208
set(SWIFT_SDK_${prefix}_OBJECT_FORMAT "MACHO")
205209
set(SWIFT_SDK_${prefix}_USE_ISYSROOT TRUE)
206210
set(SWIFT_SDK_${prefix}_SHARED_LIBRARY_PREFIX "lib")

stdlib/cmake/modules/AddSwiftStdlib.cmake

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,6 +1003,25 @@ function(add_swift_target_library_single target name)
10031003
# Define availability macros.
10041004
foreach(def ${SWIFT_STDLIB_AVAILABILITY_DEFINITIONS})
10051005
list(APPEND SWIFTLIB_SINGLE_SWIFT_COMPILE_FLAGS "-Xfrontend" "-define-availability" "-Xfrontend" "${def}")
1006+
1007+
if("${def}" MATCHES "SwiftStdlib .*")
1008+
# For each SwiftStdlib x.y, also define SwiftStdlibTargetOS x.y, which,
1009+
# will expand to the current `-target` platform if the macro defines a
1010+
# newer platform as its availability.
1011+
#
1012+
# There is a setting, SWIFT_STDLIB_ENABLE_STRICT_AVAILABILITY, which if set
1013+
# ON will cause us to use the "proper" availability instead.
1014+
string(REPLACE "SwiftStdlib" "SwiftStdlibCurrentOS" current "${def}")
1015+
if(NOT SWIFT_STDLIB_ENABLE_STRICT_AVAILABILITY AND SWIFT_SDK_${SWIFTLIB_SINGLE_SDK}_AVAILABILITY_NAME)
1016+
string(REGEX MATCH "${SWIFT_SDK_${SWIFTLIB_SINGLE_SDK}_AVAILABILITY_NAME} ([0-9]+(\.[0-9]+)+)" platform_version "${def}")
1017+
string(REGEX MATCH "[0-9]+(\.[0-9]+)+" version "${platform_version}")
1018+
if(NOT version STREQUAL "9999" AND version VERSION_GREATER "${SWIFT_SDK_${SWIFTLIB_SINGLE_SDK}_DEPLOYMENT_VERSION}")
1019+
string(REGEX REPLACE ":.*" ":${SWIFT_SDK_${SWIFTLIB_SINGLE_SDK}_AVAILABILITY_NAME} ${SWIFT_SDK_${SWIFTLIB_SINGLE_SDK}_DEPLOYMENT_VERSION}" current "${current}")
1020+
endif()
1021+
endif()
1022+
1023+
list(APPEND SWIFTLIB_SINGLE_SWIFT_COMPILE_FLAGS "-Xfrontend" "-define-availability" "-Xfrontend" "${current}")
1024+
endif()
10061025
endforeach()
10071026

10081027
# Enable -target-min-inlining-version

stdlib/public/Concurrency/CFExecutor.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ enum CoreFoundation {
4343

4444
// .. Main Executor ............................................................
4545

46-
@available(SwiftStdlib 6.2, *)
46+
@available(SwiftStdlibCurrentOS 6.2, *)
4747
public final class CFMainExecutor: DispatchMainExecutor, @unchecked Sendable {
4848

4949
override public func run() throws {
@@ -58,7 +58,7 @@ public final class CFMainExecutor: DispatchMainExecutor, @unchecked Sendable {
5858

5959
// .. Task Executor ............................................................
6060

61-
@available(SwiftStdlib 6.2, *)
61+
@available(SwiftStdlibCurrentOS 6.2, *)
6262
public final class CFTaskExecutor: DispatchGlobalTaskExecutor,
6363
@unchecked Sendable {
6464

0 commit comments

Comments
 (0)