py/makemoduledefs.py: Add a way to register extensible built-in modules.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
This commit is contained in:
Jim Mussared
2023-06-02 12:28:07 +10:00
parent 45ac651d1a
commit 24c02c4eb5
8 changed files with 97 additions and 33 deletions

View File

@@ -1,8 +1,17 @@
"""
This pre-processor parses a single file containing a list of
MP_REGISTER_MODULE(module_name, obj_module)
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
`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.
"""
from __future__ import print_function
@@ -13,7 +22,10 @@ import io
import argparse
pattern = re.compile(r"\s*MP_REGISTER_MODULE\((.*?),\s*(.*?)\);", flags=re.DOTALL)
pattern = re.compile(
r"\s*(MP_REGISTER_MODULE|MP_REGISTER_EXTENSIBLE_MODULE)\(MP_QSTR_(.*?),\s*(.*?)\);",
flags=re.DOTALL,
)
def find_module_registrations(filename):
@@ -37,14 +49,23 @@ def generate_module_table_header(modules):
# Print header file for all external modules.
mod_defs = set()
extensible_mod_defs = set()
print("// Automatically generated by makemoduledefs.py.\n")
for module_name, obj_module in modules:
for macro_name, module_name, obj_module in modules:
mod_def = "MODULE_DEF_{}".format(module_name.upper())
mod_defs.add(mod_def)
if macro_name == "MP_REGISTER_MODULE":
mod_defs.add(mod_def)
elif macro_name == "MP_REGISTER_EXTENSIBLE_MODULE":
extensible_mod_defs.add(mod_def)
if "," in obj_module:
print(
"ERROR: Call to MP_REGISTER_MODULE({}, {}) should be MP_REGISTER_MODULE({}, {})\n".format(
module_name, obj_module, module_name, obj_module.split(",")[0]
"ERROR: Call to {}({}, {}) should be {}({}, {})\n".format(
macro_name,
module_name,
obj_module,
macro_name,
module_name,
obj_module.split(",")[0],
),
file=sys.stderr,
)
@@ -53,7 +74,7 @@ def generate_module_table_header(modules):
(
"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"
"#define {mod_def} {{ MP_ROM_QSTR(MP_QSTR_{module_name}), MP_ROM_PTR(&{obj_module}) }},\n"
).format(
module_name=module_name,
obj_module=obj_module,
@@ -68,6 +89,13 @@ def generate_module_table_header(modules):
print("// MICROPY_REGISTERED_MODULES")
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")
def main():
parser = argparse.ArgumentParser()