Skip to content

Commit 330c3f2

Browse files
Fix support for Amazon Linux 2 x86_64, add EndToEndTests for all RHEL distributions (#195)
This fixes the final issue we saw with #138 where there was an error generating an `amazonlinux2` Swift SDK for x86_64. This was fixed by removing 32-bit libraries from the sysroot imported from the container for RHEL-based distributions. To test this, I've added EndToEndTests for `amazonlinux2` and `fedora39` on top of the existing RHEL UBI9 tests. All of these depend on pulling from containers to test: ``` 2.1 GiB [#################] /fedora39_aarch64_6.0.3-RELEASE_with-docker.artifactbundle 2.0 GiB [################ ] /fedora39_x86_64_6.0.3-RELEASE_with-docker.artifactbundle 2.0 GiB [################ ] /amazonlinux2_aarch64_6.0.3-RELEASE_with-docker.artifactbundle 1.9 GiB [############### ] /amazonlinux2_x86_64_6.0.3-RELEASE_with-docker.artifactbundle 1.9 GiB [############### ] /rhel_ubi9_x86_64_6.0.3-RELEASE_with-docker.artifactbundle 1.9 GiB [############### ] /rhel_ubi9_aarch64_6.0.3-RELEASE_with-docker.artifactbundle 1.8 GiB [############### ] /fedora39_x86_64_5.10.1-RELEASE_with-docker.artifactbundle 1.8 GiB [############### ] /fedora39_aarch64_5.10.1-RELEASE_with-docker.artifactbundle 1.8 GiB [############## ] /amazonlinux2_aarch64_5.10.1-RELEASE_with-docker.artifactbundle 1.8 GiB [############## ] /amazonlinux2_x86_64_5.10.1-RELEASE_with-docker.artifactbundle 1.7 GiB [############## ] /rhel_ubi9_x86_64_5.10.1-RELEASE_with-docker.artifactbundle 1.7 GiB [############## ] /rhel_ubi9_aarch64_5.10.1-RELEASE_with-docker.artifactbundle 1.7 GiB [############## ] /amazonlinux2_aarch64_5.9.2-RELEASE_with-docker.artifactbundle 1.7 GiB [############# ] /amazonlinux2_x86_64_5.9.2-RELEASE_with-docker.artifactbundle 1.7 GiB [############# ] /rhel_ubi9_x86_64_5.9.2-RELEASE_with-docker.artifactbundle 1.7 GiB [############# ] /rhel_ubi9_aarch64_5.9.2-RELEASE_with-docker.artifactbundle ``` All of these Swift SDKs take 30GB of disk space. --------- Co-authored-by: Max Desiatov <[email protected]>
1 parent 4de34ce commit 330c3f2

File tree

2 files changed

+126
-26
lines changed

2 files changed

+126
-26
lines changed

Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Copy.swift

+15
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,21 @@ extension SwiftSDKGenerator {
9191
}
9292
try await generator.createSymlink(at: sdkDirPath.appending("lib"), pointingTo: "usr/lib")
9393

94+
// Look for 32-bit libraries to remove from RHEL-based distros
95+
// These are not needed, and the amazonlinux2 x86_64 symlinks are messed up
96+
if case .rhel = targetDistribution {
97+
for gccVersion in 7...13 {
98+
let removePath = "gcc/x86_64-redhat-linux/\(gccVersion)/32"
99+
if await doesFileExist(at: sdkUsrLibPath.appending(removePath)) {
100+
logger.warning(
101+
"Removing 32-bit libraries from RHEL imported sysroot",
102+
metadata: ["removePath": .stringConvertible(removePath)]
103+
)
104+
try await removeRecursively(at: sdkUsrLibPath.appending(removePath))
105+
}
106+
}
107+
}
108+
94109
// Copy the ELF interpreter
95110
try await generator.copyFromDockerContainer(
96111
id: containerID,

Tests/SwiftSDKGeneratorTests/EndToEndTests.swift

+111-26
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,12 @@ extension FileManager {
3636
try createDirectory(at: temporaryDirectory, withIntermediateDirectories: false)
3737
defer {
3838
// Best effort cleanup.
39-
do {
40-
if cleanup {
41-
try removeItem(at: temporaryDirectory)
42-
logger.info("Removed temporary directory")
43-
} else {
44-
logger.info("Keeping temporary directory")
45-
}
46-
} catch {}
39+
if cleanup {
40+
try? removeItem(at: temporaryDirectory)
41+
logger.info("Removed temporary directory")
42+
} else {
43+
logger.info("Keeping temporary directory")
44+
}
4745
}
4846

4947
logger.info("Created temporary directory")
@@ -62,7 +60,7 @@ func buildSDK(_ logger: Logger, scratchPath: String, withArguments runArguments:
6260
logger[metadataKey: "runArguments"] = "\"\(runArguments)\""
6361
logger[metadataKey: "scratchPath"] = "\(scratchPath)"
6462

65-
logger.info("Building SDK")
63+
logger.info("Building Swift SDK")
6664

6765
var packageDirectory = FilePath(#filePath)
6866
packageDirectory.removeLastComponent()
@@ -71,7 +69,7 @@ func buildSDK(_ logger: Logger, scratchPath: String, withArguments runArguments:
7169
let generatorOutput = try await Shell.readStdout(
7270
"cd \(packageDirectory) && swift run --scratch-path \"\(scratchPath)\" swift-sdk-generator make-linux-sdk \(runArguments)"
7371
)
74-
logger.info("Finished building SDK")
72+
logger.info("Finished building Swift SDK")
7573

7674
let installCommand = try XCTUnwrap(
7775
generatorOutput.split(separator: "\n").first {
@@ -83,17 +81,17 @@ func buildSDK(_ logger: Logger, scratchPath: String, withArguments runArguments:
8381
).stem
8482
logger[metadataKey: "bundleName"] = "\(bundleName)"
8583

86-
logger.info("Checking installed SDKs")
84+
logger.info("Checking installed Swift SDKs")
8785
let installedSDKs = try await Shell.readStdout("swift experimental-sdk list").components(
8886
separatedBy: "\n")
8987

9088
// Make sure this bundle hasn't been installed already.
9189
if installedSDKs.contains(bundleName) {
92-
logger.info("Removing existing SDK")
90+
logger.info("Removing existing Swift SDK")
9391
try await Shell.run("swift experimental-sdk remove \(bundleName)")
9492
}
9593

96-
logger.info("Installing new SDK")
94+
logger.info("Installing new Swift SDK")
9795
let installOutput = try await Shell.readStdout(String(installCommand))
9896
XCTAssertTrue(installOutput.contains("successfully installed"))
9997

@@ -168,9 +166,11 @@ struct SDKConfiguration {
168166
var linuxDistributionVersion: String
169167
var architecture: String
170168
var withDocker: Bool
169+
var containerImageSuffix: String?
171170

172171
var bundleName: String {
173-
"\(linuxDistributionName)_\(linuxDistributionVersion)_\(architecture)_\(swiftVersion)-RELEASE\(withDocker ? "_with-docker" : "")"
172+
let sdkPrefix = containerImageSuffix ?? "\(linuxDistributionName)_\(linuxDistributionVersion)"
173+
return "\(sdkPrefix)_\(architecture)_\(swiftVersion)-RELEASE\(withDocker ? "_with-docker" : "")"
174174
}
175175

176176
func withDocker(_ enabled: Bool = true) -> SDKConfiguration {
@@ -179,6 +179,12 @@ struct SDKConfiguration {
179179
return res
180180
}
181181

182+
func withContainerImageSuffix(_ containerImageSuffix: String) -> SDKConfiguration {
183+
var res = self
184+
res.containerImageSuffix = containerImageSuffix
185+
return res
186+
}
187+
182188
func withArchitecture(_ arch: String) -> SDKConfiguration {
183189
var res = self
184190
res.architecture = arch
@@ -191,14 +197,22 @@ struct SDKConfiguration {
191197
}
192198

193199
var sdkGeneratorArguments: String {
200+
// Build the container image tag
201+
var containerImage: String? = nil
202+
if let containerImageSuffix {
203+
containerImage = "swift:\(swiftVersion)-\(containerImageSuffix)"
204+
}
205+
194206
return [
195207
"--sdk-name \(bundleName)",
196208
"--host-toolchain",
197209
withDocker ? "--with-docker" : nil,
210+
containerImage != nil ? "--from-container-image" : nil, containerImage,
198211
"--swift-version \(swiftVersion)-RELEASE",
199212
testLinuxSwiftSDKs ? "--host \(hostArch!)-unknown-linux-gnu" : nil,
200213
"--target \(architecture)-unknown-linux-gnu",
201214
"--linux-distribution-name \(linuxDistributionName)",
215+
"--linux-distribution-version \(linuxDistributionVersion)",
202216
].compactMap { $0 }.joined(separator: " ")
203217
}
204218
}
@@ -319,17 +333,28 @@ func buildTestcases(config: SDKConfiguration) async throws {
319333
logger, scratchPath: tempDir.path, withArguments: config.sdkGeneratorArguments)
320334
}
321335

322-
logger.info("Built SDK")
336+
logger.info("Built Swift SDK")
337+
338+
// Cleanup
339+
func cleanupSDK() async {
340+
logger.info("Removing Swift SDK to clean up...")
341+
try? await Shell.run("swift experimental-sdk remove \(bundleName)")
342+
}
323343

324344
for testcase in testcases {
325-
try await FileManager.default.withTemporaryDirectory(logger: logger) { tempDir in
326-
try await buildTestcase(logger, testcase: testcase, bundleName: bundleName, tempDir: tempDir)
345+
do {
346+
try await FileManager.default.withTemporaryDirectory(logger: logger) { tempDir in
347+
try await buildTestcase(
348+
logger, testcase: testcase, bundleName: bundleName, tempDir: tempDir
349+
)
350+
}
351+
} catch {
352+
await cleanupSDK()
353+
throw error
327354
}
328355
}
329356

330-
// Cleanup
331-
logger.info("Removing SDK to cleanup...")
332-
try await Shell.run("swift experimental-sdk remove \(bundleName)")
357+
await cleanupSDK()
333358
}
334359

335360
final class Swift59_UbuntuEndToEndTests: XCTestCase {
@@ -433,12 +458,24 @@ final class Swift59_RHELEndToEndTests: XCTestCase {
433458

434459
func testAarch64FromContainer() async throws {
435460
try skipSlow()
436-
try await buildTestcases(config: config.withArchitecture("aarch64").withDocker())
461+
try await buildTestcases(config: config.withArchitecture("aarch64"))
437462
}
438463

439464
func testX86_64FromContainer() async throws {
440465
try skipSlow()
441-
try await buildTestcases(config: config.withArchitecture("x86_64").withDocker())
466+
try await buildTestcases(config: config.withArchitecture("x86_64"))
467+
}
468+
469+
func testAmazonLinux2Aarch64FromContainer() async throws {
470+
try skipSlow()
471+
try await buildTestcases(
472+
config: config.withArchitecture("aarch64").withContainerImageSuffix("amazonlinux2"))
473+
}
474+
475+
func testAmazonLinux2X86_64FromContainer() async throws {
476+
try skipSlow()
477+
try await buildTestcases(
478+
config: config.withArchitecture("x86_64").withContainerImageSuffix("amazonlinux2"))
442479
}
443480
}
444481

@@ -453,12 +490,36 @@ final class Swift510_RHELEndToEndTests: XCTestCase {
453490

454491
func testAarch64FromContainer() async throws {
455492
try skipSlow()
456-
try await buildTestcases(config: config.withArchitecture("aarch64").withDocker())
493+
try await buildTestcases(config: config.withArchitecture("aarch64"))
457494
}
458495

459496
func testX86_64FromContainer() async throws {
460497
try skipSlow()
461-
try await buildTestcases(config: config.withArchitecture("x86_64").withDocker())
498+
try await buildTestcases(config: config.withArchitecture("x86_64"))
499+
}
500+
501+
func testAmazonLinux2Aarch64FromContainer() async throws {
502+
try skipSlow()
503+
try await buildTestcases(
504+
config: config.withArchitecture("aarch64").withContainerImageSuffix("amazonlinux2"))
505+
}
506+
507+
func testAmazonLinux2X86_64FromContainer() async throws {
508+
try skipSlow()
509+
try await buildTestcases(
510+
config: config.withArchitecture("x86_64").withContainerImageSuffix("amazonlinux2"))
511+
}
512+
513+
func testFedora39Aarch64FromContainer() async throws {
514+
try skipSlow()
515+
try await buildTestcases(
516+
config: config.withArchitecture("aarch64").withContainerImageSuffix("fedora39"))
517+
}
518+
519+
func testFedora39X86_64FromContainer() async throws {
520+
try skipSlow()
521+
try await buildTestcases(
522+
config: config.withArchitecture("x86_64").withContainerImageSuffix("fedora39"))
462523
}
463524
}
464525

@@ -473,11 +534,35 @@ final class Swift60_RHELEndToEndTests: XCTestCase {
473534

474535
func testAarch64FromContainer() async throws {
475536
try skipSlow()
476-
try await buildTestcases(config: config.withArchitecture("aarch64").withDocker())
537+
try await buildTestcases(config: config.withArchitecture("aarch64"))
477538
}
478539

479540
func testX86_64FromContainer() async throws {
480541
try skipSlow()
481-
try await buildTestcases(config: config.withArchitecture("x86_64").withDocker())
542+
try await buildTestcases(config: config.withArchitecture("x86_64"))
543+
}
544+
545+
func testAmazonLinux2Aarch64FromContainer() async throws {
546+
try skipSlow()
547+
try await buildTestcases(
548+
config: config.withArchitecture("aarch64").withContainerImageSuffix("amazonlinux2"))
549+
}
550+
551+
func testAmazonLinux2X86_64FromContainer() async throws {
552+
try skipSlow()
553+
try await buildTestcases(
554+
config: config.withArchitecture("x86_64").withContainerImageSuffix("amazonlinux2"))
555+
}
556+
557+
func testFedora39Aarch64FromContainer() async throws {
558+
try skipSlow()
559+
try await buildTestcases(
560+
config: config.withArchitecture("aarch64").withContainerImageSuffix("fedora39"))
561+
}
562+
563+
func testFedora39X86_64FromContainer() async throws {
564+
try skipSlow()
565+
try await buildTestcases(
566+
config: config.withArchitecture("x86_64").withContainerImageSuffix("fedora39"))
482567
}
483568
}

0 commit comments

Comments
 (0)