Skip to content

Commit def835d

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

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

Sources/SwiftDriver/Driver/Driver.swift

Lines changed: 41 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,42 @@ extension Driver {
14341438

14351439
return (.subcommand(subcommand), updatedArgs)
14361440
}
1441+
1442+
private static func checkIfMatchingPythonArch() {
1443+
#if arch(arm64)
1444+
let arch = "arm64"
1445+
#elseif arch(x86_64)
1446+
let arch = "amd64"
1447+
#elseif arch(x86)
1448+
let arch = "x86"
1449+
#else
1450+
return;
1451+
#endif
1452+
let process = Process()
1453+
process.executableURL = URL(fileURLWithPath: "C:\\Windows\\System32\\cmd.exe")
1454+
process.arguments = ["/c", "python.exe", "-c", "import platform; print(platform.machine())"]
1455+
1456+
let pipe = Pipe()
1457+
process.standardOutput = pipe
1458+
do {
1459+
try process.run()
1460+
process.waitUntilExit()
1461+
1462+
let data = pipe.fileHandleForReading.readDataToEndOfFile()
1463+
guard let output = String(data: data, encoding: .utf8) else {
1464+
return;
1465+
}
1466+
if !output.lowercased().contains(arch) {
1467+
stderrStream.send("There is an architecture mismatch between the installed toolchain and the resolved Python's architecture:\n")
1468+
stderrStream.send("Toolchain: \(arch)\n")
1469+
stderrStream.send("Python: \(output)\n")
1470+
}
1471+
} catch {
1472+
stderrStream.flush()
1473+
return
1474+
}
1475+
stderrStream.flush()
1476+
}
14371477
}
14381478

14391479
extension Driver {

0 commit comments

Comments
 (0)