1#! /bin/bash 2# 3# Script to setup virtual environment to run GD cert tests and devlop using IDE 4# 5# Usage 6# 1. cd packages/modules/Bluetooth/system/gd 7# 2. source cert/set_up_virtualenv.sh 8# 4. [run tests, do development, hack] using vevn/bin/python 9# 10# Note: Just use the virtualized Python binary, no need to activate 11 12## Android build main build setup script relative to top level android source root 13BUILD_SETUP=./build/envsetup.sh 14 15function UsageAndroidTree { 16 cat<<EOF 17Ensure invoked from within the android source tree 18EOF 19} 20 21function UsageSourcedNotExecuted { 22 cat<<EOF 23Ensure script is SOURCED and not executed to persist the build setup 24e.g. 25source $0 26EOF 27} 28 29function UpFind { 30 while [[ $PWD != / ]] ; do 31 rc=$(find "$PWD" -maxdepth 1 "$@") 32 if [ -n "$rc" ]; then 33 echo $(dirname "$rc") 34 return 35 fi 36 cd .. 37 done 38} 39 40function SetUpAndroidBuild { 41 pushd . 42 android_root=$(UpFind -name out -type d) 43 if [[ -z $android_root ]] ; then 44 UsageAndroidTree 45 return 46 fi 47 echo "Found android root $android_root" 48 cd $android_root && . $BUILD_SETUP 49 echo "Sourced build setup rules" 50 cd $android_root && lunch 51 popd 52} 53 54function SetupPython38 { 55 echo "Setting up python3.8" 56 sudo apt-get install python3.8-dev 57} 58 59function SetupPip3 { 60 echo "Setting up pip3" 61 sudo apt-get install python3-pip 62} 63 64# Deactivate existing virtual environment, if any, ignore errors 65deactivate > /dev/null 2>&1 66 67if [[ "${BASH_SOURCE[0]}" == "${0}" ]] ; then 68 UsageSourcedNotExecuted 69 return 1 70fi 71 72## Check python3.8 is installed properly 73## Need Python 3.8 because bluetooth_packets_python3 is compiled against 74## Python 3.8 headers 75dpkg -l python3.8-dev > /dev/null 2>&1 76if [[ $? -ne 0 ]] ; then 77 SetupPython38 78fi 79 80## Check pip3 is installed properly 81## Need pip3 for Python 3 support 82dpkg -l python3-pip > /dev/null 2>&1 83if [[ $? -ne 0 ]] ; then 84 SetupPip3 85fi 86 87# Install and upgrade virtualenv to latest version 88pip3 install --user --upgrade virtualenv > /dev/null 2>&1 89if [[ $? -ne 0 ]] ; then 90 echo "Error install and upgrade virtualenv" 91 return 1 92fi 93 94# Set-up Android environment variables 95if [[ -z "$ANDROID_BUILD_TOP" ]] ; then 96 SetUpAndroidBuild 97fi 98 99## Compile and unzip test artifacts 100echo "Compiling bluetooth_stack_with_facade ..." 101$ANDROID_BUILD_TOP/build/soong/soong_ui.bash --build-mode --"all-modules" --dir="$(pwd)" dist bluetooth_stack_with_facade 102if [[ $? -ne 0 ]] ; then 103 echo "Failed to compile bluetooth_stack_with_facade" 104 return 1 105fi 106if [[ ! -f "$ANDROID_BUILD_TOP/out/dist/bluetooth_cert_tests.zip" ]]; then 107 echo "Cannot find bluetooth_cert_tests.zip after compilation" 108 return 1 109fi 110 111CERT_TEST_VENV=$ANDROID_BUILD_TOP/out/dist/bluetooth_venv 112 113rm -rf $CERT_TEST_VENV 114 115python3.8 -m virtualenv --python `which python3.8` $CERT_TEST_VENV 116if [[ $? -ne 0 ]] ; then 117 echo "Error setting up virtualenv" 118 return 1 119fi 120 121unzip -o -q $ANDROID_BUILD_TOP/out/dist/bluetooth_cert_tests.zip -d $CERT_TEST_VENV/acts 122if [[ $? -ne 0 ]] ; then 123 echo "Error unzipping bluetooth_cert_tests.zip" 124 return 1 125fi 126 127$CERT_TEST_VENV/bin/python $CERT_TEST_VENV/acts/setup.py install 128if [[ $? -ne 0 ]] ; then 129 echo "Error installing GD libraries" 130 return 1 131fi 132 133$CERT_TEST_VENV/bin/python -c " 134import bluetooth_packets_python3 as bp3 135bp3.BaseStruct 136" 137if [[ $? -ne 0 ]] ; then 138 echo "Setup failed as bluetooth_packets_python3 cannot be imported" 139 return 1 140fi 141 142echo "" 143echo "Please mark GD root directory as \"Project Sources and Headers\" in IDE" 144echo "If still seeing errors, invalidate cached and restart" 145echo "virtualenv setup complete" 146