2022-06-08 00:47:21 -04:00
|
|
|
"""
|
|
|
|
This pre-processor parses a single file containing a list of
|
2023-06-01 22:28:07 -04:00
|
|
|
`MP_REGISTER_MODULE(MP_QSTR_module_name, obj_module)` or
|
|
|
|
`MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_module_name, obj_module)`
|
|
|
|
(i.e. the output of `py/makeqstrdefs.py cat module`).
|
|
|
|
|
|
|
|
The output is a header (typically moduledefs.h) which is included by
|
|
|
|
py/objmodule.c that contains entries to be included in the definition of
|
|
|
|
- mp_rom_map_elem_t mp_builtin_module_table[]
|
|
|
|
- mp_rom_map_elem_t mp_builtin_extensible_module_table[]
|
|
|
|
|
|
|
|
Extensible modules are modules that can be overridden from the filesystem, see
|
|
|
|
py/builtinimnport.c:process_import_at_level. Regular modules will always use
|
|
|
|
the built-in version.
|
2022-06-08 00:47:21 -04:00
|
|
|
"""
|
2019-02-17 22:58:44 -05:00
|
|
|
|
|
|
|
from __future__ import print_function
|
|
|
|
|
2022-06-14 06:17:03 -04:00
|
|
|
import sys
|
2019-02-17 22:58:44 -05:00
|
|
|
import re
|
2019-04-11 21:34:52 -04:00
|
|
|
import io
|
2019-02-17 22:58:44 -05:00
|
|
|
import argparse
|
|
|
|
|
|
|
|
|
2023-06-05 01:52:57 -04:00
|
|
|
register_pattern = re.compile(
|
2023-06-01 22:28:07 -04:00
|
|
|
r"\s*(MP_REGISTER_MODULE|MP_REGISTER_EXTENSIBLE_MODULE)\(MP_QSTR_(.*?),\s*(.*?)\);",
|
|
|
|
flags=re.DOTALL,
|
|
|
|
)
|
2019-02-17 22:58:44 -05:00
|
|
|
|
2023-06-05 01:52:57 -04:00
|
|
|
delegation_pattern = re.compile(
|
|
|
|
r"\s*(?:MP_REGISTER_MODULE_DELEGATION)\((.*?),\s*(.*?)\);",
|
|
|
|
flags=re.DOTALL,
|
|
|
|
)
|
|
|
|
|
2019-02-17 22:58:44 -05:00
|
|
|
|
2022-05-31 03:10:14 -04:00
|
|
|
def find_module_registrations(filename):
|
|
|
|
"""Find any MP_REGISTER_MODULE definitions in the provided file.
|
2019-02-17 22:58:44 -05:00
|
|
|
|
2022-05-31 03:10:14 -04:00
|
|
|
:param str filename: path to file to check
|
2022-05-31 08:56:11 -04:00
|
|
|
:return: List[(module_name, obj_module)]
|
2019-02-17 22:58:44 -05:00
|
|
|
"""
|
|
|
|
global pattern
|
|
|
|
|
2022-05-31 03:10:14 -04:00
|
|
|
with io.open(filename, encoding="utf-8") as c_file_obj:
|
2023-06-05 01:52:57 -04:00
|
|
|
c = c_file_obj.read()
|
|
|
|
return set(re.findall(register_pattern, c)), set(re.findall(delegation_pattern, c))
|
2019-02-17 22:58:44 -05:00
|
|
|
|
|
|
|
|
|
|
|
def generate_module_table_header(modules):
|
2020-08-29 01:14:29 -04:00
|
|
|
"""Generate header with module table entries for builtin modules.
|
2019-02-17 22:58:44 -05:00
|
|
|
|
2022-05-31 08:56:11 -04:00
|
|
|
:param List[(module_name, obj_module)] modules: module defs
|
2019-02-17 22:58:44 -05:00
|
|
|
:return: None
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Print header file for all external modules.
|
2022-04-20 02:05:44 -04:00
|
|
|
mod_defs = set()
|
2023-06-01 22:28:07 -04:00
|
|
|
extensible_mod_defs = set()
|
|
|
|
for macro_name, module_name, obj_module in modules:
|
2019-02-17 22:58:44 -05:00
|
|
|
mod_def = "MODULE_DEF_{}".format(module_name.upper())
|
2023-06-01 22:28:07 -04:00
|
|
|
if macro_name == "MP_REGISTER_MODULE":
|
|
|
|
mod_defs.add(mod_def)
|
|
|
|
elif macro_name == "MP_REGISTER_EXTENSIBLE_MODULE":
|
|
|
|
extensible_mod_defs.add(mod_def)
|
2022-06-14 06:17:03 -04:00
|
|
|
if "," in obj_module:
|
|
|
|
print(
|
2023-06-01 22:28:07 -04:00
|
|
|
"ERROR: Call to {}({}, {}) should be {}({}, {})\n".format(
|
|
|
|
macro_name,
|
|
|
|
module_name,
|
|
|
|
obj_module,
|
|
|
|
macro_name,
|
|
|
|
module_name,
|
|
|
|
obj_module.split(",")[0],
|
2022-06-14 06:17:03 -04:00
|
|
|
),
|
|
|
|
file=sys.stderr,
|
|
|
|
)
|
|
|
|
sys.exit(1)
|
2020-02-26 23:36:53 -05:00
|
|
|
print(
|
|
|
|
(
|
2022-05-31 08:56:11 -04:00
|
|
|
"extern const struct _mp_obj_module_t {obj_module};\n"
|
|
|
|
"#undef {mod_def}\n"
|
2023-06-01 22:28:07 -04:00
|
|
|
"#define {mod_def} {{ MP_ROM_QSTR(MP_QSTR_{module_name}), MP_ROM_PTR(&{obj_module}) }},\n"
|
2020-02-26 23:36:53 -05:00
|
|
|
).format(
|
|
|
|
module_name=module_name,
|
|
|
|
obj_module=obj_module,
|
|
|
|
mod_def=mod_def,
|
|
|
|
)
|
2019-02-17 22:58:44 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
print("\n#define MICROPY_REGISTERED_MODULES \\")
|
|
|
|
|
2022-04-20 02:05:44 -04:00
|
|
|
for mod_def in sorted(mod_defs):
|
2019-02-17 22:58:44 -05:00
|
|
|
print(" {mod_def} \\".format(mod_def=mod_def))
|
|
|
|
|
|
|
|
print("// MICROPY_REGISTERED_MODULES")
|
|
|
|
|
2023-06-01 22:28:07 -04:00
|
|
|
print("\n#define MICROPY_REGISTERED_EXTENSIBLE_MODULES \\")
|
|
|
|
|
|
|
|
for mod_def in sorted(extensible_mod_defs):
|
|
|
|
print(" {mod_def} \\".format(mod_def=mod_def))
|
|
|
|
|
|
|
|
print("// MICROPY_REGISTERED_EXTENSIBLE_MODULES")
|
|
|
|
|
2019-02-17 22:58:44 -05:00
|
|
|
|
2023-06-05 01:52:57 -04:00
|
|
|
def generate_module_delegations(delegations):
|
2023-06-06 08:49:50 -04:00
|
|
|
if not delegations:
|
|
|
|
return
|
|
|
|
|
2023-06-11 23:09:48 -04:00
|
|
|
print()
|
|
|
|
for obj_module, fun_name in delegations:
|
|
|
|
print("extern void {}(mp_obj_t self_in, qstr attr, mp_obj_t *dest);".format(fun_name))
|
|
|
|
print("#define MICROPY_MODULE_DELEGATIONS \\")
|
2023-06-05 01:52:57 -04:00
|
|
|
for obj_module, fun_name in delegations:
|
|
|
|
print(
|
2023-06-11 23:09:29 -04:00
|
|
|
" {{ MP_ROM_PTR(&{obj_module}), {fun_name} }}, \\".format(
|
2023-06-05 01:52:57 -04:00
|
|
|
obj_module=obj_module, fun_name=fun_name
|
|
|
|
)
|
|
|
|
)
|
|
|
|
print("// MICROPY_MODULE_DELEGATIONS")
|
|
|
|
|
|
|
|
|
2019-02-17 22:58:44 -05:00
|
|
|
def main():
|
|
|
|
parser = argparse.ArgumentParser()
|
2022-05-31 03:10:14 -04:00
|
|
|
parser.add_argument("file", nargs=1, help="file with MP_REGISTER_MODULE definitions")
|
2019-02-17 22:58:44 -05:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
2023-06-05 01:52:57 -04:00
|
|
|
print("// Automatically generated by makemoduledefs.py.\n")
|
|
|
|
|
|
|
|
modules, delegations = find_module_registrations(args.file[0])
|
2019-02-17 22:58:44 -05:00
|
|
|
generate_module_table_header(sorted(modules))
|
2023-06-05 01:52:57 -04:00
|
|
|
generate_module_delegations(sorted(delegations))
|
2019-02-17 22:58:44 -05:00
|
|
|
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
if __name__ == "__main__":
|
2019-02-17 22:58:44 -05:00
|
|
|
main()
|