1#!/usr/bin/env bash
2
3# Copyright (C) 2020 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17set -e
18
19if [ $# == 0 ]; then
20  echo "Usage: golden_test.sh [check|update] <input files>"
21fi
22
23function _golden_test() {
24  local update=0
25  case $1 in
26    check)
27      update=0
28      ;;
29    update)
30      update=1
31      ;;
32    *)
33      echo "Argument must be 'check' or 'update' but is $1"
34      exit 1
35      ;;
36  esac
37
38  # warning: this list must be kept in sync with system/tools/aidl/Android.bp
39  modules=(
40    "aidl-test-interface-cpp-source"
41    "aidl-test-interface-java-source"
42    "aidl-test-interface-ndk-source"
43    "aidl-test-interface-rust-source"
44    "aidl-test-extras-java-source"
45    "aidl-cpp-java-test-interface-cpp-source"
46    "aidl-cpp-java-test-interface-java-source"
47    "aidl-test-versioned-interface-V1-cpp-source"
48    "aidl-test-versioned-interface-V1-java-source"
49    "aidl-test-versioned-interface-V1-ndk-source"
50    "aidl-test-versioned-interface-V1-rust-source"
51    "aidl-test-versioned-interface-V2-cpp-source"
52    "aidl-test-versioned-interface-V2-java-source"
53    "aidl-test-versioned-interface-V2-ndk-source"
54    "aidl-test-versioned-interface-V2-rust-source"
55    "aidl-test-versioned-interface-V3-cpp-source"
56    "aidl-test-versioned-interface-V3-java-source"
57    "aidl-test-versioned-interface-V3-ndk-source"
58    "aidl-test-versioned-interface-V3-rust-source"
59    "aidl_test_loggable_interface-cpp-source"
60    "aidl_test_loggable_interface-java-source"
61    "aidl_test_loggable_interface-ndk-source"
62    "aidl-test-interface-permission-java-source"
63    "aidl-test-fixedsizearray-cpp-source"
64    "aidl-test-fixedsizearray-java-source"
65    "aidl-test-fixedsizearray-ndk-source"
66    "aidl-test-fixedsizearray-rust-source"
67    "aidl-test-interface-cpp-analyzer-source"
68    "tests/trunk_stable_test/android.aidl.test.trunk-V1-cpp-source"
69    "tests/trunk_stable_test/android.aidl.test.trunk-V2-cpp-source"
70    "tests/trunk_stable_test/android.aidl.test.trunk-V1-ndk-source"
71    "tests/trunk_stable_test/android.aidl.test.trunk-V2-ndk-source"
72    "tests/trunk_stable_test/android.aidl.test.trunk-V1-java-source"
73    "tests/trunk_stable_test/android.aidl.test.trunk-V2-java-source"
74    "tests/trunk_stable_test/android.aidl.test.trunk-V1-rust-source"
75    "tests/trunk_stable_test/android.aidl.test.trunk-V2-rust-source"
76  )
77
78  local use_unfrozen="$3"
79  # If override_unfrozen is true, we override the RELEASE_AIDL_USE_UNFROZEN build flag with an
80  # environment variable.
81  local override_unfrozen="$4"
82  local root="$2"
83  local input_files=$5
84  if [ "$update" = 1 ]; then
85    if [ "$override_unfrozen" == true ]; then
86      export AIDL_USE_UNFROZEN_OVERRIDE="$use_unfrozen"
87    fi
88    # clean up intermediates before building these modules
89    for module in "${modules[@]}"; do
90      rm -rf "$root/out/soong/.intermediates/system/tools/aidl/$module/gen"
91    done
92    "$root"/build/soong/soong_ui.bash --make-mode \
93      $(for i in "${modules[@]}"; do
94          echo "out/soong/.intermediates/system/tools/aidl/$i/timestamp"
95        done) ; export AIDL_USE_UNFROZEN_OVERRIDE=
96  fi
97  local e=0
98  if [ -z "$input_files" ]; then
99    for module in "${modules[@]}"; do
100      local built="$root/out/soong/.intermediates/system/tools/aidl/$module/"
101      local module_path
102      if [ "$use_unfrozen" == "true" ]; then
103        module_path=$module
104      else
105        module_path="frozen/$module"
106      fi
107
108      local golden="$root/system/tools/aidl/tests/golden_output/$module_path/"
109
110      if [ "$update" = 1 ]; then
111        rm -rf "$golden"
112        mkdir -p "$golden"
113        cp -r "$built/gen" "$golden"
114      else
115        diff -rN "$built/gen" "$golden/gen" || e=1
116      fi
117    done
118  else
119    for file in $input_files; do
120      if [[ "$file" =~ ^system.* ]]; then
121        continue
122      fi
123      name=$(echo "$file" | sed -e "s@^.*/system/tools/aidl/@@")
124      if [ "$use_unfrozen" == "true" ]; then
125        module_path="$name"
126      else
127        module_path="frozen/$name"
128      fi
129      golden_file="system/tools/aidl/tests/golden_output/$module_path"
130      if [ "$update" = 1 ]; then
131        # We don't get header files in the build hook's $in, and we want them in
132        # the golden_output, so run this script manually.
133        echo update does not support multiple files as arguments.
134        exit 1
135      else
136        diff -N --unified=0 "$file" "$golden_file" || e=1
137      fi
138    done
139  fi
140
141  if [ "$e" = 1 ]; then
142    echo "ERROR: The AIDL compiler is outputting files in an unknown way."
143    echo "ERROR: to accept these changes, please run:"
144    echo "ERROR:     \$ANDROID_BUILD_TOP/system/tools/aidl/tests/golden_test.sh update"
145    exit 1
146  else
147    if [ "$update" = 1 ]; then
148      echo "UPDATE GOLDEN TEST SUCCESS"
149    fi
150  fi
151}
152
153root="."
154if [ "$ANDROID_BUILD_TOP" != "" ]; then
155  root="$ANDROID_BUILD_TOP"
156fi
157if [[ "$TARGET_RELEASE" == "next" ]]; then
158  use_unfrozen=false
159else
160  use_unfrozen=true
161fi
162
163action=$1
164# we only want the input files in $@
165shift
166
167if [ "$action" == "update" ]; then
168  if [ "$use_unfrozen" == "true" ]; then
169    # build update the opposite value first, so we leave the intermediates
170    # in the state of the tree
171    _golden_test "$action" "$root" "false" true
172    _golden_test "$action" "$root" "true" false
173  else
174    _golden_test "$action" "$root" "true" true
175    _golden_test "$action" "$root" "false" false
176  fi
177else
178_golden_test "$action" "$root" "$use_unfrozen" false "$*"
179fi
180