Parent directory

size_regression.sh

3692 bytes
  1#!/bin/bash
  2
  3# Copyright 2021 Nick Brassel (@tzarc)
  4# SPDX-License-Identifier: GPL-2.0-or-later
  5
  6set -eEuo pipefail
  7
  8job_count=$(getconf _NPROCESSORS_ONLN 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null || echo 2)
  9source_ref="0.11.0"
 10dest_ref="develop"
 11ignore_ref="master"
 12unset skip_zero
 13
 14export SIZE_REGRESSION_EXECUTING=1
 15
 16function usage() {
 17    echo "Usage: $(basename "$0") [-h] [-j <jobs>] [-s <source>] [-d <dest>] [-n] planck/rev6:default"
 18    echo "    -h           : Shows this usage page."
 19    echo "    -j <threads> : Change the number of threads to execute with. Defaults to \`$job_count\`."
 20    echo "    -s <source>  : Use source commit, branch, tag, or sha1 to start the search. Defaults to \`$source_ref\`."
 21    echo "    -d <dest>    : Use destination commit, branch, tag, or sha1 to end the search. Defaults to \`$dest_ref\`."
 22    echo "    -i <ignore>  : The branch to ignore refs from. Defaults to \`$ignore_ref\`."
 23    echo "    -n           : Skips printing changes if the delta is zero."
 24    exit 1
 25}
 26
 27if [[ ${#} -eq 0 ]]; then
 28    usage
 29    exit 0
 30fi
 31
 32unset cleanup_completed
 33_internal_cleanup() {
 34    if [[ -z "${cleanup_completed:-}" ]] ; then
 35        echo
 36        echo
 37        echo 'Your git repository is in an indeterminate state!' >&2
 38        echo 'Make sure you swap to your intended branch.' >&2
 39        echo
 40        unset SIZE_REGRESSION_EXECUTING
 41    fi
 42    cleanup_completed=1
 43}
 44trap _internal_cleanup EXIT HUP INT
 45
 46while getopts "hj:s:d:i:n" opt "$@" ; do
 47    case "$opt" in
 48        h) usage; exit 0;;
 49        j) job_count="${OPTARG:-}";;
 50        s) source_ref="${OPTARG:-}";;
 51        d) dest_ref="${OPTARG:-}";;
 52        i) ignore_ref="${OPTARG:-}";;
 53        n) skip_zero=1;;
 54        \?) usage >&2; exit 1;;
 55    esac
 56done
 57
 58# Work out the target board
 59shift $((OPTIND-1))
 60keyboard_target=$1
 61
 62# Helper for resetting submodule existence
 63fixup_submodules() {
 64    [ -e lib/ugfx ] && rm -rf lib/ugfx
 65    [ -e lib/pico-sdk ] && rm -rf lib/pico-sdk
 66    [ -e lib/chibios-contrib/ext/mcux-sdk ] && rm -rf lib/chibios-contrib/ext/mcux-sdk
 67    [ -e lib/lvgl ] && rm -rf lib/lvgl
 68    make git-submodule
 69}
 70
 71last_size=0
 72last_line=""
 73function build_executor() {
 74    git rev-list --oneline --no-merges ${source_ref}...${dest_ref} ^${ignore_ref} | while IFS= read -r line ; do
 75        revision=$(echo $line | cut -d' ' -f1)
 76
 77        make distclean >/dev/null 2>&1
 78
 79        git checkout -f $revision >/dev/null 2>&1 || { echo "Failed to check out revision ${revision}" >&2 ; exit 1 ; }
 80        fixup_submodules >/dev/null 2>&1
 81        make -j${job_count} $keyboard_target >/dev/null 2>&1 || true
 82        file_size=$(arm-none-eabi-size .build/*.elf 2>/dev/null | awk '/elf/ {print $1}' 2>/dev/null || true)
 83
 84        if [[ "$last_size" == 0 ]] ; then last_size=$file_size ; fi
 85        if [[ -z "$file_size" ]] ; then file_size=0 ; fi
 86
 87        if [[ -n "$last_line" ]] ; then
 88            size_delta=$(( $last_size - $file_size ))
 89            if { [[ -n "${skip_zero:-}" ]] && [[ $size_delta -ne 0 ]] ; } || [[ -z "${skip_zero:-}" ]] || [[ $file_size -eq 0 ]] ; then
 90                printf "Size: %8d, delta: %+6d -- %s\n" "$last_size" "$size_delta" "$last_line"
 91            fi
 92        fi
 93
 94        last_size=$file_size
 95        last_line=$line
 96    done
 97
 98    if [ -n "$last_line" ] ; then
 99        size_delta=0
100        printf "Size: %8d, delta: %+6d -- %s\n" "$last_size" "$size_delta" "$last_line"
101    fi
102}
103
104# The actual execution of all the builds needs to be the last command in the entire script
105# - During builds, this script file will disappear, so we need the entire script to be
106#   loaded into the script interpreter at the time of execution. Do not refactor.
107build_executor