@@ -62,6 +62,38 @@ func processArgs(
6262 )
6363}
6464
65+ let swiftcPath : String = {
66+ guard let path = ProcessInfo . processInfo. environment [ " PATH " ] else {
67+ fputs ( " error: PATH not set " , stderr)
68+ exit ( 1 )
69+ }
70+
71+ var result : String ?
72+
73+ if let toolchainDir = ProcessInfo . processInfo. environment [ " TOOLCHAIN_DIR " ] {
74+ result = " \( toolchainDir) /usr/bin/swiftc "
75+ } else {
76+ // /Applications/Xcode-15.0.0-Beta.app/Contents/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin -> /Applications/Xcode-15.0.0-Beta.app/Contents/Developer/usr/bin
77+ let pathComponents = path. split ( separator: " : " , maxSplits: 1 )
78+ let xcodeBinPath = pathComponents [ 0 ]
79+ guard xcodeBinPath. hasSuffix ( " /Contents/Developer/usr/bin " ) else {
80+ fputs ( " Xcode based bin PATH not set \( path) " , stderr)
81+ fputs ( " error: Xcode based bin PATH not set " , stderr)
82+ exit ( 1 )
83+ }
84+ // /Applications/Xcode-15.0.0-Beta.app/Contents/Developer/usr/bin -> /Applications/Xcode-15.0.0-Beta.app/Contents/Developer
85+ let developerDir = xcodeBinPath. dropLast ( 8 )
86+ result = " \( developerDir) /Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc "
87+ }
88+
89+ guard let result else {
90+ fputs ( " error: Failed to determine swiftc path " , stderr)
91+ exit ( 1 )
92+ }
93+
94+ return result
95+ } ( )
96+
6597extension URL {
6698 mutating func touch( ) throws {
6799 let fileManager = FileManager . default
@@ -138,37 +170,6 @@ func runSubProcess(executable: String, args: [String]) throws -> Int32 {
138170}
139171
140172func handleXcodePreviewThunk( args: [ String ] , paths: [ PathKey : URL ] ) throws -> Never {
141- var swiftcPath : String ?
142-
143- if let toolchainDir = ProcessInfo . processInfo. environment [ " TOOLCHAIN_DIR " ] {
144- swiftcPath = " \( toolchainDir) /usr/bin/swiftc "
145- } else {
146- guard let sdkPath = paths [ PathKey . sdk] ? . path else {
147- fputs ( " error: No such argument '-sdk'. Using /usr/bin/swiftc. " , stderr)
148- exit ( 1 )
149- }
150-
151- // We could produce this file at the start of the build?
152- let fullRange = NSRange ( sdkPath. startIndex... , in: sdkPath)
153- let matches = try NSRegularExpression (
154- pattern: #"(.*?/Contents/Developer)/.*"#
155- ) . matches ( in: sdkPath, range: fullRange)
156- guard let match = matches. first,
157- let range = Range ( match. range ( at: 1 ) , in: sdkPath)
158- else {
159- fputs ( " error: Failed to parse DEVELOPER_DIR from '-sdk'. Using /usr/bin/swiftc. " , stderr)
160- exit ( 1 )
161- }
162- let developerDir = sdkPath [ range]
163-
164- swiftcPath = " \( developerDir) /Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc "
165- }
166-
167- guard let swiftcPath else {
168- fputs ( " error: Failed to determine swiftc path " , stderr)
169- exit ( 1 )
170- }
171-
172173 try exit ( runSubProcess ( executable: swiftcPath, args: Array ( args. dropFirst ( ) ) ) )
173174}
174175
@@ -177,37 +178,6 @@ func handleXcodePreviewThunk(args: [String], paths: [PathKey: URL]) throws -> Ne
177178let args = CommandLine . arguments
178179// Xcode 16.0 Beta 3 began using "--version" over "-v". Support both.
179180if args. count == 2 , args. last == " --version " || args. last == " -v " {
180- guard let path = ProcessInfo . processInfo. environment [ " PATH " ] else {
181- fputs ( " error: PATH not set " , stderr)
182- exit ( 1 )
183- }
184-
185- var swiftcPath : String ?
186-
187- if let toolchainDir = ProcessInfo . processInfo. environment [ " TOOLCHAIN_DIR " ] {
188- swiftcPath = """
189- \( toolchainDir) /usr/bin/swiftc
190- """
191- } else {
192- // /Applications/Xcode-15.0.0-Beta.app/Contents/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin -> /Applications/Xcode-15.0.0-Beta.app/Contents/Developer/usr/bin
193- let pathComponents = path. split ( separator: " : " , maxSplits: 1 )
194- let xcodeBinPath = pathComponents [ 0 ]
195- guard xcodeBinPath. hasSuffix ( " /Contents/Developer/usr/bin " ) else {
196- fputs ( " error: Xcode based bin PATH not set " , stderr)
197- exit ( 1 )
198- }
199- // /Applications/Xcode-15.0.0-Beta.app/Contents/Developer/usr/bin -> /Applications/Xcode-15.0.0-Beta.app/Contents/Developer
200- let developerDir = xcodeBinPath. dropLast ( 8 )
201- swiftcPath = """
202- \( developerDir) /Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc
203- """
204- }
205-
206- guard let swiftcPath else {
207- fputs ( " error: Failed to determine swiftc path " , stderr)
208- exit ( 1 )
209- }
210-
211181 // args.last allows passing in -v (Xcode < 16b3) and --version (>= 16b3)
212182 try exit ( runSubProcess ( executable: swiftcPath, args: [ args. last!] ) )
213183}
0 commit comments