-
Notifications
You must be signed in to change notification settings - Fork 1
/
Package.swift
212 lines (195 loc) · 6.12 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
// swift-tools-version:6.0
//
// "couldn't find pc file for steamworks-swift" ?
// See https://github.com/johnfairh/steamworks-swift#how-to-use-this-project
//
import PackageDescription
//
// We need the steam headers to build Steamworks and the steam libraries
// to both link and run programs that use it.
//
// SPM doesn't support binary library targets. Boo.
//
// Offer two ways of working around this:
//
// 1) Install the SDK to /usr/local (or some prefix) so the files are
// all found via a pkg-config file.
//
// This is very smooth but means you've done a system installation of
// stuff that can vary per-steam client app, not ideal.
//
// 2) a) Use "unsafe" flags to point the build at the SDK files inside the
// package, AND
//
// b) ...require the end developer to supply the unsafe library flags to
// their build AND
//
// [This should be avoidable by making Steamworks a dynamic library,
// which works fine in SPM but makes Xcode produce one of its classic
// batshit error messages]
//
// c) ...require the end developer to copy the steam library into their
// binary's directory for runtime
//
// This avoids the system installation but is tough on developers.
//
// Way (1) is easier in general so document that as the norm. It also removes
// the 'unsafe' flags so that SemVer is achievable.
//
// Way (2) is supported here and used if pkg-config is not available. One
// concrete thing this allows is a `swift build` of *this* project without any
// other system setup.
//
// To further complicate things, while SPM executes this file in the user's
// environment, Xcode thinks it knows what $PATH ought to be. Try to spot this
// so that Xcode in SPM mode can depend on Steamworks.
var steamworksSwiftFlags: [SwiftSetting] = [
.interoperabilityMode(.Cxx),
.enableExperimentalFeature("AccessLevelOnImport"),
.enableExperimentalFeature("StrictConcurrency")
]
let pkgConfigSetting = "steamworks-swift"
// Look away now...
var clientLinkerSettings: [LinkerSetting] = []
import Foundation
let pkgConfigPath: String
let pathArgs: [String]
let envPath = Context.environment["PATH"] ?? ""
if envPath.contains("/usr/local/bin") {
pathArgs = []
} else {
// Xcode's broken PATH or no standard pkgconfig; make a guess
pathArgs = ["-P", "\(envPath):/usr/local/bin"]
}
let hasPkgConfig =
(try? Process.run(URL(fileURLWithPath: "/usr/bin/env"),
arguments: pathArgs + ["pkg-config", "steamworks-swift"])).map { p in
p.waitUntilExit()
return p.terminationStatus == 0
} ?? false
if !hasPkgConfig {
let curdir = Context.packageDirectory
steamworksSwiftFlags += [
.unsafeFlags(["-I\(curdir)/sdk/redist/include"])
]
let linkBase = "-L\(curdir)/sdk/redist/lib/"
let platforms: [(String, Platform)] = [
("osx", .macOS),
("linux64", .linux),
("win64", .windows)
]
clientLinkerSettings += platforms.map {
.unsafeFlags([linkBase + $0.0], .when(platforms: [$0.1]))
}
}
// ...safe to look
let package = Package(
name: "steamworks-swift",
platforms: [
.macOS("14.0"),
],
products: [
.library(
name: "Steamworks",
targets: ["Steamworks"]),
.library(
name: "SteamworksEncryptedAppTicket",
targets: ["SteamworksEncryptedAppTicket"]),
.library(
name: "SteamworksHelpers",
targets: ["SteamworksHelpers"]),
.executable(
name: "Client",
targets: ["Client"]),
.executable(
name: "TicketClient",
targets: ["TicketClient"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-log.git", from: "1.4.2"),
.package(url: "https://github.com/jpsim/Yams.git", from: "5.0.5"),
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.3.0"),
.package(url: "https://github.com/apple/swift-atomics.git", branch: "main"),
],
targets: [
.systemLibrary(
name: "CSteamworks",
pkgConfig: pkgConfigSetting
),
.systemLibrary(
name: "CSteamworksEncryptedAppTicket",
pkgConfig: pkgConfigSetting
),
.target(
name: "Steamworks",
dependencies: [
"CSteamworks",
.product(name: "Logging", package: "swift-log")
],
swiftSettings: steamworksSwiftFlags
),
.target(
name: "SteamworksEncryptedAppTicket",
dependencies: [
"Steamworks",
"CSteamworksEncryptedAppTicket"
],
swiftSettings: steamworksSwiftFlags
),
.target(
name: "SteamworksHelpers",
dependencies: [
"Steamworks"
],
swiftSettings: [.interoperabilityMode(.Cxx)] // lies lies lies
),
.target(
name: "SteamworksConcurrency",
dependencies: [
"Steamworks",
.product(name: "Atomics", package: "swift-atomics",
condition: .when(platforms: [.macOS]))
],
swiftSettings: [.interoperabilityMode(.Cxx)] // lies lies lies
),
.executableTarget(
name: "Client",
dependencies: ["Steamworks", "SteamworksHelpers"],
swiftSettings: [.interoperabilityMode(.Cxx)], // lies lies lies
linkerSettings: clientLinkerSettings
),
.executableTarget(
name: "TicketClient",
dependencies: ["SteamworksEncryptedAppTicket", "SteamworksHelpers"],
swiftSettings: [.interoperabilityMode(.Cxx)], // lies lies lies
linkerSettings: clientLinkerSettings
),
.executableTarget(
name: "Generate",
dependencies: ["LibGenerate"]
),
.target(
name: "LibGenerate",
dependencies: ["Yams"],
resources: [
.copy("Resources/steam_api_patch.yaml"),
.copy("Resources/steam_api_extra.json"),
.copy("Resources/EXPECTED_SDK")
]
),
.testTarget(
name: "SteamworksTests",
dependencies: [
.target(name: "Steamworks"),
.target(name: "SteamworksHelpers"),
.target(name: "SteamworksConcurrency"),
.target(name: "LibGenerate",
condition: .when(platforms: [.macOS])),
],
exclude: ["Fixtures"],
swiftSettings: [.interoperabilityMode(.Cxx)], // lies lies lies
linkerSettings: clientLinkerSettings
)
],
cxxLanguageStandard: .cxx11
)