1#!/bin/bash -u
2# Regression test for the product and/or board configuration converter.
3#
4# Builds 'nothing' for a given product-variant twice: with product/board
5# config makefiles converted to Starlark, and without such conversion.
6# The generated Ninja files should be the same.
7set -u
8
9function die() {
10    echo $@ >&2
11    exit 1
12}
13
14function usage() {
15    cat <<EOF >&2
16Usage: $myname [-q] [-r] <product-variant> [product-variant ...]
17  -q: Quiet. Suppress all output other than a failure message
18  -r: Retain Ninja files
19EOF
20    exit 1
21}
22
23function build() {
24    local -r flavor="$1"
25    local -r product="$2"
26    local -r variant="$3"
27    shift 3
28    command="build/soong/soong_ui.bash --make-mode TARGET_PRODUCT=$product TARGET_BUILD_VARIANT=$variant $@ nothing"
29    if ! ANDROID_QUIET_BUILD=$quiet $command; then
30        printf "%s-%s: %s build failed, actual command:\n  %s\n" $product $variant $flavor "$command" >&2
31        exit 1
32    fi
33}
34
35mypath=$(realpath "$0")
36declare -r mydir=${mypath%/*/*/*/*}
37declare -r myname=${mypath#${mydir}/}
38
39flags_rbc=(RBC_PRODUCT_CONFIG=true)
40quiet=
41while getopts "qr" o; do
42    case "${o}" in
43        q) quiet=true ;;
44        r) retain_files=t ;;
45        *) usage ;;
46    esac
47done
48shift $((OPTIND-1))
49[[ $# -gt 0 ]] || usage
50
51cd $mydir
52rc=0
53for arg in $@; do
54    [[ "$arg" =~ ^([a-zA-Z0-9_]+)-([a-zA-Z0-9_]+)$ ]] || \
55        die "Invalid product name: $arg. Example: aosp_arm64-userdebug"
56    product="${BASH_REMATCH[1]}"
57    variant="${BASH_REMATCH[2]}"
58    ninja_files=(soong/build.${product}.ninja build-${product}.ninja build-${product}-package.ninja)
59
60    # Build with converter, save Ninja files, build without it.
61    saved_ninja_dir=out/ninja_rbc/${product}-${variant}
62    build RBC $product $variant ${flags_rbc[@]} && \
63      rm -rf $saved_ninja_dir && mkdir -p $saved_ninja_dir/soong && \
64      (for f in ${ninja_files[@]}; do mv -f out/$f $saved_ninja_dir/$f || exit 1; done) && \
65      build baseline $product $variant
66    rc=$?
67
68    # Compare Ninja files
69    if ((rc==0)); then
70        for f in "${ninja_files[@]}"; do
71            diff_file=$(mktemp)
72            diff out/$f $saved_ninja_dir/$f | head >& $diff_file
73            if [[ -s $diff_file ]]; then
74                echo ${product}-${variant}: "$f" is different '< make, > RBC):' >&2
75                cat $diff_file >&2
76                echo ...
77                rc=1
78            fi
79            rm $diff_file
80        done
81    fi
82    [[ -n "${retain_files:-}" ]] || rm -rf $saved_ninja_dir
83done
84
85((rc==0)) || printf "In order to reproduce the failures above, run\n  %s <product>-<variant>\n" $myname >&2
86exit $rc
87