-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPackage.swift
258 lines (225 loc) · 8.88 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
// swift-tools-version: 6.0
// The swift-tools-version declares the minimum version of Swift required to build this package.
import Foundation
import PackageDescription
func envEnable(_ key: String, default defaultValue: Bool = false) -> Bool {
guard let value = Context.environment[key] else {
return defaultValue
}
if value == "1" {
return true
} else if value == "0" {
return false
} else {
return defaultValue
}
}
// MARK: - Env and Config
let isXcodeEnv = Context.environment["__CFBundleIdentifier"] == "com.apple.dt.Xcode"
let development = envEnable("OPENGRAPH_DEVELOPMENT", default: false)
let swiftBinPath = Context.environment["_"] ?? "/usr/bin/swift"
let swiftBinURL = URL(fileURLWithPath: swiftBinPath)
let SDKPath = swiftBinURL.deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent().path
let includePath = SDKPath.appending("/usr/lib/swift")
var sharedCSettings: [CSetting] = [
.unsafeFlags(["-I", includePath], .when(platforms: .nonDarwinPlatforms)),
]
var sharedSwiftSettings: [SwiftSetting] = [
.enableUpcomingFeature("InternalImportsByDefault"),
.swiftLanguageMode(.v5),
]
// MARK: [env] OPENGRAPH_SWIFT_TOOLCHAIN_PATH
// Modified from: https://github.com/swiftlang/swift/blob/main/SwiftCompilerSources/Package.swift
//
// Create a couple of symlinks to an existing Ninja build:
//
// ```shell
// cd $OPENGRAPH_SWIFT_TOOLCHAIN_PATH
// mkdir -p build/Default
// ln -s build/<Ninja-Build>/llvm-<os+arch> build/Default/llvm
// ln -s build/<Ninja-Build>/swift-<os+arch> build/Default/swift
// ```
//
// where <$OPENGRAPH_SWIFT_TOOLCHAIN_PATH> is the parent directory of the swift repository.
let swiftToolchainPath = Context.environment["OPENGRAPH_SWIFT_TOOLCHAIN_PATH"] ?? (development ? "/Volumes/BuildMachine/swift-project" : "")
if !swiftToolchainPath.isEmpty {
sharedCSettings.append(
.unsafeFlags(
[
"-static",
"-DCOMPILED_WITH_SWIFT",
"-DPURE_BRIDGING_MODE",
"-UIBOutlet", "-UIBAction", "-UIBInspectable",
"-I\(swiftToolchainPath)/swift/include",
"-I\(swiftToolchainPath)/swift/stdlib/public/SwiftShims",
"-I\(swiftToolchainPath)/llvm-project/llvm/include",
"-I\(swiftToolchainPath)/llvm-project/clang/include",
"-I\(swiftToolchainPath)/build/Default/swift/include",
"-I\(swiftToolchainPath)/build/Default/llvm/include",
"-I\(swiftToolchainPath)/build/Default/llvm/tools/clang/include",
"-DLLVM_DISABLE_ABI_BREAKING_CHECKS_ENFORCING", // Required to fix LLVM link issue
]
)
)
}
// MARK: [env] OPENGRAPH_SWIFT_TOOLCHAIN_VERSION
let swiftToolchainVersion = Context.environment["OPENGRAPH_SWIFT_TOOLCHAIN_VERSION"] ?? (development ? "6.0.2" : "")
if !swiftToolchainVersion.isEmpty {
sharedCSettings.append(
.define("OPENGRAPH_SWIFT_TOOLCHAIN_VERSION", to: swiftToolchainVersion)
)
}
// MARK: - [env] OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED
let swiftToolchainSupported = envEnable("OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED", default: !swiftToolchainVersion.isEmpty)
if swiftToolchainSupported {
sharedCSettings.append(.define("OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED"))
sharedSwiftSettings.append(.define("OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED"))
}
// MARK: - [env] OPENGRAPH_TARGET_RELEASE
let releaseVersion = Context.environment["OPENGRAPH_TARGET_RELEASE"].flatMap { Int($0) } ?? 2024
sharedCSettings.append(.define("OPENGRAPH_RELEASE", to: "\(releaseVersion)"))
sharedSwiftSettings.append(.define("OPENGRAPH_RELEASE_\(releaseVersion)"))
if releaseVersion >= 2021 {
for year in 2021 ... releaseVersion {
sharedSwiftSettings.append(.define("OPENGRAPH_SUPPORT_\(year)_API"))
}
}
// MARK: - [env] OPENGRAPH_WERROR
let warningsAsErrorsCondition = envEnable("OPENGRAPH_WERROR", default: isXcodeEnv && development)
if warningsAsErrorsCondition {
sharedSwiftSettings.append(.unsafeFlags(["-warnings-as-errors"]))
}
// MARK: - Targets
let openGraphShimsTarget = Target.target(
name: "OpenGraphShims",
cSettings: sharedCSettings,
swiftSettings: sharedSwiftSettings
)
let openGraphShimsTestTarget = Target.testTarget(
name: "OpenGraphShimsTests",
dependencies: [
"OpenGraphShims",
],
exclude: ["README.md"],
cSettings: sharedCSettings,
swiftSettings: sharedSwiftSettings
)
let openGraphTestTarget = Target.testTarget(
name: "OpenGraphTests",
dependencies: [
"OpenGraph",
],
exclude: ["README.md"],
cSettings: sharedCSettings,
swiftSettings: sharedSwiftSettings
)
let openGraphCompatibilityTestTarget = Target.testTarget(
name: "OpenGraphCompatibilityTests",
dependencies: [
.product(name: "Numerics", package: "swift-numerics"),
],
exclude: ["README.md"],
cSettings: sharedCSettings,
swiftSettings: sharedSwiftSettings
)
let openGraphSPICompatibilityTestTarget = Target.testTarget(
name: "OpenGraph_SPICompatibilityTests",
dependencies: [
.product(name: "Numerics", package: "swift-numerics"),
],
exclude: ["README.md"],
cSettings: sharedCSettings,
swiftSettings: sharedSwiftSettings
)
// MARK: - Package
let package = Package(
name: "OpenGraph",
products: [
.library(name: "OpenGraph_SPI", targets: ["OpenGraph_SPI"]),
.library(name: "OpenGraph", targets: ["OpenGraph"]),
.library(name: "OpenGraphShims", targets: ["OpenGraphShims"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-numerics", from: "1.0.2"),
],
targets: [
// FIXME: Merge into one target
// OpenGraph is a C++ & Swift mix target.
// The SwiftPM support for such usage is still in progress.
.target(
name: "OpenGraph_SPI",
cSettings: sharedCSettings + [
.define("__COREFOUNDATION_FORSWIFTFOUNDATIONONLY__", to: "1", .when(platforms: .nonDarwinPlatforms)),
]
),
.target(
name: "OpenGraph",
dependencies: ["OpenGraph_SPI"],
cSettings: sharedCSettings,
swiftSettings: sharedSwiftSettings
),
openGraphShimsTarget,
openGraphTestTarget,
openGraphShimsTestTarget,
openGraphCompatibilityTestTarget,
openGraphSPICompatibilityTestTarget,
],
cxxLanguageStandard: .cxx17
)
#if os(macOS)
// FIXME: Enable it by default will cause non-iOS/macOS Apple OS build fail currently.
// Add the corresponding tbd file and framework to fix it.
let attributeGraphCondition = envEnable("OPENGRAPH_ATTRIBUTEGRAPH", default: true)
#else
let attributeGraphCondition = envEnable("OPENGRAPH_ATTRIBUTEGRAPH")
#endif
let useLocalDeps = envEnable("OPENGRAPH_USE_LOCAL_DEPS")
extension Target {
func addAGSettings() {
dependencies.append(
.product(name: "AttributeGraph", package: "DarwinPrivateFrameworks")
)
var swiftSettings = swiftSettings ?? []
swiftSettings.append(.define("OPENGRAPH_ATTRIBUTEGRAPH"))
self.swiftSettings = swiftSettings
}
func addCompatibilitySettings() {
dependencies.append(
.product(name: "AttributeGraph", package: "DarwinPrivateFrameworks")
)
var swiftSettings = swiftSettings ?? []
swiftSettings.append(.define("OPENGRAPH_COMPATIBILITY_TEST"))
self.swiftSettings = swiftSettings
}
}
if attributeGraphCondition {
let privateFrameworkRepo: Package.Dependency
if useLocalDeps {
privateFrameworkRepo = Package.Dependency.package(path: "../DarwinPrivateFrameworks")
} else {
privateFrameworkRepo = Package.Dependency.package(url: "https://github.com/OpenSwiftUIProject/DarwinPrivateFrameworks.git", branch: "main")
}
package.dependencies.append(privateFrameworkRepo)
openGraphShimsTarget.addAGSettings()
let agVersion = Context.environment["DARWIN_PRIVATE_FRAMEWORKS_TARGET_RELEASE"].flatMap { Int($0) } ?? 2024
package.platforms = switch agVersion {
case 2024: [.iOS(.v18), .macOS(.v15), .macCatalyst(.v18), .tvOS(.v18), .watchOS(.v10), .visionOS(.v2)]
case 2021: [.iOS(.v15), .macOS(.v12), .macCatalyst(.v15), .tvOS(.v15), .watchOS(.v7)]
default: nil
}
} else {
openGraphShimsTarget.dependencies.append("OpenGraph")
}
let compatibilityTestCondition = envEnable("OPENGRAPH_COMPATIBILITY_TEST")
if compatibilityTestCondition && attributeGraphCondition {
openGraphCompatibilityTestTarget.addCompatibilitySettings()
openGraphSPICompatibilityTestTarget.addCompatibilitySettings()
} else {
openGraphCompatibilityTestTarget.dependencies.append("OpenGraph")
openGraphSPICompatibilityTestTarget.dependencies.append("OpenGraph")
}
extension [Platform] {
static var nonDarwinPlatforms: [Platform] {
[.linux, .android, .wasi, .openbsd, .windows]
}
}