@@ -138,39 +138,38 @@ func runSubProcess(executable: String, args: [String]) throws -> Int32 {
138138}
139139
140140func handleXcodePreviewThunk( args: [ String ] , paths: [ PathKey : URL ] ) throws -> Never {
141- guard let sdkPath = paths [ PathKey . sdk] ? . path else {
142- fputs (
143- " error: No such argument '-sdk'. Using /usr/bin/swiftc. " ,
144- stderr
145- )
146- exit ( 1 )
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 "
147165 }
148166
149- // TODO: Make this work with custom toolchains
150- // We could produce this file at the start of the build?
151- let fullRange = NSRange ( sdkPath. startIndex... , in: sdkPath)
152- let matches = try NSRegularExpression (
153- pattern: #"(.*?/Contents/Developer)/.*"#
154- ) . matches ( in: sdkPath, range: fullRange)
155- guard let match = matches. first,
156- let range = Range ( match. range ( at: 1 ) , in: sdkPath)
157- else {
158- fputs (
159- """
160- error: Failed to parse DEVELOPER_DIR from '-sdk'. Using /usr/bin/swiftc.
161- """ ,
162- stderr
163- )
167+ guard let swiftcPath else {
168+ fputs ( " error: Failed to determine swiftc path " , stderr)
164169 exit ( 1 )
165170 }
166- let developerDir = sdkPath [ range]
167-
168- try exit ( runSubProcess (
169- executable: """
170- \( developerDir) /Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc
171- """ ,
172- args: Array ( args. dropFirst ( ) )
173- ) )
171+
172+ try exit ( runSubProcess ( executable: swiftcPath, args: Array ( args. dropFirst ( ) ) ) )
174173}
175174
176175// MARK: - Main
@@ -183,21 +182,31 @@ if args.count == 2, args.last == "--version" || args.last == "-v" {
183182 exit ( 1 )
184183 }
185184
186- // /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
187- let pathComponents = path. split ( separator: " : " , maxSplits: 1 )
188- let xcodeBinPath = pathComponents [ 0 ]
189- guard xcodeBinPath. hasSuffix ( " /Contents/Developer/usr/bin " ) else {
190- fputs ( " error: Xcode based bin PATH not set " , stderr)
191- exit ( 1 )
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+ """
192204 }
193205
194- // /Applications/Xcode-15.0.0-Beta.app/Contents/Developer/usr/bin -> /Applications/Xcode-15.0.0-Beta.app/Contents/Developer
195- let developerDir = xcodeBinPath. dropLast ( 8 )
196-
197- // TODO: Make this work with custom toolchains
198- let swiftcPath = """
199- \( developerDir) /Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc
200- """
206+ guard let swiftcPath else {
207+ fputs ( " error: Failed to determine swiftc path " , stderr)
208+ exit ( 1 )
209+ }
201210
202211 // args.last allows passing in -v (Xcode < 16b3) and --version (>= 16b3)
203212 try exit ( runSubProcess ( executable: swiftcPath, args: [ args. last!] ) )
0 commit comments