Play your own Switch games on iPhone/iPad — one-click macOS app, no terminal. | 把 Switch 游戏装进 iPhone:图形界面一键构建,不用命令行。Bring your own game.
Swift apple-siliconcustom-formathomebrewiosipaipadipadosiphonemetalnative-portnintendo-switchnxnativeromfsswitch-games
Merge upstream/main into launcher-uv-python
v0lumehie7d5dde159b2
diff --cc frontend/README.mdindex 6bee85f2,e5f95fa8..8c572cc0--- a/frontend/README.md+++ b/frontend/README.md@@@ -227,13 -221,19 +227,26 @@@ and picks the first interpreter that ca`lz4 capstone fontTools backports.zstd`. That same interpreter then runs thebuild.+The interpreters uv installs (`uv python install`) count as well: they live in+`~/.local/share/uv/python` (or `UV_PYTHON_INSTALL_DIR`) with shims in+`~/.local/bin`, neither of which a GUI launch's PATH contains, and a uv user+often has no Homebrew and no python.org framework at all. When no interpreter is+found, **Install** creates the app's venv from the newest one that does run at+3.12 or newer rather than from the venv path it is about to create.++ ### The bundled build tools and Python++ The release app (`NX2Apple.app`) carries `zstd`, `glslangValidator`, `spirv-cross`, `xcodegen` (in+ `Contents/Resources/tools/bin`) and a relocatable CPython 3.13 with `lz4 capstone fonttools+ backports.zstd nsz` (`Contents/Resources/python`), so a player needs no Homebrew and no Python.+ `tools/bundle_tools.sh` fills them in during the release workflow (pinned versions and sha256 of the+ sources, built for macOS 13 with static libraries; the Python packages come from+ `tools/bundled-python-requirements.txt`) and `tools/check_bundle.sh` runs each tool from the bundle+ with a clean PATH. The dependency check (`BundledTools.swift`) prefers them to Homebrew's and shows+ "bundled"; the build's PATH starts with them. Under `swift run`, or in a build without the bundle,+ nothing changes: Homebrew's tools, a system Python or the launcher's venv. To try the bundle by hand:+ `tools/bundle_tools.sh <folder> <work folder>`, then `tools/check_bundle.sh <folder>`.+## LanguageThe interface follows the system language at every launch: any Chinese variantdiff --cc frontend/Sources/NSPortBuilder/ContentView.swiftindex d28e542a,e41360fc..5be3cceb--- a/frontend/Sources/NSPortBuilder/ContentView.swift+++ b/frontend/Sources/NSPortBuilder/ContentView.swift@@@ -1904,19 -1950,9 +1950,21 @@@ final class BuildModel: ObservableObjecnonisolated static func venvPython() -> String { venvDir + "/bin/python3" }+ /// Where uv keeps the Pythons it installs (`uv python install`): the shims in `~/.local/bin`+ /// and the store the real binaries live in, which `UV_PYTHON_INSTALL_DIR` moves. A uv user+ /// often has neither python.org's framework nor Homebrew, and a GUI launch's PATH is+ /// /usr/bin:/bin:/usr/sbin:/sbin, so neither `which` nor the locations below see them: a+ /// working uv Python 3.12 was reported as "No Python 3.12 or newer found". Injectable so a+ /// test can point them at a scratch tree.+ nonisolated static var uvShimDir = NSHomeDirectory() + "/.local/bin"+ nonisolated static var uvPythonRoot: String = {+ let dir = ProcessInfo.processInfo.environment["UV_PYTHON_INSTALL_DIR"] ?? ""+ return dir.isEmpty ? NSHomeDirectory() + "/.local/share/uv/python" : dir+ }()+- nonisolated static func importsOK(_ py: String) -> Bool { runOK(py, ["-c", importProbe]) }+ nonisolated static func importsOK(_ py: String) -> Bool {+ runOK(py, ["-c", importProbe], env: ProcessInfo.processInfo.environment.merging(BundledTools.environment(for: py)) { $1 })+ }/// backports.zstd declares Python < 3.14, so it is only installed where it is needed./// nsz is always installed: NSZ / XCZ dumps are accepted by the pipeline now, and a player who@@@ -2048,15 -2047,9 +2097,21 @@@let out = String(data: pipe.fileHandleForReading.readDataToEndOfFile(), encoding: .utf8) ?? ""cands += out.split(separator: "\n").map(String.init)}+ return cands+ }++ /// 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) {++ nonisolated static func findPython(resources: URL? = BundledTools.resources) -> (path: String, ok: Bool) {++ // The release app's own Python (BundledTools.swift) has every package and is the one the build is tested with: it wins over++ // any Python on this Mac and needs no venv. One that does not run (a damaged copy) falls through to the search below++ // (uv's Pythons, the other candidates, then the venv).++ if let bundled = BundledTools.python(in: resources), pythonVersion(bundled).map({ $0 >= minPython }) == true, importsOK(bundled) {++ return (bundled, true)++ }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 {+ for c in pythonCandidates() where !c.isEmpty && seen.insert(c).inserted {guard FileManager.default.isExecutableFile(atPath: c), let v = pythonVersion(c) else { continue }guard v >= minPython else { continue }if importsOK(c) { return (c, true) }@@@ -2212,26 -2282,6 +2345,30 @@@}}+ /// Write what the panel just decided beside library.json (`deps-check.txt`).+ ///+ /// The panel is the only place those verdicts appear, so "it says Python is missing" cannot be+ /// checked afterwards: the interpreter that was tried, and the environment a Finder launch+ /// gives the app (its PATH is /usr/bin:/bin:/usr/sbin:/sbin, unlike the terminal's), are both+ /// invisible in a bug report. This file has them — paths and versions only, never key material.+ nonisolated static func writeDepsReport(_ deps: [Dep]) {+ let dir = (venvDir as NSString).deletingLastPathComponent+ try? FileManager.default.createDirectory(atPath: dir, withIntermediateDirectories: true)+ var out = "NX2Apple \(appVersion) dependency check\n"+ out += "date: \(ISO8601DateFormatter().string(from: Date()))\n"+ out += "HOME: \(NSHomeDirectory())\n"+ out += "PATH: \(ProcessInfo.processInfo.environment["PATH"] ?? "(unset)")\n"- out += "python candidates:\n"++ let chosen = findPython()++ let bundled = BundledTools.python()++ out += "python chosen: \(chosen.path) (\(chosen.ok ? "ok" : "not usable")\(chosen.path == bundled ? ", bundled with the app" : ""))\n"++ out += "bundled python: \(bundled ?? "(none: not the release app)")\n"++ out += "python candidates (after the bundled one, in search order):\n"+ for c in pythonCandidates() { out += " \(c)\n" }+ out += "rows:\n"+ for d in deps { out += " \(d.id): \(d.present ? "ok" : "MISSING") \(d.detail)\n" }+ try? out.write(toFile: dir + "/deps-check.txt", atomically: true, encoding: .utf8)+ }+func installDep(_ dep: Dep) {guard !dep.installCmd.isEmpty else {NSWorkspace.shared.open(URL(fileURLWithPath: "/Applications/App Store.app"))diff --cc frontend/Tests/NSPortBuilderTests/BundledToolsTests.swiftindex 00000000,4d3a7720..1ab8843emode 000000,100644..100644--- a/frontend/Tests/NSPortBuilderTests/BundledToolsTests.swift+++ b/frontend/Tests/NSPortBuilderTests/BundledToolsTests.swift@@@ -1,0 -1,205 +1,226 @@@+ import XCTest+ @testable import NSPortBuilder++ /// The release app carries its build tools and a Python (BundledTools.swift): the dependency check prefers them to Homebrew's and to+ /// a system Python, and the build's PATH starts with them. A fake bundle stands in for Contents/Resources.+ final class BundledToolsTests: XCTestCase {+ private var root: URL!++ override func setUpWithError() throws {+ root = FileManager.default.temporaryDirectory.appendingPathComponent("bundled-tools-\(UUID().uuidString)")+ try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true)+ }+ override func tearDownWithError() throws { try? FileManager.default.removeItem(at: root) }++ /// An executable file `relative` under the fake Resources folder; `script` is what it prints when it runs.+ @discardableResult+ private func install(_ relative: String, script: String = "#!/bin/sh\nexit 0\n") throws -> String {+ let url = root.appendingPathComponent(relative)+ try FileManager.default.createDirectory(at: url.deletingLastPathComponent(), withIntermediateDirectories: true)+ try script.write(to: url, atomically: true, encoding: .utf8)+ try FileManager.default.setAttributes([.posixPermissions: 0o755], ofItemAtPath: url.path)+ return url.path+ }+ private func installAllTools() throws {+ for (_, binary, _) in BuildModel.brewDeps { try install("tools/bin/" + binary) }+ }+ private func row(_ rows: [BuildModel.Dep], _ pkg: String) -> BuildModel.Dep? { rows.first { $0.id == "brew:" + pkg } }++ // ---- what the bundle holds ----++ func testNoBundleAnswersNil() {+ XCTAssertNil(BundledTools.binDirectory(in: nil))+ XCTAssertNil(BundledTools.tool("zstd", in: nil))+ XCTAssertNil(BundledTools.python(in: nil))+ XCTAssertNil(BundledTools.binDirectory(in: root), "a folder with no tools/bin")+ XCTAssertNil(BundledTools.tool("zstd", in: root))+ }++ func testSwiftRunHasNoResources() {+ // `swift test` and `swift run` execute outside a .app, so the launcher sees no bundle there+ XCTAssertNil(BundledTools.resources)+ }++ func testToolAndPythonPaths() throws {+ let zstd = try install("tools/bin/zstd")+ let py = try install("python/bin/python3")+ XCTAssertEqual(BundledTools.tool("zstd", in: root), zstd)+ XCTAssertNil(BundledTools.tool("xcodegen", in: root))+ XCTAssertEqual(BundledTools.python(in: root), py)+ XCTAssertEqual(BundledTools.binDirectory(in: root), root.appendingPathComponent("tools/bin").path)+ }++ func testAFileWithoutTheExecuteBitIsNotATool() throws {+ let url = root.appendingPathComponent("tools/bin/zstd")+ try FileManager.default.createDirectory(at: url.deletingLastPathComponent(), withIntermediateDirectories: true)+ try "x".write(to: url, atomically: true, encoding: .utf8)+ XCTAssertNil(BundledTools.tool("zstd", in: root))+ }++ // ---- PATH ----++ func testPathWithoutBundleIsTheBaseUnchanged() {+ XCTAssertEqual(BundledTools.path("/usr/bin:/bin", python: nil, in: nil), "/usr/bin:/bin")+ XCTAssertEqual(BundledTools.path("/usr/bin:/bin", python: nil, in: root), "/usr/bin:/bin")+ }++ func testPathPutsTheBundledToolsFirstThenItsPython() throws {+ try install("tools/bin/zstd")+ let py = try install("python/bin/python3")+ XCTAssertEqual(BundledTools.path("/opt/homebrew/bin:/usr/bin", python: py, in: root),+ root.path + "/tools/bin:" + root.path + "/python/bin:/opt/homebrew/bin:/usr/bin")+ }++ func testPathKeepsARejectedOrOtherPythonOff() throws {+ // review of #154: the bundled interpreter that findPython rejected must not lead the PATH, nor must it when another was chosen+ try install("tools/bin/zstd")+ try install("python/bin/python3")+ let expected = root.path + "/tools/bin:/usr/bin"+ XCTAssertEqual(BundledTools.path("/usr/bin", python: "/opt/homebrew/bin/python3", in: root), expected)+ XCTAssertEqual(BundledTools.path("/usr/bin", python: nil, in: root), expected)+ }++ func testOnlyTheBundledPythonGetsTheIsolatedEnvironment() throws {+ let py = try install("python/bin/python3")+ let env = BundledTools.environment(for: py, in: root)+ XCTAssertEqual(env["PYTHONNOUSERSITE"], "1")+ XCTAssertEqual(env["PYTHONDONTWRITEBYTECODE"], "1")+ XCTAssertTrue(BundledTools.environment(for: "/opt/homebrew/bin/python3", in: root).isEmpty)+ XCTAssertTrue(BundledTools.environment(for: py, in: nil).isEmpty)+ }++ func testPathWithToolsButNoPython() throws {+ try install("tools/bin/zstd")+ XCTAssertEqual(BundledTools.path("/usr/bin", python: nil, in: root), root.path + "/tools/bin:/usr/bin")+ }++ func testBuildToolPathKeepsTheHomebrewBaseWithoutABundle() {+ // under `swift test` there is no bundle: the helper is the old string+ XCTAssertEqual(BuildModel.toolPath(), "/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin")+ XCTAssertEqual(BuildModel.toolPath("/usr/bin:/bin"), "/usr/bin:/bin")+ }++ // ---- the dependency rows ----++ func testWithoutABundleTheRowsAreHomebrews() {+ let rows = BuildModel.toolDeps(resources: nil, installed: { "/opt/homebrew/bin/" + $0 }, brew: "/opt/homebrew/bin/brew")+ XCTAssertEqual(rows.map { $0.id }, ["brew"] + BuildModel.brewDeps.map { "brew:" + $0.formula })+ XCTAssertTrue(rows.allSatisfy { $0.present })+ XCTAssertEqual(row(rows, "zstd")?.detail, "/opt/homebrew/bin/zstd")+ XCTAssertEqual(row(rows, "glslang")?.detail, "/opt/homebrew/bin/glslangValidator")+ }++ func testWithoutABundleOrHomebrewTheRowsAreMissing() {+ let rows = BuildModel.toolDeps(resources: nil, installed: { _ in nil }, brew: nil)+ XCTAssertFalse(rows.first { $0.id == "brew" }!.present)+ XCTAssertTrue(BuildModel.brewDeps.allSatisfy { row(rows, $0.formula)?.present == false })+ }++ func testBundledToolsArePresentAndNeedNoHomebrew() throws {+ try installAllTools()+ let rows = BuildModel.toolDeps(resources: root, installed: { _ in nil }, brew: nil)+ XCTAssertNil(rows.first { $0.id == "brew" }, "nothing needs Homebrew")+ XCTAssertEqual(rows.count, BuildModel.brewDeps.count)+ XCTAssertTrue(rows.allSatisfy { $0.present && $0.detail == "dep.bundled" })+ XCTAssertEqual(rows.map { $0.name }, BuildModel.brewDeps.map { "dep.tool." + $0.binary }, "named by the tool, not \"brew: …\"")+ for (_, binary, _) in BuildModel.brewDeps {+ for lang in [Lang.en, .zh] { XCTAssertEqual(L10n.string("dep.tool." + binary, lang), binary) }+ }+ }++ func testBundledWinsOverHomebrews() throws {+ try installAllTools()+ let rows = BuildModel.toolDeps(resources: root, installed: { "/opt/homebrew/bin/" + $0 }, brew: "/opt/homebrew/bin/brew")+ XCTAssertTrue(rows.allSatisfy { $0.detail == "dep.bundled" })+ XCTAssertNil(rows.first { $0.id == "brew" })+ }++ func testOneMissingToolBringsBackTheHomebrewRowForThatTool() throws {+ try installAllTools()+ try FileManager.default.removeItem(at: root.appendingPathComponent("tools/bin/spirv-cross"))+ let rows = BuildModel.toolDeps(resources: root, installed: { _ in nil }, brew: nil)+ XCTAssertEqual(rows.first?.id, "brew")+ XCTAssertFalse(rows.first!.present)+ XCTAssertEqual(row(rows, "spirv-cross")?.present, false)+ XCTAssertEqual(row(rows, "zstd")?.detail, "dep.bundled")+ XCTAssertEqual(row(rows, "zstd")?.name, "dep.tool.zstd")+ XCTAssertEqual(row(rows, "spirv-cross")?.name, "dep.spirv", "missing: the Homebrew row keeps its label")+ }++ func testAMissingBundledToolFallsBackToHomebrewsCopy() throws {+ try installAllTools()+ try FileManager.default.removeItem(at: root.appendingPathComponent("tools/bin/xcodegen"))+ let rows = BuildModel.toolDeps(resources: root, installed: { $0 == "xcodegen" ? "/opt/homebrew/bin/xcodegen" : nil },+ brew: "/opt/homebrew/bin/brew")+ XCTAssertEqual(row(rows, "xcodegen")?.detail, "/opt/homebrew/bin/xcodegen")+ XCTAssertEqual(row(rows, "xcodegen")?.present, true)+ XCTAssertEqual(row(rows, "xcodegen")?.name, "dep.xcodegen", "a Homebrew copy keeps the Homebrew label")+ XCTAssertNil(rows.first { $0.id == "brew" }, "every tool is bundled or installed")+ }++ func testBundledRowDetailIsLocalized() {+ XCTAssertEqual(L10n.string("dep.bundled", .en), "bundled")+ XCTAssertNotEqual(L10n.string("dep.bundled", .zh), "dep.bundled")+ }++ // ---- the Python ----++ func testFindPythonUsesTheBundledOneThatRunsAndImports() throws {+ // a stand-in interpreter that answers the version probe and accepts the import probe+ let py = try install("python/bin/python3", script: """+ #!/bin/sh+ case "$2" in *sys.version_info*) echo 3.13;; esac+ exit 0++ """)+ let found = BuildModel.findPython(resources: root)+ XCTAssertEqual(found.path, py)+ XCTAssertTrue(found.ok)+ }++ func testFindPythonSkipsABundledOneThatDoesNotRun() throws {+ let py = try install("python/bin/python3", script: "#!/bin/sh\nexit 1\n")+ XCTAssertNotEqual(BuildModel.findPython(resources: root).path, py)+ }++ func testFindPythonSkipsABundledOneThatIsTooOld() throws {+ let py = try install("python/bin/python3", script: "#!/bin/sh\ncase \"$2\" in *sys.version_info*) echo 3.9;; esac\nexit 0\n")+ XCTAssertNotEqual(BuildModel.findPython(resources: root).path, py)+ }+++ /// The order of #107 and #154: the bundled Python (when it runs, is new enough and imports the++ /// packages) beats a working uv interpreter; a damaged bundled one falls through to uv's.++ func testBundledPythonBeatsUvAndAUvOneIsNextWhenItFails() throws {++ let oldShim = BuildModel.uvShimDir, oldRoot = BuildModel.uvPythonRoot++ defer { BuildModel.uvShimDir = oldShim; BuildModel.uvPythonRoot = oldRoot }++ let good = "#!/bin/sh\ncase \"$2\" in *sys.version_info*) echo 3.13;; esac\nexit 0\n"++ let shim = root.appendingPathComponent("uvhome/.local/bin")++ try FileManager.default.createDirectory(at: shim, withIntermediateDirectories: true)++ let uvPy = shim.appendingPathComponent("python3")++ try Data(good.utf8).write(to: uvPy)++ try FileManager.default.setAttributes([.posixPermissions: 0o755], ofItemAtPath: uvPy.path)++ BuildModel.uvShimDir = shim.path++ BuildModel.uvPythonRoot = root.appendingPathComponent("uvhome/none").path++ let bundled = try install("python/bin/python3", script: good)++ XCTAssertEqual(BuildModel.findPython(resources: root).path, bundled)++ try Data("#!/bin/sh\nexit 1\n".utf8).write(to: URL(fileURLWithPath: bundled))++ XCTAssertTrue(BuildModel.pythonCandidates().contains(uvPy.path))++ let next = BuildModel.findPython(resources: root)++ XCTAssertNotEqual(next.path, bundled)++ }+++ // ---- the pinned packages ----++ func testBundledPackagePinsCoverThePipPackages() throws {+ // frontend/Tests/NSPortBuilderTests/BundledToolsTests.swift -> frontend/tools+ var url = URL(fileURLWithPath: #filePath)+ for _ in 0..<3 { url.deleteLastPathComponent() }+ let text = try String(contentsOf: url.appendingPathComponent("tools/bundled-python-requirements.txt"), encoding: .utf8)+ let pins = text.split(separator: "\n").filter { !$0.hasPrefix("#") }+ let names = Set(pins.map { String($0.split(separator: "=", maxSplits: 1)[0]).lowercased() })+ for pkg in BuildModel.pipPackages((3, 13)) {+ XCTAssertTrue(names.contains(pkg.lowercased()), "\(pkg) is installed by the launcher's venv but not pinned for the bundle")+ }+ XCTAssertTrue(pins.allSatisfy { $0.contains("==") }, "every bundled package has an exact version")+ }+ }