Skip to content

Commit 0d69701

Browse files
committed
Add local build script
1 parent cea0440 commit 0d69701

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed

tools/build_all.py

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import os
2+
import sys
3+
import subprocess
4+
from subprocess import Popen, PIPE
5+
import time
6+
import glob
7+
from multiprocessing import Pool
8+
9+
SUCCEEDED = "\033[32msucceeded\033[0m"
10+
FAILED = "\033[31mfailed\033[0m"
11+
SKIPPED = "\033[35mskipped\033[0m"
12+
WARNING = "\033[33mwarnings\033[0m "
13+
14+
build_format = '| {:35} | {:9} | {:6} |'
15+
build_separator = '-' * 59
16+
17+
# ci-arduino naming, fqbn
18+
all_boards = [
19+
['metro_m0_tinyusb', 'adafruit:samd:adafruit_metro_m0:usbstack=tinyusb'],
20+
['metro_m4_tinyusb', 'adafruit:samd:adafruit_metro_m4:speed=120,usbstack=tinyusb'],
21+
['nrf52840', 'adafruit:nrf52:feather52840'],
22+
['feather_rp2040_tinyusb', 'rp2040:rp2040:adafruit_feather:flash=8388608_0,freq=125,dbgport=Disabled,dbglvl=None,usbstack=tinyusb'],
23+
['metroesp32s2', 'esp32:esp32:adafruit_metro_esp32s2:CDCOnBoot=cdc,MSCOnBoot=default,DFUOnBoot=default,UploadMode=cdc,PSRAM=enabled,PartitionScheme=tinyuf2'],
24+
#[' ', 'esp32:esp32:adafruit_feather_esp32s3:FlashMode=qio,LoopCore=1,EventsCore=1,USBMode=default,CDCOnBoot=cdc,MSCOnBoot=default,DFUOnBoot=default,UploadMode=cdc,PartitionScheme=tinyuf2'],
25+
]
26+
27+
# return [succeeded, failed, skipped]
28+
def build_sketch(variant, sketch):
29+
ret = [0, 0, 0]
30+
31+
name = variant[0]
32+
fqbn = variant[1]
33+
34+
start_time = time.monotonic()
35+
# Skip if contains: ".board.test.skip" or ".all.test.skip"
36+
# Skip if not contains: ".board.test.only" for a specific board
37+
sketchdir = os.path.dirname(sketch)
38+
if os.path.exists(sketchdir + '/.all.test.skip') or os.path.exists(sketchdir + '/.' + name + '.test.skip') or \
39+
(glob.glob(sketchdir + "/.*.test.only") and not os.path.exists(sketchdir + '/.' + name + '.test.only')):
40+
success = SKIPPED
41+
ret[2] = 1
42+
else:
43+
build_result = subprocess.run("arduino-cli compile --warnings all --fqbn {} {}".format(fqbn, sketch),
44+
shell=True, stdout=PIPE, stderr=PIPE)
45+
46+
# get stderr into a form where warning/error was output to stderr
47+
if build_result.returncode != 0:
48+
success = FAILED
49+
ret[1] = 1
50+
else:
51+
ret[0] = 1
52+
if build_result.stderr:
53+
success = WARNING
54+
else:
55+
success = SUCCEEDED
56+
57+
build_duration = time.monotonic() - start_time
58+
print(build_format.format(os.path.basename(sketch), success, '{:5.2f}s'.format(build_duration)))
59+
60+
if success != SKIPPED:
61+
# Build failed
62+
if build_result.returncode != 0:
63+
print(build_result.stdout.decode("utf-8"))
64+
65+
# Build with warnings
66+
if build_result.stderr:
67+
print(build_result.stderr.decode("utf-8"))
68+
return ret
69+
70+
71+
# return [succeeded, failed, skipped]
72+
def build_variant(variant):
73+
print()
74+
print(build_separator)
75+
print('| {:^56} |'.format('Board ' + variant[0]))
76+
print(build_separator)
77+
print(build_format.format('Example', '\033[39mResult\033[0m', 'Time'))
78+
print(build_separator)
79+
80+
with Pool(processes=os.cpu_count()) as pool:
81+
pool_args = list((map(lambda e, v=variant: [v, e], all_examples)))
82+
result = pool.starmap(build_sketch, pool_args)
83+
# sum all element of same index (column sum)
84+
return list(map(sum, list(zip(*result))))
85+
86+
87+
if __name__ == '__main__':
88+
# build all variants if input not existed
89+
if len(sys.argv) > 1:
90+
build_boards = list(filter(lambda x: sys.argv[1] in x[0], all_boards))
91+
else:
92+
build_boards = all_boards
93+
94+
all_examples = list(glob.iglob('examples/**/*.ino', recursive=True))
95+
all_examples.sort()
96+
97+
total_time = time.monotonic()
98+
total_result = [0, 0, 0]
99+
for b in build_boards:
100+
ret = build_variant(b)
101+
total_result = list(map(lambda x, y: x + y, total_result, ret))
102+
103+
# Build Summary
104+
total_time = time.monotonic() - total_time
105+
print(build_separator)
106+
print("Build Summary: {} {}, {} {}, {} {} and took {:.2f}s".format(total_result[0], SUCCEEDED, total_result[1],
107+
FAILED, total_result[2], SKIPPED, total_time))
108+
print(build_separator)
109+
sys.exit(total_result[1])

0 commit comments

Comments
 (0)