-
Notifications
You must be signed in to change notification settings - Fork 994
Expand file tree
/
Copy pathPackage.swift
More file actions
198 lines (184 loc) · 5.81 KB
/
Package.swift
File metadata and controls
198 lines (184 loc) · 5.81 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
// swift-tools-version:5.9
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
// NOTE: This package manifest is for frameworks built locally with CMake.
// It defines dependencies and linker settings for Executorch components.
//
// To use prebuilt binaries instead, switch to one of the "swiftpm" branches,
// which fetch the precompiled `.xcframeworks`.
//
// For details on building frameworks locally or using prebuilt binaries,
// see the documentation:
// https://pytorch.org/executorch/main/using-executorch-ios
import PackageDescription
import Foundation
let debug_suffix = "_debug"
let dependencies_suffix = "_with_dependencies"
func deliverables(_ dict: [String: [String: Any]]) -> [String: [String: Any]] {
dict
.reduce(into: [String: [String: Any]]()) { result, pair in
let (key, value) = pair
result[key] = value
result[key + debug_suffix] = value
}
.reduce(into: [String: [String: Any]]()) { result, pair in
let (key, value) = pair
var newValue = value
if key.hasSuffix(debug_suffix) {
for (k, v) in value where k.hasSuffix(debug_suffix) {
let trimmed = String(k.dropLast(debug_suffix.count))
newValue[trimmed] = v
}
}
result[key] = newValue.filter { !$0.key.hasSuffix(debug_suffix) }
}
}
let products = deliverables([
"backend_coreml": [
"frameworks": [
"Accelerate",
"CoreML",
],
"libraries": [
"sqlite3",
],
],
"backend_mps": [
"frameworks": [
"Metal",
"MetalPerformanceShaders",
"MetalPerformanceShadersGraph",
],
],
"backend_xnnpack": [
"targets": [
"threadpool",
],
],
"executorch": [
"libraries": [
"c++",
],
],
"executorch_llm": [
"targets": [
"executorch",
],
],
"kernels_llm": [:],
"kernels_optimized": [
"frameworks": [
"Accelerate",
],
"targets": [
"threadpool",
],
],
"kernels_quantized": [:],
"kernels_torchao": [
"targets": [
"threadpool",
],
],
])
let targets = deliverables([
"threadpool": [:],
])
let packageProducts: [Product] = products.keys.map { key -> Product in
.library(name: key, targets: ["\(key)\(dependencies_suffix)"])
}.sorted { $0.name < $1.name }
var packageTargets: [Target] = []
for (key, value) in targets {
packageTargets.append(.binaryTarget(
name: key,
path: "cmake-out/\(key).xcframework"
))
}
for (key, value) in products {
packageTargets.append(.binaryTarget(
name: key,
path: "cmake-out/\(key).xcframework"
))
let target: Target = .target(
name: "\(key)\(dependencies_suffix)",
dependencies: ([key] + (value["targets"] as? [String] ?? []).map {
key.hasSuffix(debug_suffix) ? $0 + debug_suffix : $0
}).map { .target(name: $0) },
path: ".Package.swift/\(key)",
linkerSettings:
(value["frameworks"] as? [String] ?? []).map { .linkedFramework($0) } +
(value["libraries"] as? [String] ?? []).map { .linkedLibrary($0) }
)
packageTargets.append(target)
}
// Test fixtures. add_coreml.pte and add_mul_coreml.pte are generated at CI
// time by extension/apple/ExecuTorch/__tests__/resources/generate_coreml_test_models.py
// (invoked by scripts/build_apple_frameworks.sh before `swift test`). They
// are gitignored, so include them in test resources only when present so
// that `swift test` runs on dev machines without CoreML python deps don't
// fail at the SwiftPM resolve stage.
let testResourcesDir = "extension/apple/ExecuTorch/__tests__/resources"
var testResources: [Resource] = [.copy("resources/add.pte")]
if FileManager.default.fileExists(atPath: "\(testResourcesDir)/add_coreml.pte") {
testResources.append(.copy("resources/add_coreml.pte"))
}
if FileManager.default.fileExists(atPath: "\(testResourcesDir)/add_mul_coreml.pte") {
testResources.append(.copy("resources/add_mul_coreml.pte"))
}
// SwiftPM resources must live under the target's path, so the ObjC test
// target uses symlinks to the canonical resources directory. The symlinks
// themselves are gitignored and (re)created by scripts/build_apple_frameworks.sh.
let objcTestsDir = "extension/apple/ExecuTorch/__tests__/ObjC"
var objcTestResources: [Resource] = []
if FileManager.default.fileExists(atPath: "\(objcTestsDir)/add.pte") {
objcTestResources.append(.copy("add.pte"))
}
if FileManager.default.fileExists(atPath: "\(objcTestsDir)/add_coreml.pte") {
objcTestResources.append(.copy("add_coreml.pte"))
}
if FileManager.default.fileExists(atPath: "\(objcTestsDir)/add_mul_coreml.pte") {
objcTestResources.append(.copy("add_mul_coreml.pte"))
}
let testLinkerSettings: [LinkerSetting] = [
.unsafeFlags([
"-Xlinker", "-force_load",
"-Xlinker", "cmake-out/kernels_optimized.xcframework/macos-arm64/libkernels_optimized_macos.a",
])
]
let package = Package(
name: "executorch",
platforms: [
.iOS(.v17),
.macOS(.v12),
],
products: packageProducts,
targets: packageTargets + [
.testTarget(
name: "tests",
dependencies: [
.target(name: "executorch\(debug_suffix)"),
.target(name: "kernels_optimized\(dependencies_suffix)"),
],
path: "extension/apple/ExecuTorch/__tests__",
exclude: ["ObjC", "resources/generate_coreml_test_models.py", "resources/.gitignore"],
resources: testResources,
linkerSettings: testLinkerSettings
),
.testTarget(
name: "objc_tests",
dependencies: [
.target(name: "executorch\(debug_suffix)"),
.target(name: "kernels_optimized\(dependencies_suffix)"),
],
path: "extension/apple/ExecuTorch/__tests__/ObjC",
exclude: [".gitignore"],
resources: objcTestResources,
linkerSettings: testLinkerSettings
)
]
)