Alexander Williams | 8365ac4 | 2022-08-17 17:07:28 -0700 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # Copyright lowRISC contributors. |
| 3 | # Licensed under the Apache License, Version 2.0, see LICENSE for details. |
| 4 | # SPDX-License-Identifier: Apache-2.0 |
| 5 | |
| 6 | set -e |
| 7 | |
| 8 | usage_string=" |
| 9 | Usage: get-bitstream-strategy.sh <cached-bitstream-target> [pathspec]... |
| 10 | |
| 11 | cached-bitsream-target The bazel target for the cached bitstream. This script |
| 12 | will retrieve the most recent image from a commit in this |
| 13 | this branch's history. |
| 14 | pathspec A git pathspec (multiple instances permitted) indicating which files |
| 15 | trigger building the bitstream. Please use only exclude pathspecs |
| 16 | to cause new, unconsidered paths to trigger the build. |
| 17 | " |
| 18 | |
| 19 | if [ $# -lt 1 ]; then |
| 20 | echo >&2 "ERROR: Unexpected number of arguments" |
| 21 | echo >&2 "${usage_string}" |
| 22 | exit 1 |
| 23 | fi |
| 24 | |
| 25 | . util/build_consts.sh |
| 26 | |
| 27 | bitstream_target=${*:1:1} |
| 28 | |
| 29 | if [ $# -gt 1 ]; then |
| 30 | excluded_files=( "${@:2}" ) |
| 31 | else |
| 32 | excluded_files=( "." ) |
| 33 | fi |
| 34 | |
| 35 | # Retrieve the most recent bitstream at or before HEAD. |
| 36 | BITSTREAM="--refresh HEAD" ./bazelisk.sh build ${bitstream_target} |
| 37 | |
| 38 | # The directory containing the bitstream is named after the git hash. |
| 39 | bitstream_file=$(ci/scripts/target-location.sh ${bitstream_target}) |
| 40 | bitstream_commit=$(basename "$(dirname ${bitstream_file})") |
| 41 | |
| 42 | echo "Checking for changes against pre-built bitstream from ${bitstream_commit}" |
| 43 | echo "Files changed:" |
| 44 | git diff --stat --name-only ${bitstream_commit} |
| 45 | echo |
| 46 | echo "Changed files after exclusions applied:" |
| 47 | # Use the cached bitstream if no changed files remain. |
| 48 | if git diff --exit-code --stat --name-only ${bitstream_commit} -- "${excluded_files[@]}"; then |
| 49 | bitstream_strategy=cached |
| 50 | else |
| 51 | bitstream_strategy=build |
| 52 | fi |
| 53 | |
| 54 | echo |
| 55 | echo "Bitstream strategy is ${bitstream_strategy}" |
| 56 | echo "##vso[task.setvariable variable=bitstreamStrategy]${bitstream_strategy}" |