Skip to content

Play your own Switch games on iPhone/iPad — one-click macOS app, no terminal. | 把 Switch 游戏装进 iPhone:图形界面一键构建,不用命令行。Bring your own game.

Fix the Python 3.9 build failure; refuse an unusable interpreter up front

LNPANDYe7d705665c07
diff --git a/frontend/Sources/NSPortBuilder/ContentView.swift b/frontend/Sources/NSPortBuilder/ContentView.swift
index 522640a1..0c735f96 100644
--- a/frontend/Sources/NSPortBuilder/ContentView.swift
+++ b/frontend/Sources/NSPortBuilder/ContentView.swift
@@ -444,6 +444,30 @@ final class BuildModel: ObservableObject {
/// The Python that actually has lz4/capstone/fontTools/backports.zstd — the one
/// the build must run under, not whatever /usr/bin/python3 happens to be.
+ /// The minimum the build scripts need. /usr/bin/python3 is 3.9 on a stock Mac and has none of
+ /// the packages; picking it made a build fail with a syntax error deep inside the script
+ /// (an f-string only 3.12 can parse) instead of a clear message here.
+ nonisolated static let minPython = (3, 9)
+
+ /// "3.12.4" → (3, 12). nil when the interpreter cannot be run.
+ nonisolated static func pythonVersion(_ path: String) -> (Int, Int)? {
+ let p = Process()
+ p.executableURL = URL(fileURLWithPath: path)
+ p.arguments = ["-c", "import sys; print('%d.%d' % sys.version_info[:2])"]
+ let pipe = Pipe()
+ p.standardOutput = pipe; p.standardError = Pipe()
+ p.standardInput = FileHandle.nullDevice
+ guard (try? p.run()) != nil else { return nil }
+ p.waitUntilExit()
+ guard p.terminationStatus == 0 else { return nil }
+ let out = String(data: pipe.fileHandleForReading.readDataToEndOfFile(), encoding: .utf8) ?? ""
+ let parts = out.trimmingCharacters(in: .whitespacesAndNewlines).split(separator: ".")
+ guard parts.count == 2, let a = Int(parts[0]), let b = Int(parts[1]) else { return nil }
+ return (a, b)
+ }
+
+ /// Every python3 on this Mac, newest-looking first, each tried for a usable version and the
+ /// packages. Returns the one to build with, plus why the others were rejected for the message.
nonisolated static func findPython() -> (path: String, ok: Bool) {
var cands: [String] = []
let fw = "/Library/Frameworks/Python.framework/Versions"
@@ -465,11 +489,14 @@ final class BuildModel: ObservableObject {
cands += out.split(separator: "\n").map(String.init)
}
var seen = Set<String>()
+ var fallback: (String, (Int, Int))? = nil // newest with an acceptable version but no packages
for c in cands where !c.isEmpty && seen.insert(c).inserted {
- guard FileManager.default.isExecutableFile(atPath: c) else { continue }
+ guard FileManager.default.isExecutableFile(atPath: c), let v = pythonVersion(c) else { continue }
+ guard v >= minPython else { continue }
if runOK(c, ["-c", "import lz4, capstone, fontTools, backports.zstd"]) { return (c, true) }
+ if fallback == nil || v > fallback!.1 { fallback = (c, v) }
}
- return (cands.first { FileManager.default.isExecutableFile(atPath: $0) } ?? "/usr/bin/python3", false)
+ return (fallback?.0 ?? "/usr/bin/python3", false)
}
nonisolated static func findDotnet() -> (path: String, ok: Bool) {
@@ -516,8 +543,15 @@ final class BuildModel: ObservableObject {
"curl -sSL https://dot.net/v1/dotnet-install.sh -o /tmp/di.sh && chmod +x /tmp/di.sh && /tmp/di.sh --channel 10.0 --install-dir $HOME/.dotnet"]))
let (py, pyOK) = findPython()
+ let pyVer = pythonVersion(py).map { "\($0.0).\($0.1)" } ?? "?"
+ // A too-old interpreter is a different problem from a missing package: say which one it is,
+ // because the fix differs (install a newer Python vs pip install into this one).
+ let pyDetail = pyOK ? py
+ : (pythonVersion(py).map { $0 >= minPython } ?? false)
+ ? "dep.python.missing:\(py) (\(pyVer))"
+ : "dep.python.old:\(pyVer):\(minPython.0).\(minPython.1)"
out.append(Dep(id: "python", name: "dep.python", hint: "dep.python.hint",
- present: pyOK, detail: pyOK ? py : "dep.python.missing:\(py)",
+ present: pyOK, detail: pyDetail,
installCmd: [py, "-m", "pip", "install", "--user", "lz4", "capstone", "fonttools", "backports.zstd"]))
return out
}
diff --git a/frontend/Sources/NSPortBuilder/Localization.swift b/frontend/Sources/NSPortBuilder/Localization.swift
index b974013a..7d06e203 100644
--- a/frontend/Sources/NSPortBuilder/Localization.swift
+++ b/frontend/Sources/NSPortBuilder/Localization.swift
@@ -95,6 +95,8 @@ enum L10n {
"dep.python": ("Python + 包", "Python + packages"),
"dep.python.hint": ("lz4 capstone fonttools backports.zstd", "lz4 capstone fonttools backports.zstd"),
"dep.python.missing": ("%@ 缺包", "%@ is missing packages"),
+ "dep.python.old": ("Python %@ 太旧(需要 %@ 或更新);系统的 /usr/bin/python3 没有这些包",
+ "Python %@ is too old (need %@ or newer); the system /usr/bin/python3 has none of these packages"),
// ---- progress ----
"prog.title": ("构建进度", "Build progress"),
diff --git a/scripts/build_ipa_from_nsp.py b/scripts/build_ipa_from_nsp.py
index cebd08f6..26a9f51e 100755
--- a/scripts/build_ipa_from_nsp.py
+++ b/scripts/build_ipa_from_nsp.py
@@ -418,7 +418,13 @@ def survey(nsp_dir, app, fam, keys, repo, py, env):
notes = []
if not vulkan and 'nvnBootstrapLoader' not in mods['main']['imports']: notes.append('main imports neither nvnBootstrapLoader nor Vulkan functions: the host implements only NVN and Vulkan')
if vulkan: notes.append(f'{G["graphics"][1]}; the NVN shader steps are skipped' + (', and its shaders are captured by booting it on the Mac (no SPIR-V file in the RomFS)' if 'shader_vk_capture' in G else ''))
- if extra: notes.append(f'modules besides main / sdk: {", ".join(f"{n} ({mods[n]["kind"]})" for n in extra)}; the provisional profile loads {G["modules"][0]} (load order main, subsdk*, sdk)')
+ if extra:
+ # Nested quotes of the same kind inside an f-string need Python 3.12 (PEP 701); this script
+ # has to parse on the 3.9 that ships with the Xcode command line tools, so keep the inner
+ # expressions out of the f-string.
+ listed = ', '.join('{} ({})'.format(n, mods[n]['kind']) for n in extra)
+ notes.append('modules besides main / sdk: ' + listed + '; the provisional profile loads '
+ + G['modules'][0] + ' (load order main, subsdk*, sdk)')
if rom['mags']['UnityFS'] or 'Unity IL2CPP' in F['markers']: notes.append('Unity: the shaders are Shader objects in UnityFS bundles and data.unity3d, which shader_split.py reads through unityfs.py (written for Unity 2020.3\'s Switch format; check other Unity versions first)')
if F['glslc']: notes.append(f'main has {F["glslc"]} GLSLC compiler output magics (0x19866891): ' + ('the shaders are compiled into main (13 Sentinels, the KTGL games): shader_main_glslc on' if 'shader_main_glslc' in G else 'not its shaders, the RomFS has shader containers (Engage)'))
if not (rom['mags']['BNSH'] or rom['mags']['BAHS']) and not rom['mags']['UnityFS'] and not vulkan and 'shader_main_glslc' not in G: