forked from finagolfin/swift-android-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get-packages-and-swift-source.swift
301 lines (260 loc) · 12.8 KB
/
get-packages-and-swift-source.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
import Foundation
// The Termux packages to download and unpack
// libxml2 needs liblzma and libiconv
// libcurl needs zlib, libnghttp3, libnghttp2, libssh2, and openssl
var termuxPackages = ["libandroid-spawn", "libandroid-spawn-static", "libcurl", "zlib", "libxml2", "libnghttp3", "libnghttp2", "libssh2", "openssl", "liblzma", "libiconv"]
let termuxURL = "https://packages.termux.dev/apt/termux-main"
let swiftRepos = ["llvm-project", "swift", "swift-experimental-string-processing", "swift-corelibs-libdispatch",
"swift-corelibs-foundation", "swift-corelibs-xctest", "swift-syntax", "swift-collections",
"swift-foundation", "swift-foundation-icu"]
let extraSwiftRepos = ["swift-llbuild", "swift-package-manager", "swift-driver",
"swift-tools-support-core", "swift-argument-parser", "swift-crypto",
"Yams", "indexstore-db", "sourcekit-lsp", "swift-system",
"swift-certificates", "swift-asn1", "swift-toolchain-sqlite"]
let appleRepos = ["swift-argument-parser", "swift-crypto", "swift-system", "swift-collections", "swift-certificates", "swift-asn1"]
let renameRepos = ["swift-llbuild" : "llbuild", "swift-package-manager" : "swiftpm", "Yams" : "yams"]
var repoTags = ["swift-system" : "1.3.0", "swift-collections" : "1.1.3", "swift-asn1" : "1.0.0",
"swift-certificates" : "1.0.1", "Yams" : "5.0.6", "swift-argument-parser" : "1.4.0",
"swift-crypto" : "3.0.0", "swift-toolchain-sqlite" : "1.0.1"]
if ProcessInfo.processInfo.environment["BUILD_SWIFT_PM"] != nil {
termuxPackages += ["ncurses", "libsqlite"]
}
guard let SWIFT_TAG = ProcessInfo.processInfo.environment["SWIFT_TAG"] else {
fatalError("You must specify a SWIFT_TAG environment variable.")
}
guard let ANDROID_ARCH = ProcessInfo.processInfo.environment["ANDROID_ARCH"] else {
fatalError("You must specify an ANDROID_ARCH environment variable.")
}
var sdkDir = "", swiftVersion = "", swiftBranch = "", swiftSnapshotDate = ""
let tagRange = NSRange(SWIFT_TAG.startIndex..., in: SWIFT_TAG)
let tagExtract = try NSRegularExpression(pattern: "swift-([5-9]\\.[0-9]+)?\\.?[1-9]*-?([A-Z-]+)([0-9-]+[0-9])?")
if tagExtract.numberOfMatches(in: SWIFT_TAG, range: tagRange) == 1 {
let match = tagExtract.firstMatch(in: SWIFT_TAG, range: tagRange)
if match!.range(at: 1).location != NSNotFound {
swiftVersion = (SWIFT_TAG as NSString).substring(with: match!.range(at: 1))
}
swiftBranch = (SWIFT_TAG as NSString).substring(with: match!.range(at: 2))
if match!.range(at: 3).location != NSNotFound {
swiftSnapshotDate = (SWIFT_TAG as NSString).substring(with: match!.range(at: 3))
}
} else {
fatalError("Something went wrong with extracting data from the SWIFT_TAG environment variable: \(SWIFT_TAG)")
}
if swiftBranch == "RELEASE" {
repoTags["swift-argument-parser"] = "1.2.3"
repoTags["swift-collections"] = "1.1.2"
sdkDir = "swift-release-android-\(ANDROID_ARCH)-24-sdk"
} else {
sdkDir = "swift-\(swiftVersion == "" ? "trunk" : "devel")-android-\(ANDROID_ARCH)-\(swiftSnapshotDate)-24-sdk"
}
// takes the name of a command-line executable and the arguments to pass to it
func runCommand(_ name: String, with args: [String]) -> String {
let command = Process()
#if os(Android)
command.executableURL = URL(fileURLWithPath: "/system/bin/which")
#else
command.executableURL = URL(fileURLWithPath: "/usr/bin/which")
#endif
command.arguments = [name]
let output = Pipe()
let error = Pipe()
command.standardOutput = output
command.standardError = error
do {
try command.run()
} catch {
fatalError("couldn't find \(name) with error: \(error)")
}
guard let result = String(data: output.fileHandleForReading.availableData, encoding: .utf8) else {
fatalError("couldn't read `which` output")
}
guard let errorResult = String(data: error.fileHandleForReading.availableData, encoding: .ascii) else {
fatalError("couldn't read `which` stderr")
}
if result != "" {
let chompResult = result.replacingOccurrences(of: "\\s+$", with: "", options: .regularExpression)
let command = Process()
command.executableURL = URL(fileURLWithPath: chompResult)
command.arguments = args
let output = Pipe()
let error = Pipe()
command.standardOutput = output
command.standardError = error
do {
print("running command: \(([command.executableURL!.path] + args).joined(separator: " "))")
fflush(stdout)
try command.run()
} catch {
fatalError("couldn't run \(name) \(args) with error: \(error)")
}
command.waitUntilExit()
guard let commandResult = String(data: output.fileHandleForReading.availableData, encoding: .utf8) else {
fatalError("couldn't read `\(name)` output")
}
guard let errorResult = String(data: error.fileHandleForReading.availableData, encoding: .ascii) else {
fatalError("couldn't read `\(name)` stderr")
}
if command.terminationStatus == 0 {
return commandResult.replacingOccurrences(of: "\\s+$", with: "", options: .regularExpression)
} else {
fatalError("couldn't run \(name) \(args) because of \(errorResult)")
}
} else {
if errorResult != "" {
fatalError("couldn't find \(name) because of \(errorResult)")
} else {
fatalError("couldn't find \(name), maybe a problem with `\(command.executableURL!.path)`?")
}
}
}
print("Checking if needed system utilities are installed...")
print(runCommand("cmake", with: ["--version"]))
print("ninja \(runCommand("ninja", with: ["--version"]))")
#if os(macOS)
print(runCommand("python3", with: ["--version"]))
#else
print(runCommand("python", with: ["--version"]))
#endif
print(runCommand("patchelf", with: ["--version"]))
#if !os(macOS)
// ar does not take a "--version" arg on macOS
print(runCommand("ar", with: ["--version"]))
#endif
print(runCommand("tar", with: ["--version"]))
print(runCommand("xz", with: ["--version"]))
print(runCommand("curl", with: ["--version"]))
print(runCommand("gzip", with: ["--version"]))
#if os(macOS)
extension String {
func appendingPathComponent(_ path: String) -> String {
(self as NSString).appendingPathComponent(path)
}
}
#endif
let fmd = FileManager.default
let cwd = fmd.currentDirectoryPath
let termuxArchive = cwd.appendingPathComponent("termux")
if !fmd.fileExists(atPath: termuxArchive) {
try fmd.createDirectory(atPath: termuxArchive, withIntermediateDirectories: false)
}
if !fmd.fileExists(atPath: termuxArchive.appendingPathComponent("Packages-\(ANDROID_ARCH)")) {
_ = runCommand("curl", with: ["-o", "termux/Packages-\(ANDROID_ARCH)",
"\(termuxURL)/dists/stable/main/binary-\(ANDROID_ARCH == "armv7" ? "arm" : ANDROID_ARCH)/Packages"])
}
let packages = try String(contentsOfFile: termuxArchive.appendingPathComponent("Packages-\(ANDROID_ARCH)"), encoding: .utf8)
for termuxPackage in termuxPackages {
guard let packagePathRange = packages.range(of: "Filename: \\S+/\(termuxPackage)_\\S+", options: .regularExpression) else {
fatalError("couldn't find \(termuxPackage) in Packages list")
}
let packagePath = packages[packagePathRange].dropFirst("Filename: ".count).description
guard let packageNameRange = packagePath.range(of: "\(termuxPackage)_\\S+", options: .regularExpression) else {
fatalError("couldn't extract \(termuxPackage) .deb package from package path")
}
let packageName = packagePath[packageNameRange]
print("Checking for \(packageName)")
if !fmd.fileExists(atPath: termuxArchive.appendingPathComponent(String(packageName))) {
print("Downloading \(packageName)")
_ = runCommand("curl", with: ["-f", "-o", "termux/\(packageName)",
"\(termuxURL)/\(packagePath)"])
}
if !fmd.fileExists(atPath: cwd.appendingPathComponent(sdkDir)) {
print("Unpacking \(packageName)")
#if os(macOS)
_ = runCommand("tar", with: ["xf", "\(termuxArchive.appendingPathComponent(String(packageName)))"])
#else
_ = runCommand("ar", with: ["x", "\(termuxArchive.appendingPathComponent(String(packageName)))"])
#endif
_ = runCommand("tar", with: ["xf", "data.tar.xz"])
}
}
let sdkPath = cwd.appendingPathComponent(sdkDir)
if !fmd.fileExists(atPath: sdkPath) {
try fmd.removeItem(atPath: cwd.appendingPathComponent("data.tar.xz"))
try fmd.removeItem(atPath: cwd.appendingPathComponent("control.tar.xz"))
try fmd.removeItem(atPath: cwd.appendingPathComponent("debian-binary"))
try fmd.createDirectory(atPath: sdkPath, withIntermediateDirectories: false)
try fmd.moveItem(atPath: cwd.appendingPathComponent("data/data/com.termux/files/usr"),
toPath: sdkPath.appendingPathComponent("usr"))
try fmd.removeItem(atPath: cwd.appendingPathComponent("data"))
try fmd.removeItem(atPath: sdkPath.appendingPathComponent("usr/bin/curl-config"))
try fmd.removeItem(atPath: sdkPath.appendingPathComponent("usr/bin/xml2-config"))
try fmd.removeItem(atPath: sdkPath.appendingPathComponent("usr/share/man"))
try fmd.removeItem(atPath: sdkPath.appendingPathComponent("usr/lib/ossl-modules"))
try fmd.removeItem(atPath: sdkPath.appendingPathComponent("usr/lib/engines-3"))
try fmd.removeItem(atPath: sdkPath.appendingPathComponent("usr/etc"))
}
let libPath = sdkPath.appendingPathComponent("usr/lib")
// flatten each of the shared object file links, since Android APKs do not support version-suffixed .so.x.y.z paths
var renamedSharedObjects: [String: String] = [:]
for soFile in try fmd.contentsOfDirectory(atPath: libPath) {
let parts = soFile.split(separator: ".")
guard let soIndex = parts.firstIndex(of: "so") else { continue }
// e.g., for "libtinfo.so.6.5": soBase="libtinfo.so" soVersion="6.5"
let soBase = parts[0...soIndex].joined(separator: ".")
let soVersion = parts.dropFirst(soIndex + 1).joined(separator: ".")
if !soVersion.isEmpty {
renamedSharedObjects[soFile] = soBase // libtinfo.so.6.5->libtinfo.so
}
let soPath = libPath.appendingPathComponent(soFile)
let soBasePath = libPath.appendingPathComponent(soBase)
if (try? fmd.destinationOfSymbolicLink(atPath: soPath)) != nil {
try fmd.removeItem(atPath: soPath) // clear links
} else if !soVersion.isEmpty {
// otherwise move the version-suffixed path to the un-versioned destination
if (try? fmd.destinationOfSymbolicLink(atPath: soBasePath)) != nil {
// need to remove the destination before we can move
try fmd.removeItem(atPath: soBasePath)
}
try fmd.moveItem(atPath: soPath, toPath: soBasePath)
}
}
// Rename ncurses for llbuild and add a symlink for SwiftPM
try fmd.moveItem(atPath: libPath.appendingPathComponent("libncursesw.so"), toPath: libPath.appendingPathComponent("libcurses.so"))
try fmd.createSymbolicLink(atPath: libPath.appendingPathComponent("libncurses.so"), withDestinationPath: "libcurses.so")
// update the rpath to be $ORIGIN, set the soname, and update all the "needed" sections for each of the peer libraries
for soFile in try fmd.contentsOfDirectory(atPath: libPath).filter({ $0.hasSuffix(".so")} ) {
let soPath = libPath.appendingPathComponent(soFile)
// fix the soname (e.g., libtinfo.so.6.5->libtinfo.so)
_ = runCommand("patchelf", with: ["--set-soname", soFile, soPath])
_ = runCommand("patchelf", with: ["--set-rpath", "$ORIGIN", soPath])
let needed = Set(runCommand("patchelf", with: ["--print-needed", soPath]).split(separator: "\n").map(\.description))
for needs in needed {
if let unversioned = renamedSharedObjects[needs] {
_ = runCommand("patchelf", with: ["--replace-needed", needs, unversioned, soPath])
}
}
}
for repo in swiftRepos {
print("Checking for \(repo) source")
if !fmd.fileExists(atPath: cwd.appendingPathComponent(repo)) {
print("Downloading and extracting \(repo) source")
let tag = repoTags[repo] ?? SWIFT_TAG
var repoOrg = "swiftlang"
if ["swift-corelibs-libdispatch", "swift-collections"].contains(repo) {
repoOrg = "apple"
}
_ = runCommand("curl", with: ["-f", "-L", "-O",
"https://github.com/\(repoOrg)/\(repo)/archive/refs/tags/\(tag).tar.gz"])
_ = runCommand("tar", with: ["xf", "\(tag).tar.gz"])
try fmd.moveItem(atPath: cwd.appendingPathComponent("\(repo)-\(tag)"),
toPath: cwd.appendingPathComponent(repo))
try fmd.removeItem(atPath: cwd.appendingPathComponent("\(tag).tar.gz"))
}
}
if ProcessInfo.processInfo.environment["BUILD_SWIFT_PM"] != nil {
for repo in extraSwiftRepos {
let tag = repoTags[repo] ?? SWIFT_TAG
var repoOrg = "swiftlang"
if repo == "Yams" {
repoOrg = "jpsim"
} else if appleRepos.contains(repo) {
repoOrg = "apple"
}
_ = runCommand("curl", with: ["-f", "-L", "-O",
"https://github.com/\(repoOrg)/\(repo)/archive/refs/tags/\(tag).tar.gz"])
_ = runCommand("tar", with: ["xf", "\(tag).tar.gz"])
try fmd.moveItem(atPath: cwd.appendingPathComponent("\(repo)-\(tag)"),
toPath: cwd.appendingPathComponent(renameRepos[repo] ?? repo))
try fmd.removeItem(atPath: cwd.appendingPathComponent("\(tag).tar.gz"))
}
}