1#!/bin/sh
2# Copyright 2020 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15if [ "${BASH_SOURCE-}" = "$0" ]; then
16  echo "You must source this script: \$ source $0" >&2
17  echo "It will create a virtual environment in which emu-docker will be installed."
18  exit 33
19fi
20
21panic() {
22  echo "ERROR: $@" >&2
23  exit 1
24}
25
26# Return the build machine's operating system tag.
27# Valid return values are:
28#    linux
29#    darwin
30#    freebsd
31#    windows   (really MSys)
32#    cygwin
33get_build_os() {
34  if [ -z "$_SHU_BUILD_OS" ]; then
35    _SHU_BUILD_OS=$(uname -s)
36    case $_SHU_BUILD_OS in
37    Darwin)
38      _SHU_BUILD_OS=darwin
39      ;;
40    FreeBSD) # note: this is not tested
41      _SHU_BUILD_OS=freebsd
42      ;;
43    Linux)
44      # note that building  32-bit binaries on x86_64 is handled later
45      _SHU_BUILD_OS=linux
46      ;;
47    CYGWIN* | *_NT-*)
48      _SHU_BUILD_OS=windows
49      if [ "x$OSTYPE" = xcygwin ]; then
50        _SHU_BUILD_OS=cygwin
51      fi
52      ;;
53    esac
54  fi
55  echo "$_SHU_BUILD_OS"
56}
57
58aosp_find_python() {
59  local AOSP_PREBUILTS_DIR=$AOSP_DIR/prebuilts
60  local OS_NAME=$(get_build_os)
61  local PYTHON=$AOSP_PREBUILTS_DIR/python/$OS_NAME-x86/bin/python3
62  $PYTHON --version >/dev/null || panic "Unable to get python version from $PYTHON"
63  printf "$PYTHON"
64}
65
66aosp_find_python_include() {
67    local AOSP_PREBUILTS_DIR=$AOSP_DIR/prebuilts
68    local OS_NAME=$(get_build_os)
69    local PYTHON_H=$(find $AOSP_PREBUILTS_DIR/python/$OS_NAME-x86/include -name 'Python.h')
70    local PYTHON_INCLUDE=$(dirname $PYTHON_H)
71    printf "$PYTHON_INCLUDE"
72}
73
74AOSP_DIR=$(
75  cd ../../../..
76  pwd
77)
78
79HERE=$AOSP_DIR/tools/netsim/testing/netsim-grpc
80PYTHON=$(aosp_find_python)
81PY_VER=$($PYTHON --version)
82
83
84devpi_dir() {
85    DEVPI_DIR=$(
86        cd $AOSP_DIR/external/adt-infra/devpi
87        pwd
88    )
89    printf "$DEVPI_DIR"
90}
91
92setup_virtual_env() {
93    # We need a virtual environment, so we can set up the proper include directories
94    # as, well, it seem that our python release does not report the proper include
95    # directory
96    local PYTHON=$(aosp_find_python)
97    local PYTHON_INCLUDE=$(aosp_find_python_include)
98    local WHEEL_DIR=$(devpi_dir)/repo/simple
99
100    $PYTHON -m venv $VIRTUAL_ENV_DEST
101    rm -r $VIRTUAL_ENV_DEST/include
102    ln -sf $PYTHON_INCLUDE $VIRTUAL_ENV_DEST/include
103
104    # Activate and setup a pip conf that points to our local devpi server
105    # This will make sure all our packages are from the local file system.
106    . $VIRTUAL_ENV_DEST/bin/activate
107    cat $HERE/cfg/pip.conf | sed "s,REPO_DIR,$WHEEL_DIR,g" >$VIRTUAL_ENV_DEST/pip.conf
108    cp $HERE/cfg/pypirc $VIRTUAL_ENV_DEST/pypirc
109    pip install --upgrade pip wheel setuptools
110    pip install wheel
111}
112
113VIRTUAL_ENV_DEST=./.venv
114if [ -e $VIRTUAL_ENV_DEST/bin/activate ]; then
115  . $VIRTUAL_ENV_DEST/bin/activate
116  pip install -e .
117else
118  setup_virtual_env
119  pip install -e .
120fi