11"""
22This pre-processor parses a single file containing a list of
3- MP_REGISTER_MODULE(module_name, obj_module)
4- These are used to generate a header with the required entries for
5- "mp_rom_map_elem_t mp_builtin_module_table[]" in py/objmodule.c
3+ `MP_REGISTER_MODULE(MP_QSTR_module_name, obj_module)` or
4+ `MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_module_name, obj_module)`
5+ (i.e. the output of `py/makeqstrdefs.py cat module`).
6+
7+ The output is a header (typically moduledefs.h) which is included by
8+ py/objmodule.c that contains entries to be included in the definition of
9+ - mp_rom_map_elem_t mp_builtin_module_table[]
10+ - mp_rom_map_elem_t mp_builtin_extensible_module_table[]
11+
12+ Extensible modules are modules that can be overridden from the filesystem, see
13+ py/builtinimnport.c:process_import_at_level. Regular modules will always use
14+ the built-in version.
615"""
716
817from __future__ import print_function
1322import argparse
1423
1524
16- pattern = re .compile (r"\s*MP_REGISTER_MODULE\((.*?),\s*(.*?)\);" , flags = re .DOTALL )
25+ pattern = re .compile (
26+ r"\s*(MP_REGISTER_MODULE|MP_REGISTER_EXTENSIBLE_MODULE)\(MP_QSTR_(.*?),\s*(.*?)\);" ,
27+ flags = re .DOTALL ,
28+ )
1729
1830
1931def find_module_registrations (filename ):
@@ -37,14 +49,23 @@ def generate_module_table_header(modules):
3749
3850 # Print header file for all external modules.
3951 mod_defs = set ()
52+ extensible_mod_defs = set ()
4053 print ("// Automatically generated by makemoduledefs.py.\n " )
41- for module_name , obj_module in modules :
54+ for macro_name , module_name , obj_module in modules :
4255 mod_def = "MODULE_DEF_{}" .format (module_name .upper ())
43- mod_defs .add (mod_def )
56+ if macro_name == "MP_REGISTER_MODULE" :
57+ mod_defs .add (mod_def )
58+ elif macro_name == "MP_REGISTER_EXTENSIBLE_MODULE" :
59+ extensible_mod_defs .add (mod_def )
4460 if "," in obj_module :
4561 print (
46- "ERROR: Call to MP_REGISTER_MODULE({}, {}) should be MP_REGISTER_MODULE({}, {})\n " .format (
47- module_name , obj_module , module_name , obj_module .split ("," )[0 ]
62+ "ERROR: Call to {}({}, {}) should be {}({}, {})\n " .format (
63+ macro_name ,
64+ module_name ,
65+ obj_module ,
66+ macro_name ,
67+ module_name ,
68+ obj_module .split ("," )[0 ],
4869 ),
4970 file = sys .stderr ,
5071 )
@@ -53,7 +74,7 @@ def generate_module_table_header(modules):
5374 (
5475 "extern const struct _mp_obj_module_t {obj_module};\n "
5576 "#undef {mod_def}\n "
56- "#define {mod_def} {{ MP_ROM_QSTR({module_name}), MP_ROM_PTR(&{obj_module}) }},\n "
77+ "#define {mod_def} {{ MP_ROM_QSTR(MP_QSTR_ {module_name}), MP_ROM_PTR(&{obj_module}) }},\n "
5778 ).format (
5879 module_name = module_name ,
5980 obj_module = obj_module ,
@@ -68,6 +89,13 @@ def generate_module_table_header(modules):
6889
6990 print ("// MICROPY_REGISTERED_MODULES" )
7091
92+ print ("\n #define MICROPY_REGISTERED_EXTENSIBLE_MODULES \\ " )
93+
94+ for mod_def in sorted (extensible_mod_defs ):
95+ print (" {mod_def} \\ " .format (mod_def = mod_def ))
96+
97+ print ("// MICROPY_REGISTERED_EXTENSIBLE_MODULES" )
98+
7199
72100def main ():
73101 parser = argparse .ArgumentParser ()
0 commit comments