Skip to content

Commit a1ef87c

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

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

Sources/SwiftDriver/Driver/Driver.swift

Lines changed: 39 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,12 @@ extension Driver {
14251426
if firstArg == "repl" {
14261427
updatedArgs.remove(at: 1)
14271428
updatedArgs.append("-repl")
1429+
#if os(Windows)
1430+
if !hasMatchingPythonArch() {
1431+
stderrStream.send("There is an architecture mismatch between the installed toolchain and the resolved Python's architecture.")
1432+
stderrStream.flush()
1433+
}
1434+
#endif
14281435
return (.normal(isRepl: true), updatedArgs)
14291436
}
14301437

@@ -1434,6 +1441,37 @@ extension Driver {
14341441

14351442
return (.subcommand(subcommand), updatedArgs)
14361443
}
1444+
1445+
private static func hasMatchingPythonArch() -> Bool {
1446+
#if arch(arm64)
1447+
let arch = "arm64"
1448+
#elseif arch(x86_64)
1449+
let arch = "amd64"
1450+
#elseif arch(x86)
1451+
let arch = "x86"
1452+
#else
1453+
return false;
1454+
#endif
1455+
let process = Process()
1456+
process.executableURL = URL(fileURLWithPath: "python")
1457+
process.arguments = ["-c", "import platform; print(platform.machine())"]
1458+
1459+
let pipe = Pipe()
1460+
process.standardOutput = pipe
1461+
1462+
do {
1463+
try process.run()
1464+
process.waitUntilExit()
1465+
1466+
let data = pipe.fileHandleForReading.readDataToEndOfFile()
1467+
guard let output = String(data: data, encoding: .utf8) else {
1468+
return false;
1469+
}
1470+
return output.lowercased().contains(arch)
1471+
} catch {
1472+
return false
1473+
}
1474+
}
14371475
}
14381476

14391477
extension Driver {

0 commit comments

Comments
 (0)