1#!/bin/bash
2#
3# Copyright (C) 2017 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
18# Test script for build/make/core/tasks/vndk.mk.
19# Makes sure VNDK snapshots include all required prebuilts and config files.
20#
21# Local usage:
22#   First, generate VNDK snapshots with development/vndk/snapshot/build.sh or
23#   fetch VNDK snapshot build artifacts to $DIST_DIR, then run this script.
24
25set -eo pipefail
26
27if [[ "$#" -ne 1 ]]; then
28    echo "Usage: \"$0 all\" to test all VNDK snapshot variants at once."
29    echo "       \"$0 \$TARGET_PRODUCT\" to test a specific VNDK snapshot."
30    exit 1
31fi
32
33if [[ "$1" == 'all' ]]; then
34    readonly TARGET_PRODUCTS=('aosp_arm' 'aosp_arm_ab' 'aosp_arm64' 'aosp_x86' 'aosp_x86_ab' 'aosp_x86_64')
35else
36    readonly TARGET_PRODUCTS=($1)
37fi
38
39script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
40readonly ANDROID_BUILD_TOP=$(dirname $(dirname $(dirname "${script_dir}")))
41echo "ANDROID_BUILD_TOP: "${ANDROID_BUILD_TOP}""
42
43OUT_DIR=${OUT_DIR:-}
44DIST_DIR=${DIST_DIR:-}
45if [[ -z "${DIST_DIR}" ]]; then
46    if [[ -z "${OUT_DIR}" ]]; then
47        DIST_DIR="${ANDROID_BUILD_TOP}"/out/dist
48    else
49        DIST_DIR="${OUT_DIR}"/dist
50    fi
51fi
52
53# Get PLATFORM_VNDK_VERSION
54source ""${ANDROID_BUILD_TOP}"/build/envsetup.sh" >/dev/null
55readonly PLATFORM_VNDK_VERSION="$(get_build_var PLATFORM_VNDK_VERSION)"
56
57readonly TEMP_DIR="$(mktemp -d /tmp/"$(basename $0)"_XXXXXXXX)"
58readonly SNAPSHOT_TOP="${TEMP_DIR}"/android-vndk-snapshot
59readonly SNAPSHOT_TEMPFILE="${TEMP_DIR}"/snapshot_libs.txt
60readonly SYSTEM_TEMPFILE="${TEMP_DIR}"/system_libs.txt
61readonly BINDER_32_DIRNAME='binder32'
62
63readonly RED='\033[0;31m'
64readonly NC='\033[0m'
65readonly PASS="::: PASS :::"
66readonly FAIL=""${RED}"::: FAIL :::"${NC}""
67
68
69function set_vars() {
70    TARGET_PRODUCT="$1"
71    ARCH=''
72    PRODUCT_OUT=''
73    BITNESS_SUFFIX=''
74    BINDER_BITNESS_PATH=''
75    TARGET_2ND_ARCH=''
76    case "$1" in
77        aosp_arm64)
78            ARCH='arm64'
79            PRODUCT_OUT='generic_arm64'
80            BITNESS_SUFFIX='64'
81            TARGET_2ND_ARCH='arm'
82            ;;
83        aosp_arm)
84            ARCH='arm'
85            PRODUCT_OUT='generic'
86            ;;
87        aosp_arm_ab)
88            ARCH='arm'
89            PRODUCT_OUT='generic_arm_ab'
90            BINDER_BITNESS_PATH="${BINDER_32_DIRNAME}"
91            ;;
92        aosp_x86_64)
93            ARCH='x86_64'
94            PRODUCT_OUT='generic_x86_64'
95            BITNESS_SUFFIX='64'
96            TARGET_2ND_ARCH='x86'
97            ;;
98        aosp_x86)
99            ARCH='x86'
100            PRODUCT_OUT='generic_x86'
101            ;;
102        aosp_x86_ab)
103            ARCH='x86'
104            PRODUCT_OUT='generic_x86'
105            BINDER_BITNESS_PATH="${BINDER_32_DIRNAME}"
106            ;;
107        *)
108            echo "Unrecognized \$TARGET_PRODUCT: "$1""
109            exit 1
110            ;;
111    esac
112}
113
114
115function cleanup {
116    echo "[Cleanup]"
117    echo "Removing TEMP_DIR: "${TEMP_DIR}""
118    rm -rf ""${TEMP_DIR}""
119}
120trap cleanup EXIT
121
122
123#######################################
124# Compares the list of VNDK-core and VNDK-SP
125# libs included in the snapshot and installed
126# under $PRODUCT_OUT/system/lib*
127#
128# Arguments:
129#   $1: vndk_type: one of [vndk-core, vndk-sp]
130#######################################
131function compare_vndk_libs() {
132    local vndk_type="$1"
133    local vndk_dir_suffix
134    local system_vndk_dir
135    local snapshot_dir
136    local snapshot_dir_2nd
137    local system_lib_dir
138    local system_lib_dir_2nd
139
140    if [[ -z "${PLATFORM_VNDK_VERSION}" ]]; then
141        vndk_dir_suffix=""
142    else
143        vndk_dir_suffix="-${PLATFORM_VNDK_VERSION}"
144    fi
145
146    if [[ "${vndk_type}" == 'vndk-core' ]]; then
147        system_vndk_dir="vndk${vndk_dir_suffix}"
148    else
149        system_vndk_dir="vndk-sp${vndk_dir_suffix}"
150    fi
151
152    function diff_vndk_dirs() {
153        local snapshot="$1"
154        local system="$2"
155        local target_arch="$3"
156
157        ls -1 ${snapshot} > "${SNAPSHOT_TEMPFILE}"
158        find "${system}" -type f | xargs -n 1 -I file bash -c "basename file" | sort > "${SYSTEM_TEMPFILE}"
159
160        echo "Comparing libs for TARGET_PRODUCT="${TARGET_PRODUCT}", VNDK="${vndk_type}", ARCH="${target_arch}""
161        echo "Snapshot dir:" ${snapshot}
162        echo "System dir: "${system}""
163        (diff --old-line-format="Only found in VNDK snapshot: %L" \
164              --new-line-format="Only found in /system/lib*: %L" \
165              --unchanged-line-format="" \
166              "${SNAPSHOT_TEMPFILE}" "${SYSTEM_TEMPFILE}" && echo "${PASS}") \
167        || (echo -e "${FAIL}"; exit 1)
168    }
169
170    if [[ -n "${BINDER_BITNESS_PATH}" ]]; then
171        snapshot_dir="${SNAPSHOT_TOP}"/"${ARCH}"/"${BINDER_BITNESS_PATH}"/arch-"${ARCH}"-*/shared/"${vndk_type}"
172    else
173        snapshot_dir="${SNAPSHOT_TOP}"/"${ARCH}"/arch-"${ARCH}"-*/shared/"${vndk_type}"
174    fi
175
176    system_lib_dir="${ANDROID_BUILD_TOP}"/out/target/product/"${PRODUCT_OUT}"/system/lib"${BITNESS_SUFFIX}"/"${system_vndk_dir}"
177    diff_vndk_dirs "${snapshot_dir}" $system_lib_dir "${ARCH}"
178
179    if [[ -n "${TARGET_2ND_ARCH}" ]]; then
180        snapshot_dir_2nd="${SNAPSHOT_TOP}"/"${ARCH}"/arch-"${TARGET_2ND_ARCH}"-*/shared/"${vndk_type}"
181        system_lib_dir_2nd="${ANDROID_BUILD_TOP}"/out/target/product/"${PRODUCT_OUT}"/system/lib/"${system_vndk_dir}"
182        diff_vndk_dirs "${snapshot_dir_2nd}" "${system_lib_dir_2nd}" "${TARGET_2ND_ARCH}"
183    fi
184}
185
186
187#######################################
188# Executes tests against VNDK snapshot of
189# specified $TARGET_PRODUCT
190#
191# Arguments:
192#   $1: TARGET_PRODUCT
193#######################################
194function run_tests() {
195    set_vars "$1"
196    local snapshot_zip="${DIST_DIR}"/android-vndk-"${TARGET_PRODUCT}".zip
197    local snapshot_variant_top="${SNAPSHOT_TOP}"/"${ARCH}"
198
199    echo "[Setup] Unzipping \"android-vndk-"${TARGET_PRODUCT}".zip\""
200    unzip -qn "${snapshot_zip}" -d "${SNAPSHOT_TOP}"
201
202    echo "[Test] Comparing VNDK-core and VNDK-SP libs in snapshot vs /system/lib*"
203    compare_vndk_libs 'vndk-core'
204    compare_vndk_libs 'vndk-sp'
205
206    echo "[Test] Checking required config files are present"
207    if [[ -z "${PLATFORM_VNDK_VERSION}" ]]; then
208        config_file_suffix=""
209    else
210        config_file_suffix=".${PLATFORM_VNDK_VERSION}"
211    fi
212
213    config_files=(
214        "ld.config"${config_file_suffix}".txt"
215        "llndk.libraries"${config_file_suffix}".txt"
216        "vndksp.libraries"${config_file_suffix}".txt"
217        "vndkcore.libraries.txt"
218        "vndkprivate.libraries.txt"
219        "module_paths.txt")
220    for config_file in "${config_files[@]}"; do
221        config_file_abs_path="${snapshot_variant_top}"/configs/"${config_file}"
222        if [[ ! -e "${config_file_abs_path}" ]]; then
223            echo -e ""${FAIL}" The file \""${config_file_abs_path}"\" was not found in snapshot."
224            exit 1
225        else
226            echo ""${PASS}" Found "${config_file}""
227        fi
228    done
229
230    echo "[Test] Checking directory structure of snapshot"
231    directories=(
232        "configs/"
233        "NOTICE_FILES/")
234    for sub_dir in "${directories[@]}"; do
235        dir_abs_path="${snapshot_variant_top}"/"${sub_dir}"
236        if [[ ! -d "${dir_abs_path}" ]]; then
237            echo -e ""${FAIL}" The directory \""${dir_abs_path}"\" was not found in snapshot."
238            exit 1
239        else
240            echo ""${PASS}" Found "${sub_dir}""
241        fi
242    done
243}
244
245
246# Run tests for each target product
247for target_product in "${TARGET_PRODUCTS[@]}"; do
248    echo -e "\n::::::::: Running tests for TARGET_PRODUCT="${target_product}" :::::::::"
249    run_tests "${target_product}"
250done
251
252echo "Done. All tests passed!"
253