py/make_root_pointers: Add MP_REGISTER_ROOT_POINTER parser/generator.

This adds new compile-time infrastructure to parse source code files for
`MP_REGISTER_ROOT_POINTER()` and generates a new `root_pointers.h` header
file containing the collected declarations.  This works the same as the
existing `MP_REGISTER_MODULE()` feature.

Signed-off-by: David Lechner <david@pybricks.com>
This commit is contained in:
David Lechner
2022-07-01 12:29:08 -05:00
committed by Damien George
parent a8d78cc398
commit fc3d7ae11b
10 changed files with 154 additions and 12 deletions

View File

@@ -24,6 +24,9 @@ _MODE_COMPRESS = "compress"
# Extract MP_REGISTER_MODULE(...) macros.
_MODE_MODULE = "module"
# Extract MP_REGISTER_ROOT_POINTER(...) macros.
_MODE_ROOT_POINTER = "root_pointer"
def is_c_source(fname):
return os.path.splitext(fname)[1] in [".c"]
@@ -90,6 +93,8 @@ def process_file(f):
re_match = re.compile(r'MP_COMPRESSED_ROM_TEXT\("([^"]*)"\)')
elif args.mode == _MODE_MODULE:
re_match = re.compile(r"MP_REGISTER_MODULE\(.*?,\s*.*?\);")
elif args.mode == _MODE_ROOT_POINTER:
re_match = re.compile(r"MP_REGISTER_ROOT_POINTER\(.*?\);")
output = []
last_fname = None
for line in f:
@@ -111,7 +116,7 @@ def process_file(f):
if args.mode == _MODE_QSTR:
name = match.replace("MP_QSTR_", "")
output.append("Q(" + name + ")")
elif args.mode in (_MODE_COMPRESS, _MODE_MODULE):
elif args.mode in (_MODE_COMPRESS, _MODE_MODULE, _MODE_ROOT_POINTER):
output.append(match)
if last_fname:
@@ -148,6 +153,8 @@ def cat_together():
mode_full = "Compressed data"
elif args.mode == _MODE_MODULE:
mode_full = "Module registrations"
elif args.mode == _MODE_ROOT_POINTER:
mode_full = "Root pointer registrations"
if old_hash != new_hash:
print(mode_full, "updated")
try:
@@ -208,7 +215,7 @@ if __name__ == "__main__":
args.output_dir = sys.argv[4]
args.output_file = None if len(sys.argv) == 5 else sys.argv[5] # Unused for command=split
if args.mode not in (_MODE_QSTR, _MODE_COMPRESS, _MODE_MODULE):
if args.mode not in (_MODE_QSTR, _MODE_COMPRESS, _MODE_MODULE, _MODE_ROOT_POINTER):
print("error: mode %s unrecognised" % sys.argv[2])
sys.exit(2)