blob: 21b476bad41bc87891cb91c29b8677a62cb2cb2d [file] [log] [blame]
Yu Liufb458e92023-05-25 17:20:44 -07001#!/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
17set -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
22function die() { format=$1; shift; printf >&2 "$format\n" $@; exit 1; }
23
24function usage() {
25 die "usage: ${0##*/} <-t lunch_target> [module]..."
26}
27
28if [ ! -e "build/make/core/Makefile" ]; then
29 die "$0 must be run from the top of the Android source tree."
30fi
31
32declare TARGET=
33while getopts "t:" opt; do
34 case $opt in
35 t)
36 TARGET=$OPTARG ;;
37 *) usage ;;
38 esac
39done
40
41shift $((OPTIND-1))
42MODULES="$@"
43
44source build/envsetup.sh
45
46if [[ -n $TARGET ]]; then
47 lunch $TARGET
48fi
49
50if [[ -z ${OUT_DIR+x} ]]; then
51 OUT_DIR="out"
52fi
53
54OUTPUT_DIR="$(mktemp -d tmp.XXXXXX)"
55PASS=true
56
57function cleanup {
58 if [ $PASS = true ]; then
59 rm -rf "${OUTPUT_DIR}"
60 fi
61}
62trap cleanup EXIT
63
64declare -A GEN_PATH_MAP
65
66function 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
75function 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
86function 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
101if [ ! -f "$ANDROID_PRODUCT_OUT/module-info.json" ]; then
102 refreshmod
103fi
104
105find_gen_paths
106m --skip-soong-tests GENRULE_SANDBOXING=true "${MODULES[@]}"
107store_outputs "$OUTPUT_DIR/sandbox"
108m --skip-soong-tests GENRULE_SANDBOXING=false "${MODULES[@]}"
109store_outputs "$OUTPUT_DIR/non_sandbox"
110
111cmp_outputs "$OUTPUT_DIR/non_sandbox" "$OUTPUT_DIR/sandbox"