"""Adds recorded symbols to .tbd stubs as an extra arm64/arm64e export block (see resolve-undefined). Usage: patch-tbd.py — prints the SDK-relative paths it changed.""" import collections import re import sys lst, sdk = sys.argv[1], sys.argv[2] by = collections.defaultdict(list) for line in open(lst): if '\t' in line: lib, sym = line.rstrip('\n').split('\t', 1) by[lib].append(sym) for lib, syms in sorted(by.items()): rel = lib[:-len('.dylib')] + '.tbd' if lib.endswith('.dylib') else lib + '.tbd' path = sdk + rel try: text = open(path).read() except FileNotFoundError: print('missing stub ' + rel, file=sys.stderr) continue text = re.sub(r' # mulberry-extra\n.*? # mulberry-end\n', '', text, flags=re.S) objc = sorted({s.split('_$_', 1)[1] for s in syms if s.startswith(('_OBJC_CLASS_$_', '_OBJC_METACLASS_$_'))}) plain = sorted(s for s in syms if not s.startswith('_OBJC_')) block = ' # mulberry-extra\n - archs: [ arm64, arm64e ]\n' if plain: block += ' symbols: [ ' + ', '.join("'%s'" % s.replace("'", "''") for s in plain) + ' ]\n' if objc: block += ' objc-classes: [ ' + ', '.join(objc) + ' ]\n' block += ' # mulberry-end\n' text, n = re.subn(r'^exports:[ \t]*\n', lambda m: m.group(0) + block, text, count=1, flags=re.M) if n == 0: # Stub without any exports: add the section before the document end marker. text, n = re.subn(r'^\.\.\.[ \t]*$', lambda m: 'exports:\n' + block + m.group(0), text, count=1, flags=re.M) if n == 0: print('cannot patch ' + rel, file=sys.stderr) continue open(path, 'w').write(text) print(rel.lstrip('/'))