Skip to content

Commit 5549103

Browse files
committed
tools/mpy_cross_all.py: Helper tool to run mpy-cross on the entire project.
1 parent 0c57979 commit 5549103

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

tools/mpy_cross_all.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env python3
2+
import argparse
3+
import os
4+
import os.path
5+
6+
argparser = argparse.ArgumentParser(description="Compile all .py files to .mpy recursively")
7+
argparser.add_argument("-o", "--out", help="output directory (default: input dir)")
8+
argparser.add_argument("--target", help="select MicroPython target config")
9+
argparser.add_argument("-mcache-lookup-bc", action="store_true", help="cache map lookups in the bytecode")
10+
argparser.add_argument("dir", help="input directory")
11+
args = argparser.parse_args()
12+
13+
TARGET_OPTS = {
14+
"unix": "-mcache-lookup-bc",
15+
"baremetal": "",
16+
}
17+
18+
args.dir = args.dir.rstrip("/")
19+
20+
if not args.out:
21+
args.out = args.dir
22+
23+
path_prefix_len = len(args.dir) + 1
24+
25+
for path, subdirs, files in os.walk(args.dir):
26+
for f in files:
27+
if f.endswith(".py"):
28+
fpath = path + "/" + f
29+
#print(fpath)
30+
out_fpath = args.out + "/" + fpath[path_prefix_len:-3] + ".mpy"
31+
out_dir = os.path.dirname(out_fpath)
32+
if not os.path.isdir(out_dir):
33+
os.makedirs(out_dir)
34+
cmd = "mpy-cross -v -v %s -s %s %s -o %s" % (TARGET_OPTS.get(args.target, ""),
35+
fpath[path_prefix_len:], fpath, out_fpath)
36+
#print(cmd)
37+
res = os.system(cmd)
38+
assert res == 0

0 commit comments

Comments
 (0)