Skip to content

Commit 50f9480

Browse files
authored
Merge pull request #2647 from drexin/wip-add-amazon-linux
Add platform detection for Amazon Linux 2
2 parents b3e68e2 + c537dca commit 50f9480

File tree

2 files changed

+75
-16
lines changed

2 files changed

+75
-16
lines changed

swift-tools-support-core/Sources/TSCUtility/Platform.swift

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,44 +12,59 @@ import TSCBasic
1212
import Foundation
1313

1414
/// Recognized Platform types.
15-
public enum Platform {
15+
public enum Platform: Equatable {
1616
case android
1717
case darwin
1818
case linux(LinuxFlavor)
1919

2020
/// Recognized flavors of linux.
21-
public enum LinuxFlavor {
21+
public enum LinuxFlavor: Equatable {
2222
case debian
2323
case fedora
2424
}
2525

2626
/// Lazily checked current platform.
27-
public static var currentPlatform = Platform.findCurrentPlatform()
27+
public static var currentPlatform = Platform._findCurrentPlatform(localFileSystem)
2828
/// Attempt to match `uname` with recognized platforms.
29-
private static func findCurrentPlatform() -> Platform? {
29+
public static func _findCurrentPlatform(_ fs: FileSystem) -> Platform? {
3030
guard let uname = try? Process.checkNonZeroExit(args: "uname").spm_chomp().lowercased() else { return nil }
3131
switch uname {
3232
case "darwin":
3333
return .darwin
3434
case "linux":
35-
if localFileSystem.isFile(AbsolutePath("/etc/debian_version")) {
36-
return .linux(.debian)
37-
}
38-
if localFileSystem.isFile(AbsolutePath("/system/bin/toolbox")) ||
39-
localFileSystem.isFile(AbsolutePath("/system/bin/toybox")) {
40-
return .android
41-
}
42-
if localFileSystem.isFile(AbsolutePath("/etc/redhat-release")) ||
43-
localFileSystem.isFile(AbsolutePath("/etc/centos-release")) ||
44-
localFileSystem.isFile(AbsolutePath("/etc/fedora-release")) {
45-
return .linux(.fedora)
46-
}
35+
return Platform._findCurrentPlatformLinux(fs)
4736
default:
4837
return nil
4938
}
39+
}
40+
41+
public static func _findCurrentPlatformLinux(_ fs: FileSystem) -> Platform? {
42+
if fs.isFile(AbsolutePath("/etc/debian_version")) {
43+
return .linux(.debian)
44+
}
45+
if fs.isFile(AbsolutePath("/system/bin/toolbox")) ||
46+
fs.isFile(AbsolutePath("/system/bin/toybox")) {
47+
return .android
48+
}
49+
if fs.isFile(AbsolutePath("/etc/redhat-release")) ||
50+
fs.isFile(AbsolutePath("/etc/centos-release")) ||
51+
fs.isFile(AbsolutePath("/etc/fedora-release")) ||
52+
Platform.isAmazonLinux2(fs) {
53+
return .linux(.fedora)
54+
}
55+
5056
return nil
5157
}
5258

59+
private static func isAmazonLinux2(_ fs: FileSystem) -> Bool {
60+
do {
61+
let release = try fs.readFileContents(AbsolutePath("/etc/system-release")).cString
62+
return release.hasPrefix("Amazon Linux release 2")
63+
} catch {
64+
return false
65+
}
66+
}
67+
5368
/// Returns the cache directories used in Darwin.
5469
public static func darwinCacheDirectories() -> [AbsolutePath] {
5570
if let value = Platform._darwinCacheDirectories {
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
This source file is part of the Swift.org open source project
3+
4+
Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See http://swift.org/LICENSE.txt for license information
8+
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
9+
*/
10+
11+
import XCTest
12+
import TSCBasic
13+
import TSCTestSupport
14+
15+
import TSCUtility
16+
17+
final class PlatformTests: XCTestCase {
18+
func testFindCurrentPlatformDebian() {
19+
let fs = InMemoryFileSystem(files: ["/etc/debian_version": "xxx"])
20+
XCTAssertEqual(Platform.linux(.debian), Platform._findCurrentPlatformLinux(fs))
21+
}
22+
23+
func testFindCurrentPlatformAndroid() {
24+
var fs = InMemoryFileSystem(files: ["/system/bin/toolbox": "xxx"])
25+
XCTAssertEqual(Platform.android, Platform._findCurrentPlatformLinux(fs))
26+
27+
fs = InMemoryFileSystem(files: ["/system/bin/toybox": "xxx"])
28+
XCTAssertEqual(Platform.android, Platform._findCurrentPlatformLinux(fs))
29+
}
30+
31+
func testFindCurrentPlatformFedora() {
32+
var fs = InMemoryFileSystem(files: ["/etc/fedora-release": "xxx"])
33+
XCTAssertEqual(Platform.linux(.fedora), Platform._findCurrentPlatformLinux(fs))
34+
35+
fs = InMemoryFileSystem(files: ["/etc/redhat-release": "xxx"])
36+
XCTAssertEqual(Platform.linux(.fedora), Platform._findCurrentPlatformLinux(fs))
37+
38+
fs = InMemoryFileSystem(files: ["/etc/centos-release": "xxx"])
39+
XCTAssertEqual(Platform.linux(.fedora), Platform._findCurrentPlatformLinux(fs))
40+
41+
fs = InMemoryFileSystem(files: ["/etc/system-release": "Amazon Linux release 2 (Karoo)"])
42+
XCTAssertEqual(Platform.linux(.fedora), Platform._findCurrentPlatformLinux(fs))
43+
}
44+
}

0 commit comments

Comments
 (0)