Skip to content

Commit a5d247c

Browse files
committed
build without Foundation for embedded use
1 parent 85fcb6d commit a5d247c

36 files changed

+193
-51
lines changed

Examples/OCADevice/DeviceApp.swift

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public enum DeviceApp {
3838
public static func main() async throws {
3939
var listenAddress = sockaddr_in()
4040
listenAddress.sin_family = sa_family_t(AF_INET)
41-
listenAddress.sin_addr.s_addr = 0 // INADDR_ANY equivalent
41+
listenAddress.sin_addr.s_addr = 0 // INADDR_ANY equivalent
4242
listenAddress.sin_port = port.bigEndian
4343
#if canImport(Darwin) || os(FreeBSD) || os(OpenBSD)
4444
listenAddress.sin_len = UInt8(MemoryLayout<sockaddr_in>.size)
@@ -61,7 +61,8 @@ public enum DeviceApp {
6161
}
6262
let delegate = DeviceEventDelegate()
6363
await device.setEventDelegate(delegate)
64-
#if os(Linux)
64+
65+
#if os(Linux) && NonEmbeddedBuild
6566
let streamEndpoint = try await Ocp1IORingStreamDeviceEndpoint(address: listenAddress.data)
6667
let datagramEndpoint = try await Ocp1IORingDatagramDeviceEndpoint(address: listenAddress.data)
6768
let stream6Endpoint = try await Ocp1IORingStreamDeviceEndpoint(address: listen6Address.data)
@@ -70,7 +71,7 @@ public enum DeviceApp {
7071
try? await Ocp1IORingStreamDeviceEndpoint(path: "/tmp/oca-device.sock")
7172
let domainSocketDatagramEndpoint =
7273
try? await Ocp1IORingDatagramDeviceEndpoint(path: "/tmp/oca-device-dg.sock")
73-
#elseif canImport(FlyingSocks)
74+
#elseif canImport(FlyingSocks) && NonEmbeddedBuild
7475
let streamEndpoint = try await Ocp1FlyingSocksStreamDeviceEndpoint(address: listenAddress.data)
7576
let datagramEndpoint =
7677
try await Ocp1FlyingSocksDatagramDeviceEndpoint(address: listenAddress.data)
@@ -87,9 +88,10 @@ public enum DeviceApp {
8788
#else
8889
let streamEndpoint = try await Ocp1StreamDeviceEndpoint(address: listenAddress.data)
8990
#endif
90-
#if canImport(FlyingSocks)
91+
92+
#if canImport(FlyingFox) && NonEmbeddedBuild
9193
listenAddress.sin_family = sa_family_t(AF_INET)
92-
listenAddress.sin_addr.s_addr = 0 // INADDR_ANY equivalent
94+
listenAddress.sin_addr.s_addr = 0 // INADDR_ANY equivalent
9395
listenAddress.sin_port = (port + 2).bigEndian
9496
#if canImport(Darwin) || os(FreeBSD) || os(OpenBSD)
9597
listenAddress.sin_len = UInt8(MemoryLayout<sockaddr_in>.size)
@@ -132,7 +134,9 @@ public enum DeviceApp {
132134
)
133135
try await block.add(actionObject: gain)
134136

137+
#if NonEmbeddedBuild
135138
try await serializeDeserialize(device.rootBlock)
139+
#endif
136140

137141
let controlNetwork = try await SwiftOCADevice.OcaControlNetwork(deviceDelegate: device)
138142
Task { @OcaDevice in controlNetwork.state = .running }
@@ -145,6 +149,7 @@ public enum DeviceApp {
145149
}
146150
}
147151

152+
#if NonEmbeddedBuild
148153
try await withThrowingTaskGroup(of: Void.self) { taskGroup in
149154
taskGroup.addTask {
150155
print("Starting OCP.1 IPv4 stream endpoint \(streamEndpoint)...")
@@ -186,9 +191,14 @@ public enum DeviceApp {
186191
#endif
187192
try await taskGroup.next()
188193
}
194+
#else
195+
print("Starting OCP.1 IPv4 stream endpoint \(streamEndpoint)...")
196+
try await streamEndpoint.run()
197+
#endif
189198
}
190199
}
191200

201+
#if NonEmbeddedBuild
192202
func serializeDeserialize(
193203
_ object: SwiftOCADevice
194204
.OcaBlock<SwiftOCADevice.OcaRoot>
@@ -203,3 +213,4 @@ func serializeDeserialize(
203213
debugPrint("serialization error: \(error)")
204214
}
205215
}
216+
#endif

Package.swift

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,62 +15,64 @@ if EnableASAN {
1515
ASANLinkerSettings.append(LinkerSetting.linkedLibrary("asan"))
1616
}
1717

18-
let PlatformPackageDependencies: [Package.Dependency]
19-
let PlatformTargetDependencies: [Target.Dependency]
18+
var PlatformPackageDependencies: [Package.Dependency] = []
19+
var PlatformTargetDependencies: [Target.Dependency] = []
2020
let PlatformProducts: [Product]
2121
let PlatformTargets: [Target]
2222
let SwiftLanguageVersionSetting: [SwiftSetting]
2323

24+
PlatformPackageDependencies += [
25+
.package(url: "https://github.com/PADL/FlyingFox", branch: "main"),
26+
]
27+
28+
PlatformTargetDependencies += [
29+
.product(
30+
name: "FlyingSocks",
31+
package: "FlyingFox"
32+
),
33+
.product(
34+
name: "FlyingFox",
35+
package: "FlyingFox",
36+
condition: .when(traits: ["NonEmbeddedBuild"])
37+
),
38+
]
39+
2440
#if os(Linux)
25-
PlatformPackageDependencies = [.package(url: "https://github.com/PADL/IORingSwift", from: "0.9.2")]
41+
PlatformPackageDependencies += [.package(url: "https://github.com/PADL/IORingSwift", from: "0.9.2")]
2642

27-
PlatformTargetDependencies = [
43+
PlatformTargetDependencies += [
2844
.target(
2945
name: "dnssd",
3046
condition: .when(platforms: [.linux])
3147
),
3248
.product(
3349
name: "IORing",
3450
package: "IORingSwift",
35-
condition: .when(platforms: [.linux])
51+
condition: .when(platforms: [.linux], traits: ["NonEmbeddedBuild"])
3652
),
3753
.product(
3854
name: "IORingUtils",
3955
package: "IORingSwift",
40-
condition: .when(platforms: [.linux])
56+
condition: .when(platforms: [.linux], traits: ["NonEmbeddedBuild"])
4157
),
4258
.product(
4359
name: "IORingFoundation",
4460
package: "IORingSwift",
45-
condition: .when(platforms: [.linux])
61+
condition: .when(platforms: [.linux], traits: ["NonEmbeddedBuild"])
4662
),
4763
]
4864

4965
PlatformProducts = []
5066
PlatformTargets = []
5167
SwiftLanguageVersionSetting = []
5268
#elseif os(macOS) || os(iOS)
53-
PlatformPackageDependencies = [
54-
.package(url: "https://github.com/swhitty/FlyingFox", from: "0.20.0"),
69+
PlatformPackageDependencies += [
5570
.package(
5671
url: "https://github.com/spacenation/swiftui-sliders",
5772
from: "2.1.0"
5873
),
5974
]
6075

61-
PlatformTargetDependencies = [
62-
.product(
63-
name: "FlyingFox",
64-
package: "FlyingFox",
65-
condition: .when(platforms: [.macOS, .iOS, .android])
66-
),
67-
.product(
68-
name: "FlyingSocks",
69-
package: "FlyingFox",
70-
condition: .when(platforms: [.macOS, .iOS, .android])
71-
),
72-
]
73-
7476
PlatformProducts = [
7577
.library(
7678
name: "SwiftOCAUI",
@@ -147,7 +149,11 @@ let CommonTargets: [Target] = [
147149
name: "SwiftOCA",
148150
dependencies: [
149151
"AsyncExtensions",
150-
"AnyCodable",
152+
.product(
153+
name: "AnyCodable",
154+
package: "AnyCodable",
155+
condition: .when(traits: ["NonEmbeddedBuild"])
156+
),
151157
"SocketAddress",
152158
.product(name: "AsyncAlgorithms", package: "swift-async-algorithms"),
153159
.product(name: "SystemPackage", package: "swift-system"),
@@ -233,6 +239,10 @@ let package = Package(
233239
.iOS(.v17),
234240
],
235241
products: CommonProducts + PlatformProducts,
242+
traits: [
243+
.default(enabledTraits: ["NonEmbeddedBuild"]),
244+
.init(name: "NonEmbeddedBuild", description: "Default build footprint"),
245+
],
236246
dependencies: CommonPackageDependencies + PlatformPackageDependencies,
237247
targets: CommonTargets + PlatformTargets
238248
)

Sources/SwiftOCA/OCC/ControlClasses/Root+JSON.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
// limitations under the License.
1515
//
1616

17+
#if NonEmbeddedBuild
1718
import AnyCodable
1819
#if canImport(FoundationEssentials)
1920
import FoundationEssentials
@@ -37,3 +38,4 @@ enum OcaJSONPropertyKeys: String {
3738
case type
3839
case members
3940
}
41+
#endif

Sources/SwiftOCA/OCC/ControlClasses/Root.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ open class OcaRoot: CustomStringConvertible, @unchecked Sendable, _OcaObjectKeyP
134134
}
135135
}
136136

137+
#if NonEmbeddedBuild
137138
open func getJsonValue(
138139
flags: OcaPropertyResolutionFlags = .defaultFlags
139140
) async -> [String: any Sendable] {
@@ -178,6 +179,7 @@ open class OcaRoot: CustomStringConvertible, @unchecked Sendable, _OcaObjectKeyP
178179
await getJsonValue(flags: .defaultFlags)
179180
}
180181
}
182+
#endif
181183

182184
public func propertyKeyPath(for propertyID: OcaPropertyID) async -> AnyKeyPath? {
183185
await OcaPropertyKeyPathCache.shared.lookupProperty(byID: propertyID, for: self)
@@ -335,13 +337,15 @@ public extension OcaRoot {
335337
value
336338
}
337339

340+
#if NonEmbeddedBuild
338341
func getJsonValue(
339342
_ object: OcaRoot,
340343
keyPath: AnyKeyPath,
341344
flags: OcaPropertyResolutionFlags = .defaultFlags
342345
) async throws -> [String: any Sendable] {
343346
try [keyPath.jsonKey: String(describing: value)]
344347
}
348+
#endif
345349

346350
@_spi(SwiftOCAPrivate)
347351
public func _setValue(_ object: OcaRoot, _ anyValue: Any) async throws {

Sources/SwiftOCA/OCC/ControlClasses/Workers/BlocksAndMatrices/Block.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,7 @@ Sendable {
507507
true
508508
}
509509

510+
#if NonEmbeddedBuild
510511
override open func getJsonValue(
511512
flags: OcaPropertyResolutionFlags = .defaultFlags
512513
) async -> [String: any Sendable] {
@@ -516,6 +517,7 @@ Sendable {
516517
.asyncMap { await $0.getJsonValue(flags: flags) }
517518
return jsonObject
518519
}
520+
#endif
519521
}
520522

521523
public extension OcaBlock {

Sources/SwiftOCA/OCC/ControlClasses/Workers/BlocksAndMatrices/Matrix.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ Sendable {
104104
true
105105
}
106106

107+
#if NonEmbeddedBuild
107108
override open func getJsonValue(
108109
flags: OcaPropertyResolutionFlags = .defaultFlags
109110
) async -> [String: any Sendable] {
@@ -113,6 +114,7 @@ Sendable {
113114
.reencodeAsValidJSONObject(membersJson)
114115
return jsonObject
115116
}
117+
#endif
116118
}
117119

118120
public extension OcaMatrix {

Sources/SwiftOCA/OCC/PropertyTypes/BoundedProperty.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ public struct OcaBoundedProperty<
191191
try await _storage._getValue(object, flags: flags)
192192
}
193193

194+
#if NonEmbeddedBuild
194195
public func getJsonValue(
195196
_ object: OcaRoot,
196197
keyPath: AnyKeyPath,
@@ -204,6 +205,7 @@ public struct OcaBoundedProperty<
204205
"\(jsonKey)": value.value,
205206
]
206207
}
208+
#endif
207209

208210
@_spi(SwiftOCAPrivate)
209211
public func _setValue(_ object: OcaRoot, _ anyValue: Any) async throws {

Sources/SwiftOCA/OCC/PropertyTypes/Property.swift

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Copyright (c) 2023 PADL Software Pty Ltd
2+
// Copyright (c) 2023-2026 PADL Software Pty Ltd
33
//
44
// Licensed under the Apache License, Version 2.0 (the License);
55
// you may not use this file except in compliance with the License.
@@ -16,7 +16,11 @@
1616

1717
import AsyncAlgorithms
1818
import AsyncExtensions
19+
#if NonEmbeddedBuild || !canImport(FoundationEssentials)
1920
import Foundation
21+
#else
22+
import FoundationEssentials
23+
#endif
2024

2125
public struct OcaPropertyResolutionFlags: OptionSet, Sendable {
2226
public typealias RawValue = UInt32
@@ -56,12 +60,14 @@ public protocol OcaPropertyRepresentable: CustomStringConvertible {
5660
func refresh(_ object: OcaRoot) async
5761
func subscribe(_ object: OcaRoot) async
5862

63+
#if NonEmbeddedBuild
5964
func getJsonValue(
6065
_ object: OcaRoot,
6166
keyPath: AnyKeyPath,
6267
flags: OcaPropertyResolutionFlags
6368
) async throws
6469
-> [String: any Sendable]
70+
#endif
6571
}
6672

6773
public extension OcaPropertyRepresentable {
@@ -432,14 +438,20 @@ public struct OcaProperty<Value: Codable & Sendable>: Codable, Sendable,
432438
}
433439
}
434440

441+
#if NonEmbeddedBuild
435442
public func getJsonValue(
436443
_ object: OcaRoot,
437444
keyPath: AnyKeyPath,
438445
flags: OcaPropertyResolutionFlags = .defaultFlags
439446
) async throws -> [String: any Sendable] {
440447
let value = try await _getValue(object, flags: flags)
441448
let jsonValue: any Sendable = if isNil(value) {
449+
#if canImport(Foundation) && !canImport(FoundationEssentials)
442450
NSNull()
451+
#else
452+
(any Sendable)?.none
453+
#endif
454+
443455
} else if JSONSerialization.isValidJSONObject(value) {
444456
value
445457
} else {
@@ -448,6 +460,7 @@ public struct OcaProperty<Value: Codable & Sendable>: Codable, Sendable,
448460

449461
return try [keyPath.jsonKey: jsonValue]
450462
}
463+
#endif
451464

452465
@_spi(SwiftOCAPrivate)
453466
public func _setValue(_ object: OcaRoot, _ anyValue: Any) async throws {
@@ -486,6 +499,7 @@ extension OcaProperty.PropertyValue: Hashable where Value: Hashable & Codable {
486499
}
487500
}
488501

502+
#if NonEmbeddedBuild
489503
extension AnyKeyPath {
490504
var jsonKey: String {
491505
get throws {
@@ -510,3 +524,4 @@ extension AnyKeyPath {
510524
}
511525
}
512526
}
527+
#endif

Sources/SwiftOCA/OCC/PropertyTypes/VectorProperty.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ public struct OcaVectorProperty<
165165
try await _storage._getValue(object, flags: flags)
166166
}
167167

168+
#if NonEmbeddedBuild
168169
public func getJsonValue(
169170
_ object: OcaRoot,
170171
keyPath: AnyKeyPath,
@@ -173,6 +174,7 @@ public struct OcaVectorProperty<
173174
let value = try await _getValue(object, flags: flags)
174175
return try [keyPath.jsonKey: [value.x, value.y]]
175176
}
177+
#endif
176178

177179
@_spi(SwiftOCAPrivate)
178180
public func _setValue(_ object: OcaRoot, _ anyValue: Any) async throws {

Sources/SwiftOCA/OCP.1/Backend/Ocp1CFSocketConnection.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// limitations under the License.
1515
//
1616

17-
#if canImport(CoreFoundation)
17+
#if canImport(CoreFoundation) && NonEmbeddedBuild
1818

1919
import AsyncAlgorithms
2020
import AsyncExtensions

0 commit comments

Comments
 (0)