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