makemoduledefs.py 3.3 KB

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