-
-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathPackage.swift
More file actions
298 lines (287 loc) · 8.96 KB
/
Copy pathPackage.swift
File metadata and controls
298 lines (287 loc) · 8.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// swift-tools-version:6.2
import PackageDescription
import CompilerPluginSupport
// get environment variables
let dynamicLibrary = Context.environment["SWIFT_BUILD_DYNAMIC_LIBRARY"] == "1"
let generateCode = Context.environment["SWIFTPM_ENABLE_PLUGINS"] != "0"
let enableMacros = Context.environment["SWIFTPM_ENABLE_MACROS"] != "0"
let buildDocs = Context.environment["BUILDING_FOR_DOCUMENTATION_GENERATION"] == "1"
let embeddedFoundation = Context.environment["SWIFTPM_EMBEDDED_FOUNDATION"] == "1"
// The C ABI adds targets and a product, which traits cannot gate — they
// condition dependencies and build settings, not the target graph — so
// it stays an environment variable.
let buildCABI = Context.environment["SWIFTPM_BLUETOOTH_CABI"] == "1"
// force building as dynamic library
let libraryType: PackageDescription.Product.Library.LibraryType? = dynamicLibrary ? .dynamic : nil
var package = Package(
name: "Bluetooth",
platforms: [
.macOS(.v10_15),
.iOS(.v13),
.watchOS(.v6),
.tvOS(.v13)
],
products: [
.library(
name: "Bluetooth",
type: libraryType,
targets: ["Bluetooth"]
),
.library(
name: "BluetoothMetadata",
type: libraryType,
targets: ["BluetoothMetadata"]
),
.library(
name: "BluetoothGAP",
type: libraryType,
targets: ["BluetoothGAP"]
),
.library(
name: "BluetoothGATT",
type: libraryType,
targets: ["BluetoothGATT"]
),
.library(
name: "BluetoothHCI",
type: libraryType,
targets: ["BluetoothHCI"]
),
.library(
name: "BluetoothSDP",
type: libraryType,
targets: ["BluetoothSDP"]
)
],
traits: [
.trait(
name: "Metadata",
description: """
Bluetooth SIG assigned-numbers metadata: the `name` \
properties of `CompanyIdentifier`, `BluetoothUUID` and \
`UnitIdentifier`.
Opt-in because it pulls in Foundation and a resource \
bundle, which makes the package neither Embedded-clean \
nor buildable outside SwiftPM. The numeric definitions \
themselves (`CompanyIdentifier.apple` and friends) are \
generated at build time and remain available without it.
"""
),
.default(enabledTraits: [])
],
targets: [
.target(
name: "Bluetooth"
),
.target(
name: "BluetoothMetadata",
resources: [
.copy("Resources/CompanyIdentifier.json"),
.copy("Resources/CharacteristicUUID.json"),
.copy("Resources/DeclarationUUID.json"),
.copy("Resources/DescriptorUUID.json"),
.copy("Resources/ServiceUUID.json"),
.copy("Resources/MemberUUID.json"),
.copy("Resources/UnitIdentifier.json")
]
),
.target(
name: "BluetoothGAP",
dependencies: [
"Bluetooth"
]
),
.target(
name: "BluetoothGATT",
dependencies: [
"Bluetooth"
]
),
.target(
name: "BluetoothHCI",
dependencies: [
"Bluetooth",
"BluetoothGAP"
]
),
.target(
name: "BluetoothSDP",
dependencies: [
"Bluetooth"
]
),
.testTarget(
name: "BluetoothTests",
dependencies: [
"Bluetooth",
.target(
name: "BluetoothMetadata",
condition: .when(
platforms: [.macOS, .linux, .macCatalyst, .windows],
traits: ["Metadata"]
)
),
.target(
name: "BluetoothGAP",
condition: .when(platforms: [.macOS, .linux, .macCatalyst, .windows, .android])
),
.target(
name: "BluetoothGATT",
condition: .when(platforms: [.macOS, .linux, .macCatalyst, .windows, .android])
),
.target(
name: "BluetoothHCI",
condition: .when(platforms: [.macOS, .linux, .macCatalyst, .windows, .android])
),
.target(
name: "BluetoothSDP",
condition: .when(platforms: [.macOS, .linux, .macCatalyst, .windows, .android])
)
]
)
]
)
// Optional dependencies
//
// The metadata tables are reached only through `canImport`-guarded code
// in `Sources/Bluetooth`, so conditioning the dependency on the trait is
// enough to compile them out.
package.targets[0].dependencies += [
.target(
name: "BluetoothMetadata",
condition: .when(traits: ["Metadata"])
)
]
if embeddedFoundation {
package.dependencies += [
.package(
url: "https://github.com/PureSwift/swift-embedded-foundation.git",
from: "0.2.0"
)
]
let foundationEmbedded: PackageDescription.Target.Dependency = .product(
name: "FoundationEmbedded",
package: "swift-embedded-foundation"
)
for name in ["Bluetooth", "BluetoothGAP", "BluetoothGATT", "BluetoothHCI", "BluetoothSDP"] {
guard let index = package.targets.firstIndex(where: { $0.name == name }) else {
fatalError("Missing target \(name)")
}
package.targets[index].dependencies += [
foundationEmbedded
]
}
}
// SwiftPM plugins
if buildDocs {
package.dependencies += [
.package(
url: "https://github.com/swiftlang/swift-docc-plugin.git",
from: "1.0.0"
)
]
}
if generateCode {
for index in package.targets.indices {
package.targets[index].swiftSettings = [
.define("SWIFTPM_ENABLE_PLUGINS")
]
}
package.targets += [
.executableTarget(
name: "GenerateBluetooth",
dependencies: [
"BluetoothMetadata"
]
),
.plugin(
name: "GenerateBluetoothDefinitions",
capability: .buildTool(),
dependencies: [
"GenerateBluetooth"
]
)
]
// Name-based lookup, not a hardcoded index — the target array's
// order has changed before (silently detaching this plugin from
// BluetoothTests when a new target was inserted ahead of it) and
// will again.
for name in ["Bluetooth", "BluetoothTests"] {
guard let index = package.targets.firstIndex(where: { $0.name == name }) else {
fatalError("Missing target \(name)")
}
package.targets[index].plugins = [
"GenerateBluetoothDefinitions"
]
}
}
if enableMacros {
package.targets[0].swiftSettings = [
.define("SWIFTPM_ENABLE_MACROS")
]
package.dependencies += [
.package(
url: "https://github.com/swiftlang/swift-syntax.git",
"600.0.1"..<"604.0.0"
)
]
package.targets += [
.macro(
name: "BluetoothMacros",
dependencies: [
.product(
name: "SwiftSyntaxMacros",
package: "swift-syntax"
),
.product(
name: "SwiftCompilerPlugin",
package: "swift-syntax"
)
]
)
]
package.targets[0].dependencies += [
"BluetoothMacros"
]
}
// C ABI (libbluetooth replacement)
//
// Off by default so the ordinary Swift build stays dependency-free and
// Embedded-clean. Enable with `SWIFTPM_BLUETOOTH_CABI=1` to build the
// `bluetooth.h` C surface (`CBluetooth`, vendored BlueZ headers + shims)
// and its Swift implementations (`BluetoothABI`, which also carries the
// 75 pure `sdp_*` symbols — they ship in the same shared object as the
// rest of the C ABI, so there's no separate BluetoothSDP target here).
if buildCABI {
package.products += [
.library(
name: "BluetoothABI",
type: .dynamic,
targets: ["BluetoothABI"]
)
]
package.targets += [
.target(
name: "CBluetooth",
exclude: [
"README.md",
"include/bluetooth/LICENSE"
]
),
.target(
name: "BluetoothABI",
dependencies: [
"Bluetooth",
"CBluetooth"
]
),
.testTarget(
name: "BluetoothABITests",
dependencies: [
"BluetoothABI",
"CBluetooth",
"Bluetooth"
]
)
]
}