1#!/bin/bash
2
3#
4# This script runs the full set of tests for product config:
5# 1. Build the product-config tool.
6# 2. Run the unit tests.
7# 3. Run the product config for every product available in the current
8#    source tree, for each of user, userdebug and eng.
9#       - To restrict which products or variants are run, set the
10#         PRODUCTS or VARIANTS environment variables.
11#       - Products for which the make based product config fails are
12#         skipped.
13#
14
15# The PRODUCTS variable is used by the build, and setting it in the environment
16# interferes with that, so unset it.  (That should probably be fixed)
17products=$PRODUCTS
18variants=$VARIANTS
19unset PRODUCTS
20unset VARIANTS
21
22# Don't use lunch from the user's shell
23unset TARGET_PRODUCT
24unset TARGET_BUILD_VARIANT
25
26function die() {
27    format=$1
28    shift
29    printf "$format\nStopping...\n" $@ >&2
30    exit 1;
31}
32
33[[ -f build/make/envsetup.sh ]] || die "Run this script from the root of the tree."
34: ${products:=$(build/soong/soong_ui.bash --dumpvar-mode all_named_products | sed -e "s/ /\n/g" | sort -u )}
35: ${variants:="user userdebug eng"}
36: ${CKATI_BIN:=prebuilts/build-tools/$(build/soong/soong_ui.bash --dumpvar-mode HOST_PREBUILT_TAG)/bin/ckati}
37
38function if_signal_exit() {
39    [[ $1 -lt 128 ]] || exit $1
40}
41
42build/soong/soong_ui.bash --build-mode --all-modules --dir="$(pwd)" product-config-test product-config \
43    || die "Build failed."
44
45echo
46echo Running unit tests
47java -jar out/host/linux-x86/testcases/product-config-test/product-config-test.jar
48unit_tests=$?
49if_signal_exit $unit_tests
50
51failed_baseline_checks=
52for product in $products ; do
53    for variant in $variants ; do
54        echo
55        echo "Checking: lunch $product-$variant"
56
57        TARGET_PRODUCT=$product \
58            TARGET_BUILD_VARIANT=$variant \
59            build/soong/soong_ui.bash --dumpvar-mode TARGET_PRODUCT &> /dev/null
60        exit_status=$?
61        if_signal_exit $exit_status
62        if [ $exit_status -ne 0 ] ; then
63            echo "*** Combo fails with make, skipping product-config test run for $product-$variant"
64        else
65            rm -rf out/config/$product-$variant
66            TARGET_PRODUCT=$product TARGET_BUILD_VARIANT=$variant product-config \
67                            --ckati_bin $CKATI_BIN \
68                            --error 1000
69            exit_status=$?
70            if_signal_exit $exit_status
71            if [ $exit_status -ne 0 ] ; then
72                failed_baseline_checks="$failed_baseline_checks $product-$variant"
73            fi
74            if [ "$CHECK_FOR_RULES" != "" ] ; then
75                # This is a little bit of sleight of hand for good output formatting at the
76                # expense of speed. We've already run the command once without
77                # ALLOW_RULES_IN_PRODUCT_CONFIG, so we know it passes there. We run it again
78                # with ALLOW_RULES_IN_PRODUCT_CONFIG=error to see if it fails, but that will
79                # cause it to only print the first error. But we want to see all of them,
80                # so if it fails we run it a third time with ALLOW_RULES_IN_PRODUCT_CONFIG=warning,
81                # so we can see all the warnings.
82                TARGET_PRODUCT=$product \
83                    TARGET_BUILD_VARIANT=$variant \
84                    ALLOW_RULES_IN_PRODUCT_CONFIG=error \
85                    build/soong/soong_ui.bash --dumpvar-mode TARGET_PRODUCT &> /dev/null
86                exit_status=$?
87                if_signal_exit $exit_status
88                if [ $exit_status -ne 0 ] ; then
89                    TARGET_PRODUCT=$product \
90                        TARGET_BUILD_VARIANT=$variant \
91                        ALLOW_RULES_IN_PRODUCT_CONFIG=warning \
92                        build/soong/soong_ui.bash --dumpvar-mode TARGET_PRODUCT > /dev/null
93                    failed_rule_checks="$failed_rule_checks $product-$variant"
94                fi
95            fi
96        fi
97    done
98done
99
100echo
101echo
102echo "------------------------------"
103echo SUMMARY
104echo "------------------------------"
105
106echo -n "Unit tests        "
107if [ $unit_tests -eq 0 ] ; then echo PASSED ; else echo FAILED ; fi
108
109echo -n "Baseline checks   "
110if [ "$failed_baseline_checks" = "" ] ; then echo PASSED ; else echo FAILED ; fi
111for combo in $failed_baseline_checks ; do
112    echo "                   ... $combo"
113done
114
115echo -n "Rules checks      "
116if [ "$failed_rule_checks" = "" ] ; then echo PASSED ; else echo FAILED ; fi
117for combo in $failed_rule_checks ; do
118    echo "                   ... $combo"
119done
120
121