Skip to content

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

Windows: fix the engine recovery's own crash, and repair cached .tbd stubs before every probe

LNPANDY2d55fb7db5e9
diff --git a/frontend-qt/nspb_qt/win/sdk_fetch.py b/frontend-qt/nspb_qt/win/sdk_fetch.py
index 795f63b2..0dfcded5 100644
--- a/frontend-qt/nspb_qt/win/sdk_fetch.py
+++ b/frontend-qt/nspb_qt/win/sdk_fetch.py
@@ -62,35 +62,10 @@ 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 repair_tbd_stubs(sdk: Path, log: Callable[[str], None] | None = None) -> int:
+ """The toolchain's repair, re-exported for the clone route: fix `.tbd` text stubs in place."""
+ return tc.repair_tbd_stubs(sdk, log=log)
+
diff --git a/frontend-qt/nspb_qt/win/toolchain.py b/frontend-qt/nspb_qt/win/toolchain.py
index 3b6e8db9..abaadb36 100644
--- a/frontend-qt/nspb_qt/win/toolchain.py
+++ b/frontend-qt/nspb_qt/win/toolchain.py
@@ -610,14 +610,56 @@ def sdk_cache_dir() -> Path:
return base / 'NS Port Builder' / 'sdks'
+def repair_tbd_stubs(sdk: Path | str, log=None) -> int:
+ """Fix `.tbd` files that a Windows git checkout left as text stubs, without git.
+
+ An SDK downloaded through a git route before the symlink materialization (or copied from a
+ broken checkout) keeps every symlink `.tbd` as a small text file naming its target
+ (`libSystem.tbd` → `libSystem.B.tbd`). `ld64.lld` opens it, finds no `--- !tapi-tbd`, and
+ refuses: "unhandled file type" (LNPANDY/NX2Apple PR #596 fixed the clone route; cached SDKs
+ from before it still carry the stubs). Content-keyed repair: a `.tbd` that does not start
+ with the format's `--- !tapi-tbd` marker and names an existing sibling file gets that
+ sibling's bytes copied over it. Real TBD files and dangling stubs are never touched.
+ Returns how many were repaired.
+ """
+ fixed = 0
+ for p in sorted(Path(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.resolve() == p.resolve():
+ continue
+ if real.read_bytes()[:13] != b'--- !tapi-tbd':
+ continue
+ p.write_bytes(real.read_bytes())
+ fixed += 1
+ if log:
+ log(f'repaired {p.name} ← {target}')
+ return fixed
+
+
+_TBD_REPAIRED = False
+
def ios_sdk() -> Path | None:
"""The `iPhoneOS<ver>.sdk` folder: a directory with `SDKSettings.json` and stub libraries.
Accepting only a folder that looks like an SDK keeps a stale or half-extracted download from
being reported as `ok` and then failing deep inside the build.
"""
+ global _TBD_REPAIRED
for p in _sdk_candidates():
if is_sdk_dir(p):
+ if not _TBD_REPAIRED:
+ _TBD_REPAIRED = True
+ try: # a cached SDK from a pre-fix git route carries text stubs; fix once
+ repair_tbd_stubs(p)
+ except OSError:
+ pass
return p
return None
diff --git a/frontend-qt/tests/test_win.py b/frontend-qt/tests/test_win.py
index 99f45eaf..2c5ef500 100644
--- a/frontend-qt/tests/test_win.py
+++ b/frontend-qt/tests/test_win.py
@@ -1844,7 +1844,7 @@ class TbdStubRepairTest(unittest.TestCase):
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))
+ self.assertTrue(any(l.startswith('repaired libSystem.tbd') for l in log), log)
def test_the_call_is_idempotent(self) -> None:
sdk_fetch.repair_tbd_stubs(self.sdk.parent.parent, lambda l: None)
diff --git a/tools/engine.py b/tools/engine.py
index a053ce10..24f1e519 100755
--- a/tools/engine.py
+++ b/tools/engine.py
@@ -193,7 +193,7 @@ def fetch(eng, sha, url=None):
def ensure_repo(eng):
if is_repo(eng): return
if eng.exists() and any(eng.iterdir()):
- if not any(f.is_file() for _, _, fs in os.walk(eng) for f in fs):
+ if not any(f.is_file() for f in eng.rglob('*')):
# 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.
diff --git a/tools/tests/test_engine.py b/tools/tests/test_engine.py
index f830d224..a0e365e3 100644
--- a/tools/tests/test_engine.py
+++ b/tools/tests/test_engine.py
@@ -86,6 +86,36 @@ class Fixture(unittest.TestCase):
return git(self.up, 'rev-parse', 'HEAD')
+class EnsureRepo(Fixture):
+ """The engine folder a failed first sync leaves behind: empty trees are the Windows case
+ (git missing, the failure hit right after mkdir), and a retry must start over instead of
+ stopping on the same line forever. A folder with real content still refuses."""
+
+ def test_an_empty_leftover_folder_is_removed_and_the_repo_inited(self):
+ (self.eng / 'sub').mkdir(parents=True) # dirs only: no file anywhere
+ E.ensure_repo(self.eng)
+ self.assertTrue(E.is_repo(self.eng))
+ self.assertTrue((self.eng / '.git').is_dir())
+
+ def test_a_folder_with_a_real_file_still_refuses(self):
+ (self.eng / 'sub').mkdir(parents=True)
+ (self.eng / 'sub' / 'keep.txt').write_text('data')
+ with self.assertRaises(E.Fail):
+ E.ensure_repo(self.eng)
+ self.assertFalse(E.is_repo(self.eng))
+ self.assertTrue((self.eng / 'sub' / 'keep.txt').is_file()) # nothing deleted
+
+ def test_a_real_symlink_is_left_alone(self):
+ # macOS/Linux check symlinks out as symlinks; the walk must not treat one as junk
+ (self.eng / 'usr/lib').mkdir(parents=True)
+ (self.eng / 'usr/lib/libSystem.B.tbd').write_text('--- !tapi-tbd-v3\n')
+ (self.eng / 'usr/lib/libSystem.tbd').symlink_to('libSystem.B.tbd')
+ with self.assertRaises(E.Fail):
+ E.ensure_repo(self.eng)
+ self.assertTrue((self.eng / 'usr/lib/libSystem.tbd').is_symlink())
+
+
+
class Sync(Fixture):
def test_sync_builds_the_pin_plus_one_commit_per_patch_and_the_generated_files(self):
out = self.tool('sync')