|
| 1 | +import os |
| 2 | +import glob |
| 3 | +import sys |
| 4 | +import subprocess |
| 5 | +import time |
| 6 | + |
| 7 | +all_warnings = False |
| 8 | +exit_status = 0 |
| 9 | +success_count = 0 |
| 10 | +fail_count = 0 |
| 11 | + |
| 12 | +build_format = '| {:20} | {:30} | {:9} ' |
| 13 | +build_separator = '-' * 78 |
| 14 | + |
| 15 | +all_boards = [ 'metro_m0' ] |
| 16 | + |
| 17 | +build_boards = [] |
| 18 | + |
| 19 | +# build all variants if input not existed |
| 20 | +if len(sys.argv) > 1: |
| 21 | + if (sys.argv[1] in all_boards): |
| 22 | + build_boards.append(sys.argv[1]) |
| 23 | + else: |
| 24 | + print('\033[31INTERNAL ERR\033[0m - invalid variant name "{}"'.format(sys.argv[1])) |
| 25 | + sys.exit(-1) |
| 26 | +else: |
| 27 | + build_boards = all_boards |
| 28 | + |
| 29 | +def errorOutputFilter(line): |
| 30 | + if len(line) == 0: |
| 31 | + return False |
| 32 | + if line.isspace(): # Note: empty string does not match here! |
| 33 | + return False |
| 34 | + # TODO: additional items to remove? |
| 35 | + return True |
| 36 | + |
| 37 | + |
| 38 | +def build_examples(variant): |
| 39 | + global exit_status, success_count, fail_count, build_format, build_separator |
| 40 | + |
| 41 | + print('\n') |
| 42 | + print(build_separator) |
| 43 | + print('| {:^74} |'.format('Board ' + variant)) |
| 44 | + print(build_separator) |
| 45 | + print((build_format + '| {:6} |').format('Library', 'Example', 'Result', 'Time')) |
| 46 | + print(build_separator) |
| 47 | + |
| 48 | + fqbn = "adafruit:samd:adafruit_{}".format(variant) |
| 49 | + |
| 50 | + for sketch in glob.iglob('libraries/**/*.ino', recursive=True): |
| 51 | + start_time = time.monotonic() |
| 52 | + |
| 53 | + # skip if example contains: ".skip" or ".skip.variant" |
| 54 | + # however ".build.variant" file can overwrite ".skip", used to build a specific variant only |
| 55 | + sketchdir = os.path.dirname(sketch) |
| 56 | + if ( (os.path.exists(sketchdir + '/.skip') or os.path.exists(sketchdir + '/.skip.' + variant)) and |
| 57 | + not os.path.exists(sketchdir + '/.build.' + variant)): |
| 58 | + success = "skipped" |
| 59 | + else: |
| 60 | + # TODO - preferably, would have STDERR show up in **both** STDOUT and STDERR. |
| 61 | + # preferably, would use Python logging handler to get both distinct outputs and one merged output |
| 62 | + # for now, split STDERR when building with all warnings enabled, so can detect warning/error output. |
| 63 | + if all_warnings: |
| 64 | + build_result = subprocess.run("arduino-cli compile --warnings all --fqbn {} {}".format(fqbn, sketch), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 65 | + else: |
| 66 | + build_result = subprocess.run("arduino-cli compile --warnings default --fqbn {} {}".format(fqbn, sketch), shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 67 | + |
| 68 | + # get stderr into a form where len(warningLines) indicates a true warning was output to stderr |
| 69 | + warningLines = []; |
| 70 | + if all_warnings and build_result.stderr: |
| 71 | + tmpWarningLines = build_result.stderr.decode("utf-8").splitlines() |
| 72 | + warningLines = list(filter(errorOutputFilter, (tmpWarningLines))) |
| 73 | + |
| 74 | + if build_result.returncode != 0: |
| 75 | + exit_status = build_result.returncode |
| 76 | + success = "\033[31mfailed\033[0m " |
| 77 | + fail_count += 1 |
| 78 | + elif len(warningLines) != 0: |
| 79 | + exit_status = -1 |
| 80 | + success = "\033[31mwarnings\033[0m " |
| 81 | + fail_count += 1 |
| 82 | + else: |
| 83 | + success = "\033[32msucceeded\033[0m" |
| 84 | + success_count += 1 |
| 85 | + |
| 86 | + build_duration = time.monotonic() - start_time |
| 87 | + |
| 88 | + print((build_format + '| {:5.2f}s |').format(sketch.split(os.path.sep)[1], os.path.basename(sketch), success, build_duration)) |
| 89 | + |
| 90 | + if success != "skipped": |
| 91 | + if build_result.returncode != 0: |
| 92 | + print(build_result.stdout.decode("utf-8")) |
| 93 | + if (build_result.stderr): |
| 94 | + print(build_result.stderr.decode("utf-8")) |
| 95 | + if len(warningLines) != 0: |
| 96 | + for line in warningLines: |
| 97 | + print(line) |
| 98 | + |
| 99 | + |
| 100 | +build_time = time.monotonic() |
| 101 | + |
| 102 | +for board in build_boards: |
| 103 | + build_examples(board) |
| 104 | + |
| 105 | +print(build_separator) |
| 106 | +build_time = time.monotonic() - build_time |
| 107 | +print("Build Summary: {} \033[32msucceeded\033[0m, {} \033[31mfailed\033[0m and took {:.2f}s".format(success_count, fail_count, build_time)) |
| 108 | +print(build_separator) |
| 109 | + |
| 110 | +sys.exit(exit_status) |
0 commit comments