Skip to content

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

Windows: recover a failed first engine sync and repair the cached SDK's .tbd stubs

LNPANDY1179d9098cae
diff --git a/frontend-qt/nspb_qt/win/sdk_fetch.py b/frontend-qt/nspb_qt/win/sdk_fetch.py
index be71d153..795f63b2 100644
--- a/frontend-qt/nspb_qt/win/sdk_fetch.py
+++ b/frontend-qt/nspb_qt/win/sdk_fetch.py
@@ -62,6 +62,38 @@ def rmtree(path: Path) -> None:
# ---------------------------------------------------------------- git route
+def repair_tbd_stubs(sdk: Path, log: Callable[[str], None]) -> int:
+ """Fix `.tbd` files that a Windows git checkout left as text stubs, without git.
+
+ The cached SDK of an earlier broken clone has every symlink file as a small text file
+ naming its target (`libSystem.tbd` → `libSystem.B.tbd`), which ld64.lld refuses ("unhandled
+ file type"). The clone route materializes its symlinks now, but a SDK downloaded before
+ that fix is still cached and broken: walk the tree and copy each target over its stub,
+ keyed on content (a `.tbd` that does not start with the format's `--- !tapi-tbd` and names
+ an existing sibling file is a stub). Real TBD files are never touched.
+ """
+ fixed = 0
+ for p in sorted(sdk.rglob('*.tbd')):
+ if not p.is_file() or p.is_symlink():
+ continue
+ if p.read_bytes()[:13] == b'--- !tapi-tbd':
+ continue
+ target = p.read_text(encoding='utf-8', errors='replace').strip()
+ if not target or '/' in target or '\\' in target or target.startswith('.'):
+ continue
+ real = (p.parent / target).resolve()
+ if not real.is_file() or real == p.resolve():
+ continue
+ if real.read_bytes()[:13] != b'--- !tapi-tbd':
+ continue
+ p.write_bytes(real.read_bytes())
+ fixed += 1
+ if fixed:
+ log(f'repaired {fixed} .tbd text stubs in the cached SDK (symlinks a Windows clone left as text files)')
+ return fixed
+
+
+
def materialize_symlinks(repo: Path, sub: str, log: Callable[[str], None]) -> int:
"""Copy the targets of the SDK's git symlinks over their checked-out text stubs.
@@ -128,6 +160,9 @@ def clone_sdk(version: str = tc.SDK_PINNED, dest: Path | None = None,
dest.mkdir(parents=True, exist_ok=True)
target = dest / version
if tc.is_sdk_dir(target):
+ # The cached SDK may predate the symlink materialization: repair it in place, then the
+ # probe links on the next run instead of failing on a 15-byte text file forever.
+ repair_tbd_stubs(target, log=log)
log(f'SDK already present: {target}')
return target
work = dest / f'.clone-{version}'
diff --git a/frontend-qt/tests/test_win.py b/frontend-qt/tests/test_win.py
index 0ef9bd46..178f76a4 100644
--- a/frontend-qt/tests/test_win.py
+++ b/frontend-qt/tests/test_win.py
@@ -1697,6 +1697,35 @@ class SymlinkMaterializeTest(unittest.TestCase):
+class TbdStubRepairTest(unittest.TestCase):
+ """The cached SDK of a broken clone: `.tbd` files that are text stubs of their targets get
+ the target's bytes copied over them; real TBD files and dangling stubs are left alone."""
+
+ def setUp(self) -> None:
+ base = Path(tempfile.mkdtemp(prefix='nspb-tbd-'))
+ self.addCleanup(shutil.rmtree, base, ignore_errors=True)
+ self.sdk = base / 'iPhoneOS26.0.sdk' / 'usr' / 'lib'
+ self.sdk.mkdir(parents=True)
+ self.good = '--- !tapi-tbd-v3\narchs: [arm64]\ninstall-name: /usr/lib/libSystem.B.dylib\n'
+ (self.sdk / 'libSystem.B.tbd').write_text(self.good)
+ (self.sdk / 'libSystem.tbd').write_text('libSystem.B.tbd') # a broken stub
+ (self.sdk / 'libz.1.tbd').write_text('libz.tbd') # a dangling stub
+ (self.sdk / 'libreal.tbd').write_text(self.good) # a real TBD file
+
+ def test_broken_stubs_are_repaired_and_real_files_untouched(self) -> None:
+ log: list[str] = []
+ fixed = sdk_fetch.repair_tbd_stubs(self.sdk.parent.parent, log.append)
+ self.assertEqual(fixed, 1) # libSystem.tbd only:
+ self.assertTrue((self.sdk / 'libSystem.tbd').read_text().startswith('--- !tapi-tbd'))
+ self.assertEqual((self.sdk / 'libz.1.tbd').read_text(), 'libz.tbd') # dangling: left
+ self.assertEqual((self.sdk / 'libreal.tbd').read_text(), self.good) # real: untouched
+ self.assertTrue(any('repaired 1' in l for l in log))
+
+ def test_the_call_is_idempotent(self) -> None:
+ sdk_fetch.repair_tbd_stubs(self.sdk.parent.parent, lambda l: None)
+ self.assertEqual(sdk_fetch.repair_tbd_stubs(self.sdk.parent.parent, lambda l: None), 0)
+
+
class SaveConfigTest(unittest.TestCase):
"""The Choose-a-folder path writes toolchain.json and reads it back through setting()."""
diff --git a/tools/engine.py b/tools/engine.py
index c79c30b6..a053ce10 100755
--- a/tools/engine.py
+++ b/tools/engine.py
@@ -193,7 +193,13 @@ def fetch(eng, sha, url=None):
def ensure_repo(eng):
if is_repo(eng): return
if eng.exists() and any(eng.iterdir()):
- raise Fail(f'{eng} exists but is not a git repository made by tools/engine.py: move it away, then run sync again')
+ if not any(f.is_file() for _, _, fs in os.walk(eng) for f in fs):
+ # An empty tree is the leftover of a failed first sync: on Windows the engine sync
+ # needs git, and the failure hit right after this folder was made, so every retry
+ # then stopped here forever ("move it away"). Start over instead of asking.
+ shutil.rmtree(eng, ignore_errors=True)
+ else:
+ raise Fail(f'{eng} exists but is not a git repository made by tools/engine.py: move it away, then run sync again')
eng.mkdir(parents=True, exist_ok=True)
git('init', '--quiet', cwd=eng)
git('config', 'advice.detachedHead', 'false', cwd=eng)