Bug 1676896 - Remove pycparser dependencies from rnp_symbols.py to make it more accessible. r=kaie

The recently added RNP_API macro makes it much easier to find the necessary functions to export
with a simple regex.

Differential Revision: https://phabricator.services.mozilla.com/D96839

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Rob Lemley 2020-12-09 03:46:42 +00:00
Родитель 3269720087
Коммит dc88fb6d82
1 изменённых файлов: 38 добавлений и 36 удалений

Просмотреть файл

@ -5,56 +5,58 @@
"""
Parse rnp/rnp.h header file and build a symbols file suitable
for use with mozbuild.
This script is meant to be run when the public C API of librnp adds or removes functions so that
they can be exported by the shared library.
Limitations: The regex that captures the function name is very basic and may need adjusting if
the third_party/rnp/include/rnp/rnp.h format changes too much.
Also note that APIs that are marked deprecated are not checked for.
Dependencies: Only Python 3
Running:
python3 rnp_symbols.py
Output will be on stdout, this is to give the developer the opportunity to compare the old and
new versions and check for accuracy.
"""
from __future__ import absolute_import, print_function
import sys
import os
try:
import pycparser_fake_libc
from pycparser import parse_file
from pycparser.c_ast import Decl
from pycparserext.ext_c_parser import (
FuncDeclExt,
GnuCParser,
)
except ImportError:
print(
"One or more dependencies not found: pycparser, pycparserext, pycparser_fake_libc"
)
sys.exit(1)
import re
HERE = os.path.dirname(__file__)
TOPSRCDIR = os.path.abspath(os.path.join(HERE, "../../../../"))
RNPSRCDIR = os.path.join(TOPSRCDIR, "comm/third_party/rnp")
def is_func(obj):
if isinstance(obj, Decl):
return isinstance(obj.type, FuncDeclExt)
FUNC_DECL_RE = re.compile(r"^RNP_API\s+.*?([a-zA-Z0-9_]+)\(.*$")
def get_func_name(line):
"""
Extract the function name from a RNP_API function declaration.
Examples:
RNP_API rnp_result_t rnp_enable_debug(const char *file);
RNP_API rnp_result_t rnp_ffi_create(rnp_ffi_t * ffi,
"""
m = FUNC_DECL_RE.match(line)
return m.group(1)
def extract_func_defs(filename):
# Note that cpp is used. Provide a path to your own cpp or
# make sure one exists in PATH.
rnp_export_path = os.path.join(RNPSRCDIR, "src/lib")
ast = parse_file(
filename,
use_cpp=True,
cpp_args=[
"-E",
"-I{}".format(rnp_export_path),
"-I{}".format(pycparser_fake_libc.directory),
],
parser=GnuCParser(lex_optimize=False, yacc_optimize=False),
)
for node in ast.children():
decl = node[1]
if is_func(decl):
yield decl.name
"""
Look for RNP_API in the header file to find the names of the symbols that should be exported
"""
with open(filename) as fp:
for line in fp:
if line.startswith("RNP_API"):
func_name = get_func_name(line)
yield func_name
if __name__ == "__main__":
@ -63,5 +65,5 @@ if __name__ == "__main__":
else:
FILENAME = os.path.join(RNPSRCDIR, "include/rnp/rnp.h")
for f in sorted(extract_func_defs(FILENAME)):
for f in sorted(list(extract_func_defs(FILENAME))):
print(f)