Yu Liu | fb458e9 | 2023-05-25 17:20:44 -0700 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | # Copyright (C) 2023 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 | |
| 17 | set -e |
| 18 | |
| 19 | # Build the given genrule modules with GENRULE_SANDBOXING enabled and disabled, |
| 20 | # then compare the output of the modules and report result. |
| 21 | |
| 22 | function die() { format=$1; shift; printf >&2 "$format\n" $@; exit 1; } |
| 23 | |
| 24 | function usage() { |
| 25 | die "usage: ${0##*/} <-t lunch_target> [module]..." |
| 26 | } |
| 27 | |
| 28 | if [ ! -e "build/make/core/Makefile" ]; then |
| 29 | die "$0 must be run from the top of the Android source tree." |
| 30 | fi |
| 31 | |
| 32 | declare TARGET= |
| 33 | while getopts "t:" opt; do |
| 34 | case $opt in |
| 35 | t) |
| 36 | TARGET=$OPTARG ;; |
| 37 | *) usage ;; |
| 38 | esac |
| 39 | done |
| 40 | |
| 41 | shift $((OPTIND-1)) |
| 42 | MODULES="$@" |
| 43 | |
| 44 | source build/envsetup.sh |
| 45 | |
| 46 | if [[ -n $TARGET ]]; then |
| 47 | lunch $TARGET |
| 48 | fi |
| 49 | |
| 50 | if [[ -z ${OUT_DIR+x} ]]; then |
| 51 | OUT_DIR="out" |
| 52 | fi |
| 53 | |
| 54 | OUTPUT_DIR="$(mktemp -d tmp.XXXXXX)" |
| 55 | PASS=true |
| 56 | |
| 57 | function cleanup { |
| 58 | if [ $PASS = true ]; then |
| 59 | rm -rf "${OUTPUT_DIR}" |
| 60 | fi |
| 61 | } |
| 62 | trap cleanup EXIT |
| 63 | |
| 64 | declare -A GEN_PATH_MAP |
| 65 | |
| 66 | function find_gen_paths() { |
| 67 | for module in $MODULES; do |
| 68 | module_path=$(pathmod "$module") |
| 69 | package_path=${module_path#$ANDROID_BUILD_TOP} |
| 70 | gen_path=$OUT_DIR/soong/.intermediates$package_path/$module |
| 71 | GEN_PATH_MAP[$module]=$gen_path |
| 72 | done |
| 73 | } |
| 74 | |
| 75 | function store_outputs() { |
| 76 | local dir=$1; shift |
| 77 | |
| 78 | for module in $MODULES; do |
| 79 | dest_dir=$dir/${module} |
| 80 | mkdir -p $dest_dir |
| 81 | gen_path=${GEN_PATH_MAP[$module]} |
| 82 | cp -r $gen_path $dest_dir |
| 83 | done |
| 84 | } |
| 85 | |
| 86 | function cmp_outputs() { |
| 87 | local dir1=$1; shift |
| 88 | local dir2=$1; shift |
| 89 | |
| 90 | for module in $MODULES; do |
| 91 | if ! diff -rq --exclude=genrule.sbox.textproto $dir1/$module $dir2/$module; then |
| 92 | PASS=false |
| 93 | echo "$module differ" |
| 94 | fi |
| 95 | done |
| 96 | if [ $PASS = true ]; then |
| 97 | echo "Test passed" |
| 98 | fi |
| 99 | } |
| 100 | |
| 101 | if [ ! -f "$ANDROID_PRODUCT_OUT/module-info.json" ]; then |
| 102 | refreshmod |
| 103 | fi |
| 104 | |
| 105 | find_gen_paths |
| 106 | m --skip-soong-tests GENRULE_SANDBOXING=true "${MODULES[@]}" |
| 107 | store_outputs "$OUTPUT_DIR/sandbox" |
| 108 | m --skip-soong-tests GENRULE_SANDBOXING=false "${MODULES[@]}" |
| 109 | store_outputs "$OUTPUT_DIR/non_sandbox" |
| 110 | |
| 111 | cmp_outputs "$OUTPUT_DIR/non_sandbox" "$OUTPUT_DIR/sandbox" |