@@ -16,6 +16,9 @@ import Foundation
1616
1717@PluginExtensionSystemActor public func initializePlugin( _ manager: PluginManager ) {
1818 manager. register ( GenericUnixPlatformSpecsExtension ( ) , type: SpecificationsExtensionPoint . self)
19+ manager. register ( GenericUnixPlatformInfoExtension ( ) , type: PlatformInfoExtensionPoint . self)
20+ manager. register ( GenericUnixSDKRegistryExtension ( ) , type: SDKRegistryExtensionPoint . self)
21+ manager. register ( GenericUnixToolchainRegistryExtension ( ) , type: ToolchainRegistryExtensionPoint . self)
1922}
2023
2124struct GenericUnixPlatformSpecsExtension : SpecificationsExtension {
@@ -27,3 +30,131 @@ struct GenericUnixPlatformSpecsExtension: SpecificationsExtension {
2730 [ " linux " : [ " generic-unix " ] ]
2831 }
2932}
33+
34+ struct GenericUnixPlatformInfoExtension : PlatformInfoExtension {
35+ func additionalPlatforms( context: any PlatformInfoExtensionAdditionalPlatformsContext ) throws -> [ ( path: Path , data: [ String : PropertyListItem ] ) ] {
36+ let operatingSystem = context. hostOperatingSystem
37+ guard operatingSystem. createFallbackSystemToolchain else {
38+ return [ ]
39+ }
40+
41+ return try [
42+ ( . root, [
43+ " Type " : . plString( " Platform " ) ,
44+ " Name " : . plString( operatingSystem. xcodePlatformName) ,
45+ " Identifier " : . plString( operatingSystem. xcodePlatformName) ,
46+ " Description " : . plString( operatingSystem. xcodePlatformName) ,
47+ " FamilyName " : . plString( operatingSystem. xcodePlatformName. capitalized) ,
48+ " FamilyIdentifier " : . plString( operatingSystem. xcodePlatformName) ,
49+ " IsDeploymentPlatform " : . plString( " YES " ) ,
50+ ] )
51+ ]
52+ }
53+ }
54+
55+ struct GenericUnixSDKRegistryExtension : SDKRegistryExtension {
56+ func additionalSDKs( context: any SDKRegistryExtensionAdditionalSDKsContext ) async throws -> [ ( path: Path , platform: SWBCore . Platform ? , data: [ String : PropertyListItem ] ) ] {
57+ let operatingSystem = context. hostOperatingSystem
58+ guard operatingSystem. createFallbackSystemToolchain, let platform = try context. platformRegistry. lookup ( name: operatingSystem. xcodePlatformName) else {
59+ return [ ]
60+ }
61+
62+ let defaultProperties : [ String : PropertyListItem ]
63+ switch operatingSystem {
64+ case . linux:
65+ defaultProperties = [
66+ // Workaround to avoid `-dependency_info` on Linux.
67+ " LD_DEPENDENCY_INFO_FILE " : . plString( " " ) ,
68+
69+ " GENERATE_TEXT_BASED_STUBS " : " NO " ,
70+ " GENERATE_INTERMEDIATE_TEXT_BASED_STUBS " : " NO " ,
71+
72+ " CHOWN " : " /usr/bin/chown " ,
73+ " AR " : " llvm-ar " ,
74+ ]
75+ default :
76+ defaultProperties = [ : ]
77+ }
78+
79+ let tripleEnvironment : String
80+ switch operatingSystem {
81+ case . linux:
82+ tripleEnvironment = " gnu "
83+ default :
84+ tripleEnvironment = " "
85+ }
86+
87+ return try [ ( . root, platform, [
88+ " Type " : . plString( " SDK " ) ,
89+ " Version " : . plString( Version ( ProcessInfo . processInfo. operatingSystemVersion) . zeroTrimmed. description) ,
90+ " CanonicalName " : . plString( operatingSystem. xcodePlatformName) ,
91+ " IsBaseSDK " : . plBool( true ) ,
92+ " DefaultProperties " : . plDict( [
93+ " PLATFORM_NAME " : . plString( operatingSystem. xcodePlatformName) ,
94+ ] . merging ( defaultProperties, uniquingKeysWith: { _, new in new } ) ) ,
95+ " SupportedTargets " : . plDict( [
96+ operatingSystem. xcodePlatformName: . plDict( [
97+ " Archs " : . plArray( [ . plString( Architecture . hostStringValue ?? " unknown " ) ] ) ,
98+ " LLVMTargetTripleEnvironment " : . plString( tripleEnvironment) ,
99+ " LLVMTargetTripleSys " : . plString( operatingSystem. xcodePlatformName) ,
100+ " LLVMTargetTripleVendor " : . plString( " unknown " ) ,
101+ ] )
102+ ] ) ,
103+ ] ) ]
104+ }
105+ }
106+
107+ struct GenericUnixToolchainRegistryExtension : ToolchainRegistryExtension {
108+ func additionalToolchains( context: any ToolchainRegistryExtensionAdditionalToolchainsContext ) async throws -> [ Toolchain ] {
109+ let operatingSystem = context. hostOperatingSystem
110+ guard operatingSystem. createFallbackSystemToolchain else {
111+ return [ ]
112+ }
113+
114+ let fs = context. fs
115+
116+ if let swift = StackedSearchPath ( environment: . current, fs: fs) . lookup ( Path ( " swift " ) ) , fs. exists ( swift) {
117+ let realSwiftPath = try fs. realpath ( swift) . dirname. normalize ( )
118+ let hasUsrBin = realSwiftPath. str. hasSuffix ( " /usr/bin " )
119+ let hasUsrLocalBin = realSwiftPath. str. hasSuffix ( " /usr/local/bin " )
120+ let path : Path
121+ switch ( hasUsrBin, hasUsrLocalBin) {
122+ case ( true , false ) :
123+ path = realSwiftPath. dirname. dirname
124+ case ( false , true ) :
125+ path = realSwiftPath. dirname. dirname. dirname
126+ case ( false , false ) :
127+ throw StubError . error ( " Unexpected toolchain layout for Swift installation path: \( realSwiftPath) " )
128+ case ( true , true ) :
129+ preconditionFailure ( )
130+ }
131+ let llvmDirectories = try Array ( fs. listdir ( Path ( " /usr/lib " ) ) . filter { $0. hasPrefix ( " llvm- " ) } . sorted ( ) . reversed ( ) )
132+ let llvmDirectoriesLocal = try Array ( fs. listdir ( Path ( " /usr/local " ) ) . filter { $0. hasPrefix ( " llvm " ) } . sorted ( ) . reversed ( ) )
133+ return [
134+ Toolchain (
135+ ToolchainRegistry . defaultToolchainIdentifier,
136+ " Default " ,
137+ Version ( ) ,
138+ [ " default " ] ,
139+ path,
140+ [ ] ,
141+ llvmDirectories. map { " /usr/lib/ \( $0) /lib " } + llvmDirectoriesLocal. map { " /usr/local/ \( $0) /lib " } + [ " /usr/lib64 " ] ,
142+ [ : ] ,
143+ [ : ] ,
144+ [ : ] ,
145+ executableSearchPaths: realSwiftPath. dirname. relativeSubpath ( from: path) . map { [ path. join ( $0) . join ( " bin " ) ] } ?? [ ] ,
146+ testingLibraryPlatformNames: [ ] ,
147+ fs: fs)
148+ ]
149+ }
150+
151+ return [ ]
152+ }
153+ }
154+
155+ extension OperatingSystem {
156+ /// Whether the Core is allowed to create a fallback toolchain, SDK, and platform for this operating system in cases where no others have been provided.
157+ var createFallbackSystemToolchain : Bool {
158+ return self == . linux
159+ }
160+ }
0 commit comments