1#!/bin/bash 2 3# Copyright (C) 2022 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 -uo pipefail 18 19# Integration test for verifying arch variant cflags set on cc modules included 20# in Bazel-built apexes in the real source tree. 21 22if [ ! -e "build/make/core/Makefile" ]; then 23 echo "$0 must be run from the top of the Android source tree." 24 exit 1 25fi 26 27############ 28# Test Setup 29############ 30 31OUTPUT_DIR="$(mktemp -d tmp.XXXXXX)" 32BAZEL_OUTPUT_DIR="$OUTPUT_DIR/bazel" 33 34export TARGET_PRODUCT="aosp_arm64" 35[ "$#" -ge 1 ] && export TARGET_PRODUCT="$1" 36ARCH_VARIANT_CFLAG="armv8-a" 37[ "$#" -ge 2 ] && ARCH_VARIANT_CFLAG="$2" 38CPU_VARIANT_CFLAG="" 39[ "$#" -ge 3 ] && CPU_VARIANT_CFLAG="$3" 40 41function call_bazel() { 42 build/bazel/bin/bazel --output_base="$BAZEL_OUTPUT_DIR" $@ 43} 44 45function cleanup { 46 # call bazel clean because some bazel outputs don't have w bits. 47 call_bazel clean 48 rm -rf "${OUTPUT_DIR}" 49} 50trap cleanup EXIT 51 52###################### 53# Run bp2build / Bazel 54###################### 55build/soong/soong_ui.bash --make-mode BP2BUILD_VERBOSE=1 --skip-soong-tests bp2build 56 57# Number of CppCompile actions with arch variant flag 58actions_with_arch_variant_num=$(call_bazel aquery --config=bp2build --config=ci --config=android \ 59 'mnemonic("CppCompile", deps(//build/bazel/examples/apex/minimal:build.bazel.examples.apex.minimal))' | grep -c \'-march=$ARCH_VARIANT_CFLAG\') 60 61# Number of all CppCompile actions 62all_cppcompile_actions_num=0 63aquery_summary=$(call_bazel aquery --config=bp2build --config=ci --config=android --output=summary \ 64 'mnemonic("CppCompile", deps(//build/bazel/examples/apex/minimal:build.bazel.examples.apex.minimal))' \ 65 | egrep -o '.*opt-ST.*: ([0-9]+)$' \ 66 | cut -d: -f2 -) 67 68while read -r num; 69do 70 all_cppcompile_actions_num=$(($all_cppcompile_actions_num + $num)) 71done <<< "$aquery_summary" 72 73if [ $actions_with_arch_variant_num -eq $all_cppcompile_actions_num ] 74then 75 echo "Pass: arch variant is set." 76else 77 echo "Error: number of CppCompile actions with arch variant set: actual=$actions_with_arch_variant_num, expected=$all_cppcompile_actions_num" 78 exit 1 79fi 80 81if [ $CPU_VARIANT_CFLAG ] 82then 83 # Number of CppCompiler actions with cpu variant flag 84 actions_with_cpu_variant_num=$(call_bazel aquery --config=bp2build --config=ci --config=android \ 85 'mnemonic("CppCompile", deps(//build/bazel/examples/apex/minimal:build.bazel.examples.apex.minimal))' | grep -c "\-mcpu=$CPU_VARIANT_CFLAG") 86 87 if [ $actions_with_cpu_variant_num -eq $all_cppcompile_actions_num ] 88 then 89 echo "Pass: cpu variant is set." 90 else 91 echo "Error: number of CppCompile actions with cpu variant set: actual=$actions_with_cpu_variant_num, expected=$all_cppcompile_actions_num" 92 exit 1 93 fi 94fi 95