Skip to content

Commit bbe504b

Browse files
authored
Merge pull request #856 from nkcsgexi/infer-macabi-properly-5.5
[5.5] PrebuiltModuleGen: check if an interface is for macabi by reading interface contents
2 parents 5d1a843 + 7e29dcf commit bbe504b

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.
@@ -440,7 +453,7 @@ extension Driver {
440453
commandLine.appendFlag(.parseStdlib)
441454
}
442455
// Add macabi-specific search path.
443-
if isIosMac(inputPath.path.file) {
456+
if try isIosMacInterface(inputPath.path.file) {
444457
commandLine.appendFlag(.Fsystem)
445458
commandLine.append(.path(iosMacFrameworksSearchPath))
446459
}

Tests/SwiftDriverTests/SwiftDriverTests.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4742,6 +4742,17 @@ final class SwiftDriverTests: XCTestCase {
47424742
}
47434743
}
47444744

4745+
func testIsIosMacInterface() throws {
4746+
try withTemporaryFile { file in
4747+
try localFileSystem.writeFileContents(file.path) { $0 <<< "// swift-module-flags: -target x86_64-apple-ios15.0-macabi" }
4748+
XCTAssertTrue(try isIosMacInterface(VirtualPath.absolute(file.path)))
4749+
}
4750+
try withTemporaryFile { file in
4751+
try localFileSystem.writeFileContents(file.path) { $0 <<< "// swift-module-flags: -target arm64e-apple-macos12.0" }
4752+
XCTAssertFalse(try isIosMacInterface(VirtualPath.absolute(file.path)))
4753+
}
4754+
}
4755+
47454756
func testSupportedFeatureJson() throws {
47464757
let driver = try Driver(args: ["swiftc", "-emit-module", "foo.swift"])
47474758
XCTAssertFalse(driver.supportedFrontendFeatures.isEmpty)

0 commit comments

Comments
 (0)