2019-04-05 15:38:32 -04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
# This pre-processor parses provided objects' c files for
|
|
|
|
# MP_REGISTER_MODULE(module_name, obj_module, enabled_define)
|
|
|
|
# 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
|
2021-04-22 20:55:39 -04:00
|
|
|
import io
|
2019-04-05 15:38:32 -04:00
|
|
|
import os
|
|
|
|
import argparse
|
|
|
|
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
pattern = re.compile(r"[\n;]\s*MP_REGISTER_MODULE\((.*?),\s*(.*?),\s*(.*?)\);", flags=re.DOTALL)
|
2019-04-05 15:38:32 -04:00
|
|
|
|
|
|
|
|
|
|
|
def find_c_file(obj_file, vpath):
|
2021-03-15 09:57:36 -04:00
|
|
|
"""Search vpaths for the c file that matches the provided object_file.
|
2019-04-05 15:38:32 -04:00
|
|
|
|
|
|
|
:param str obj_file: object file to find the matching c file for
|
|
|
|
:param List[str] vpath: List of base paths, similar to gcc vpath
|
|
|
|
:return: str path to c file or None
|
|
|
|
"""
|
|
|
|
c_file = None
|
|
|
|
relative_c_file = os.path.splitext(obj_file)[0] + ".c"
|
2021-03-15 09:57:36 -04:00
|
|
|
relative_c_file = relative_c_file.lstrip("/\\")
|
2019-04-05 15:38:32 -04:00
|
|
|
for p in vpath:
|
|
|
|
possible_c_file = os.path.join(p, relative_c_file)
|
|
|
|
if os.path.exists(possible_c_file):
|
|
|
|
c_file = possible_c_file
|
|
|
|
break
|
|
|
|
|
|
|
|
return c_file
|
|
|
|
|
|
|
|
|
|
|
|
def find_module_registrations(c_file):
|
2021-03-15 09:57:36 -04:00
|
|
|
"""Find any MP_REGISTER_MODULE definitions in the provided c file.
|
2019-04-05 15:38:32 -04:00
|
|
|
|
|
|
|
:param str c_file: path to c file to check
|
|
|
|
:return: List[(module_name, obj_module, enabled_define)]
|
|
|
|
"""
|
|
|
|
global pattern
|
|
|
|
|
|
|
|
if c_file is None:
|
|
|
|
# No c file to match the object file, skip
|
|
|
|
return set()
|
|
|
|
|
2021-04-22 20:55:39 -04:00
|
|
|
with io.open(c_file, encoding="utf-8") as c_file_obj:
|
2019-04-05 15:38:32 -04:00
|
|
|
return set(re.findall(pattern, c_file_obj.read()))
|
|
|
|
|
|
|
|
|
|
|
|
def generate_module_table_header(modules):
|
2021-03-15 09:57:36 -04:00
|
|
|
"""Generate header with module table entries for builtin modules.
|
2019-04-05 15:38:32 -04:00
|
|
|
|
|
|
|
:param List[(module_name, obj_module, enabled_define)] modules: module defs
|
|
|
|
:return: None
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Print header file for all external modules.
|
|
|
|
mod_defs = []
|
|
|
|
print("// Automatically generated by makemoduledefs.py.\n")
|
2021-04-22 20:55:39 -04:00
|
|
|
print("#include \"py/mpconfig.h\"")
|
2019-04-05 15:38:32 -04:00
|
|
|
for module_name, obj_module, enabled_define in modules:
|
|
|
|
mod_def = "MODULE_DEF_{}".format(module_name.upper())
|
|
|
|
mod_defs.append(mod_def)
|
2021-03-15 09:57:36 -04:00
|
|
|
print(
|
|
|
|
(
|
|
|
|
"#if ({enabled_define})\n"
|
|
|
|
" extern const struct _mp_obj_module_t {obj_module};\n"
|
|
|
|
" #define {mod_def} {{ MP_ROM_QSTR({module_name}), MP_ROM_PTR(&{obj_module}) }},\n"
|
|
|
|
"#else\n"
|
|
|
|
" #define {mod_def}\n"
|
|
|
|
"#endif\n"
|
|
|
|
).format(
|
|
|
|
module_name=module_name,
|
|
|
|
obj_module=obj_module,
|
|
|
|
enabled_define=enabled_define,
|
|
|
|
mod_def=mod_def,
|
|
|
|
)
|
2019-04-05 15:38:32 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
print("\n#define MICROPY_REGISTERED_MODULES \\")
|
|
|
|
|
|
|
|
for mod_def in mod_defs:
|
|
|
|
print(" {mod_def} \\".format(mod_def=mod_def))
|
|
|
|
|
|
|
|
print("// MICROPY_REGISTERED_MODULES")
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
parser = argparse.ArgumentParser()
|
2021-03-15 09:57:36 -04:00
|
|
|
parser.add_argument(
|
|
|
|
"--vpath", default=".", help="comma separated list of folders to search for c files in"
|
|
|
|
)
|
|
|
|
parser.add_argument("files", nargs="*", help="list of c files to search")
|
2019-04-05 15:38:32 -04:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
vpath = [p.strip() for p in args.vpath.split(",")]
|
2019-04-05 15:38:32 -04:00
|
|
|
|
|
|
|
modules = set()
|
|
|
|
for obj_file in args.files:
|
|
|
|
c_file = find_c_file(obj_file, vpath)
|
|
|
|
modules |= find_module_registrations(c_file)
|
|
|
|
|
|
|
|
generate_module_table_header(sorted(modules))
|
|
|
|
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
if __name__ == "__main__":
|
2019-04-05 15:38:32 -04:00
|
|
|
main()
|