Skip to content

Commit 890e42f

Browse files
authored
Fix layout of artifacts in build prebuilts script. (#8256)
Fix layout of artifacts in build prebuilts script so that it replicates what we hope on the downloads site. Facilitates local builds the using that for testing. Also a fix in the manifest downloader to remove the old version of the manifest before copying the new one over.
1 parent 225bcbf commit 890e42f

File tree

2 files changed

+32
-73
lines changed

2 files changed

+32
-73
lines changed

Sources/Workspace/Workspace+Prebuilts.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ extension Workspace {
207207
identity: .plain("swift-syntax"),
208208
kind: .remoteSourceControl("[email protected]:swiftlang/swift-syntax.git")
209209
),
210-
],
210+
]
211211
),
212212
]
213213
}
@@ -309,6 +309,9 @@ extension Workspace {
309309
try fileSystem.copy(from: sourcePath, to: destination)
310310
// and cache it
311311
if let cacheDest {
312+
if fileSystem.exists(cacheDest) {
313+
try fileSystem.removeFileTree(cacheDest)
314+
}
312315
try fileSystem.createDirectory(cacheDest.parentDirectory, recursive: true)
313316
try fileSystem.copy(from: destination, to: cacheDest)
314317
}
@@ -351,6 +354,9 @@ extension Workspace {
351354
if let manifest = try loadManifest() {
352355
// Cache the manifest
353356
if let cacheDest {
357+
if fileSystem.exists(cacheDest) {
358+
try fileSystem.removeFileTree(cacheDest)
359+
}
354360
try fileSystem.createDirectory(cacheDest.parentDirectory, recursive: true)
355361
try fileSystem.copy(from: destination, to: cacheDest)
356362
}

Sources/swift-build-prebuilts/BuildPrebuilts.swift

Lines changed: 25 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ var prebuiltRepos: IdentifiableSet<PrebuiltRepos> = [
7676
),
7777
]
7878

79-
let manifestHost = URL(string: "https://github.com/dschaefer2/swift-syntax/releases/download")!
8079
let swiftVersion = "\(SwiftVersion.current.major).\(SwiftVersion.current.minor)"
8180
let dockerImageRoot = "swiftlang/swift:nightly-6.1-"
8281

@@ -102,33 +101,36 @@ struct BuildPrebuilts: AsyncParsableCommand {
102101
try fm.removeItem(atPath: stageDir.pathString)
103102
}
104103
try fm.createDirectory(atPath: stageDir.pathString, withIntermediateDirectories: true)
105-
_ = fm.changeCurrentDirectoryPath(stageDir.pathString)
104+
105+
let srcDir = stageDir.appending("src")
106+
try fm.createDirectory(atPath: srcDir.pathString, withIntermediateDirectories: true)
107+
108+
let libDir = stageDir.appending("lib")
109+
let modulesDir = stageDir.appending("modules")
110+
let includesDir = stageDir.appending("include")
106111

107112
for repo in prebuiltRepos.values {
108-
let repoDir = stageDir.appending(repo.url.lastPathComponent)
109-
let libDir = stageDir.appending("lib")
110-
let modulesDir = stageDir.appending("modules")
111-
let includesDir = stageDir.appending("include")
113+
let repoDir = srcDir.appending(repo.url.lastPathComponent)
112114
let scratchDir = repoDir.appending(".build")
113115
let buildDir = scratchDir.appending("release")
114116
let srcModulesDir = buildDir.appending("Modules")
117+
let prebuiltDir = stageDir.appending(repo.url.lastPathComponent)
115118

116-
try await shell("git clone \(repo.url)")
119+
try await shell("git clone \(repo.url)", cwd: srcDir)
117120

118121
for version in repo.versions {
119-
let versionDir = stageDir.appending(version.tag)
122+
let versionDir = prebuiltDir.appending(version.tag)
120123
if !fm.fileExists(atPath: versionDir.pathString) {
121124
try fm.createDirectory(atPath: versionDir.pathString, withIntermediateDirectories: true)
122125
}
123126

124-
_ = fm.changeCurrentDirectoryPath(repoDir.pathString)
125-
try await shell("git checkout \(version.tag)")
127+
try await shell("git checkout \(version.tag)", cwd: repoDir)
126128

127129
var newLibraries: IdentifiableSet<Workspace.PrebuiltsManifest.Library> = []
128130

129131
for library in version.manifest.libraries {
130132
// TODO: this is assuming products map to target names which is not always true
131-
try await shell("swift package add-product \(library.name) --type static-library --targets \(library.products.joined(separator: " "))")
133+
try await shell("swift package add-product \(library.name) --type static-library --targets \(library.products.joined(separator: " "))", cwd: repoDir)
132134

133135
var newArtifacts: [Workspace.PrebuiltsManifest.Library.Artifact] = []
134136

@@ -152,7 +154,7 @@ struct BuildPrebuilts: AsyncParsableCommand {
152154
cmd += "\(dockerCommand) run --rm --platform \(dockerPlatform) -v \(repoDir):\(repoDir) -w \(repoDir) \(dockerImageRoot)\(dockerTag) "
153155
}
154156
cmd += "swift build -c release -debug-info-format none --arch \(platform.arch) --product \(library.name)"
155-
try await shell(cmd)
157+
try await shell(cmd, cwd: repoDir)
156158

157159
// Copy the library to staging
158160
let lib = "lib\(library.name).a"
@@ -175,16 +177,14 @@ struct BuildPrebuilts: AsyncParsableCommand {
175177
}
176178

177179
// Zip it up
178-
_ = fm.changeCurrentDirectoryPath(stageDir.pathString)
179180
let zipFile = versionDir.appending("\(swiftVersion)-\(library.name)-\(platform).zip")
180181
let contentDirs = ["lib", "Modules"] + (library.cModules.isEmpty ? [] : ["include"])
181182
#if os(Windows)
182-
try await shell("tar -acf \(zipFile.pathString) \(contentDirs.joined(separator: " "))")
183+
try await shell("tar -acf \(zipFile.pathString) \(contentDirs.joined(separator: " "))", cwd: stageDir)
183184
#else
184-
try await shell("zip -r \(zipFile.pathString) \(contentDirs.joined(separator: " "))")
185+
try await shell("zip -r \(zipFile.pathString) \(contentDirs.joined(separator: " "))", cwd: stageDir)
185186
#endif
186187

187-
_ = fm.changeCurrentDirectoryPath(repoDir.pathString)
188188
let contents = try ByteString(Data(contentsOf: zipFile.asURL))
189189
let checksum = SHA256().hash(contents).hexadecimalRepresentation
190190

@@ -203,35 +203,20 @@ struct BuildPrebuilts: AsyncParsableCommand {
203203
)
204204
newLibraries.insert(newLibrary)
205205

206-
try await shell("git reset --hard")
207-
}
208-
209-
if let oldManifest = try await downloadManifest(version: version) {
210-
// Add in elements from the old manifest we haven't generated
211-
for library in oldManifest.libraries {
212-
if var newLibrary = newLibraries[library.name] {
213-
var newArtifacts = IdentifiableSet<Workspace.PrebuiltsManifest.Library.Artifact>(newLibrary.artifacts)
214-
for oldArtifact in library.artifacts {
215-
if !newArtifacts.contains(id: oldArtifact.id) {
216-
newArtifacts.insert(oldArtifact)
217-
}
218-
}
219-
newLibrary.artifacts = .init(newArtifacts.values)
220-
newLibraries.insert(newLibrary)
221-
} else {
222-
newLibraries.insert(library)
223-
}
224-
}
206+
try await shell("git reset --hard", cwd: repoDir)
225207
}
226-
let newManifest = Workspace.PrebuiltsManifest(libraries: .init(newLibraries.values))
227208

209+
let manifest = Workspace.PrebuiltsManifest(libraries: .init(newLibraries.values))
228210
let encoder = JSONEncoder()
229211
encoder.outputFormatting = .prettyPrinted
230-
let manifestData = try encoder.encode(newManifest)
212+
let manifestData = try encoder.encode(manifest)
231213
let manifestFile = versionDir.appending("\(swiftVersion)-manifest.json")
232214
try manifestData.write(to: manifestFile.asURL)
233215
}
234216
}
217+
218+
_ = FileManager.default.changeCurrentDirectoryPath(stageDir.pathString)
219+
try fm.removeItem(atPath: srcDir.pathString)
235220
}
236221

237222
func canBuild(_ platform: Workspace.PrebuiltsManifest.Platform) -> Bool {
@@ -250,7 +235,9 @@ struct BuildPrebuilts: AsyncParsableCommand {
250235
return docker && platform.os == .linux
251236
}
252237

253-
func shell(_ command: String) async throws {
238+
func shell(_ command: String, cwd: AbsolutePath) async throws {
239+
_ = FileManager.default.changeCurrentDirectoryPath(cwd.pathString)
240+
254241
#if os(Windows)
255242
let arguments = ["C:\\Windows\\System32\\cmd.exe", "/c", command]
256243
#else
@@ -277,40 +264,6 @@ struct BuildPrebuilts: AsyncParsableCommand {
277264
#endif
278265
}
279266
}
280-
281-
func downloadManifest(version: PrebuiltRepos.Version) async throws -> Workspace.PrebuiltsManifest? {
282-
let fm = FileManager.default
283-
let manifestFile = swiftVersion + "-manifest.json"
284-
let destination = stageDir.appending(components: version.tag, manifestFile)
285-
if fm.fileExists(atPath: destination.pathString) {
286-
do {
287-
return try JSONDecoder().decode(
288-
Workspace.PrebuiltsManifest.self,
289-
from: Data(contentsOf: destination.asURL)
290-
)
291-
} catch {
292-
// redownload it
293-
try fm.removeItem(atPath: destination.pathString)
294-
}
295-
}
296-
let manifestURL = manifestHost.appending(components: version.tag, manifestFile)
297-
print("Downloading:", manifestURL.absoluteString)
298-
let httpClient = HTTPClient()
299-
var headers = HTTPClientHeaders()
300-
headers.add(name: "Accept", value: "application/json")
301-
var request = HTTPClient.Request(kind: .generic(.get), url: manifestURL)
302-
request.options.validResponseCodes = [200]
303-
304-
let response = try? await httpClient.execute(request) { _, _ in }
305-
if let body = response?.body {
306-
return try JSONDecoder().decode(
307-
Workspace.PrebuiltsManifest.self,
308-
from: body
309-
)
310-
}
311-
312-
return nil
313-
}
314267
}
315268

316269
extension Workspace.PrebuiltsManifest.Platform {

0 commit comments

Comments
 (0)