6
6
7
7
See http://swift.org/LICENSE.txt for license information
8
8
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
9
- */
9
+ */
10
10
11
11
import TSCBasic
12
12
import Foundation
@@ -25,45 +25,50 @@ public enum Platform: Equatable {
25
25
}
26
26
27
27
/// Lazily checked current platform.
28
- public static var currentPlatform = Platform . _findCurrentPlatform ( localFileSystem)
28
+ public static var currentPlatform = Platform . findCurrentPlatform ( localFileSystem)
29
+
30
+ /// Returns the cache directories used in Darwin.
31
+ private static var darwinCacheDirectoriesLock = Lock ( )
32
+ private static var _darwinCacheDirectories : [ AbsolutePath ] ? = . none
33
+
29
34
/// Attempt to match `uname` with recognized platforms.
30
- public static func _findCurrentPlatform ( _ fs : FileSystem ) -> Platform ? {
31
- #if os(Windows)
35
+ internal static func findCurrentPlatform ( _ fileSystem : FileSystem ) -> Platform ? {
36
+ #if os(Windows)
32
37
return . windows
33
- #else
38
+ #else
34
39
guard let uname = try ? Process . checkNonZeroExit ( args: " uname " ) . spm_chomp ( ) . lowercased ( ) else { return nil }
35
40
switch uname {
36
41
case " darwin " :
37
42
return . darwin
38
43
case " linux " :
39
- return Platform . _findCurrentPlatformLinux ( fs )
44
+ return Platform . findCurrentPlatformLinux ( fileSystem )
40
45
default :
41
46
return nil
42
47
}
43
- #endif
48
+ #endif
44
49
}
45
50
46
- public static func _findCurrentPlatformLinux ( _ fs : FileSystem ) -> Platform ? {
47
- if fs . isFile ( AbsolutePath ( " /etc/debian_version " ) ) {
51
+ internal static func findCurrentPlatformLinux ( _ fileSystem : FileSystem ) -> Platform ? {
52
+ if fileSystem . isFile ( AbsolutePath ( " /etc/debian_version " ) ) {
48
53
return . linux( . debian)
49
54
}
50
- if fs . isFile ( AbsolutePath ( " /system/bin/toolbox " ) ) ||
51
- fs . isFile ( AbsolutePath ( " /system/bin/toybox " ) ) {
55
+ if fileSystem . isFile ( AbsolutePath ( " /system/bin/toolbox " ) ) ||
56
+ fileSystem . isFile ( AbsolutePath ( " /system/bin/toybox " ) ) {
52
57
return . android
53
58
}
54
- if fs . isFile ( AbsolutePath ( " /etc/redhat-release " ) ) ||
55
- fs . isFile ( AbsolutePath ( " /etc/centos-release " ) ) ||
56
- fs . isFile ( AbsolutePath ( " /etc/fedora-release " ) ) ||
57
- Platform . isAmazonLinux2 ( fs ) {
59
+ if fileSystem . isFile ( AbsolutePath ( " /etc/redhat-release " ) ) ||
60
+ fileSystem . isFile ( AbsolutePath ( " /etc/centos-release " ) ) ||
61
+ fileSystem . isFile ( AbsolutePath ( " /etc/fedora-release " ) ) ||
62
+ Platform . isAmazonLinux2 ( fileSystem ) {
58
63
return . linux( . fedora)
59
64
}
60
65
61
66
return nil
62
67
}
63
68
64
- private static func isAmazonLinux2( _ fs : FileSystem ) -> Bool {
69
+ private static func isAmazonLinux2( _ fileSystem : FileSystem ) -> Bool {
65
70
do {
66
- let release = try fs . readFileContents ( AbsolutePath ( " /etc/system-release " ) ) . cString
71
+ let release = try fileSystem . readFileContents ( AbsolutePath ( " /etc/system-release " ) ) . cString
67
72
return release. hasPrefix ( " Amazon Linux release 2 " )
68
73
} catch {
69
74
return false
@@ -72,23 +77,25 @@ public enum Platform: Equatable {
72
77
73
78
/// Returns the cache directories used in Darwin.
74
79
public static func darwinCacheDirectories( ) -> [ AbsolutePath ] {
75
- if let value = Platform . _darwinCacheDirectories {
76
- return value
80
+ Self . darwinCacheDirectoriesLock. withLock {
81
+ if let darwinCacheDirectories = Self . _darwinCacheDirectories {
82
+ return darwinCacheDirectories
83
+ }
84
+ var directories = [ AbsolutePath] ( )
85
+ // Compute the directories.
86
+ directories. append ( AbsolutePath ( " /private/var/tmp " ) )
87
+ ( try ? TSCBasic . determineTempDirectory ( ) ) . map { directories. append ( $0) }
88
+ #if os(macOS)
89
+ getConfstr ( _CS_DARWIN_USER_TEMP_DIR) . map ( { directories. append ( $0) } )
90
+ getConfstr ( _CS_DARWIN_USER_CACHE_DIR) . map ( { directories. append ( $0) } )
91
+ #endif
92
+ Self . _darwinCacheDirectories = directories
93
+ return directories
77
94
}
78
- var directories = [ AbsolutePath] ( )
79
- // Compute the directories.
80
- directories. append ( AbsolutePath ( " /private/var/tmp " ) )
81
- ( try ? TSCBasic . determineTempDirectory ( ) ) . map { directories. append ( $0) }
82
- #if os(macOS)
83
- getConfstr ( _CS_DARWIN_USER_TEMP_DIR) . map ( { directories. append ( $0) } )
84
- getConfstr ( _CS_DARWIN_USER_CACHE_DIR) . map ( { directories. append ( $0) } )
85
- #endif
86
- Platform . _darwinCacheDirectories = directories
87
- return directories
88
95
}
89
- private static var _darwinCacheDirectories : [ AbsolutePath ] ?
90
96
91
- #if os(macOS)
97
+
98
+ #if os(macOS)
92
99
/// Returns the value of given path variable using `getconf` utility.
93
100
///
94
101
/// - Note: This method returns `nil` if the value is an invalid path.
@@ -101,5 +108,5 @@ public enum Platform: Equatable {
101
108
guard value. hasSuffix ( AbsolutePath . root. pathString) else { return nil }
102
109
return resolveSymlinks ( AbsolutePath ( value) )
103
110
}
104
- #endif
111
+ #endif
105
112
}
0 commit comments