Skip to content

Commit f163a58

Browse files
[windows] add a check for a matching python architecture
1 parent 03f4816 commit f163a58

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

Sources/SwiftDriver/Driver/Driver.swift

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
1010
//
1111
//===----------------------------------------------------------------------===//
12+
import Foundation
1213
import SwiftOptions
1314

1415
import class Dispatch.DispatchQueue
@@ -955,7 +956,7 @@ public struct Driver {
955956
negative: .disableIncrementalFileHashing,
956957
default: false)
957958
self.recordedInputMetadata = .init(uniqueKeysWithValues:
958-
Set(inputFiles).compactMap { inputFile -> (TypedVirtualPath, FileMetadata)? in
959+
Set(inputFiles).compactMap { inputFile -> (TypedVirtualPath, FileMetadata)? in
959960
guard let modTime = try? fileSystem.lastModificationTime(for: inputFile.file) else { return nil }
960961
if incrementalFileHashes {
961962
guard let data = try? fileSystem.readFileContents(inputFile.file) else { return nil }
@@ -1425,6 +1426,9 @@ extension Driver {
14251426
if firstArg == "repl" {
14261427
updatedArgs.remove(at: 1)
14271428
updatedArgs.append("-repl")
1429+
#if os(Windows)
1430+
checkIfMatchingPythonArch()
1431+
#endif
14281432
return (.normal(isRepl: true), updatedArgs)
14291433
}
14301434

@@ -1434,6 +1438,49 @@ extension Driver {
14341438

14351439
return (.subcommand(subcommand), updatedArgs)
14361440
}
1441+
1442+
/// Ensure that the architecture of the toolchain matches the architecture
1443+
/// of the Python installation.
1444+
///
1445+
/// When installing the x86 toolchain on ARM64 Windows, if the user does not
1446+
/// install an x86 version of Python, they will get acryptic error message
1447+
/// when running lldb (`0xC000007B`). Calling this function before invoking
1448+
/// lldb gives them a warning to help troublshoot the issue.
1449+
private static func checkIfMatchingPythonArch() {
1450+
#if arch(arm64)
1451+
let arch = "arm64"
1452+
#elseif arch(x86_64)
1453+
let arch = "amd64"
1454+
#elseif arch(x86)
1455+
let arch = "x86"
1456+
#else
1457+
return;
1458+
#endif
1459+
let process = Process()
1460+
process.executableURL = URL(fileURLWithPath: "C:\\Windows\\System32\\cmd.exe")
1461+
process.arguments = ["/c", "python.exe", "-c", "import platform; print(platform.machine())"]
1462+
1463+
let pipe = Pipe()
1464+
process.standardOutput = pipe
1465+
do {
1466+
try process.run()
1467+
process.waitUntilExit()
1468+
1469+
let data = pipe.fileHandleForReading.readDataToEndOfFile()
1470+
guard let output = String(data: data, encoding: .utf8) else {
1471+
return;
1472+
}
1473+
if !output.lowercased().contains(arch) {
1474+
stderrStream.send("There is an architecture mismatch between the installed toolchain and the resolved Python's architecture:\n")
1475+
stderrStream.send("Toolchain: \(arch)\n")
1476+
stderrStream.send("Python: \(output)\n")
1477+
}
1478+
} catch {
1479+
stderrStream.flush()
1480+
return
1481+
}
1482+
stderrStream.flush()
1483+
}
14371484
}
14381485

14391486
extension Driver {

0 commit comments

Comments
 (0)