Skip to content

Commit 121f1ad

Browse files
author
Karim Alweheshy
committed
Add linked libraries and search paths to generated xcodeproj targets
1 parent 93a4a17 commit 121f1ad

File tree

1 file changed

+44
-6
lines changed

1 file changed

+44
-6
lines changed

tools/swiftc_stub/main.swift

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,31 @@
1+
// #!/usr/bin/env swift
2+
13
import Foundation
24

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+
329
// MARK: - Helpers
430

531
enum PathKey: String {
@@ -24,7 +50,7 @@ func processArgs(
2450
var paths: [PathKey: [URL]] = [:]
2551

2652
var previousArg: String?
27-
func processArg(_ arg: String) {
53+
func processArg(_ arg: String) throws{
2854
if let rawPathKey = previousArg,
2955
let key = PathKey(rawValue: rawPathKey)
3056
{
@@ -40,7 +66,8 @@ func processArgs(
4066

4167
if arg == "-wmo" || arg == "-whole-module-optimization" {
4268
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"))
4471
isPreviewThunk = true
4572
} else {
4673
previousArg = arg
@@ -53,13 +80,13 @@ func processArgs(
5380
= URL(fileURLWithPath: String(arg.dropFirst()))
5481
for try await line in argumentFileURL.lines {
5582
if line.hasPrefix(#"""#) && line.hasSuffix(#"""#) {
56-
processArg(String(line.dropFirst().dropLast()))
83+
try processArg(String(line.dropFirst().dropLast()))
5784
} else {
58-
processArg(String(line))
85+
try processArg(String(line))
5986
}
6087
}
6188
} else {
62-
processArg(arg)
89+
try processArg(arg)
6390
}
6491
}
6592

@@ -198,11 +225,22 @@ error: Failed to parse DEVELOPER_DIR from '-sdk'. Using /usr/bin/swiftc.
198225
}
199226
let developerDir = sdkPath[range]
200227

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"))
201239
try exit(runSubProcess(
202240
executable: """
203241
\(developerDir)/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc
204242
""",
205-
args: Array(args.dropFirst())
243+
args: Array(processedArgs)
206244
))
207245
}
208246

0 commit comments

Comments
 (0)