| 1 |
#!/usr/bin/env python3 |
| 2 |
# Copyright (c) 2022 L. E. Segovia <[email protected]> |
| 3 |
# |
| 4 |
# This file is part of the FFmpeg Meson build |
| 5 |
# |
| 6 |
# This library is free software; you can redistribute it and/or |
| 7 |
# modify it under the terms of the GNU Lesser General Public |
| 8 |
# License as published by the Free Software Foundation; either |
| 9 |
# version 2.1 of the License, or (at your option) any later version. |
| 10 |
# |
| 11 |
# This library is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
# Lesser General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU Lesser General Public |
| 17 |
# License along with this library; if not, see <http://www.gnu.org/licenses/>. |
| 18 |
|
| 19 |
import argparse |
| 20 |
import errno |
| 21 |
import os |
| 22 |
import pathlib |
| 23 |
import re |
| 24 |
import subprocess |
| 25 |
|
| 26 |
|
| 27 |
def output(platform, symbols): |
| 28 |
if platform == 'win': |
| 29 |
print("EXPORTS") |
| 30 |
print(*[f' {symbol}' for symbol in sorted(set(symbols))], sep='\n') |
| 31 |
elif platform == 'darwin': |
| 32 |
print(*[f'{prefix}{symbol}' for symbol in sorted(set(symbols))], sep='\n') |
| 33 |
else: |
| 34 |
print('{') |
| 35 |
print(' global:') |
| 36 |
print( |
| 37 |
*[f' {prefix}{symbol};' for symbol in sorted(set(symbols))], sep='\n') |
| 38 |
print(' local:') |
| 39 |
print(' *;') |
| 40 |
print('};') |
| 41 |
|
| 42 |
|
| 43 |
if __name__ == '__main__': |
| 44 |
arg_parser = argparse.ArgumentParser( |
| 45 |
description='Craft the symbols exports file') |
| 46 |
|
| 47 |
arg_parser.add_argument('--prefix', metavar='PREFIX', |
| 48 |
help='Prefix for extern symbols') |
| 49 |
g = arg_parser.add_argument_group('Library parsing tool') |
| 50 |
group = g.add_mutually_exclusive_group(required=True) |
| 51 |
group.add_argument('--nm', metavar='NM_PATH', type=pathlib.Path, |
| 52 |
help='If specified, runs this instead of dumpbin (MinGW)') |
| 53 |
group.add_argument('--dumpbin', metavar='DUMPBIN_PATH', type=pathlib.Path, |
| 54 |
help='If specified, runs this instead of nm (MSVC)') |
| 55 |
group.add_argument( |
| 56 |
'--list', action='store_true', help='If specified, consider FILE as an exported symbols list instead of a library') |
| 57 |
g = arg_parser.add_argument_group('Symbol naming') |
| 58 |
group = g.add_mutually_exclusive_group(required=True) |
| 59 |
group.add_argument('--regex', metavar='REGEX', type=str, |
| 60 |
nargs='+', |
| 61 |
help='Regular expression for exported symbols') |
| 62 |
group.add_argument('--vscript', metavar='VERSION_SCRIPT', |
| 63 |
type=argparse.FileType('r'), help='Version script') |
| 64 |
arg_parser.add_argument('--os', type=str, choices=('win', 'linux', 'darwin'), |
| 65 |
default='linux', required=True, |
| 66 |
help='Target operating system for the exports file (win = MSVC module definition file, linux = version script, darwin = exported symbols list)') |
| 67 |
arg_parser.add_argument('libnames', metavar='FILE', type=pathlib.Path, |
| 68 |
nargs='+', |
| 69 |
help='Source file(s) to parse') |
| 70 |
|
| 71 |
args = arg_parser.parse_args() |
| 72 |
|
| 73 |
libnames = args.libnames |
| 74 |
|
| 75 |
for libname in libnames: |
| 76 |
if not libname.exists(): |
| 77 |
raise FileNotFoundError( |
| 78 |
errno.ENOENT, os.strerror(errno.ENOENT), libname) |
| 79 |
|
| 80 |
if not args.list and len(libnames) > 1: |
| 81 |
raise ValueError("Expect 1 filename as argument.") |
| 82 |
|
| 83 |
prefix = args.prefix or '' |
| 84 |
started = 0 |
| 85 |
regex = [] |
| 86 |
|
| 87 |
if args.vscript: |
| 88 |
for line in args.vscript: |
| 89 |
# We only care about global symbols |
| 90 |
if re.match(r'^\s+global:', line): |
| 91 |
started = 1 |
| 92 |
line = re.sub(r'^\s+global: *', '', line) |
| 93 |
else: |
| 94 |
if re.match(r'^\s+local:', line): |
| 95 |
started = 0 |
| 96 |
|
| 97 |
if started == 0: |
| 98 |
continue |
| 99 |
|
| 100 |
line = line.replace(';', '') |
| 101 |
|
| 102 |
for exp in line.split(): |
| 103 |
# Remove leading and trailing whitespace |
| 104 |
regex.append(exp.strip()) |
| 105 |
else: |
| 106 |
regex.extend(args.regex) |
| 107 |
|
| 108 |
# Ensure things are compatible on Windows with Python 3.7.x |
| 109 |
libname_path_posix = pathlib.Path(libnames[0]).as_posix() |
| 110 |
|
| 111 |
if args.list: |
| 112 |
dump = [] |
| 113 |
for libname in libnames: |
| 114 |
syms = libname.open('r', encoding='utf-8').readlines() |
| 115 |
# Strip whitespaces |
| 116 |
syms = [x.strip() for x in syms] |
| 117 |
# Exclude blank lines |
| 118 |
syms = [x for x in syms if len(x) > 0] |
| 119 |
dump.extend(syms) |
| 120 |
elif args.nm is not None: |
| 121 |
# Use eval, since NM="nm -g" |
| 122 |
# Add -j to ensure only symbol names are output (otherwise in macOS |
| 123 |
# a race condition can occur in the redirection) |
| 124 |
# And use `--no-llvm-bc` in case it's /usr/bin/nm on macOS |
| 125 |
s = subprocess.run( |
| 126 |
[args.nm, '-U', '-g', '-j', '--no-llvm-bc', libname_path_posix], |
| 127 |
stdout=subprocess.PIPE, |
| 128 |
stderr=subprocess.STDOUT, |
| 129 |
universal_newlines=True, |
| 130 |
check=False, |
| 131 |
) |
| 132 |
if s.returncode != 0: |
| 133 |
# If it fails, retry with --defined-only (non macOS) |
| 134 |
s = subprocess.run( |
| 135 |
[args.nm, '--defined-only', '-g', '-j', '--no-llvm-bc', libname_path_posix], |
| 136 |
stdout=subprocess.PIPE, |
| 137 |
stderr=subprocess.STDOUT, |
| 138 |
universal_newlines=True, |
| 139 |
check=False, |
| 140 |
) |
| 141 |
if s.returncode != 0: |
| 142 |
# If it fails, retry without skipping LLVM bitcode (macOS flag) |
| 143 |
# Don't use -U, as that was an alias for --unicode= instead of |
| 144 |
# --defined-only before Binutils 2.39 |
| 145 |
s = subprocess.run( |
| 146 |
[args.nm, '--defined-only', '-g', '-j', libname_path_posix], |
| 147 |
stdout=subprocess.PIPE, |
| 148 |
stderr=subprocess.STDOUT, |
| 149 |
universal_newlines=True, |
| 150 |
check=False, |
| 151 |
) |
| 152 |
if s.returncode != 0: |
| 153 |
# -j was added only in Binutils 2.37 |
| 154 |
s = subprocess.run( |
| 155 |
[args.nm, '--defined-only', '-g', libname_path_posix], |
| 156 |
stdout=subprocess.PIPE, |
| 157 |
stderr=subprocess.DEVNULL, |
| 158 |
universal_newlines=True, |
| 159 |
check=True, |
| 160 |
) |
| 161 |
dump = s.stdout.splitlines() |
| 162 |
# Exclude lines with ':' (object name) |
| 163 |
dump = [x for x in dump if ":" not in x] |
| 164 |
# Exclude blank lines |
| 165 |
dump = [x for x in dump if len(x) > 0] |
| 166 |
# Subst the prefix out |
| 167 |
dump = [re.sub(f'^{prefix}', '', x) for x in dump] |
| 168 |
else: |
| 169 |
dump = subprocess.run([pathlib.Path(args.dumpbin).as_posix(), '-linkermember:1', libname_path_posix], |
| 170 |
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True).stdout.splitlines() |
| 171 |
# Find the index of the first line with |
| 172 |
# "public symbols", keep the rest |
| 173 |
# Then the line with " Summary", |
| 174 |
# delete it and the rest |
| 175 |
for i, line in enumerate(dump): |
| 176 |
if 'public symbols' in line: |
| 177 |
start = i |
| 178 |
elif re.match(r'\s+Summary', line): |
| 179 |
end = i |
| 180 |
dump = dump[start:end] |
| 181 |
# Substitute prefix out |
| 182 |
dump = [re.sub(fr'\s+{prefix}', ' ', x) for x in dump] |
| 183 |
# Substitute big chonky spaces out |
| 184 |
dump = [re.sub(r'\s+', ' ', x) for x in dump] |
| 185 |
# Exclude blank lines |
| 186 |
dump = [x for x in dump if len(x) > 0] |
| 187 |
# Take only the *second* field (split by spaces) |
| 188 |
# Python's split excludes whitespace at the beginning |
| 189 |
dump = [x.split()[1] for x in dump] |
| 190 |
|
| 191 |
symbols = [] |
| 192 |
for exp in regex: |
| 193 |
for i in dump: |
| 194 |
if re.match(exp, i): |
| 195 |
symbols.append(i) |
| 196 |
|
| 197 |
output(args.os, symbols) |