makemoduledefs.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/usr/bin/env python
  2. # This pre-processor parses provided objects' c files for
  3. # MP_REGISTER_MODULE(module_name, obj_module, enabled_define)
  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
  6. from __future__ import print_function
  7. import re
  8. import io
  9. import os
  10. import argparse
  11. pattern = re.compile(r"[\n;]\s*MP_REGISTER_MODULE\((.*?),\s*(.*?),\s*(.*?)\);", flags=re.DOTALL)
  12. def find_c_file(obj_file, vpath):
  13. """Search vpaths for the c file that matches the provided object_file.
  14. :param str obj_file: object file to find the matching c file for
  15. :param List[str] vpath: List of base paths, similar to gcc vpath
  16. :return: str path to c file or None
  17. """
  18. c_file = None
  19. relative_c_file = os.path.splitext(obj_file)[0] + ".c"
  20. relative_c_file = relative_c_file.lstrip("/\\")
  21. for p in vpath:
  22. possible_c_file = os.path.join(p, relative_c_file)
  23. if os.path.exists(possible_c_file):
  24. c_file = possible_c_file
  25. break
  26. return c_file
  27. def find_module_registrations(c_file):
  28. """Find any MP_REGISTER_MODULE definitions in the provided c file.
  29. :param str c_file: path to c file to check
  30. :return: List[(module_name, obj_module, enabled_define)]
  31. """
  32. global pattern
  33. if c_file is None:
  34. # No c file to match the object file, skip
  35. return set()
  36. with io.open(c_file, encoding="utf-8") as c_file_obj:
  37. return set(re.findall(pattern, c_file_obj.read()))
  38. def generate_module_table_header(modules):
  39. """Generate header with module table entries for builtin modules.
  40. :param List[(module_name, obj_module, enabled_define)] modules: module defs
  41. :return: None
  42. """
  43. # Print header file for all external modules.
  44. mod_defs = []
  45. print("// Automatically generated by makemoduledefs.py.\n")
  46. for module_name, obj_module, enabled_define in modules:
  47. mod_def = "MODULE_DEF_{}".format(module_name.upper())
  48. mod_defs.append(mod_def)
  49. print(
  50. (
  51. "#if ({enabled_define})\n"
  52. " extern const struct _mp_obj_module_t {obj_module};\n"
  53. " #define {mod_def} {{ MP_ROM_QSTR({module_name}), MP_ROM_PTR(&{obj_module}) }},\n"
  54. "#else\n"
  55. " #define {mod_def}\n"
  56. "#endif\n"
  57. ).format(
  58. module_name=module_name,
  59. obj_module=obj_module,
  60. enabled_define=enabled_define,
  61. mod_def=mod_def,
  62. )
  63. )
  64. print("\n#define MICROPY_REGISTERED_MODULES \\")
  65. for mod_def in mod_defs:
  66. print(" {mod_def} \\".format(mod_def=mod_def))
  67. print("// MICROPY_REGISTERED_MODULES")
  68. def main():
  69. parser = argparse.ArgumentParser()
  70. parser.add_argument(
  71. "--vpath", default=".", help="comma separated list of folders to search for c files in"
  72. )
  73. parser.add_argument("files", nargs="*", help="list of c files to search")
  74. args = parser.parse_args()
  75. vpath = [p.strip() for p in args.vpath.split(",")]
  76. modules = set()
  77. for obj_file in args.files:
  78. c_file = find_c_file(obj_file, vpath)
  79. modules |= find_module_registrations(c_file)
  80. generate_module_table_header(sorted(modules))
  81. if __name__ == "__main__":
  82. main()