Loading...
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
# SPDX-License-Identifier: GPL-2.0+
# Copyright (c) 2013 The Chromium OS Authors.
#
# Bloat-o-meter code used here Copyright 2004 Matt Mackall <mpm@selenic.com>
#

"""Result writer for buildman build results"""

from datetime import datetime, timedelta
import sys

from buildman.outcome import (BoardStatus, ErrLine, Outcome,
                              OUTCOME_OK, OUTCOME_WARNING, OUTCOME_ERROR,
                              OUTCOME_UNKNOWN)
from u_boot_pylib.terminal import tprint


class ResultHandler:
    """Handles display of build size results and summaries

    This class is responsible for displaying size information from builds,
    including per-architecture summaries, per-board details, and per-function
    bloat analysis. It also manages baseline state for comparing results
    between commits.

    Private members:
        _base_board_dict: Last-summarised Dict of boards
        _base_config: Last-summarised config
        _base_environment: Last-summarised environment
        _base_err_line_boards: Dict of error lines to boards
        _base_err_lines: Last-summarised list of errors
        _base_warn_line_boards: Dict of warning lines to boards
        _base_warn_lines: Last-summarised list of warnings
        _col: terminal.Color object for coloured output
        _config_filenames: List of config filenames to track
        _error_lines: Number of error lines output
        _opts: DisplayOptions for result output
        _result_getter: Callback to get result summary data
    """

    def __init__(self, col, opts):
        """Create a new ResultHandler

        Args:
            col: terminal.Color object for coloured output
            opts (DisplayOptions): Options controlling what to display
        """
        self._col = col
        self._opts = opts
        self._builder = None
        self._config_filenames = None
        self._result_getter = None
        self._error_lines = 0

        # Baseline state for result comparisons
        self._base_board_dict = {}
        self._base_err_lines = []
        self._base_warn_lines = []
        self._base_err_line_boards = {}
        self._base_warn_line_boards = {}
        self._base_config = None
        self._base_environment = None

    @property
    def opts(self):
        """Get the display options"""
        return self._opts

    def set_builder(self, builder):
        """Set the builder for this result handler

        Args:
            builder (Builder): Builder object to use for getting results
        """
        self._builder = builder
        self._config_filenames = builder.config_filenames
        self._result_getter = builder.get_result_summary

    def reset_result_summary(self, board_selected):
        """Reset the results summary ready for use.

        Set up the base board list to be all those selected, and set the
        error lines to empty.

        Following this, calls to _print_result_summary() will use this
        information to work out what has changed.

        Args:
            board_selected (dict): Dict containing boards to summarise, keyed
                by board.target
        """
        outcome_init = Outcome(0, [], [], {}, {}, {})
        self._base_board_dict = {}
        for brd in board_selected:
            self._base_board_dict[brd] = outcome_init
        self._base_err_lines = []
        self._base_warn_lines = []
        self._base_err_line_boards = {}
        self._base_warn_line_boards = {}
        self._base_config = None
        self._base_environment = None
        self._error_lines = 0

    def _print_result_summary(self, board_selected, board_dict, err_lines,
                             err_line_boards, warn_lines, warn_line_boards,
                             config, environment):
        """Compare results with the base results and display delta.

        Only boards mentioned in board_selected will be considered. This
        function is intended to be called repeatedly with the results of
        each commit. It therefore shows a 'diff' between what it saw in
        the last call and what it sees now.

        Args:
            board_selected (dict): Dict containing boards to summarise, keyed
                by board.target
            board_dict (dict): Dict containing boards for which we built this
                commit, keyed by board.target. The value is an Outcome object.
            err_lines (list): A list of errors for this commit, or [] if there
                is none, or we don't want to print errors
            err_line_boards (dict): Dict keyed by error line, containing a list
                of the Board objects with that error
            warn_lines (list): A list of warnings for this commit, or [] if
                there is none, or we don't want to print errors
            warn_line_boards (dict): Dict keyed by warning line, containing a
                list of the Board objects with that warning
            config (dict): Dictionary keyed by filename - e.g. '.config'. Each
                    value is itself a dictionary:
                        key: config name
                        value: config value
            environment (dict): Dictionary keyed by environment variable, Each
                     value is the value of environment variable.
        """
        brd_status = self._classify_boards(
            board_selected, board_dict, self._base_board_dict)

        # Get a list of errors and warnings that have appeared, and disappeared
        better_err, worse_err = self._calc_error_delta(
            self._base_err_lines, self._base_err_line_boards, err_lines,
            err_line_boards, '', self._opts.list_error_boards)
        better_warn, worse_warn = self._calc_error_delta(
            self._base_warn_lines, self._base_warn_line_boards, warn_lines,
            warn_line_boards, 'w', self._opts.list_error_boards)

        # For the IDE mode, print out all the output
        if self._opts.ide:
            self._print_ide_output(board_selected, board_dict)

        # Display results by arch
        if not self._opts.ide:
            self._error_lines += self._display_arch_results(
                board_selected, brd_status, better_err, worse_err, better_warn,
                worse_warn, self._opts.show_unknown)

        if self._opts.show_sizes:
            self._print_size_summary(
                board_selected, board_dict, self._base_board_dict,
                self._opts.show_detail, self._opts.show_bloat)

        if self._opts.show_environment and self._base_environment:
            self._show_environment_changes(
                board_selected, board_dict, environment, self._base_environment)

        if self._opts.show_config and self._base_config:
            self._show_config_changes(
                board_selected, board_dict, config, self._base_config)

        # Save our updated information for the next call to this function
        self._base_board_dict = board_dict
        self._base_err_lines = err_lines
        self._base_warn_lines = warn_lines
        self._base_err_line_boards = err_line_boards
        self._base_warn_line_boards = warn_line_boards
        self._base_config = config
        self._base_environment = environment

        self._show_not_built(board_selected, board_dict)

    def _get_error_lines(self):
        """Get the number of error lines output

        Returns:
            int: Number of error lines output
        """
        return self._error_lines

    def produce_result_summary(self, commit_upto, commits, board_selected):
        """Produce a summary of the results for a single commit

        Args:
            commit_upto (int): Commit number to summarise (0..count-1)
            commits (list): List of commits being built
            board_selected (dict): Dict containing boards to summarise
        """
        (board_dict, err_lines, err_line_boards, warn_lines,
         warn_line_boards, config, environment) = self._result_getter(
                board_selected, commit_upto, self._opts.show_bloat,
                self._opts.show_config, self._opts.show_environment)
        if commits:
            msg = f'{commit_upto + 1:02d}: {commits[commit_upto].subject}'
            tprint(msg, colour=self._col.BLUE)
        self._print_result_summary(
            board_selected, board_dict,
            err_lines if self._opts.show_errors else [], err_line_boards,
            warn_lines if self._opts.show_errors else [], warn_line_boards,
            config, environment)

    def show_summary(self, commits, board_selected, step):
        """Show a build summary for U-Boot for a given board list.

        Reset the result summary, then repeatedly call produce_result_summary
        on each commit's results, then display the differences we see.

        Args:
            commits (list): Commit objects to summarise
            board_selected (dict): Dict containing boards to summarise
            step (int): Step size for iterating through commits
        """
        commit_count = len(commits) if commits else 1
        self.reset_result_summary(board_selected)

        for commit_upto in range(0, commit_count, step):
            self.produce_result_summary(
                commit_upto, commits, board_selected)
        if not self._get_error_lines():
            tprint('(no errors to report)', colour=self._col.GREEN)

    def print_build_summary(self, count, already_done, kconfig_reconfig,
                            start_time, thread_exceptions):
        """Print a summary of the build results

        Show the number of boards built, how many were already done, duration
        and build rate. Also show any thread exceptions that occurred.

        Args:
            count (int): Total number of builds
            already_done (int): Number of builds already completed previously
            kconfig_reconfig (int): Number of builds triggered by Kconfig changes
            start_time (datetime): When the build started
            thread_exceptions (list): List of thread exceptions that occurred
        """
        tprint()

        msg = f'Completed: {count} total built'
        if already_done or kconfig_reconfig:
            parts = []
            if already_done:
                parts.append(f'{already_done} previously')
            if already_done != count:
                parts.append(f'{count - already_done} newly')
            if kconfig_reconfig:
                parts.append(f'{kconfig_reconfig} reconfig')
            msg += ' (' + ', '.join(parts) + ')'
        duration = datetime.now() - start_time
        if duration > timedelta(microseconds=1000000):
            if duration.microseconds >= 500000:
                duration = duration + timedelta(seconds=1)
            duration -= timedelta(microseconds=duration.microseconds)
            rate = float(count) / duration.total_seconds()
            msg += f', duration {duration}, rate {rate:1.2f}'
        tprint(msg)
        if thread_exceptions:
            tprint(
                f'Failed: {len(thread_exceptions)} thread exceptions',
                colour=self._col.RED)

    def _colour_num(self, num):
        """Format a number with colour depending on its value

        Args:
            num (int): Number to format

        Returns:
            str: Formatted string (red if positive, green if negative/zero)
        """
        color = self._col.RED if num > 0 else self._col.GREEN
        if num == 0:
            return '0'
        return self._col.build(color, str(num))

    def _print_func_size_detail(self, fname, old, new):
        """Print detailed size information for each function

        Args:
            fname (str): Filename to print (e.g. 'u-boot')
            old (dict): Dictionary of old function sizes, keyed by function name
            new (dict): Dictionary of new function sizes, keyed by function name
        """
        grow, shrink, add, remove, up, down = 0, 0, 0, 0, 0, 0
        delta, common = [], {}

        for a in old:
            if a in new:
                common[a] = 1

        for name in old:
            if name not in common:
                remove += 1
                down += old[name]
                delta.append([-old[name], name])

        for name in new:
            if name not in common:
                add += 1
                up += new[name]
                delta.append([new[name], name])

        for name in common:
            diff = new.get(name, 0) - old.get(name, 0)
            if diff > 0:
                grow, up = grow + 1, up + diff
            elif diff < 0:
                shrink, down = shrink + 1, down - diff
            delta.append([diff, name])

        delta.sort()
        delta.reverse()

        args = [add, -remove, grow, -shrink, up, -down, up - down]
        if max(args) == 0 and min(args) == 0:
            return
        args = [self._colour_num(x) for x in args]
        indent = ' ' * 15
        tprint(f'{indent}{self._col.build(self._col.YELLOW, fname)}: add: '
               f'{args[0]}/{args[1]}, grow: {args[2]}/{args[3]} bytes: '
               f'{args[4]}/{args[5]} ({args[6]})')
        tprint(f'{indent}  {"function":<38s} {"old":>7s} {"new":>7s} '
               f'{"delta":>7s}')
        for diff, name in delta:
            if diff:
                color = self._col.RED if diff > 0 else self._col.GREEN
                msg = (f'{indent}  {name:<38s} {old.get(name, "-"):>7} '
                       f'{new.get(name, "-"):>7} {diff:+7d}')
                tprint(msg, colour=color)

    def _print_size_detail(self, target_list, base_board_dict, board_dict,
                          show_bloat):
        """Show detailed size information for each board

        Args:
            target_list (list): List of targets, each a dict containing:
                    'target': Target name
                    'total_diff': Total difference in bytes across all areas
                    <part_name>: Difference for that part
            base_board_dict (dict): Dict of base board outcomes
            board_dict (dict): Dict of current board outcomes
            show_bloat (bool): Show detail for each function
        """
        targets_by_diff = sorted(target_list, reverse=True,
        key=lambda x: x['_total_diff'])
        for result in targets_by_diff:
            printed_target = False
            for name in sorted(result):
                diff = result[name]
                if name.startswith('_'):
                    continue
                colour = self._col.RED if diff > 0 else self._col.GREEN
                msg = f' {name} {diff:+d}'
                if not printed_target:
                    tprint(f'{"":10s}  {result["_target"]:<15s}:',
                          newline=False)
                    printed_target = True
                tprint(msg, colour=colour, newline=False)
            if printed_target:
                tprint()
                if show_bloat:
                    target = result['_target']
                    outcome = board_dict[target]
                    base_outcome = base_board_dict[target]
                    for fname in outcome.func_sizes:
                        self._print_func_size_detail(fname,
                                                 base_outcome.func_sizes[fname],
                                                 outcome.func_sizes[fname])

    @staticmethod
    def _calc_image_size_changes(target, sizes, base_sizes):
        """Calculate size changes for each image/part

        Args:
            target (str): Target board name
            sizes (dict): Dict of image sizes, keyed by image name
            base_sizes (dict): Dict of base image sizes, keyed by image name

        Returns:
            dict: Size changes, e.g.:
                {'_target': 'snapper9g45', 'data': 5, 'u-boot-spl:text': -4}
                meaning U-Boot data increased by 5 bytes, SPL text decreased
                by 4
        """
        err = {'_target' : target}
        for image in sizes:
            if image in base_sizes:
                base_image = base_sizes[image]
                # Loop through the text, data, bss parts
                for part in sorted(sizes[image]):
                    diff = sizes[image][part] - base_image[part]
                    if diff:
                        if image == 'u-boot':
                            name = part
                        else:
                            name = image + ':' + part
                        err[name] = diff
        return err

    def _calc_size_changes(self, board_selected, board_dict, base_board_dict):
        """Calculate changes in size for different image parts

        The previous sizes are in Board.sizes, for each board

        Args:
            board_selected (dict): Dict containing boards to summarise, keyed
                by board.target
            board_dict (dict): Dict containing boards for which we built this
                commit, keyed by board.target. The value is an Outcome object.
            base_board_dict (dict): Dict of base board outcomes

        Returns:
            tuple: (arch_list, arch_count) where:
                arch_list: dict keyed by arch name, containing a list of
                    size-change dicts
                arch_count: dict keyed by arch name, containing the number of
                    boards for that arch
        """
        arch_list = {}
        arch_count = {}
        for target in board_dict:
            if target not in board_selected:
                continue
            base_sizes = base_board_dict[target].sizes
            outcome = board_dict[target]
            sizes = outcome.sizes
            err = self._calc_image_size_changes(target, sizes, base_sizes)
            arch = board_selected[target].arch
            if not arch in arch_count:
                arch_count[arch] = 1
            else:
                arch_count[arch] += 1
            if not sizes:
                pass    # Only add to our list when we have some stats
            elif not arch in arch_list:
                arch_list[arch] = [err]
            else:
                arch_list[arch].append(err)
        return arch_list, arch_count

    def _print_size_summary(self, board_selected, board_dict, base_board_dict,
                           show_detail, show_bloat):
        """Print a summary of image sizes broken down by section.

        The summary takes the form of one line per architecture. The
        line contains deltas for each of the sections (+ means the section
        got bigger, - means smaller). The numbers are the average number
        of bytes that a board in this section increased by.

        For example:
           powerpc: (622 boards)   text -0.0
          arm: (285 boards)   text -0.0

        Args:
            board_selected (dict): Dict containing boards to summarise, keyed
                by board.target
            board_dict (dict): Dict containing boards for which we built this
                commit, keyed by board.target. The value is an Outcome object.
            base_board_dict (dict): Dict of base board outcomes
            show_detail (bool): Show size delta detail for each board
            show_bloat (bool): Show detail for each function
        """
        arch_list, arch_count = self._calc_size_changes(board_selected,
                                                       board_dict,
                                                       base_board_dict)

        # We now have a list of image size changes sorted by arch
        # Print out a summary of these
        for arch, target_list in arch_list.items():
            # Get total difference for each type
            totals = {}
            for result in target_list:
                total = 0
                for name, diff in result.items():
                    if name.startswith('_'):
                        continue
                    total += diff
                    if name in totals:
                        totals[name] += diff
                    else:
                        totals[name] = diff
                result['_total_diff'] = total

            self._print_arch_size_summary(arch, target_list, arch_count,
                                          totals, base_board_dict, board_dict,
                                          show_detail, show_bloat)

    def _print_arch_size_summary(self, arch, target_list, arch_count, totals,
                                 base_board_dict, board_dict,
                                 show_detail, show_bloat):
        """Print size summary for a single architecture

        Args:
            arch (str): Architecture name
            target_list (list): List of size-change dicts for this arch
            arch_count (dict): Dict of arch name to board count
            totals (dict): Dict of name to total size diff
            base_board_dict (dict): Dict of base board outcomes
            board_dict (dict): Dict of current board outcomes
            show_detail (bool): Show size delta detail for each board
            show_bloat (bool): Show detail for each function
        """
        count = len(target_list)
        printed_arch = False
        for name in sorted(totals):
            diff = totals[name]
            if diff:
                # Display the average difference in this name for this
                # architecture
                avg_diff = float(diff) / count
                color = self._col.RED if avg_diff > 0 else self._col.GREEN
                msg = f' {name} {avg_diff:+1.1f}'
                if not printed_arch:
                    tprint(f'{arch:>10s}: (for {count}/{arch_count[arch]} '
                           'boards)', newline=False)
                    printed_arch = True
                tprint(msg, colour=color, newline=False)

        if printed_arch:
            tprint()
            if show_detail:
                self._print_size_detail(target_list, base_board_dict, board_dict,
                                       show_bloat)

    def _add_outcome(self, board_dict, arch_list, changes, char, color):
        """Add an output to our list of outcomes for each architecture

        This simple function adds failing boards (changes) to the
        relevant architecture string, so we can print the results out
        sorted by architecture.

        Args:
             board_dict (dict): Dict containing all boards
             arch_list (dict): Dict keyed by arch name. Value is a string
                 containing a list of board names which failed for that arch.
             changes (list): List of boards to add to arch_list
             char (str): Character to display for this board
             color (int): terminal.Colour object
        """
        done_arch = {}
        for target in changes:
            if target in board_dict:
                arch = board_dict[target].arch
            else:
                arch = 'unknown'
            text = self._col.build(color, ' ' + target)
            if arch not in done_arch:
                text = f' {self._col.build(color, char)}  {text}'
                done_arch[arch] = True
            if arch not in arch_list:
                arch_list[arch] = text
            else:
                arch_list[arch] += text

    def _output_err_lines(self, err_lines, colour):
        """Output the line of error/warning lines, if not empty

        Args:
            err_lines: List of ErrLine objects, each an error or warning
                line, possibly including a list of boards with that
                error/warning
            colour: Colour to use for output

        Returns:
            int: 1 if any lines were output, 0 otherwise
        """
        if err_lines:
            out_list = []
            for line in err_lines:
                names = [brd.target for brd in line.brds]
                board_str = ' '.join(names) if names else ''
                if board_str:
                    out = self._col.build(colour, line.char + '(')
                    out += self._col.build(self._col.MAGENTA, board_str,
                                          bright=False)
                    out += self._col.build(colour, f') {line.errline}')
                else:
                    out = self._col.build(colour, line.char + line.errline)
                out_list.append(out)
            tprint('\n'.join(out_list))
            return 1
        return 0

    def _display_arch_results(self, board_selected, brd_status, better_err,
                             worse_err, better_warn, worse_warn, show_unknown):
        """Display results by architecture

        Args:
            board_selected (dict): Dict containing boards to summarise
            brd_status (BoardStatus): Named tuple with board classifications
            better_err: List of ErrLine for fixed errors
            worse_err: List of ErrLine for new errors
            better_warn: List of ErrLine for fixed warnings
            worse_warn: List of ErrLine for new warnings
            show_unknown (bool): Whether to show unknown boards

        Returns:
            int: Number of error lines output
        """
        error_lines = 0
        if not any((brd_status.ok, brd_status.warn, brd_status.err,
                    brd_status.unknown, brd_status.new, worse_err, better_err,
                    worse_warn, better_warn)):
            return error_lines
        arch_list = {}
        self._add_outcome(board_selected, arch_list, brd_status.ok, '',
                         self._col.GREEN)
        self._add_outcome(board_selected, arch_list, brd_status.warn, 'w+',
                         self._col.YELLOW)
        self._add_outcome(board_selected, arch_list, brd_status.err, '+',
                         self._col.RED)
        self._add_outcome(board_selected, arch_list, brd_status.new, '*',
                         self._col.BLUE)
        if show_unknown:
            self._add_outcome(board_selected, arch_list, brd_status.unknown,
                             '?', self._col.MAGENTA)
        for arch, target_list in arch_list.items():
            tprint(f'{arch:>10s}: {target_list}')
            error_lines += 1
        error_lines += self._output_err_lines(better_err, colour=self._col.GREEN)
        error_lines += self._output_err_lines(worse_err, colour=self._col.RED)
        error_lines += self._output_err_lines(better_warn, colour=self._col.CYAN)
        error_lines += self._output_err_lines(worse_warn, colour=self._col.YELLOW)
        return error_lines

    @staticmethod
    def _print_ide_output(board_selected, board_dict):
        """Print output for IDE mode

        Args:
            board_selected (dict): Dict of selected boards, keyed by target
            board_dict (dict): Dict of boards that were built, keyed by target
        """
        for target in board_dict:
            if target not in board_selected:
                continue
            outcome = board_dict[target]
            for line in outcome.err_lines:
                sys.stderr.write(line)

    @staticmethod
    def _calc_config(delta, name, config):
        """Calculate configuration changes

        Args:
            delta: Type of the delta, e.g. '+'
            name: name of the file which changed (e.g. .config)
            config: configuration change dictionary
                key: config name
                value: config value
        Returns:
            String containing the configuration changes which can be
                printed
        """
        out = ''
        for key in sorted(config.keys()):
            out += f'{key}={config[key]} '
        return f'{delta} {name}: {out}'

    @classmethod
    def _add_config(cls, lines, name, config_plus, config_minus, config_change):
        """Add changes in configuration to a list

        Args:
            lines: list to add to
            name: config file name
            config_plus: configurations added, dictionary
                key: config name
                value: config value
            config_minus: configurations removed, dictionary
                key: config name
                value: config value
            config_change: configurations changed, dictionary
                key: config name
                value: config value
        """
        if config_plus:
            lines.append(cls._calc_config('+', name, config_plus))
        if config_minus:
            lines.append(cls._calc_config('-', name, config_minus))
        if config_change:
            lines.append(cls._calc_config('c', name, config_change))

    def _output_config_info(self, lines):
        """Output configuration change information

        Args:
            lines: List of configuration change strings
        """
        for line in lines:
            if not line:
                continue
            col = None
            if line[0] == '+':
                col = self._col.GREEN
            elif line[0] == '-':
                col = self._col.RED
            elif line[0] == 'c':
                col = self._col.YELLOW
            tprint('   ' + line, newline=True, colour=col)

    def _show_environment_changes(self, board_selected, board_dict,
                                 environment, base_environment):
        """Show changes in environment variables

        Args:
            board_selected (dict): Dict containing boards to summarise, keyed
                by board.target
            board_dict (dict): Dict containing boards for which we built this
                commit, keyed by board.target. The value is an Outcome object.
            environment (dict): Dict of environment changes, keyed by
                board.target
            base_environment (dict): Dict of base environment, keyed by
                board.target
        """
        lines = []
        for target in board_dict:
            if target not in board_selected:
                continue

            tbase = base_environment[target]
            tenvironment = environment[target]
            environment_plus = {}
            environment_minus = {}
            environment_change = {}
            base = tbase.environment
            for key, value in tenvironment.environment.items():
                if key not in base:
                    environment_plus[key] = value
            for key, value in base.items():
                if key not in tenvironment.environment:
                    environment_minus[key] = value
            for key, value in base.items():
                new_value = tenvironment.environment.get(key)
                if new_value and value != new_value:
                    desc = f'{value} -> {new_value}'
                    environment_change[key] = desc

            self._add_config(lines, target, environment_plus,
                           environment_minus, environment_change)
        self._output_config_info(lines)

    def __calc_config_changes(self, target, config, base_config,
                            arch, arch_config_plus, arch_config_minus,
                            arch_config_change):
        """Calculate configuration changes for a single target

        Args:
            target (str): Target board name
            config (dict): Dict of config changes, keyed by board.target
            base_config (dict): Dict of base config, keyed by board.target
            arch (str): Architecture name
            arch_config_plus (dict): Dict to update with added configs by arch
            arch_config_minus (dict): Dict to update with removed configs by
                arch
            arch_config_change (dict): Dict to update with changed configs by
                arch

        Returns:
            str: Summary of config changes for this target
        """
        all_config_plus = {}
        all_config_minus = {}
        all_config_change = {}
        tbase = base_config[target]
        tconfig = config[target]
        lines = []
        for name in self._config_filenames:
            if not tconfig.config[name]:
                continue
            config_plus = {}
            config_minus = {}
            config_change = {}
            base = tbase.config[name]
            for key, value in tconfig.config[name].items():
                if key not in base:
                    config_plus[key] = value
                    all_config_plus[key] = value
            for key, value in base.items():
                if key not in tconfig.config[name]:
                    config_minus[key] = value
                    all_config_minus[key] = value
            for key, value in base.items():
                new_value = tconfig.config[name].get(key)
                if new_value and value != new_value:
                    desc = f'{value} -> {new_value}'
                    config_change[key] = desc
                    all_config_change[key] = desc

            arch_config_plus[arch][name].update(config_plus)
            arch_config_minus[arch][name].update(config_minus)
            arch_config_change[arch][name].update(config_change)

            self._add_config(lines, name, config_plus, config_minus,
                           config_change)
        self._add_config(lines, 'all', all_config_plus,
                       all_config_minus, all_config_change)
        return '\n'.join(lines)

    def _print_arch_config_summary(self, arch, arch_config_plus,
                                  arch_config_minus, arch_config_change):
        """Print configuration summary for a single architecture

        Args:
            arch (str): Architecture name
            arch_config_plus (dict): Dict of added configs by arch/filename
            arch_config_minus (dict): Dict of removed configs by arch/filename
            arch_config_change (dict): Dict of changed configs by arch/filename
        """
        lines = []
        all_plus = {}
        all_minus = {}
        all_change = {}
        for name in self._config_filenames:
            all_plus.update(arch_config_plus[arch][name])
            all_minus.update(arch_config_minus[arch][name])
            all_change.update(arch_config_change[arch][name])
            self._add_config(lines, name,
                           arch_config_plus[arch][name],
                           arch_config_minus[arch][name],
                           arch_config_change[arch][name])
        self._add_config(lines, 'all', all_plus, all_minus, all_change)
        if lines:
            tprint(f'{arch}:')
            self._output_config_info(lines)

    def _show_config_changes(self, board_selected, board_dict, config,
                            base_config):
        """Show changes in configuration

        Args:
            board_selected (dict): Dict containing boards to summarise, keyed
                by board.target
            board_dict (dict): Dict containing boards for which we built this
                commit, keyed by board.target. The value is an Outcome object.
            config (dict): Dict of config changes, keyed by board.target
            base_config (dict): Dict of base config, keyed by board.target
        """
        summary = {}
        arch_config_plus = {}
        arch_config_minus = {}
        arch_config_change = {}
        arch_list = []

        for target in board_dict:
            if target not in board_selected:
                continue
            arch = board_selected[target].arch
            if arch not in arch_list:
                arch_list.append(arch)

        for arch in arch_list:
            arch_config_plus[arch] = {}
            arch_config_minus[arch] = {}
            arch_config_change[arch] = {}
            for name in self._config_filenames:
                arch_config_plus[arch][name] = {}
                arch_config_minus[arch][name] = {}
                arch_config_change[arch][name] = {}

        for target in board_dict:
            if target not in board_selected:
                continue
            arch = board_selected[target].arch
            summary[target] = self.__calc_config_changes(
                target, config, base_config, arch,
                arch_config_plus, arch_config_minus, arch_config_change)

        lines_by_target = {}
        for target, lines in summary.items():
            if lines in lines_by_target:
                lines_by_target[lines].append(target)
            else:
                lines_by_target[lines] = [target]

        for arch in arch_list:
            self._print_arch_config_summary(arch, arch_config_plus,
                                          arch_config_minus,
                                          arch_config_change)

        for lines, targets in lines_by_target.items():
            if not lines:
                continue
            tprint(f"{' '.join(sorted(targets))} :")
            self._output_config_info(lines.split('\n'))

    @staticmethod
    def _classify_boards(board_selected, board_dict, base_board_dict):
        """Classify boards into outcome categories

        Args:
            board_selected (dict): Dict containing boards to summarise, keyed
                by board.target
            board_dict (dict): Dict containing boards for which we built this
                commit, keyed by board.target. The value is an Outcome object.
            base_board_dict (dict): Dict of base board outcomes

        Returns:
            BoardStatus: Named tuple containing lists of board targets
        """
        ok = []      # List of boards fixed since last commit
        warn = []    # List of boards with warnings since last commit
        err = []     # List of new broken boards since last commit
        new = []     # List of boards that didn't exist last time
        unknown = [] # List of boards that were not built

        for target in board_dict:
            if target not in board_selected:
                continue

            # If the board was built last time, add its outcome to a list
            if target in base_board_dict:
                base_outcome = base_board_dict[target].rc
                outcome = board_dict[target]
                if outcome.rc == OUTCOME_UNKNOWN:
                    unknown.append(target)
                elif outcome.rc < base_outcome:
                    if outcome.rc == OUTCOME_WARNING:
                        warn.append(target)
                    else:
                        ok.append(target)
                elif outcome.rc > base_outcome:
                    if outcome.rc == OUTCOME_WARNING:
                        warn.append(target)
                    else:
                        err.append(target)
            else:
                new.append(target)
        return BoardStatus(ok, warn, err, new, unknown)

    @staticmethod
    def _show_not_built(board_selected, board_dict):
        """Show boards that were not built

        This reports boards that couldn't be built due to toolchain issues.
        These have OUTCOME_UNKNOWN (no result file) or OUTCOME_ERROR with
        "Tool chain error" in the error lines.

        Args:
            board_selected (dict): Dict of selected boards, keyed by target
            board_dict (dict): Dict of boards that were built, keyed by target
        """
        not_built = []
        for target in board_selected:
            if target not in board_dict:
                not_built.append(target)
            else:
                outcome = board_dict[target]
                if outcome.rc == OUTCOME_UNKNOWN:
                    not_built.append(target)
                elif outcome.rc == OUTCOME_ERROR:
                    # Check for toolchain error in the error lines
                    for line in outcome.err_lines:
                        if 'Tool chain error' in line:
                            not_built.append(target)
                            break
        if not_built:
            tprint(f"Boards not built ({len(not_built)}): "
                   f"{', '.join(not_built)}")

    @staticmethod
    def _board_list(line, line_boards, list_error_boards):
        """Get a list of boards containing a particular error/warning line

        Args:
            line (str): Error line to search for
            line_boards (dict): Dict keyed by line, containing list of Board
                objects with that line
            list_error_boards (bool): True to return the board list, False to
                return empty list

        Returns:
            list: List of Board objects with that error line, or [] if
                list_error_boards is False
        """
        brds = []
        board_set = set()
        if list_error_boards:
            for brd in line_boards[line]:
                if brd not in board_set:
                    brds.append(brd)
                    board_set.add(brd)
        return brds

    @classmethod
    def _calc_error_delta(cls, base_lines, base_line_boards, lines, line_boards,
                         char, list_error_boards):
        """Calculate the required output based on changes in errors

        Args:
            base_lines (list): List of errors/warnings for previous commit
            base_line_boards (dict): Dict keyed by error line, containing a
                list of the Board objects with that error in the previous
                commit
            lines (list): List of errors/warning for this commit, each a str
            line_boards (dict): Dict keyed by error line, containing a list
                of the Board objects with that error in this commit
            char (str): Character representing error ('') or warning ('w'). The
                broken ('+') or fixed ('-') characters are added in this
                function
            list_error_boards (bool): True to include board list in output

        Returns:
            tuple: (better_lines, worse_lines) where each is a list of
                ErrLine objects
        """
        better_lines = []
        worse_lines = []
        for line in lines:
            if line not in base_lines:
                errline = ErrLine(
                    char + '+',
                    cls._board_list(line, line_boards, list_error_boards),
                    line)
                worse_lines.append(errline)
        for line in base_lines:
            if line not in lines:
                errline = ErrLine(
                    char + '-',
                    cls._board_list(line, base_line_boards, list_error_boards),
                    line)
                better_lines.append(errline)
        return better_lines, worse_lines