Skip to content

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

Windows: materialize the SDK's git symlinks so ld64.lld can link

LNPANDY628da573a7c5
diff --git a/frontend-qt/nspb_qt/win/sdk_fetch.py b/frontend-qt/nspb_qt/win/sdk_fetch.py
index 0fe073fc..be71d153 100644
--- a/frontend-qt/nspb_qt/win/sdk_fetch.py
+++ b/frontend-qt/nspb_qt/win/sdk_fetch.py
@@ -62,6 +62,51 @@ def rmtree(path: Path) -> None:
# ---------------------------------------------------------------- git route
+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.
+
+ The repository stores the SDK's internal aliases as git symlinks (`usr/lib/libSystem.tbd` →
+ `libSystem.B.tbd`; about 300 `.tbd` files). macOS and Linux check those out as symlinks;
+ Windows checks each one out as a small text file holding the target's path (real symlinks
+ need Developer Mode). `ld64.lld` then opens `libSystem.tbd`, finds no `--- !tapi-tbd` in it,
+ and refuses: "libSystem.tbd: unhandled file type", which fails the link probe four ways and
+ no build starts. Copying each target over its stub makes the SDK link; the files are small.
+
+ `repo` is the clone (its `.git` still exists at this point), `sub` the SDK folder inside it
+ ("" = the whole repo). Returns how many stubs were replaced. Real symlinks are left alone.
+ """
+ args = ['ls-files', '-s', '-z']
+ if sub:
+ args += ['--', sub]
+ try:
+ r = subprocess.run(['git', '-C', str(repo), *args], capture_output=True, **tc.no_window())
+ except OSError:
+ return 0
+ if r.returncode:
+ return 0
+ fixed = 0
+ for entry in r.stdout.split(b'\x00'):
+ if not entry:
+ continue
+ meta, _, rel = entry.partition(b'\t')
+ if meta.split(b' ')[0] != b'120000':
+ continue
+ p = repo / os.fsdecode(rel)
+ if not p.is_file() or p.is_symlink():
+ continue
+ target = p.read_text(encoding='utf-8', errors='replace').strip()
+ real = (p.parent / target).resolve()
+ if not real.is_file():
+ log(f' {rel.decode("utf-8", "replace")}: its target {target!r} is missing, left as it is')
+ continue
+ p.write_bytes(real.read_bytes())
+ fixed += 1
+ if fixed:
+ log(f' materialized {fixed} symlink stubs into real files (Windows checkout of git symlinks)')
+ return fixed
+
+
+
def clone_sdk(version: str = tc.SDK_PINNED, dest: Path | None = None,
log: Callable[[str], None] | None = None) -> Path:
"""Sparse-clone one SDK out of `xybp888/iOS-SDKs` and return the folder.
@@ -148,6 +193,9 @@ def clone_sdk(version: str = tc.SDK_PINNED, dest: Path | None = None,
src = work / version
if not tc.is_sdk_dir(src):
raise SdkError(f'{tc.SDK_REPO} has no usable {version}: pick another version, or use a .xip')
+ # The .git folder is deleted with `work` after the move, so this has to happen here, while
+ # the index can still tell symlinks from the text files a Windows checkout left behind.
+ materialize_symlinks(work, version, log=log)
if target.exists():
shutil.rmtree(target, ignore_errors=True)
shutil.move(str(src), str(target))
diff --git a/frontend-qt/tests/test_win.py b/frontend-qt/tests/test_win.py
index fe772cc6..0ef9bd46 100644
--- a/frontend-qt/tests/test_win.py
+++ b/frontend-qt/tests/test_win.py
@@ -23,6 +23,7 @@ import json
import os
import subprocess
import sys
+import shutil
import tempfile
import unittest
import zipfile
@@ -33,7 +34,7 @@ HERE = Path(__file__).resolve().parent
QT = HERE.parent
sys.path.insert(0, str(QT))
-from nspb_qt.win import build_app, fix_ipa_modes, shim # noqa: E402
+from nspb_qt.win import build_app, fix_ipa_modes, sdk_fetch, shim # noqa: E402
from nspb_qt.win import toolchain as tc # noqa: E402
@@ -1623,6 +1624,79 @@ class LocalizationTest(unittest.TestCase):
self.assertTrue(zh and zh != k and en and en != k, f'{k} is not bilingual')
+class SymlinkMaterializeTest(unittest.TestCase):
+ """The SDK git route on Windows: symlinks checked out as text stubs get their targets copied
+ over them, so ld64.lld finds a real `--- !tapi-tbd` file instead of 15 bytes of path text."""
+
+ def _git(self, *args: str, cwd: Path) -> None:
+ env = dict(os.environ, GIT_AUTHOR_NAME='t', GIT_AUTHOR_EMAIL='t@t',
+ GIT_COMMITTER_NAME='t', GIT_COMMITTER_EMAIL='t@t')
+ r = subprocess.run(['git', *args], cwd=cwd, capture_output=True, text=True, env=env)
+ self.assertEqual(r.returncode, 0, r.stderr or r.stdout)
+
+ def test_a_windows_text_stub_gets_its_target_copied_over(self) -> None:
+ if shutil.which('git') is None:
+ self.skipTest('git is not on PATH')
+ base = Path(tempfile.mkdtemp(prefix='nspb-symlink-'))
+ self.addCleanup(shutil.rmtree, base, ignore_errors=True)
+ remote, work = base / 'remote', base / 'work'
+ (remote / 'usr/lib').mkdir(parents=True)
+ tbd = '--- !tapi-tbd-v3\narchs: [arm64]\ninstall-name: /usr/lib/libSystem.B.dylib\n'
+ (remote / 'usr/lib/libSystem.B.tbd').write_text(tbd)
+ (remote / 'usr/lib/libSystem.tbd').symlink_to('libSystem.B.tbd')
+ self._git('init', '-q', cwd=remote)
+ self._git('add', '.', cwd=remote)
+ self._git('commit', '-qm', 'sdk', cwd=remote)
+ # A Windows plain clone checks the symlink out as a text file holding the target's path.
+ self._git('-c', 'core.symlinks=false', 'clone', '-q', str(remote), str(work), cwd=base)
+ stub = work / 'usr/lib/libSystem.tbd'
+ self.assertEqual(stub.read_text(), 'libSystem.B.tbd') # the broken shape the log showed
+ log: list[str] = []
+ fixed = sdk_fetch.materialize_symlinks(work, '', log.append)
+ self.assertEqual(fixed, 1)
+ self.assertFalse(stub.is_symlink())
+ self.assertEqual(stub.read_text(), tbd)
+ self.assertTrue((work / 'usr/lib/libSystem.B.tbd').exists())
+
+ def test_a_real_symlink_is_left_alone(self) -> None:
+ # macOS and Linux check the SDK's symlinks out as symlinks: nothing to materialize.
+ if shutil.which('git') is None:
+ self.skipTest('git is not on PATH')
+ base = Path(tempfile.mkdtemp(prefix='nspb-symlink-'))
+ self.addCleanup(shutil.rmtree, base, ignore_errors=True)
+ repo = base / 'sdk'
+ (repo / 'usr/lib').mkdir(parents=True)
+ (repo / 'usr/lib/libSystem.B.tbd').write_text('--- !tapi-tbd-v3\n')
+ (repo / 'usr/lib/libSystem.tbd').symlink_to('libSystem.B.tbd')
+ self._git('init', '-q', cwd=repo)
+ self._git('add', '.', cwd=repo)
+ self._git('commit', '-qm', 'sdk', cwd=repo)
+ log: list[str] = []
+ self.assertEqual(sdk_fetch.materialize_symlinks(repo, '', log.append), 0)
+ self.assertTrue((repo / 'usr/lib/libSystem.tbd').is_symlink())
+ self.assertEqual(log, [])
+
+ def test_a_stub_whose_target_is_missing_is_reported_and_left(self) -> None:
+ # The target was deleted after a broken checkout: nothing to copy, said so in the log.
+ if shutil.which('git') is None:
+ self.skipTest('git is not on PATH')
+ base = Path(tempfile.mkdtemp(prefix='nspb-symlink-'))
+ self.addCleanup(shutil.rmtree, base, ignore_errors=True)
+ remote, work = base / 'remote', base / 'work'
+ (remote / 'usr/lib').mkdir(parents=True)
+ (remote / 'usr/lib/libSystem.B.tbd').write_text('--- !tapi-tbd-v3\n')
+ (remote / 'usr/lib/libSystem.tbd').symlink_to('libSystem.B.tbd')
+ self._git('init', '-q', cwd=remote)
+ self._git('add', '.', cwd=remote)
+ self._git('commit', '-qm', 'sdk', cwd=remote)
+ self._git('-c', 'core.symlinks=false', 'clone', '-q', str(remote), str(work), cwd=base)
+ (work / 'usr/lib/libSystem.B.tbd').unlink()
+ log: list[str] = []
+ self.assertEqual(sdk_fetch.materialize_symlinks(work, '', log.append), 0)
+ self.assertTrue(any('missing' in l for l in log))
+
+
+
class SaveConfigTest(unittest.TestCase):
"""The Choose-a-folder path writes toolchain.json and reads it back through setting()."""