forked from smumriak/AppKid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Package.swift
executable file
·653 lines (586 loc) · 20.1 KB
/
Package.swift
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
// swift-tools-version: 5.8
//
// Package.swift
// AppKidDemo
//
// Created by Serhii Mumriak on 29.01.2020.
//
import PackageDescription
import Foundation
// MARK: Vulkan pre-build parsing
enum Vulkan {
struct ValidUsage: Codable {
let versionInfo: VersionInfo
public enum CodingKeys: String, CodingKey {
case versionInfo = "version info"
}
}
struct VersionInfo: Codable {
public let apiVersion: String
public enum CodingKeys: String, CodingKey {
case apiVersion = "api version"
}
}
#if os(Linux)
static let possibleRegistryLocations = [
"/usr/share/vulkan/registry",
]
#elseif os(macOS)
static let possibleRegistryLocations = [
"/usr/local/share/vulkan/registry",
]
#elseif os(Android)
#error("FIX ME")
static let possibleRegistryLocations = []
#elseif os(Windows)
#error("FIX ME")
static let possibleRegistryLocations = []
#else
#error("Platform not supported")
static let possibleRegistryLocations = []
#endif
static let registryPath: String? = {
let fileManager = FileManager.default
var isDirectory: ObjCBool = false
for path in possibleRegistryLocations {
if fileManager.fileExists(atPath: path, isDirectory: &isDirectory) && isDirectory.boolValue == true {
return path
}
}
print("Can not find vulkan registry in known locations. Building anything related to Vulkan will fail. Locations checked: \(possibleRegistryLocations)")
return nil
}()
static let version: String? = {
guard let registryPath else {
return nil
}
let validUsageURL = URL(fileURLWithPath: registryPath, isDirectory: true).appendingPathComponent("validusage.json")
do {
let validUsage = try JSONDecoder().decode(Vulkan.ValidUsage.self, from: Data(contentsOf: validUsageURL))
return validUsage.versionInfo.apiVersion
} catch {
print("Vulkan valid usage parsing failed. You need to have valid vulkan SDK installed before the build. Error \(error). Building anything related to Vulkan will fail.")
return nil
}
}()
}
// MARK: Package
let package = Package(
name: "AppKid",
platforms: [
.macOS(.v13),
],
products: [
.executable(name: "AppKidDemo", targets: ["AppKidDemo"]),
.library(.appKid),
.library(.cCairo),
.library(.cPango),
.library(.cairoGraphics),
.library("STBImage", [.stbImageRead, .stbImageWrite, .stbImageResize], type: .static),
.library(.stbImageRead, type: .static),
.library(.stbImageWrite, type: .static),
.library(.stbImageResize, type: .static),
.library(.contentAnimation),
.library(.simpleGLM),
.library(.cXlib),
.library(.swiftXlib),
// .library(.cGLib),
// .library(.swiftyGLib, type: .dynamic),
.library(.linuxSys, type: .static),
.library(.tinyFoundation),
.library(.cVulkan, type: .static),
.library(.volcano),
.library(.vulkanMemoryAllocatorAdapted, type: .static),
] + {
guard Vulkan.version != nil else { return [] }
return [
.tool(.vkthings),
.plugin(.vkThingsBuildToolPlugin),
.plugin(.vkThingsCommandPlugin),
.tool(.volcanoSL),
.plugin(.volcanoSLPlugin),
]
}(),
dependencies: [
.package(url: "https://github.com/apple/swift-collections", .upToNextMinor(from: "1.0.0")),
.package(url: "https://github.com/recp/cglm", branch: "master"),
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.2.0"),
.package(url: "https://github.com/CoreOffice/XMLCoder.git", from: "0.13.1"),
.package(url: "https://github.com/apple/swift-tools-support-core", branch: "main"),
.package(url: "https://github.com/apple/swift-atomics.git", .upToNextMajor(from: "1.0.0")),
.package(url: "https://github.com/smumriak/SemanticVersion.git", branch: "main"),
.package(url: "https://github.com/jpsim/Yams.git", from: "5.0.6"),
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-system", from: "1.0.0"),
],
targets: [
.executableTarget(
name: "AppKidDemo",
dependencies: [
.appKid,
.cairoGraphics,
.contentAnimation,
.tinyFoundation,
.volcano,
],
resources: [
.copy("Resources/fan.png"),
]
),
.testTarget(
name: "AppKidDemoTests",
dependencies: ["AppKidDemo"]
),
.appKid,
.cCairo,
.cPango,
.cairoGraphics,
.stbImageRead,
.stbImageWrite,
.stbImageResize,
.cairoGraphicsTests,
.contentAnimation,
.layerRenderingData,
.contentAnimationTests,
.simpleGLM,
.simpleGLMTests,
.cXlib,
.swiftXlib,
.swiftXlibTests,
// .cGLib,
// .swiftyGLib,
// .swiftyGLibTests,
.linuxSys,
.tinyFoundation,
.hijackingHacks,
.spoon,
.tinyFoundationTests,
.cVulkan,
.cClang,
.volcano,
.vulkanMemoryAllocatorAdapted,
] + {
guard Vulkan.version != nil else { return [] }
return [
.vkthings,
.vkthingsLib,
.vkThingsBuildToolPlugin,
.vkThingsCommandPlugin,
.volcanoSL,
.volcanoSLPlugin,
]
}()
)
extension Product {
static func library(_ target: Target, type: Library.LibraryType? = nil) -> Product {
library(target.name, [target], type: type)
}
static func library(_ name: String, _ targets: [Target], type: Library.LibraryType? = nil) -> Product {
library(name: name, type: type, targets: targets.map { $0.name })
}
static func tool(_ target: Target) -> Product {
executable(name: target.name.lowercased(), targets: [target.name])
}
static func plugin(_ target: Target) -> Product {
plugin(name: target.name, targets: [target.name])
}
static func plugin(_ name: String, _ targets: [Target]) -> Product {
plugin(name: name, targets: targets.map { $0.name })
}
}
extension Target.Dependency {
static let appKid = Target.appKid.asDependency()
static let cCairo = Target.cCairo.asDependency()
static let cPango = Target.cPango.asDependency()
static let cairoGraphics = Target.cairoGraphics.asDependency()
static let stbImageRead = Target.stbImageRead.asDependency()
static let stbImageWrite = Target.stbImageWrite.asDependency()
static let stbImageResize = Target.stbImageResize.asDependency()
static let cairoGraphicsTests = Target.cairoGraphicsTests.asDependency()
static let contentAnimation = Target.contentAnimation.asDependency()
static let layerRenderingData = Target.layerRenderingData.asDependency()
static let contentAnimationTests = Target.contentAnimationTests.asDependency()
static let simpleGLM = Target.simpleGLM.asDependency()
static let simpleGLMTests = Target.simpleGLMTests.asDependency()
static let cXlib = Target.cXlib.asDependency()
static let swiftXlib = Target.swiftXlib.asDependency()
static let swiftXlibTests = Target.swiftXlibTests.asDependency()
static let cGLib = Target.cGLib.asDependency()
static let swiftyGLib = Target.swiftyGLib.asDependency()
static let swiftyGLibTests = Target.swiftyGLibTests.asDependency()
static let linuxSys = Target.linuxSys.asDependency()
static let tinyFoundation = Target.tinyFoundation.asDependency()
static let hijackingHacks = Target.hijackingHacks.asDependency()
static let spoon = Target.spoon.asDependency()
static let tinyFoundationTests = Target.tinyFoundationTests.asDependency()
static let cVulkan = Target.cVulkan.asDependency()
static let cClang = Target.cClang.asDependency()
static let volcano = Target.volcano.asDependency()
static let vulkanMemoryAllocatorAdapted = Target.vulkanMemoryAllocatorAdapted.asDependency()
static let vkthings = Target.vkthings.asDependency()
static let vkthingsLib = Target.vkthingsLib.asDependency()
static let vkThingsBuildToolPlugin = Target.vkThingsBuildToolPlugin.asDependency()
static let vkThingsCommandPlugin = Target.vkThingsCommandPlugin.asDependency()
static let volcanoSL = Target.volcanoSL.asDependency()
static let volcanoSLPlugin = Target.volcanoSLPlugin.asDependency()
}
extension Target.PluginUsage {
static func plugin(_ target: Target) -> Self {
plugin(name: target.name)
}
static let volcanoSL: Self? = Vulkan.version != nil ? .plugin(.volcanoSLPlugin) : nil
}
extension Array where Element == Target.PluginUsage {
static let volcanoSL: Self = [] + Element.volcanoSL
}
extension SwiftSetting {
static let emitModule: Self = .unsafeFlags(["-emit-module"])
static let vulkanVersion: Self? = {
if let vulkanVersion = Vulkan.version {
return .define("VULKAN_VERSION_\(vulkanVersion.replacingOccurrences(of: ".", with: "_"))")
} else {
return nil
}
}()
}
extension Array where Element == SwiftSetting {
static let emitModule: Self = [.emitModule]
static let vulkanPlatform: Self = [
.define("VOLCANO_PLATFORM_LINUX", .when(platforms: [.linux])),
.define("VOLCANO_PLATFORM_MACOS", .when(platforms: [.macOS])),
.define("VOLCANO_PLATFORM_IOS", .when(platforms: [.iOS])),
.define("VOLCANO_PLATFORM_APPLE_METAL", .when(platforms: [.iOS, .macOS])),
.define("VOLCANO_PLATFORM_WINDOWS", .when(platforms: [.windows])),
.define("VOLCANO_PLATFORM_ANDROID", .when(platforms: [.android])),
]
}
extension Target {
func asDependency(condition: TargetDependencyCondition? = nil) -> Dependency {
return .targetItem(name: name, condition: condition)
}
func asLibraryProduct(type: Product.Library.LibraryType? = nil) -> Product {
.library(name: name, type: type, targets: [name])
}
}
extension Target {
static let appKid: Target = target(
name: "AppKid",
dependencies: [
.cairoGraphics,
.contentAnimation,
.tinyFoundation,
.volcano,
.simpleGLM,
.cXlib,
.swiftXlib,
],
path: "AppKid/Sources/AppKid",
swiftSettings: .emitModule
)
}
extension Target {
static let cCairo: Target = systemLibrary(
name: "CCairo",
path: "CairoGraphics/Sources/CCairo",
pkgConfig: "cairo gobject-2.0",
providers: [
.apt(["libcairo2-dev"]),
.brew(["cairo glib"]),
]
)
static let cPango: Target = systemLibrary(
name: "CPango",
path: "CairoGraphics/Sources/CPango",
pkgConfig: "pango gobject-2.0",
providers: [
.apt(["libpango1.0-dev"]),
.brew(["pango glib"]),
]
)
static let cairoGraphics: Target = target(
name: "CairoGraphics",
dependencies: [
.cCairo,
.cPango,
.tinyFoundation,
.simpleGLM,
.stbImageRead,
.stbImageWrite,
.stbImageResize,
],
path: "CairoGraphics/Sources/CairoGraphics",
swiftSettings: .emitModule
)
static let stbImageRead: Target = target(
name: "STBImageRead",
path: "CairoGraphics/SwiftSTB/Sources/STBImageRead")
static let stbImageWrite: Target = target(
name: "STBImageWrite",
path: "CairoGraphics/SwiftSTB/Sources/STBImageWrite")
static let stbImageResize: Target = target(
name: "STBImageResize",
path: "CairoGraphics/SwiftSTB/Sources/STBImageResize")
static let cairoGraphicsTests: Target = testTarget(
name: "CairoGraphicsTests",
dependencies: [.cairoGraphics],
path: "CairoGraphics/Tests/CairoGraphicsTests"
)
}
extension Target {
static let contentAnimation: Target = target(
name: "ContentAnimation",
dependencies: [
.cairoGraphics,
.tinyFoundation,
.volcano,
.simpleGLM,
.layerRenderingData,
.product(name: "DequeModule", package: "swift-collections"),
.product(name: "OrderedCollections", package: "swift-collections"),
],
path: "ContentAnimation/Sources/ContentAnimation",
// exclude: [
// "Resources/Shaders",
// ],
resources: [
.process("Resources/Shaders"),
],
swiftSettings: .emitModule,
plugins: .volcanoSL
)
static let layerRenderingData: Target = target(
name: "LayerRenderingData",
dependencies: [
.simpleGLM,
],
path: "ContentAnimation/Sources/LayerRenderingData"
)
static let contentAnimationTests: Target = testTarget(
name: "ContentAnimationTests",
dependencies: [.contentAnimation],
path: "ContentAnimation/Tests/ContentAnimationTests"
)
}
extension Target {
static let simpleGLM: Target = target(
name: "SimpleGLM",
dependencies: [
.product(name: "cglm", package: "cglm"),
],
path: "SimpleGLM/Sources/SimpleGLM",
swiftSettings: .emitModule
)
static let simpleGLMTests: Target = testTarget(
name: "SimpleGLMTests",
dependencies: [
.simpleGLM,
],
path: "SimpleGLM/Tests/SimpleGLMTests"
)
}
extension Target {
static let cXlib: Target = systemLibrary(
name: "CXlib",
path: "SwiftXlib/Sources/CXlib",
pkgConfig: "x11 xext xi xcb",
providers: [
.apt(["libx11-dev libxext-dev libxi-dev libxcb1-dev"]),
.brew(["xquartz"]),
]
)
static let swiftXlib: Target = target(
name: "SwiftXlib",
dependencies: [
.cXlib,
.tinyFoundation,
],
path: "SwiftXlib/Sources/SwiftXlib",
swiftSettings: .emitModule
)
static let swiftXlibTests: Target = testTarget(
name: "SwiftXlibTests",
dependencies: ["SwiftXlib"],
path: "SwiftXlib/Tests/SwiftXlibTests"
)
}
extension Target {
static let cGLib: Target = systemLibrary(
name: "CGLib",
path: "SwiftyGLib/Sources/CGLib",
pkgConfig: "glib-2.0",
providers: [
.apt(["libglib2.0-dev"]),
.brew(["glib"]),
]
)
static let swiftyGLib: Target = target(
name: "SwiftyGLib",
dependencies: [
.tinyFoundation,
.cGLib,
],
path: "SwiftyGLib/Sources/SwiftyGLib",
swiftSettings: .emitModule
)
static let swiftyGLibTests: Target = testTarget(
name: "SwiftyGLibTests",
dependencies: [.swiftyGLib],
path: "SwiftyGLib/Tests/SwiftyGLibTests"
)
}
extension Target {
static let linuxSys: Target = target(
name: "LinuxSys",
path: "Sys/Sources/LinuxSys"
)
}
extension Target {
static let tinyFoundation: Target = target(
name: "TinyFoundation",
dependencies: [
.product(name: "DequeModule", package: "swift-collections"),
.product(name: "Atomics", package: "swift-atomics"),
.product(name: "SystemPackage", package: "swift-system"),
.linuxSys,
.hijackingHacks,
.spoon,
],
path: "TinyFoundation/Sources/TinyFoundation"
)
static let hijackingHacks: Target = target(
name: "HijackingHacks",
path: "TinyFoundation/Sources/HijackingHacks",
cSettings: [
.unsafeFlags([
"-I/opt/swift/usr/lib/swift",
]),
]
)
static let spoon: Target = target(
name: "Spoon",
path: "TinyFoundation/Sources/Spoon"
)
static let tinyFoundationTests: Target = testTarget(
name: "TinyFoundationTests",
dependencies: [.tinyFoundation],
path: "TinyFoundation/Tests/TinyFoundationTests"
)
}
extension Target {
static let cVulkan: Target = target(
name: "CVulkan",
path: "Volcano/Sources/CVulkan",
linkerSettings: [
.linkedLibrary("vulkan"),
]
)
static let cClang: Target = systemLibrary(
name: "CClang",
path: "Volcano/Sources/CClang",
pkgConfig: "libclang-14",
providers: [
.apt(["libclang-14"]),
]
)
static let volcano: Target = target(
name: "Volcano",
dependencies: [
.product(name: "Atomics", package: "swift-atomics"),
.cVulkan,
.cXlib,
.tinyFoundation,
.simpleGLM,
.vulkanMemoryAllocatorAdapted,
],
path: "Volcano/Sources/Volcano",
swiftSettings: .emitModule
+ .vulkanPlatform
+ .vulkanVersion,
plugins: [
// SourceKit-lsp does not support generated source code. this means no autocompletion, which is WORSE than manual codegen trigger
// .plugin(.vkThingsBuildToolPlugin),
]
)
static let vulkanMemoryAllocatorAdapted: Target = target(
name: "VulkanMemoryAllocatorAdapted",
dependencies: [
.cVulkan,
],
path: "Volcano/Sources/VulkanMemoryAllocatorAdapted",
cSettings: [
.unsafeFlags(["-Wno-nullability-completeness"]),
],
cxxSettings: [
.unsafeFlags(["-Wno-nullability-completeness", "-std=c++17"]),
]
)
static let vkthingsLib: Target = target(
name: "VkThingsLib",
dependencies: [
.product(name: "XMLCoder", package: "XMLCoder"),
.product(name: "SemanticVersion", package: "SemanticVersion"),
.product(name: "Yams", package: "Yams"),
.tinyFoundation,
],
path: "Volcano/Sources/VkThingsLib"
)
static let vkthings: Target = executableTarget(
name: "vkthings",
dependencies: [
.product(name: "ArgumentParser", package: "swift-argument-parser"),
.product(name: "XMLCoder", package: "XMLCoder"),
.product(name: "SemanticVersion", package: "SemanticVersion"),
.tinyFoundation,
.vkthingsLib,
],
path: "Volcano/Sources/vkthings"
)
static let vkThingsBuildToolPlugin: Target = plugin(
name: "VkThingsBuildToolPlugin",
capability: .buildTool(),
dependencies: [
.vkthings,
],
path: "Volcano/Plugins/VkThingsBuildToolPlugin"
)
static let vkThingsCommandPlugin: Target = plugin(
// VK is not a typo here! for unknown reasons swift package manager refuses to consume this name if K is lowerkased in Vk
name: "VKThingsCommandPlugin",
capability: .command(
intent: .custom(verb: "vkthings", description: "VkThings codegen"),
permissions: [.writeToPackageDirectory(reason: "Generating code")]
),
dependencies: [
.vkthings,
],
path: "Volcano/Plugins/VkThingsCommandPlugin"
)
static let volcanoSL: Target = executableTarget(
name: "volcanosl",
dependencies: [
.cClang,
.tinyFoundation,
.product(name: "ArgumentParser", package: "swift-argument-parser"),
.product(name: "TSCBasic", package: "swift-tools-support-core"),
],
path: "Volcano/Sources/VolcanoSL",
resources: [
.copy("Resources/GLSLTypesInclude.h"),
]
)
static let volcanoSLPlugin: Target = plugin(
name: "VolcanoSLPlugin",
capability: .buildTool(),
dependencies: [.volcanoSL],
path: "Volcano/Plugins/VolcanoSLPlugin"
)
}
// MARK: Utility
extension Array {
static func + (lhs: Self, rhs: Element?) -> Self {
if let rhs {
return lhs + [rhs]
}
return lhs
}
}