Skip to content

Add support for building Swift SDKs for armv7 with target Swift toolchain or docker container #170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Feb 11, 2025
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ extension SwiftSDKGenerator {
// But not in all containers, so don't fail if it does not exist.
if case .ubuntu = targetDistribution {
subpaths += [("\(targetTriple.archName)-linux-gnu", false)]

// Custom subpath for armv7
if targetTriple.archName == "armv7" {
subpaths += [("arm-linux-gnueabihf", false)]
}
}

for (subpath, failIfNotExists) in subpaths {
Expand Down Expand Up @@ -104,13 +109,16 @@ extension SwiftSDKGenerator {
func copyTargetSwift(from distributionPath: FilePath, sdkDirPath: FilePath) async throws {
logger.info("Copying Swift core libraries for the target triple into Swift SDK bundle...")

for (pathWithinPackage, pathWithinSwiftSDK) in [
("lib/swift", sdkDirPath.appending("usr/lib")),
("lib/swift_static", sdkDirPath.appending("usr/lib")),
("lib/clang", sdkDirPath.appending("usr/lib")),
("include", sdkDirPath.appending("usr")),
for (pathWithinPackage, pathWithinSwiftSDK, ignoreIfMissing) in [
("lib/swift", sdkDirPath.appending("usr/lib"), false),
("lib/swift_static", sdkDirPath.appending("usr/lib"), false),
("lib/clang", sdkDirPath.appending("usr/lib"), true),
("include", sdkDirPath.appending("usr"), false),
] {
try await rsync(from: distributionPath.appending(pathWithinPackage), to: pathWithinSwiftSDK)
try await rsync(
from: distributionPath.appending(pathWithinPackage),
to: pathWithinSwiftSDK, ignoreIfMissing: ignoreIfMissing
)
}
}
}
Expand All @@ -120,6 +128,7 @@ extension Triple {
switch self.archName {
case "x86_64": return "/lib64/ld-linux-x86-64.so.2"
case "aarch64": return "/lib/ld-linux-aarch64.so.1"
case "armv7": return "/lib/ld-linux-armhf.so.3"
default: fatalError("unsupported architecture \(self.archName)")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public extension Triple.Arch {
case .aarch64: return "arm64"
case .x86_64: return "amd64"
case .wasm32: return "wasm32"
case .arm: return "armhf"
default: fatalError("\(self) is not supported yet")
}
}
Expand Down
5 changes: 3 additions & 2 deletions Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,10 @@ public actor SwiftSDKGenerator {
try Data(contentsOf: URL(fileURLWithPath: path.string))
}

func rsync(from source: FilePath, to destination: FilePath) async throws {
func rsync(from source: FilePath, to destination: FilePath, ignoreIfMissing: Bool = false) async throws {
try self.createDirectoryIfNeeded(at: destination)
try await Shell.run("rsync -a \(source) \(destination)", shouldLogCommands: self.isVerbose)
let ignoreMissingArgs = ignoreIfMissing ? "--ignore-missing-args" : ""
try await Shell.run("rsync -a \(ignoreMissingArgs) \(source) \(destination)", shouldLogCommands: self.isVerbose)
}

func rsyncContents(from source: FilePath, to destination: FilePath) async throws {
Expand Down
1 change: 1 addition & 0 deletions Sources/SwiftSDKGenerator/PlatformModels/Triple.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ extension Triple.Arch {
case .aarch64: return "aarch64"
case .x86_64: return "x86_64"
case .wasm32: return "wasm32"
case .arm: return "arm"
default: fatalError("\(self) is not supported yet")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public struct LinuxRecipe: SwiftSDKRecipe {
self.linuxDistribution
.release
)_\(
self.mainTargetTriple.arch!.linuxConventionName
self.mainTargetTriple.archName
)
"""
}
Expand Down Expand Up @@ -217,6 +217,12 @@ public struct LinuxRecipe: SwiftSDKRecipe {
engine: QueryEngine,
httpClient client: some HTTPClientProtocol
) async throws -> SwiftSDKProduct {
if self.linuxDistribution.name == .rhel && self.mainTargetTriple.archName == "armv7" {
throw GeneratorError.distributionDoesNotSupportArchitecture(
self.linuxDistribution, targetArchName: self.mainTargetTriple.archName
)
}

let sdkDirPath = self.sdkDirPath(paths: generator.pathsConfiguration)
if !generator.isIncremental {
try await generator.removeRecursively(at: sdkDirPath)
Expand Down
5 changes: 5 additions & 0 deletions Sources/SwiftSDKGenerator/SystemUtils/GeneratorError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ enum GeneratorError: Error {
case unknownCPUArchitecture(String)
case unknownLLDVersion(String)
case distributionSupportsOnlyDockerGenerator(LinuxDistribution)
case distributionDoesNotSupportArchitecture(LinuxDistribution, targetArchName: String)
case fileDoesNotExist(FilePath)
case fileDownloadFailed(URL, String)
case ubuntuPackagesDecompressionFailure
Expand Down Expand Up @@ -50,6 +51,10 @@ extension GeneratorError: CustomStringConvertible {
Target Linux distribution \(linuxDistribution) supports Swift SDK generation only when `--with-docker` flag is \
passed.
"""
case let .distributionDoesNotSupportArchitecture(linuxDistribution, targetArchName):
return """
Target Linux distribution \(linuxDistribution) does not support the target architecture: \(targetArchName)
"""
case let .fileDoesNotExist(filePath):
return "Expected to find a file at path `\(filePath)`."
case let .fileDownloadFailed(url, status):
Expand Down