Skip to content

Commit 7272a47

Browse files
authored
Refactoring some import conditions (#298)
1 parent 85ddb13 commit 7272a47

32 files changed

+264
-92
lines changed

Sources/FoundationEssentials/Calendar/Calendar_Cache.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,9 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
1413
#if FOUNDATION_FRAMEWORK
1514
@_implementationOnly import _ForSwiftFoundation
1615
import CoreFoundation
17-
#else
1816
#endif
1917

2018
/// Singleton which listens for notifications about preference changes for Calendar and holds cached singletons for the current locale, calendar, and time zone.
@@ -24,7 +22,7 @@ struct CalendarCache : Sendable {
2422

2523
// _CalendarICU, if present
2624
static var calendarICUClass: _CalendarProtocol.Type = {
27-
#if FOUNDATION_FRAMEWORK
25+
#if FOUNDATION_FRAMEWORK && canImport(FoundationICU)
2826
_CalendarICU.self
2927
#else
3028
if let name = _typeByName("FoundationInternationalization._CalendarICU"), let t = name as? _CalendarProtocol.Type {

Sources/FoundationEssentials/Data/Data.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ private func malloc_good_size(_ size: Int) -> Int {
3737
return size
3838
}
3939

40+
#elseif canImport(C)
41+
42+
private func malloc_good_size(_ size: Int) -> Int {
43+
return size
44+
}
45+
4046
#endif
4147

4248
#if os(Windows)
@@ -46,6 +52,8 @@ import func WinSDK.UnmapViewOfFile
4652
internal func __DataInvokeDeallocatorUnmap(_ mem: UnsafeMutableRawPointer, _ length: Int) {
4753
#if os(Windows)
4854
_ = UnmapViewOfFile(mem)
55+
#elseif canImport(C)
56+
free(mem)
4957
#else
5058
munmap(mem, length)
5159
#endif

Sources/FoundationEssentials/Data/DataProtocol.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ extension DataProtocol {
210210
extension DataProtocol where Self : ContiguousBytes {
211211
public func copyBytes<DestinationType, R: RangeExpression>(to ptr: UnsafeMutableBufferPointer<DestinationType>, from range: R) where R.Bound == Index {
212212
precondition(ptr.baseAddress != nil)
213-
213+
214214
let concreteRange = range.relative(to: self)
215215
withUnsafeBytes { fullBuffer in
216216
let adv = distance(from: startIndex, to: concreteRange.lowerBound)

Sources/FoundationEssentials/Date.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ extension NSDate : _HasCustomAnyHashableRepresentation {
358358
#endif // FOUNDATION_FRAMEWORK
359359

360360
// MARK: - Playground Support
361-
#if FOUNDATION_FRAMEWORK
361+
#if FOUNDATION_FRAMEWORK && !NO_FORMATTERS
362362
@available(macOS 10.10, iOS 8.0, watchOS 2.0, tvOS 9.0, *)
363363
extension Date : _CustomPlaygroundQuickLookable {
364364
var summary: String {

Sources/FoundationEssentials/ErrorCodes+POSIX.swift

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,7 @@
99
//
1010
//===----------------------------------------------------------------------===//
1111

12-
13-
#if FOUNDATION_FRAMEWORK
14-
/// Describes an error in the POSIX error domain.
15-
@available(macOS 10.10, iOS 8.0, watchOS 2.0, tvOS 9.0, *)
16-
public struct POSIXError : _BridgedStoredNSError {
17-
public let _nsError: NSError
18-
19-
public init(_nsError error: NSError) {
20-
precondition(error.domain == NSPOSIXErrorDomain)
21-
self._nsError = error
22-
}
23-
24-
public static var errorDomain: String { return NSPOSIXErrorDomain }
25-
26-
public var hashValue: Int {
27-
return _nsError.hashValue
28-
}
29-
30-
public typealias Code = POSIXErrorCode
31-
}
32-
#else
12+
#if !FOUNDATION_FRAMEWORK || canImport(C)
3313
// Define the POSIXErrorCode for all platforms here.
3414
public enum POSIXErrorCode : Int32 {
3515
/// Operation not permitted.
@@ -366,6 +346,28 @@ public enum POSIXErrorCode : Int32 {
366346

367347
extension POSIXErrorCode : Equatable, Hashable, RawRepresentable {
368348
}
349+
#endif
350+
351+
#if FOUNDATION_FRAMEWORK
352+
/// Describes an error in the POSIX error domain.
353+
@available(macOS 10.10, iOS 8.0, watchOS 2.0, tvOS 9.0, *)
354+
public struct POSIXError : _BridgedStoredNSError {
355+
public let _nsError: NSError
356+
357+
public init(_nsError error: NSError) {
358+
precondition(error.domain == NSPOSIXErrorDomain)
359+
self._nsError = error
360+
}
361+
362+
public static var errorDomain: String { return NSPOSIXErrorDomain }
363+
364+
public var hashValue: Int {
365+
return _nsError.hashValue
366+
}
367+
368+
public typealias Code = POSIXErrorCode
369+
}
370+
#else
369371

370372
/// Describes an error in the POSIX error domain.
371373
@available(macOS 10.10, iOS 8.0, watchOS 2.0, tvOS 9.0, *)

Sources/FoundationEssentials/JSON/JSONDecoder.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ open class JSONDecoder {
6262
/// Decode the `Date` as UNIX millisecond timestamp from a JSON number.
6363
case millisecondsSince1970
6464

65-
#if FOUNDATION_FRAMEWORK
65+
#if FOUNDATION_FRAMEWORK && !NO_FORMATTERS
6666
// TODO: Reenable once DateFormatStyle has been moved: https://github.com/apple/swift-foundation/issues/46
6767
/// Decode the `Date` as an ISO-8601-formatted string (in RFC 3339 format).
6868
@available(macOS 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *)
@@ -647,7 +647,8 @@ extension JSONDecoderImpl: Decoder {
647647
case .millisecondsSince1970:
648648
let double = try self.unwrapFloatingPoint(from: mapValue, as: Double.self, for: codingPathNode, additionalKey)
649649
return Date(timeIntervalSince1970: double / 1000.0)
650-
#if FOUNDATION_FRAMEWORK // TODO: Reenable once DateFormatStyle has been moved
650+
#if FOUNDATION_FRAMEWORK && !NO_FORMATTERS
651+
// TODO: Reenable once DateFormatStyle has been moved
651652
case .iso8601:
652653
let string = try self.unwrapString(from: mapValue, for: codingPathNode, additionalKey)
653654
guard let date = try? Date.ISO8601FormatStyle().parse(string) else {

Sources/FoundationEssentials/JSON/JSONEncoder.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ open class JSONEncoder {
6767
/// Encode the `Date` as UNIX millisecond timestamp (as a JSON number).
6868
case millisecondsSince1970
6969

70-
#if FOUNDATION_FRAMEWORK
70+
#if FOUNDATION_FRAMEWORK && !NO_FORMATTERS
7171
// TODO: Reenable once DateFormatStyle has been ported: https://github.com/apple/swift-foundation/issues/46
7272
/// Encode the `Date` as an ISO-8601-formatted string (in RFC 3339 format).
7373
@available(macOS 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *)
@@ -999,7 +999,7 @@ private extension __JSONEncoder {
999999
case .millisecondsSince1970:
10001000
return try .number(from: 1000.0 * date.timeIntervalSince1970, with: .throw, for: codingPathNode, additionalKey)
10011001

1002-
#if FOUNDATION_FRAMEWORK
1002+
#if FOUNDATION_FRAMEWORK && !NO_FORMATTERS
10031003
case .iso8601:
10041004
return self.wrap(date.formatted(.iso8601))
10051005

Sources/FoundationEssentials/JSON/JSONWriter.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ internal struct JSONWriter {
351351
pretty ? writer(" : ") : writer(":")
352352
try serializeJSON(value, depth: depth)
353353
}
354-
#if FOUNDATION_FRAMEWORK
354+
#if FOUNDATION_FRAMEWORK && !NO_LOCALIZATION
355355
if sortedKeys {
356356
// TODO: Until we have a solution for sorting like Locale.system in FoundationEssentials or with the help of FoundationLocalization, this comparison requires bridging back to NSString. To avoid the extreme overhead of bridging the strings on every comparison, we'll do it up front instead.
357357
// https://github.com/apple/swift-foundation/issues/284

Sources/FoundationEssentials/Locale/Locale+Language.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ extension Locale {
5959

6060
/// Returns a list of system languages, includes the languages of all product localization for the current platform
6161
public static var systemLanguages: [Language] {
62-
#if FOUNDATION_FRAMEWORK
62+
#if FOUNDATION_FRAMEWORK && canImport(FoundationICU)
6363
NSLocale.systemLanguages().map {
6464
let comp = Components(identifier: $0 as! String)
6565
return Language(components: comp)

Sources/FoundationEssentials/Locale/Locale.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public struct Locale : Hashable, Equatable, Sendable {
7878
Locale(inner: LocaleCache.cache.system)
7979
}
8080

81-
#if FOUNDATION_FRAMEWORK
81+
#if FOUNDATION_FRAMEWORK && canImport(FoundationICU)
8282
/// This returns an instance of `Locale` that's set up exactly like it would be if the user changed the current locale to that identifier, set the preferences keys in the overrides dictionary, then called `current`.
8383
internal static func localeAsIfCurrent(name: String?, cfOverrides: CFDictionary? = nil, disableBundleMatching: Bool = false) -> Locale {
8484
return LocaleCache.cache.localeAsIfCurrent(name: name, cfOverrides: cfOverrides, disableBundleMatching: disableBundleMatching)
@@ -483,7 +483,7 @@ public struct Locale : Hashable, Equatable, Sendable {
483483
_locale.forceMinDaysInFirstWeek(calendar)
484484
}
485485

486-
#if FOUNDATION_FRAMEWORK
486+
#if FOUNDATION_FRAMEWORK && !NO_FORMATTERS
487487
// This is framework-only because Date.FormatStyle.DateStyle is Internationalization-only.
488488
package func customDateFormat(_ style: Date.FormatStyle.DateStyle) -> String? {
489489
_locale.customDateFormat(style)

Sources/FoundationEssentials/Locale/Locale_Autoupdating.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ internal final class _LocaleAutoupdating : _LocaleProtocol, @unchecked Sendable
231231
LocaleCache.cache.current.forceTemperatureUnit
232232
}
233233

234-
#if FOUNDATION_FRAMEWORK
234+
#if FOUNDATION_FRAMEWORK && !NO_FORMATTERS
235235
func customDateFormat(_ style: Date.FormatStyle.DateStyle) -> String? {
236236
LocaleCache.cache.current.customDateFormat(style)
237237
}

Sources/FoundationEssentials/Locale/Locale_Cache.swift

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ struct LocaleCache : Sendable {
2626

2727
// _LocaleICU, if present. Otherwise we use _LocaleUnlocalized. The `Locale` initializers are not failable, so we just fall back to the unlocalized type when needed without failure.
2828
static var localeICUClass: _LocaleProtocol.Type = {
29-
#if FOUNDATION_FRAMEWORK
29+
#if FOUNDATION_FRAMEWORK && canImport(FoundationICU)
3030
return _LocaleICU.self
3131
#else
3232
if let name = _typeByName("FoundationInternationalization._LocaleICU"), let t = name as? _LocaleProtocol.Type {
@@ -140,6 +140,7 @@ struct LocaleCache : Sendable {
140140
}
141141
}
142142

143+
#if canImport(FoundationICU)
143144
mutating func fixedNSLocale(_ locale: _LocaleICU) -> _NSSwiftLocale {
144145
let id = IdentifierAndPrefs(identifier: locale.identifier, prefs: locale.prefs)
145146
if let locale = cachedFixedLocaleToNSLocales[id] {
@@ -152,6 +153,7 @@ struct LocaleCache : Sendable {
152153
return nsLocale
153154
}
154155
}
156+
#endif
155157

156158
mutating func currentNSLocale(preferences: LocalePreferences?, cache: Bool) -> _NSSwiftLocale? {
157159
resetCurrentIfNeeded()
@@ -172,9 +174,14 @@ struct LocaleCache : Sendable {
172174
return nil
173175
}
174176

177+
#if canImport(FoundationICU)
175178
// We have neither a Swift Locale nor an NSLocale. Recalculate and set both.
176179
let locale = _LocaleICU(name: nil, prefs: preferences, disableBundleMatching: false)
180+
#else
181+
let locale = _LocaleUnlocalized(name: nil, prefs: preferences, disableBundleMatching: false)
182+
#endif
177183
let nsLocale = _NSSwiftLocale(Locale(inner: locale))
184+
178185
if cache {
179186
// It's possible this was an 'incomplete locale', in which case we will want to calculate it again later.
180187
self.cachedCurrentLocale = locale
@@ -281,9 +288,11 @@ struct LocaleCache : Sendable {
281288
lock.withLock { $0.fixedNSLocale(identifier: id) }
282289
}
283290

291+
#if canImport(FoundationICU)
284292
func fixedNSLocale(_ locale: _LocaleICU) -> _NSSwiftLocale {
285293
lock.withLock { $0.fixedNSLocale(locale) }
286294
}
295+
#endif
287296

288297
func autoupdatingCurrentNSLocale() -> _NSSwiftLocale {
289298
lock.withLock { $0.autoupdatingNSLocale() }
@@ -319,7 +328,7 @@ struct LocaleCache : Sendable {
319328
lock.withLock { $0.fixedComponents(comps) }
320329
}
321330

322-
#if FOUNDATION_FRAMEWORK
331+
#if FOUNDATION_FRAMEWORK && !NO_CFPREFERENCES
323332
func preferences() -> (LocalePreferences, Bool) {
324333
// On Darwin, we check the current user preferences for Locale values
325334
var wouldDeadlock: DarwinBoolean = false
@@ -372,7 +381,7 @@ struct LocaleCache : Sendable {
372381
}
373382
#endif
374383

375-
#if FOUNDATION_FRAMEWORK
384+
#if FOUNDATION_FRAMEWORK && !NO_CFPREFERENCES
376385
/// This returns an instance of `Locale` that's set up exactly like it would be if the user changed the current locale to that identifier, set the preferences keys in the overrides dictionary, then called `current`.
377386
func localeAsIfCurrent(name: String?, cfOverrides: CFDictionary? = nil, disableBundleMatching: Bool = false) -> Locale {
378387

@@ -399,7 +408,7 @@ struct LocaleCache : Sendable {
399408
}
400409

401410
func localeAsIfCurrentWithBundleLocalizations(_ availableLocalizations: [String], allowsMixedLocalizations: Bool) -> Locale? {
402-
#if FOUNDATION_FRAMEWORK
411+
#if FOUNDATION_FRAMEWORK && canImport(FoundationICU)
403412
guard !allowsMixedLocalizations else {
404413
let (prefs, _) = preferences()
405414
let inner = _LocaleICU(name: nil, prefs: prefs, disableBundleMatching: true)

Sources/FoundationEssentials/Locale/Locale_Preferences.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ package struct LocalePreferences: Hashable {
7575
// The OS no longer writes out this preference, but we keep it here for compatibility with CFDateFormatter behavior.
7676
package var icuNumberFormatStrings: CFDictionary?
7777
package var icuNumberSymbols: CFDictionary?
78+
#if !NO_FORMATTERS
7879
package var dateFormats: [Date.FormatStyle.DateStyle: String]? // Bridged version of `icuDateFormatStrings`
80+
#endif
7981
#endif
8082
package var numberSymbols: [UInt32 : String]? // Bridged version of `icuNumberSymbols`
8183

@@ -87,7 +89,7 @@ package struct LocalePreferences: Hashable {
8789

8890
package init() { }
8991

90-
#if FOUNDATION_FRAMEWORK
92+
#if FOUNDATION_FRAMEWORK && canImport(FoundationICU)
9193
// The framework init supports customized dateFormats
9294
package init(metricUnits: Bool? = nil,
9395
languages: [String]? = nil,
@@ -153,7 +155,7 @@ package struct LocalePreferences: Hashable {
153155
}
154156
#endif
155157

156-
#if FOUNDATION_FRAMEWORK
158+
#if FOUNDATION_FRAMEWORK && !NO_CFPREFERENCES
157159
/// Interpret a dictionary (from user defaults) according to a predefined set of strings and convert it into the more strongly-typed `LocalePreferences` values.
158160
/// Several dictionaries may need to be applied to the same instance, which is why this is structured as a mutating setter rather than an initializer.
159161
/// Why use a `CFDictionary` instead of a Swift dictionary here? The input prefs may be a complete copy of the user's prefs, and we don't want to bridge a ton of unrelated data into Swift just to extract a few keys. Keeping it as a `CFDictionary` avoids that overhead, and we call into small CF helper functions to get the data we need, if it is there.
@@ -284,15 +286,17 @@ package struct LocalePreferences: Hashable {
284286
if let other = prefs.collationOrder { self.collationOrder = other }
285287
if let other = prefs.firstWeekday { self.firstWeekday = other }
286288
if let other = prefs.minDaysInFirstWeek { self.minDaysInFirstWeek = other }
289+
if let other = prefs.numberSymbols { self.numberSymbols = other }
287290
#if FOUNDATION_FRAMEWORK
288291
if let other = prefs.icuDateTimeSymbols { self.icuDateTimeSymbols = other }
289292
if let other = prefs.icuDateFormatStrings { self.icuDateFormatStrings = other }
290293
if let other = prefs.icuTimeFormatStrings { self.icuTimeFormatStrings = other }
291294
if let other = prefs.icuNumberFormatStrings { self.icuNumberFormatStrings = other }
292295
if let other = prefs.icuNumberSymbols { self.icuNumberSymbols = other }
296+
#if !NO_FORMATTERS
293297
if let other = prefs.dateFormats { self.dateFormats = other }
294-
#endif
295-
if let other = prefs.numberSymbols { self.numberSymbols = other }
298+
#endif // !NO_FORMATTERS
299+
#endif // FOUNDATION_FRAMEWORK
296300
if let other = prefs.country { self.country = other }
297301
if let other = prefs.measurementUnits { self.measurementUnits = other }
298302
if let other = prefs.temperatureUnit { self.temperatureUnit = other }

Sources/FoundationEssentials/Locale/Locale_Protocol.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,11 @@ package protocol _LocaleProtocol : AnyObject, Sendable, CustomDebugStringConvert
9191

9292
func bridgeToNSLocale() -> NSLocale
9393

94+
#if !NO_FORMATTERS
9495
// This is framework-only because Date.FormatStyle.DateStlye is Internationalization-only
9596
func customDateFormat(_ style: Date.FormatStyle.DateStyle) -> String?
9697
#endif
98+
#endif
9799
}
98100

99101
extension _LocaleProtocol {

Sources/FoundationEssentials/Locale/Locale_Unlocalized.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ internal final class _LocaleUnlocalized : _LocaleProtocol, @unchecked Sendable {
233233
nil
234234
}
235235

236-
#if FOUNDATION_FRAMEWORK
236+
#if FOUNDATION_FRAMEWORK && !NO_FORMATTERS
237237
func customDateFormat(_ style: Date.FormatStyle.DateStyle) -> String? {
238238
nil
239239
}

Sources/FoundationEssentials/Platform.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
#if canImport(Darwin)
1313
import Darwin
1414

15-
fileprivate let _pageSize = Int(vm_page_size)
15+
fileprivate var _pageSize: Int {
16+
Int(vm_page_size)
17+
}
1618
#elseif canImport(WinSDK)
1719
import WinSDK
1820
fileprivate var _pageSize: Int {
@@ -23,13 +25,17 @@ fileprivate var _pageSize: Int {
2325
#elseif os(WASI)
2426
// WebAssembly defines a fixed page size
2527
fileprivate let _pageSize: Int = 65_536
26-
#else
28+
#elseif canImport(Glibc)
2729
import Glibc
2830
fileprivate let _pageSize: Int = Int(getpagesize())
31+
#elseif canImport(C)
32+
fileprivate let _pageSize: Int = Int(getpagesize())
2933
#endif // canImport(Darwin)
3034

3135
internal struct Platform {
32-
static var pageSize: Int = _pageSize
36+
static var pageSize: Int {
37+
_pageSize
38+
}
3339

3440
static func roundDownToMultipleOfPageSize(_ size: Int) -> Int {
3541
return size & ~(self.pageSize - 1)

Sources/FoundationEssentials/Predicate/Archiving/PredicateCodableConfiguration.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#if FOUNDATION_FRAMEWORK
1414

15+
#if canImport(ReflectionInternal)
1516
@_implementationOnly
1617
import ReflectionInternal
1718

@@ -499,4 +500,5 @@ extension PredicateCodableConfiguration {
499500
}()
500501
}
501502

503+
#endif // canImport(ReflectionInternal)
502504
#endif // FOUNDATION_FRAMEWORK

0 commit comments

Comments
 (0)