Skip to content

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

Xcode detection asks xcode-select, so a beta counts (issue #21)

LNPANDY720e73eb745d
diff --git a/frontend-qt/nspb_qt/localization.py b/frontend-qt/nspb_qt/localization.py
index 6e4f7d08..57eff7e8 100644
--- a/frontend-qt/nspb_qt/localization.py
+++ b/frontend-qt/nspb_qt/localization.py
@@ -122,7 +122,7 @@ TABLE: dict[str, tuple[str, str]] = {
'dep.installing': ('安装中…', 'Installing…'),
'dep.openAppStore': ('打开 App Store', 'Open App Store'),
'dep.xcode': ('Xcode + iOS SDK', 'Xcode + iOS SDK'),
- 'dep.xcode.hint': ('App Store 安装,约 12 GB', 'from the App Store, about 12 GB'),
+ 'dep.xcode.hint': ('App Store 安装(Beta 版也行,用 xcode-select 指向它),约 12 GB', 'from the App Store (a beta works too — point xcode-select at it), about 12 GB'),
'dep.xcode.noSDK': ('Xcode 已装,但 iOS SDK 未下载(Xcode 15 起 iOS 平台是单独下载的)', 'Xcode is installed but the iOS SDK is not downloaded (a separate download since Xcode 15)'),
'dep.metal': ('Metal Toolchain', 'Metal Toolchain'),
'dep.metal.hint': ('xcodebuild -downloadComponent MetalToolchain(约 840 MB)', 'xcodebuild -downloadComponent MetalToolchain (about 840 MB)'),
diff --git a/frontend-qt/nspb_qt/platform_util.py b/frontend-qt/nspb_qt/platform_util.py
index d311b5cc..7fbd3c6b 100644
--- a/frontend-qt/nspb_qt/platform_util.py
+++ b/frontend-qt/nspb_qt/platform_util.py
@@ -187,10 +187,35 @@ def ios_sdk_ok() -> bool:
return _runs('/usr/bin/xcrun', ['--sdk', 'iphoneos', '--show-sdk-version'])
+def xcode_developer_dir() -> str:
+ """The selected developer directory (`xcode-select -p`), or '' when none is selected.
+
+ Asked rather than assumed: an Xcode **beta** lives at /Applications/Xcode-beta.app, and the
+ hard-coded /Applications/Xcode.app check reported "no Xcode" to anyone whose only install is
+ a beta (issue #21). `xcode-select` is the same source of truth xcodebuild and xcrun use, so
+ agreeing with it is what makes the panel match what the build will actually do.
+ """
+ if IS_WIN:
+ return ''
+ try:
+ r = subprocess.run(['/usr/bin/xcode-select', '-p'], capture_output=True, text=True,
+ timeout=30)
+ except (OSError, subprocess.SubprocessError):
+ return ''
+ return (r.stdout or '').strip() if r.returncode == 0 else ''
+
+
def xcode_ok() -> bool:
if IS_WIN:
return False # Xcode is macOS-only; Windows uses the cross toolchain instead
- return Path('/Applications/Xcode.app').is_dir() and Path('/usr/bin/xcodebuild').is_file()
+ # A selected developer directory that exists is the real condition: it covers Xcode.app,
+ # Xcode-beta.app, a custom location, and a Command Line Tools-only setup (which selects
+ # /Library/Developer/CommandLineTools and has no xcodebuild-driven iOS build at all — that
+ # case is caught by the iOS SDK probe below, which is what the row reports).
+ dev = xcode_developer_dir()
+ if not dev or not Path(dev).is_dir():
+ return False
+ return Path('/usr/bin/xcodebuild').is_file()
# ---------------------------------------------------------------- process helpers
diff --git a/frontend-qt/tests/test_win.py b/frontend-qt/tests/test_win.py
index a1413871..71c7a5ce 100644
--- a/frontend-qt/tests/test_win.py
+++ b/frontend-qt/tests/test_win.py
@@ -1157,6 +1157,61 @@ class PythonInstallTest(unittest.TestCase):
self.assertFalse(ok) # the packages are not installed in it (yet)
+class XcodeBetaTest(unittest.TestCase):
+ """Xcode detection asks xcode-select, not a fixed /Applications/Xcode.app path.
+
+ Issue #21: an Xcode **beta** lives at /Applications/Xcode-beta.app, so the hard-coded check
+ reported "no Xcode" on a machine that could build perfectly well — xcodebuild and xcrun both
+ follow xcode-select, and that is what the panel has to agree with.
+ """
+
+ def test_xcode_ok_follows_xcode_select(self) -> None:
+ from nspb_qt import platform_util as pu
+ if pu.IS_WIN:
+ self.skipTest('macOS only')
+ with tempfile.TemporaryDirectory() as td:
+ beta = Path(td) / 'Xcode-beta.app' / 'Contents' / 'Developer'
+ beta.mkdir(parents=True)
+ with mock.patch.object(pu, 'IS_WIN', False), \
+ mock.patch.object(pu, 'xcode_developer_dir', return_value=str(beta)), \
+ mock.patch.object(pu.Path, 'is_file', return_value=True):
+ self.assertTrue(pu.xcode_ok())
+
+ def test_xcode_ok_rejects_a_stale_selection(self) -> None:
+ """xcode-select can point at a deleted Xcode (uninstalled without re-selecting)."""
+ from nspb_qt import platform_util as pu
+ with mock.patch.object(pu, 'IS_WIN', False), \
+ mock.patch.object(pu, 'xcode_developer_dir', return_value='/nope/Xcode.app/Contents/Developer'):
+ self.assertFalse(pu.xcode_ok())
+
+ def test_xcode_ok_rejects_no_selection(self) -> None:
+ from nspb_qt import platform_util as pu
+ with mock.patch.object(pu, 'IS_WIN', False), \
+ mock.patch.object(pu, 'xcode_developer_dir', return_value=''):
+ self.assertFalse(pu.xcode_ok())
+
+ def test_developer_dir_is_empty_on_windows(self) -> None:
+ from nspb_qt import platform_util as pu
+ with mock.patch.object(pu, 'IS_WIN', True):
+ self.assertEqual(pu.xcode_developer_dir(), '')
+
+ def test_developer_dir_reads_xcode_select_output(self) -> None:
+ from nspb_qt import platform_util as pu
+ fake = subprocess.CompletedProcess([], 0, '/Applications/Xcode-beta.app/Contents/Developer\n', '')
+ with mock.patch.object(pu, 'IS_WIN', False), \
+ mock.patch.object(pu.subprocess, 'run', return_value=fake):
+ self.assertEqual(pu.xcode_developer_dir(),
+ '/Applications/Xcode-beta.app/Contents/Developer')
+
+ def test_developer_dir_ignores_a_failing_xcode_select(self) -> None:
+ """A machine with no Xcode at all: xcode-select exits non-zero."""
+ from nspb_qt import platform_util as pu
+ fake = subprocess.CompletedProcess([], 2, '', 'xcode-select: error: unable to get active developer directory')
+ with mock.patch.object(pu, 'IS_WIN', False), \
+ mock.patch.object(pu.subprocess, 'run', return_value=fake):
+ self.assertEqual(pu.xcode_developer_dir(), '')
+
+
class SdkLongPathsTest(unittest.TestCase):
"""The SDK clone carries core.longpaths: a swiftinterface in the SDK tree tops Windows'
260-character MAX_PATH and the sparse checkout died with "Filename too long"."""
diff --git a/frontend/Sources/NSPortBuilder/ContentView.swift b/frontend/Sources/NSPortBuilder/ContentView.swift
index fe172db1..f89d22d8 100644
--- a/frontend/Sources/NSPortBuilder/ContentView.swift
+++ b/frontend/Sources/NSPortBuilder/ContentView.swift
@@ -1086,13 +1086,35 @@ final class BuildModel: ObservableObject {
return ("dotnet", false)
}
+ /// Whether the selected developer directory exists (`xcode-select -p`), which is the same
+ /// source of truth xcodebuild and xcrun use — so an Xcode beta, a custom location, or a
+ /// Command Line Tools-only setup is reported for what it is instead of "no Xcode".
+ nonisolated static func developerDirExists() -> Bool {
+ let p = Process()
+ p.executableURL = URL(fileURLWithPath: "/usr/bin/xcode-select")
+ p.arguments = ["-p"]
+ let pipe = Pipe()
+ p.standardOutput = pipe
+ p.standardError = FileHandle.nullDevice
+ do { try p.run() } catch { return false }
+ let data = pipe.fileHandleForReading.readDataToEndOfFile()
+ p.waitUntilExit()
+ guard p.terminationStatus == 0,
+ let dir = String(data: data, encoding: .utf8)?.trimmingCharacters(in: .whitespacesAndNewlines),
+ !dir.isEmpty else { return false }
+ var isDir: ObjCBool = false
+ return FileManager.default.fileExists(atPath: dir, isDirectory: &isDir) && isDir.boolValue
+ }
+
nonisolated static func detectDepsSync() -> [Dep] {
var out: [Dep] = []
// Xcode.app alone is not enough: since Xcode 15 the iOS platform ships as a separate
// download, so a fresh install has xcodebuild but no iOS SDK and the build dies deep in
// xcodebuild. Ask for the SDK version itself — that is the check that matches the name.
- let xcodePresent = FileManager.default.fileExists(atPath: "/Applications/Xcode.app")
+ // And ask xcode-select where Xcode is, not a fixed path: a beta lives at
+ // /Applications/Xcode-beta.app and the hard-coded check called that "no Xcode" (#21).
+ let xcodePresent = Self.developerDirExists()
&& FileManager.default.isExecutableFile(atPath: "/usr/bin/xcodebuild")
let iosSDK = xcodePresent && runOK("/usr/bin/xcrun", ["--sdk", "iphoneos", "--show-sdk-version"])
out.append(Dep(id: "xcode", name: "dep.xcode", hint: "dep.xcode.hint",
diff --git a/frontend/Sources/NSPortBuilder/Localization.swift b/frontend/Sources/NSPortBuilder/Localization.swift
index 50d6d036..b4f209b0 100644
--- a/frontend/Sources/NSPortBuilder/Localization.swift
+++ b/frontend/Sources/NSPortBuilder/Localization.swift
@@ -186,7 +186,8 @@ enum L10n {
"dep.openAppStore": ("打开 App Store", "Open App Store"),
"dep.xcode": ("Xcode + iOS SDK", "Xcode + iOS SDK"),
- "dep.xcode.hint": ("App Store 安装,约 12 GB", "from the App Store, about 12 GB"),
+ "dep.xcode.hint": ("App Store 安装(Beta 版也行,用 xcode-select 指向它),约 12 GB",
+ "from the App Store (a beta works too — point xcode-select at it), about 12 GB"),
"dep.xcode.noSDK": ("Xcode 已装,但 iOS SDK 未下载(Xcode 15 起 iOS 平台是单独下载的)",
"Xcode is installed but the iOS SDK is not downloaded (a separate download since Xcode 15)"),
"dep.metal": ("Metal Toolchain", "Metal Toolchain"),