| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
# Copyright (c) 2023 Firebuild Inc. |
| 4 |
# All rights reserved. |
| 5 |
# Free for personal use and commercial trial. |
| 6 |
# Non-trial commercial use requires licenses available from https://firebuild.com. |
| 7 |
# Modification and redistribution are permitted, but commercial use of |
| 8 |
# derivative works is subject to the same requirements of this license |
| 9 |
# |
| 10 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 11 |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 12 |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 13 |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 14 |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 15 |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 16 |
# SOFTWARE. |
| 17 |
|
| 18 |
# Parse mac tbd and print exported symbols for a library and an architecture |
| 19 |
|
| 20 |
import re |
| 21 |
import sys |
| 22 |
|
| 23 |
list_pattern=r'[^:^[^ ^,^\]^\']+' |
| 24 |
|
| 25 |
|
| 26 |
def parse_tapi_tbd(text): |
| 27 |
entries = re.split(r'--- !tapi-tbd', text)[1:] |
| 28 |
result = {} |
| 29 |
|
| 30 |
for entry in entries: |
| 31 |
lines = entry.strip().split('\n') |
| 32 |
entry_data = {'tbd-version': None, |
| 33 |
'targets': [], |
| 34 |
'install-name': None, |
| 35 |
'current-version': None, |
| 36 |
'reexported-libraries': {}, |
| 37 |
'parent-umbrella': {}, |
| 38 |
'exports': {}} |
| 39 |
key = None # remember top level key |
| 40 |
subkey = None |
| 41 |
targets = None |
| 42 |
for line in lines: |
| 43 |
if line.startswith("targets:"): |
| 44 |
key, value = map(str.strip, line.split(':', 1)) |
| 45 |
entry_data[key].extend(re.findall(list_pattern, value)) |
| 46 |
subkey = None |
| 47 |
elif line.startswith("install-name:") \ |
| 48 |
or line.startswith("current-version:") \ |
| 49 |
or line.startswith("compatibility-version:") \ |
| 50 |
or line.startswith("tbd-version:"): |
| 51 |
key, value = map(str.strip, line.split(':', 1)) |
| 52 |
entry_data[key] = re.sub(r'\'', "", value) |
| 53 |
subkey = None |
| 54 |
elif line == "reexported-libraries:" \ |
| 55 |
or line == "parent-umbrella:" \ |
| 56 |
or line == "reexports:" \ |
| 57 |
or line == "exports:": |
| 58 |
key, _value = map(str.strip, line.split(':', 1)) |
| 59 |
entry_data[key] = {} |
| 60 |
for target in entry_data['targets']: |
| 61 |
entry_data[key][target] = [] |
| 62 |
elif line.startswith(" umbrella:"): |
| 63 |
subkey, value = map(str.strip, line.split(':', 1)) |
| 64 |
entry_data[key][subkey] = value |
| 65 |
elif line.startswith(" - targets:"): |
| 66 |
subkey, value = map(str.strip, line.split(':', 1)) |
| 67 |
subkey = subkey.replace("-", " ").strip() |
| 68 |
targets = re.findall(list_pattern, value) |
| 69 |
elif line.startswith(" libraries:") \ |
| 70 |
or line.startswith(" symbols:"): |
| 71 |
subkey, value = map(str.strip, line.split(':', 1)) |
| 72 |
subkey = subkey.replace("-", " ").strip() |
| 73 |
for target in targets: |
| 74 |
entry_data[key][target].extend(re.findall(list_pattern, value)) |
| 75 |
else: |
| 76 |
if subkey is None: |
| 77 |
try: |
| 78 |
entry_data[key].extend(re.findall(list_pattern, line)) |
| 79 |
except Exception as e: |
| 80 |
print("Error parsing line \"{}\" in {}: [{}] {}".format(line, sys.argv[1], type(e).__name__, str(e)), file=sys.stderr) |
| 81 |
sys.exit(1) |
| 82 |
elif subkey == 'targets': |
| 83 |
targets.extend(re.findall(list_pattern, line)) |
| 84 |
elif subkey in ('symbols', 'libraries'): |
| 85 |
for target in targets: |
| 86 |
entry_data[key][target].extend(re.findall(list_pattern, line)) |
| 87 |
result[entry_data['install-name']] = entry_data |
| 88 |
|
| 89 |
return result |
| 90 |
|
| 91 |
if len(sys.argv) != 4: |
| 92 |
print("Usage:") |
| 93 |
print(" {} <.tbd> <library> <architecture>".format(sys.argv[0])) |
| 94 |
sys.exit(1) |
| 95 |
|
| 96 |
with open(sys.argv[1], "r") as f: |
| 97 |
tbd = parse_tapi_tbd(f.read()) |
| 98 |
for symbol in tbd[sys.argv[2]]['exports'][sys.argv[3]]: |
| 99 |
print(symbol) |
| 100 |
|