Skip to content
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

Add a global variable cpuArch #216

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 12 additions & 18 deletions Sources/LinuxPlatform/Linux.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ var swiftGPGKeysRefreshed = false
/// This implementation can be reused for any supported Linux platform.
/// TODO: replace dummy implementations
public struct Linux: Platform {
let linuxPlatforms = [
PlatformDefinition.ubuntu2404,
PlatformDefinition.ubuntu2204,
PlatformDefinition.ubuntu2004,
PlatformDefinition.ubuntu1804,
PlatformDefinition.fedora39,
PlatformDefinition.rhel9,
PlatformDefinition.amazonlinux2,
PlatformDefinition.debian12,
let linuxPlatforms: [PlatformDefinition] = [
.ubuntu2404,
.ubuntu2204,
.ubuntu2004,
.ubuntu1804,
.fedora39,
.rhel9,
.amazonlinux2,
.debian12,
]

public init() {}
Expand Down Expand Up @@ -382,13 +382,7 @@ public struct Linux: Platform {
}

public func getExecutableName() -> String {
#if arch(x86_64)
let arch = "x86_64"
#elseif arch(arm64)
let arch = "aarch64"
#else
fatalError("Unsupported processor architecture")
#endif
let arch = cpuArch

return "swiftly-\(arch)-unknown-linux-gnu"
}
Expand Down Expand Up @@ -516,7 +510,7 @@ public struct Linux: Platform {
return await self.manualSelectPlatform(platformPretty)
}

return PlatformDefinition.amazonlinux2
return .amazonlinux2
} else if (id + (idlike ?? "")).contains("rhel") {
guard versionID.hasPrefix("9") else {
let message = "Unsupported version of RHEL"
Expand All @@ -528,7 +522,7 @@ public struct Linux: Platform {
return await self.manualSelectPlatform(platformPretty)
}

return PlatformDefinition.rhel9
return .rhel9
} else if let pd = [PlatformDefinition.ubuntu1804, .ubuntu2004, .ubuntu2204, .ubuntu2404, .debian12, .fedora39].first(where: { $0.name == id + versionID }) {
return pd
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/MacOSPlatform/MacOS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public struct MacOS: Platform {

public func detectPlatform(disableConfirmation _: Bool, platform _: String?) async -> PlatformDefinition {
// No special detection required on macOS platform
PlatformDefinition.macOS
.macOS
}

public func getShell() async throws -> String {
Expand Down
24 changes: 2 additions & 22 deletions Sources/SwiftlyCore/HTTPClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -295,17 +295,7 @@ public struct SwiftlyHTTPClient {
limit: Int? = nil,
filter: ((ToolchainVersion.StableRelease) -> Bool)? = nil
) async throws -> [ToolchainVersion.StableRelease] {
let arch = if a == nil {
#if arch(x86_64)
"x86_64"
#elseif arch(arm64)
"aarch64"
#else
#error("Unsupported processor architecture")
#endif
} else {
a!
}
let arch = a ?? cpuArch

let url = "https://www.swift.org/api/v1/install/releases.json"
let swiftOrgReleases: [SwiftOrgRelease] = try await self.getFromJSON(url: url, type: [SwiftOrgRelease].self)
Expand Down Expand Up @@ -359,17 +349,7 @@ public struct SwiftlyHTTPClient {
limit: Int? = nil,
filter: ((ToolchainVersion.Snapshot) -> Bool)? = nil
) async throws -> [ToolchainVersion.Snapshot] {
let arch = if a == nil {
#if arch(x86_64)
"x86_64"
#elseif arch(arm64)
"aarch64"
#else
#error("Unsupported processor architecture")
#endif
} else {
a!
}
let arch = a ?? cpuArch

let platformName = if platform.name == PlatformDefinition.macOS.name {
"macos"
Expand Down
8 changes: 8 additions & 0 deletions Sources/SwiftlyCore/SwiftlyCore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,11 @@ public func readLine(prompt: String) -> String? {
}
return provider.readLine()
}

#if arch(x86_64)
public let cpuArch = "x86_64"
#elseif arch(arm64)
public let cpuArch = "aarch64"
#else
#error("Unsupported processor architecture")
#endif
34 changes: 17 additions & 17 deletions Tests/SwiftlyTests/HTTPClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,27 +51,27 @@ final class HTTPClientTests: SwiftlyTests {
}

func testGetToolchainMetdataFromSwiftOrg() async throws {
let supportedPlatforms = [
PlatformDefinition.macOS,
PlatformDefinition.ubuntu2404,
PlatformDefinition.ubuntu2204,
PlatformDefinition.ubuntu2004,
// PlatformDefinition.ubuntu1804, // There are no releases for Ubuntu 18.04 in the branches being tested below
PlatformDefinition.rhel9,
PlatformDefinition.fedora39,
PlatformDefinition.amazonlinux2,
PlatformDefinition.debian12,
let supportedPlatforms: [PlatformDefinition] = [
.macOS,
.ubuntu2404,
.ubuntu2204,
.ubuntu2004,
// .ubuntu1804, // There are no releases for Ubuntu 18.04 in the branches being tested below
.rhel9,
.fedora39,
.amazonlinux2,
.debian12,
]

let newPlatforms = [
PlatformDefinition.ubuntu2404,
PlatformDefinition.fedora39,
PlatformDefinition.debian12,
let newPlatforms: [PlatformDefinition] = [
.ubuntu2404,
.fedora39,
.debian12,
]

let branches = [
ToolchainVersion.Snapshot.Branch.main,
ToolchainVersion.Snapshot.Branch.release(major: 6, minor: 0), // This is available in swift.org API
let branches: [ToolchainVersion.Snapshot.Branch] = [
.main,
.release(major: 6, minor: 0), // This is available in swift.org API
]

for arch in ["x86_64", "aarch64"] {
Expand Down
8 changes: 4 additions & 4 deletions Tests/SwiftlyTests/SwiftlyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -331,10 +331,10 @@ class SwiftlyTests: XCTestCase {

// Snapshots are currently unavailable for these platforms on swift.org
// TODO: remove these once snapshots are available for them
let snapshotsUnavailable = [
PlatformDefinition.ubuntu2404,
PlatformDefinition.fedora39,
PlatformDefinition.debian12,
let snapshotsUnavailable: [PlatformDefinition] = [
.ubuntu2404,
.fedora39,
.debian12,
]

return !snapshotsUnavailable.contains(pd)
Expand Down