2019-02-18 03:58:44 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2022-05-31 08:10:14 +01:00
|
|
|
# This pre-processor parses a single file containing a list of
|
2022-05-31 13:56:11 +01:00
|
|
|
# MP_REGISTER_MODULE(module_name, obj_module)
|
2019-02-18 03:58:44 +00:00
|
|
|
# These are used to generate a header with the required entries for
|
|
|
|
# "mp_rom_map_elem_t mp_builtin_module_table[]" in py/objmodule.c
|
|
|
|
|
|
|
|
from __future__ import print_function
|
|
|
|
|
|
|
|
import re
|
2019-04-12 02:34:52 +01:00
|
|
|
import io
|
2019-02-18 03:58:44 +00:00
|
|
|
import argparse
|
|
|
|
|
|
|
|
|
2022-05-31 13:56:11 +01:00
|
|
|
pattern = re.compile(r"\s*MP_REGISTER_MODULE\((.*?),\s*(.*?)\);", flags=re.DOTALL)
|
2019-02-18 03:58:44 +00:00
|
|
|
|
|
|
|
|
2022-05-31 08:10:14 +01:00
|
|
|
def find_module_registrations(filename):
|
|
|
|
"""Find any MP_REGISTER_MODULE definitions in the provided file.
|
2019-02-18 03:58:44 +00:00
|
|
|
|
2022-05-31 08:10:14 +01:00
|
|
|
:param str filename: path to file to check
|
2022-05-31 13:56:11 +01:00
|
|
|
:return: List[(module_name, obj_module)]
|
2019-02-18 03:58:44 +00:00
|
|
|
"""
|
|
|
|
global pattern
|
|
|
|
|
2022-05-31 08:10:14 +01:00
|
|
|
with io.open(filename, encoding="utf-8") as c_file_obj:
|
2019-02-18 03:58:44 +00:00
|
|
|
return set(re.findall(pattern, c_file_obj.read()))
|
|
|
|
|
|
|
|
|
|
|
|
def generate_module_table_header(modules):
|
|
|
|
"""Generate header with module table entries for builtin modules.
|
|
|
|
|
2022-05-31 13:56:11 +01:00
|
|
|
:param List[(module_name, obj_module)] modules: module defs
|
2019-02-18 03:58:44 +00:00
|
|
|
:return: None
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Print header file for all external modules.
|
2022-04-20 07:05:44 +01:00
|
|
|
mod_defs = set()
|
2019-02-18 03:58:44 +00:00
|
|
|
print("// Automatically generated by makemoduledefs.py.\n")
|
2022-05-31 13:56:11 +01:00
|
|
|
for module_name, obj_module in modules:
|
2019-02-18 03:58:44 +00:00
|
|
|
mod_def = "MODULE_DEF_{}".format(module_name.upper())
|
2022-04-20 07:05:44 +01:00
|
|
|
mod_defs.add(mod_def)
|
2019-02-18 03:58:44 +00:00
|
|
|
print(
|
2020-02-27 04:36:53 +00:00
|
|
|
(
|
2022-05-31 13:56:11 +01:00
|
|
|
"extern const struct _mp_obj_module_t {obj_module};\n"
|
|
|
|
"#undef {mod_def}\n"
|
|
|
|
"#define {mod_def} {{ MP_ROM_QSTR({module_name}), MP_ROM_PTR(&{obj_module}) }},\n"
|
2019-02-18 03:58:44 +00:00
|
|
|
).format(
|
|
|
|
module_name=module_name,
|
|
|
|
obj_module=obj_module,
|
|
|
|
mod_def=mod_def,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
print("\n#define MICROPY_REGISTERED_MODULES \\")
|
|
|
|
|
2022-04-20 07:05:44 +01:00
|
|
|
for mod_def in sorted(mod_defs):
|
2019-02-18 03:58:44 +00:00
|
|
|
print(" {mod_def} \\".format(mod_def=mod_def))
|
|
|
|
|
|
|
|
print("// MICROPY_REGISTERED_MODULES")
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
parser = argparse.ArgumentParser()
|
2022-05-31 08:10:14 +01:00
|
|
|
parser.add_argument("file", nargs=1, help="file with MP_REGISTER_MODULE definitions")
|
2019-02-18 03:58:44 +00:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
2022-05-31 08:10:14 +01:00
|
|
|
modules = find_module_registrations(args.file[0])
|
2019-02-18 03:58:44 +00:00
|
|
|
generate_module_table_header(sorted(modules))
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|