Skip to content

Commit d1e1d31

Browse files
committed
PrebuiltModuleGen: check if an interface is for macabi by reading interface contents
Previously, we were using the file name to infer whether an interface is for macabi. However, this seems to be inaccurate because some macabi interfaces don't have "-macabi" in the file names. This patch teaches the driver to open interface files and check module target specifically.
1 parent 37d920c commit d1e1d31

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

Sources/SwiftDriver/Jobs/PrebuiltModulesJob.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,19 @@
1212
import TSCBasic
1313
import SwiftOptions
1414

15+
@_spi(Testing) public func isIosMacInterface(_ path: VirtualPath) throws -> Bool {
16+
let data = try localFileSystem.readFileContents(path).cString
17+
let myStrings = data.components(separatedBy: .newlines)
18+
let prefix = "// swift-module-flags: "
19+
if let argLine = myStrings.first(where: { $0.hasPrefix(prefix) }) {
20+
let args = argLine.dropFirst(prefix.count).components(separatedBy: " ")
21+
if let idx = args.firstIndex(of: "-target"), idx + 1 < args.count {
22+
return args[idx + 1].contains("macabi")
23+
}
24+
}
25+
return false
26+
}
27+
1528
func isIosMac(_ path: VirtualPath) -> Bool {
1629
// Infer macabi interfaces by the file name.
1730
// FIXME: more robust way to do this.
@@ -432,7 +445,7 @@ extension Driver {
432445
commandLine.appendFlag(.parseStdlib)
433446
}
434447
// Add macabi-specific search path.
435-
if isIosMac(inputPath.path.file) {
448+
if try isIosMacInterface(inputPath.path.file) {
436449
commandLine.appendFlag(.Fsystem)
437450
commandLine.append(.path(iosMacFrameworksSearchPath))
438451
}

Tests/SwiftDriverTests/SwiftDriverTests.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5030,6 +5030,17 @@ final class SwiftDriverTests: XCTestCase {
50305030
}
50315031
}
50325032

5033+
func testIsIosMacInterface() throws {
5034+
try withTemporaryFile { file in
5035+
try localFileSystem.writeFileContents(file.path) { $0 <<< "// swift-module-flags: -target x86_64-apple-ios15.0-macabi" }
5036+
XCTAssertTrue(try isIosMacInterface(VirtualPath.absolute(file.path)))
5037+
}
5038+
try withTemporaryFile { file in
5039+
try localFileSystem.writeFileContents(file.path) { $0 <<< "// swift-module-flags: -target arm64e-apple-macos12.0" }
5040+
XCTAssertFalse(try isIosMacInterface(VirtualPath.absolute(file.path)))
5041+
}
5042+
}
5043+
50335044
func testSupportedFeatureJson() throws {
50345045
let driver = try Driver(args: ["swiftc", "-emit-module", "foo.swift"])
50355046
XCTAssertFalse(driver.supportedFrontendFeatures.isEmpty)

0 commit comments

Comments
 (0)