diff options
| author | David Lechner <david@pybricks.com> | 2022-07-01 12:29:08 -0500 |
|---|---|---|
| committer | Damien George <damien@micropython.org> | 2022-07-18 13:48:23 +1000 |
| commit | fc3d7ae11be11a7f05709ebfd439061fce9ee555 (patch) | |
| tree | 8c0ec32de2abad065c7adc8b6d18d1d7d3dffc18 /py/make_root_pointers.py | |
| parent | a8d78cc39839aaf4a77bef05377c457c7ba75ead (diff) | |
py/make_root_pointers: Add MP_REGISTER_ROOT_POINTER parser/generator.
This adds new compile-time infrastructure to parse source code files for
`MP_REGISTER_ROOT_POINTER()` and generates a new `root_pointers.h` header
file containing the collected declarations. This works the same as the
existing `MP_REGISTER_MODULE()` feature.
Signed-off-by: David Lechner <david@pybricks.com>
Diffstat (limited to 'py/make_root_pointers.py')
| -rw-r--r-- | py/make_root_pointers.py | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/py/make_root_pointers.py b/py/make_root_pointers.py new file mode 100644 index 000000000..efe398b82 --- /dev/null +++ b/py/make_root_pointers.py @@ -0,0 +1,55 @@ +""" +This pre-processor parses a single file containing a list of +MP_REGISTER_ROOT_POINTER(variable declaration) items. + +These are used to generate a header with the required entries for +"struct _mp_state_vm_t" in py/mpstate.h +""" + +from __future__ import print_function + +import argparse +import io +import re + + +PATTERN = re.compile(r"MP_REGISTER_ROOT_POINTER\((.*?)\);") + + +def find_root_pointer_registrations(filename): + """Find any MP_REGISTER_ROOT_POINTER definitions in the provided file. + + :param str filename: path to file to check + :return: List[variable_declaration] + """ + with io.open(filename, encoding="utf-8") as c_file_obj: + return set(re.findall(PATTERN, c_file_obj.read())) + + +def generate_root_pointer_header(root_pointers): + """Generate header with root pointer entries. + + :param List[variable_declaration] root_pointers: root pointer declarations + :return: None + """ + + # Print header file for all external modules. + print("// Automatically generated by make_root_pointers.py.") + print() + + for item in root_pointers: + print(item, end=";") + print() + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument("file", nargs=1, help="file with MP_REGISTER_ROOT_POINTER definitions") + args = parser.parse_args() + + root_pointers = find_root_pointer_registrations(args.file[0]) + generate_root_pointer_header(sorted(root_pointers)) + + +if __name__ == "__main__": + main() |
