1
+ // #!/usr/bin/env swift
2
+
1
3
import Foundation
2
4
5
+ // Log command and arguments
6
+ let logMessage = """
7
+ \( CommandLine . arguments. joined ( separator: " " ) )
8
+ """
9
+ try logMessage. appendLineToURL ( fileURL: URL ( fileURLWithPath: NSHomeDirectory ( ) ) . appendingPathComponent ( " rulesxcodeproj_ld.log " ) )
10
+
11
+ extension String {
12
+ func appendLineToURL( fileURL: URL ) throws {
13
+ try ( self + " \n " ) . appendToURL ( fileURL: fileURL)
14
+ }
15
+
16
+ func appendToURL( fileURL: URL ) throws {
17
+ let data = self . data ( using: String . Encoding. utf8) !
18
+ if FileManager . default. fileExists ( atPath: fileURL. path) {
19
+ let fileHandle = try FileHandle ( forWritingTo: fileURL)
20
+ fileHandle. seekToEndOfFile ( )
21
+ fileHandle. write ( data)
22
+ fileHandle. closeFile ( )
23
+ } else {
24
+ try self . write ( to: fileURL, atomically: true , encoding: String . Encoding. utf8)
25
+ }
26
+ }
27
+ }
28
+
3
29
// MARK: - Helpers
4
30
5
31
enum PathKey : String {
@@ -24,7 +50,7 @@ func processArgs(
24
50
var paths : [ PathKey : [ URL ] ] = [ : ]
25
51
26
52
var previousArg : String ?
27
- func processArg( _ arg: String ) {
53
+ func processArg( _ arg: String ) throws {
28
54
if let rawPathKey = previousArg,
29
55
let key = PathKey ( rawValue: rawPathKey)
30
56
{
@@ -40,7 +66,8 @@ func processArgs(
40
66
41
67
if arg == " -wmo " || arg == " -whole-module-optimization " {
42
68
isWMO = true
43
- } else if arg. hasSuffix ( " .preview-thunk.swift " ) {
69
+ } else if arg. hasSuffix ( " .preview-thunk.o " ) {
70
+ try " isPreviewThunk " . appendLineToURL ( fileURL: URL ( fileURLWithPath: NSHomeDirectory ( ) ) . appendingPathComponent ( " rulesxcodeproj_ld.log " ) )
44
71
isPreviewThunk = true
45
72
} else {
46
73
previousArg = arg
@@ -53,13 +80,13 @@ func processArgs(
53
80
= URL ( fileURLWithPath: String ( arg. dropFirst ( ) ) )
54
81
for try await line in argumentFileURL. lines {
55
82
if line. hasPrefix ( #"""# ) && line. hasSuffix ( #"""# ) {
56
- processArg ( String ( line. dropFirst ( ) . dropLast ( ) ) )
83
+ try processArg ( String ( line. dropFirst ( ) . dropLast ( ) ) )
57
84
} else {
58
- processArg ( String ( line) )
85
+ try processArg ( String ( line) )
59
86
}
60
87
}
61
88
} else {
62
- processArg ( arg)
89
+ try processArg ( arg)
63
90
}
64
91
}
65
92
@@ -198,11 +225,22 @@ error: Failed to parse DEVELOPER_DIR from '-sdk'. Using /usr/bin/swiftc.
198
225
}
199
226
let developerDir = sdkPath [ range]
200
227
228
+ let processedArgs = args. dropFirst ( ) . map { arg in
229
+ if let range = arg. range ( of: " BazelRulesXcodeProj16B40.xctoolchain " ) {
230
+ let substring = arg [ ..< range. lowerBound]
231
+ return arg. replacingOccurrences (
232
+ of: String ( substring) + " BazelRulesXcodeProj16B40.xctoolchain " ,
233
+ with: " \( developerDir) /Toolchains/XcodeDefault.xctoolchain "
234
+ )
235
+ }
236
+ return arg
237
+ }
238
+ try " \( developerDir) /Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc \( processedArgs. joined ( separator: " " ) ) " . appendLineToURL ( fileURL: URL ( fileURLWithPath: NSHomeDirectory ( ) ) . appendingPathComponent ( " rulesxcodeproj_ld.log " ) )
201
239
try exit ( runSubProcess (
202
240
executable: """
203
241
\( developerDir) /Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc
204
242
""" ,
205
- args: Array ( args . dropFirst ( ) )
243
+ args: Array ( processedArgs )
206
244
) )
207
245
}
208
246
0 commit comments