-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Package.swift
230 lines (205 loc) · 7.33 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
// swift-tools-version:5.7
import PackageDescription
enum Env: String, CaseIterable {
/// Make SDL platform available.
case FRB_ENABLE_PLATFORM_SDL
/// Make Apple platforms available
case FRB_ENABLE_PLATFORM_APPL
/// Make Metal graphics API available.
case FRB_ENABLE_GRAPHICS_METAL
/// Make Vulkan graphics API available.
case FRB_ENABLE_GRAPHICS_VULKAN
/// Make OpenGL graphics API available.
case FRB_ENABLE_GRAPHICS_OPENGL
}
/// Defines for available platform abstractions
let platformDefines: [SwiftSetting] = {
var settings: [SwiftSetting] = []
// FRB_PLATFORM_SDL
if Env.get(bool: .FRB_ENABLE_PLATFORM_SDL, orElse: true) {
settings.append(.define("FRB_PLATFORM_SDL"))
}
// FRB_PLATFORM_APPL
let isApplePlatform: Bool
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
isApplePlatform = false /* disabled for now */
#else
isApplePlatform = false
#endif
if Env.get(bool: .FRB_ENABLE_PLATFORM_APPL, orElse: isApplePlatform) {
settings.append(.define("FRB_PLATFORM_APPL"))
#if os(macOS)
settings.append(.define("FRB_PLATFORM_APPL_MACOS"))
#elseif os(iOS) || os(tvOS)
settings.append(.define("FRB_PLATFORM_APPL_IOS"))
#endif
}
return settings
}()
/// Defines for available rendering APIs
let renderingAPIDefines: [SwiftSetting] = {
var swiftSettings: [SwiftSetting] = []
// FRB_GRAPHICS_METAL
let isMetalDefaultEnabled: Bool
#if canImport(Metal)
isMetalDefaultEnabled = true
#else
isMetalDefaultEnabled = false
#endif
if Env.get(bool: .FRB_ENABLE_GRAPHICS_METAL, orElse: isMetalDefaultEnabled) {
swiftSettings.append(.define("FRB_GRAPHICS_METAL"))
}
// FRB_GRAPHICS_VULKAN
let isVulkanDefaultEnabled: Bool
#if os(Linux) || os(macOS)
isVulkanDefaultEnabled = true
#else
isVulkanDefaultEnabled = false
#endif
if Env.get(bool: .FRB_ENABLE_GRAPHICS_VULKAN, orElse: isVulkanDefaultEnabled) {
swiftSettings.append(.define("FRB_GRAPHICS_VULKAN"))
}
// FRB_GRAPHICS_OPENGL
let isOpenGLDefaultEnabled = true
if Env.get(bool: .FRB_ENABLE_GRAPHICS_OPENGL, orElse: isOpenGLDefaultEnabled) {
swiftSettings.append(.define("FRB_GRAPHICS_OPENGL"))
}
return swiftSettings
}()
let swiftSettings: [SwiftSetting] = renderingAPIDefines + platformDefines
let package = Package(
name: "FirebladePAL",
platforms: [
.macOS(.v11),
.iOS(.v13),
.tvOS(.v13),
],
products: [
.library(
name: "FirebladePAL",
type: .static,
targets: ["FirebladePAL"]
),
],
dependencies: [
.package(url: "https://github.com/fireblade-engine/math.git", from: "0.13.0"),
.package(url: "https://github.com/fireblade-engine/time.git", from: "0.2.0"),
.package(url: "https://github.com/ctreffs/SwiftSDL2.git", from: "1.4.1"),
.package(url: "https://github.com/ctreffs/SwiftVulkan", from: "0.2.1"),
.package(url: "https://github.com/ctreffs/SwiftNFD.git", from: "1.0.2"),
],
targets: [
.target(
name: "FirebladePAL",
dependencies: [
.product(name: "FirebladeMath", package: "math"),
.product(name: "FirebladeTime", package: "time"),
.product(name: "SDL", package: "SwiftSDL2"),
.product(name: "Vulkan", package: "SwiftVulkan"),
.product(name: "NFD", package: "SwiftNFD"),
],
swiftSettings: swiftSettings
),
.executableTarget(
name: "CPUBackendDemoApp",
dependencies: ["FirebladePAL"],
path: "Demos/CPUBackendDemoApp"
),
.executableTarget(
name: "VulkanBackendDemoApp",
dependencies: ["FirebladePAL", .product(name: "Vulkan", package: "SwiftVulkan")],
path: "Demos/VulkanBackendDemoApp",
swiftSettings: swiftSettings
),
.executableTarget(
name: "MetalBackendDemoApp",
dependencies: ["FirebladePAL"],
path: "Demos/MetalBackendDemoApp",
swiftSettings: swiftSettings
),
.testTarget(name: "FirebladePALTests",
dependencies: ["FirebladePAL"]),
]
)
import Foundation
extension Env {
/// Set environment variable.
/// - Parameters:
/// - varName: Env variable name
/// - value: Value to set variable to
/// - override: Override existing variable, true/false.
/// - Returns: True if set, false otherwise.
@discardableResult
static func set(variable varName: Env, to value: some CustomStringConvertible, override: Bool = true) -> Bool {
setenv(varName.rawValue, value.description, override ? 1 : 0) == 0
}
/// Get environment variable as string if present.
/// - Parameter varName: Env variable name
/// - Returns: The value if present.
static func get(string varName: Env) -> String? {
guard let cEnvVarPtr = getenv(varName.rawValue) else {
return nil
}
return String(cString: cEnvVarPtr, encoding: .utf8)
}
/// Get environment variable as string or return default value.
/// - Parameters:
/// - varName: Env variable name
/// - defaultValue: Default value to be returned if variable is not set.
/// - Returns: The value or defaultValue if not present.
static func get(string varName: Env, orElse defaultValue: String) -> String {
get(string: varName) ?? defaultValue
}
/// Get environment variable as boolean if present.
/// - Parameter varName: Env variable name
/// - Returns: true/false or nil if not present
static func get(bool varName: Env) -> Bool? {
Bool(string: get(string: varName))
}
/// Get environment variable as boolean or return default value.
/// - Parameters:
/// - varName: Env variable name
/// - defaultValue: Default value to be returned if variable is not set.
/// - Returns: Boolean value.
static func get(bool varName: Env, orElse defaultValue: Bool) -> Bool {
Bool(string: get(string: varName)) ?? defaultValue
}
/// Clear environment variable
/// - Parameter varName: Env variable name
/// - Returns: True if cleared; false otherwise
@discardableResult
static func clear(variable varName: Env) -> Bool {
unsetenv(varName.rawValue) == 0
}
/// Convenience to dump the current state of set environment variables.
static func dump() {
let output = Env
.allCases
.compactMap {
guard let value = get(string: $0) else {
return nil
}
return "\($0.rawValue)=\(value)"
}
.sorted()
.joined(separator: "\n")
Swift.dump(output)
}
}
public extension Bool {
/// Initialize boolean from string representations.
/// - Parameter value: "true" or "1" result in `true`; "false" or "0" result in `false`.
/// All other values return `nil`.
init?(string value: String?) {
switch value?.lowercased() {
case "true",
"1":
self = true
case "false",
"0":
self = false
default:
return nil
}
}
}