Skip to content

Commit 412bbcd

Browse files
committed
[tibs] Add -sdk to swift compiles on macOS
When compiling with toolchains that do not include the swift stdlib (e.g. the Xcode toolchain), we need the SDK path to be explicitly passed as an argument.
1 parent 91f0a33 commit 412bbcd

File tree

4 files changed

+68
-9
lines changed

4 files changed

+68
-9
lines changed

Sources/ISDBTibs/TibsBuilder.swift

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ public final class TibsBuilder {
6868
emitHeaderPath: clangSources.isEmpty ? nil : "\(name)-Swift.h",
6969
outputFileMap: outputFileMap,
7070
bridgingHeader: bridgingHeader,
71-
moduleDeps: targetDesc.dependencies?.map { "\($0).swiftmodule" } ?? [])
71+
moduleDeps: targetDesc.dependencies?.map { "\($0).swiftmodule" } ?? [],
72+
sdk: TibsBuilder.defaultSDKPath)
7273
}
7374

7475
var clangTUs: [TibsResolvedTarget.ClangTU] = []
@@ -189,6 +190,7 @@ extension TibsBuilder {
189190
"-emit-objc-header-path", $0
190191
] } ?? []
191192
args += module.bridgingHeader.map { ["-import-objc-header", $0.path] } ?? []
193+
args += module.sdk.map { ["-sdk", $0] } ?? []
192194
args += module.extraArgs
193195

194196
// FIXME: handle via 'directory' field?
@@ -285,7 +287,7 @@ extension TibsBuilder {
285287
-output-file-map $OUTPUT_FILE_MAP \
286288
-emit-module -emit-module-path $MODULE_PATH -emit-dependencies \
287289
-pch-output-dir pch -module-cache-path ModuleCache \
288-
$EMIT_HEADER $BRIDGING_HEADER $EXTRA_ARGS \
290+
$EMIT_HEADER $BRIDGING_HEADER $SDK $EXTRA_ARGS \
289291
&& \(toolchain.tibs.path) swift-deps-merge $out $DEP_FILES > $out.d
290292
depfile = $out.d
291293
deps = gcc
@@ -336,6 +338,7 @@ extension TibsBuilder {
336338
EXTRA_ARGS = \(module.extraArgs.joined(separator: " "))
337339
DEP_FILES = \(module.outputFileMap.values.compactMap { $0.dependencies }.joined(separator: " "))
338340
OUTPUT_FILE_MAP = \(module.outputFileMapPath)
341+
SDK = \(module.sdk.map { "-sdk \($0)" } ?? "")
339342
""")
340343
}
341344

@@ -350,3 +353,40 @@ extension TibsBuilder {
350353
""")
351354
}
352355
}
356+
357+
extension TibsBuilder {
358+
359+
/// The default sdk path to use on Darwin (on other platforms, returns nil).
360+
public static var defaultSDKPath: String? = {
361+
#if !os(macOS)
362+
return nil
363+
#else
364+
return xcrunSDKPath()
365+
#endif
366+
}()
367+
}
368+
369+
func xcrunSDKPath() -> String {
370+
let p = Process()
371+
p.launchPath = "/usr/bin/xcrun"
372+
p.arguments = ["--show-sdk-path"]
373+
374+
let out = Pipe()
375+
p.standardOutput = out
376+
377+
p.launch()
378+
p.waitUntilExit()
379+
380+
if p.terminationReason != .exit || p.terminationStatus != 0 {
381+
fatalError("unexpected non-zero exit \(p.terminationStatus) from xcrun --show-sdkpath")
382+
}
383+
384+
let data = out.fileHandleForReading.readDataToEndOfFile()
385+
guard var path = String(data: data, encoding: .utf8) else {
386+
fatalError("invalid output \(data) from xcrun --show-sdkpath")
387+
}
388+
if path.last == "\n" {
389+
path = String(path.dropLast())
390+
}
391+
return path
392+
}

Sources/ISDBTibs/TibsResolvedTarget.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public final class TibsResolvedTarget {
3131
public var bridgingHeader: URL?
3232
public var moduleDeps: [String]
3333
public var importPaths: [String] { moduleDeps.isEmpty ? [] : ["."] }
34+
public var sdk: String?
3435

3536
public init(
3637
name: String,
@@ -40,7 +41,8 @@ public final class TibsResolvedTarget {
4041
emitHeaderPath: String? = nil,
4142
outputFileMap: OutputFileMap,
4243
bridgingHeader: URL? = nil,
43-
moduleDeps: [String] = [])
44+
moduleDeps: [String] = [],
45+
sdk: String? = nil)
4446
{
4547
self.name = name
4648
self.extraArgs = extraArgs
@@ -50,6 +52,7 @@ public final class TibsResolvedTarget {
5052
self.outputFileMap = outputFileMap
5153
self.bridgingHeader = bridgingHeader
5254
self.moduleDeps = moduleDeps
55+
self.sdk = sdk
5356
}
5457
}
5558

Tests/ISDBTibsTests/TibsCompilationDatabaseTests.swift

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ final class TibsCompilationDatabaseTests: XCTestCase {
3131
let build = URL(fileURLWithPath: "/build", isDirectory: true)
3232
let builder = try TibsBuilder(manifest: m, sourceRoot: src, buildRoot: build, toolchain: tc)
3333

34+
let sdkargs = TibsBuilder.defaultSDKPath.map { ["-sdk", $0] } ?? []
35+
3436
let expected = JSONCompilationDatabase(commands: [
3537
Command(
3638
directory: "/build",
@@ -43,8 +45,10 @@ final class TibsCompilationDatabaseTests: XCTestCase {
4345
"-emit-module", "-emit-module-path", "A.swiftmodule",
4446
"-emit-dependencies",
4547
"-pch-output-dir", "pch",
46-
"-module-cache-path", "ModuleCache",
47-
"-working-directory", "/build"]),
48+
"-module-cache-path", "ModuleCache"
49+
] + sdkargs + [
50+
"-working-directory", "/build"
51+
]),
4852
Command(
4953
directory: "/build",
5054
file: "/src/b.swift",
@@ -57,8 +61,10 @@ final class TibsCompilationDatabaseTests: XCTestCase {
5761
"-emit-module", "-emit-module-path", "B.swiftmodule",
5862
"-emit-dependencies",
5963
"-pch-output-dir", "pch",
60-
"-module-cache-path", "ModuleCache",
61-
"-working-directory", "/build"]),
64+
"-module-cache-path", "ModuleCache"
65+
] + sdkargs + [
66+
"-working-directory", "/build"
67+
]),
6268
Command(
6369
directory: "/build",
6470
file: "/src/c.swift",
@@ -70,8 +76,10 @@ final class TibsCompilationDatabaseTests: XCTestCase {
7076
"-emit-module", "-emit-module-path", "C.swiftmodule",
7177
"-emit-dependencies",
7278
"-pch-output-dir", "pch",
73-
"-module-cache-path", "ModuleCache",
74-
"-working-directory", "/build"]),
79+
"-module-cache-path", "ModuleCache"
80+
] + sdkargs + [
81+
"-working-directory", "/build"
82+
]),
7583
])
7684

7785
XCTAssertEqual(builder.compilationDatabase, expected)
@@ -85,6 +93,8 @@ final class TibsCompilationDatabaseTests: XCTestCase {
8593
let build = URL(fileURLWithPath: "/build", isDirectory: true)
8694
let builder = try TibsBuilder(manifest: m, sourceRoot: src, buildRoot: build, toolchain: tc)
8795

96+
let sdkargs = TibsBuilder.defaultSDKPath.map { ["-sdk", $0] } ?? []
97+
8898
let swiftArgs = [
8999
"/swiftc", "/src/a.swift", "/src/b.swift",
90100
"-module-name", "main",
@@ -96,6 +106,7 @@ final class TibsCompilationDatabaseTests: XCTestCase {
96106
"-module-cache-path", "ModuleCache",
97107
"-emit-objc-header", "-emit-objc-header-path", "main-Swift.h",
98108
"-import-objc-header", "/src/bridging-header.h",
109+
] + sdkargs + [
99110
"-Xcc", "-Wno-objc-root-class",
100111
"-working-directory", "/build"
101112
]

Tests/ISDBTibsTests/TibsResolutionTests.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ final class TibsResolutionTests: XCTestCase {
5353
src.appendingPathComponent("b.swift", isDirectory: false),
5454
src.appendingPathComponent("rec/c.swift" , isDirectory: false),
5555
])
56+
#if os(macOS)
57+
XCTAssertNotNil(module.sdk)
58+
#else
59+
XCTAssertNil(module.sdk)
60+
#endif
5661
}
5762

5863
func testResolutionMixedLangTarget() throws {

0 commit comments

Comments
 (0)