Nathaniel Manista | 840615e | 2015-01-22 20:31:47 +0000 | [diff] [blame] | 1 | #!/bin/bash |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 2 | # Copyright 2015 gRPC authors. |
Craig Tiller | 8342881 | 2015-02-16 12:13:57 -0800 | [diff] [blame] | 3 | # |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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 |
Craig Tiller | 8342881 | 2015-02-16 12:13:57 -0800 | [diff] [blame] | 7 | # |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
Craig Tiller | 8342881 | 2015-02-16 12:13:57 -0800 | [diff] [blame] | 9 | # |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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. |
Nathaniel Manista | 840615e | 2015-01-22 20:31:47 +0000 | [diff] [blame] | 15 | |
| 16 | set -ex |
| 17 | |
| 18 | # change to grpc repo root |
Mehrdad Afshari | 3bfa56f | 2017-06-27 11:36:45 -0700 | [diff] [blame] | 19 | cd "$(dirname "$0")/../../.." |
Nathaniel Manista | 840615e | 2015-01-22 20:31:47 +0000 | [diff] [blame] | 20 | |
Masood Malekghassemi | fbf15e4 | 2016-06-07 19:13:11 -0700 | [diff] [blame] | 21 | ########################## |
| 22 | # Portability operations # |
| 23 | ########################## |
| 24 | |
Mehrdad Afshari | 3bfa56f | 2017-06-27 11:36:45 -0700 | [diff] [blame] | 25 | PLATFORM=$(uname -s) |
Masood Malekghassemi | fbf15e4 | 2016-06-07 19:13:11 -0700 | [diff] [blame] | 26 | |
Masood Malekghassemi | cad0908 | 2016-10-26 15:08:13 -0700 | [diff] [blame] | 27 | function is_msys() { |
| 28 | if [ "${PLATFORM/MSYS}" != "$PLATFORM" ]; then |
| 29 | echo true |
| 30 | else |
| 31 | exit 1 |
| 32 | fi |
| 33 | } |
| 34 | |
Masood Malekghassemi | fbf15e4 | 2016-06-07 19:13:11 -0700 | [diff] [blame] | 35 | function is_mingw() { |
| 36 | if [ "${PLATFORM/MINGW}" != "$PLATFORM" ]; then |
| 37 | echo true |
| 38 | else |
| 39 | exit 1 |
| 40 | fi |
| 41 | } |
| 42 | |
| 43 | function is_darwin() { |
| 44 | if [ "${PLATFORM/Darwin}" != "$PLATFORM" ]; then |
| 45 | echo true |
| 46 | else |
| 47 | exit 1 |
| 48 | fi |
| 49 | } |
| 50 | |
| 51 | function is_linux() { |
| 52 | if [ "${PLATFORM/Linux}" != "$PLATFORM" ]; then |
| 53 | echo true |
| 54 | else |
| 55 | exit 1 |
| 56 | fi |
| 57 | } |
| 58 | |
| 59 | # Associated virtual environment name for the given python command. |
| 60 | function venv() { |
| 61 | $1 -c "import sys; print('py{}{}'.format(*sys.version_info[:2]))" |
| 62 | } |
| 63 | |
| 64 | # Path to python executable within a virtual environment depending on the |
| 65 | # system. |
| 66 | function venv_relative_python() { |
Mehrdad Afshari | 3bfa56f | 2017-06-27 11:36:45 -0700 | [diff] [blame] | 67 | if [ "$(is_mingw)" ]; then |
Masood Malekghassemi | fbf15e4 | 2016-06-07 19:13:11 -0700 | [diff] [blame] | 68 | echo 'Scripts/python.exe' |
| 69 | else |
| 70 | echo 'bin/python' |
| 71 | fi |
| 72 | } |
| 73 | |
| 74 | # Distutils toolchain to use depending on the system. |
| 75 | function toolchain() { |
Mehrdad Afshari | 3bfa56f | 2017-06-27 11:36:45 -0700 | [diff] [blame] | 76 | if [ "$(is_mingw)" ]; then |
Masood Malekghassemi | fbf15e4 | 2016-06-07 19:13:11 -0700 | [diff] [blame] | 77 | echo 'mingw32' |
| 78 | else |
| 79 | echo 'unix' |
| 80 | fi |
| 81 | } |
| 82 | |
| 83 | # Command to invoke the linux command `realpath` or equivalent. |
| 84 | function script_realpath() { |
| 85 | # Find `realpath` |
| 86 | if [ -x "$(command -v realpath)" ]; then |
| 87 | realpath "$@" |
| 88 | elif [ -x "$(command -v grealpath)" ]; then |
| 89 | grealpath "$@" |
| 90 | else |
| 91 | exit 1 |
| 92 | fi |
| 93 | } |
| 94 | |
| 95 | #################### |
| 96 | # Script Arguments # |
| 97 | #################### |
| 98 | |
Masood Malekghassemi | 3b5b206 | 2016-06-02 20:27:20 -0700 | [diff] [blame] | 99 | PYTHON=${1:-python2.7} |
Mehrdad Afshari | 3bfa56f | 2017-06-27 11:36:45 -0700 | [diff] [blame] | 100 | VENV=${2:-$(venv "$PYTHON")} |
Masood Malekghassemi | fbf15e4 | 2016-06-07 19:13:11 -0700 | [diff] [blame] | 101 | VENV_RELATIVE_PYTHON=${3:-$(venv_relative_python)} |
| 102 | TOOLCHAIN=${4:-$(toolchain)} |
Jan Tattermusch | 825471c | 2016-04-25 16:52:25 -0700 | [diff] [blame] | 103 | |
Mehrdad Afshari | 3bfa56f | 2017-06-27 11:36:45 -0700 | [diff] [blame] | 104 | if [ "$(is_msys)" ]; then |
Masood Malekghassemi | cad0908 | 2016-10-26 15:08:13 -0700 | [diff] [blame] | 105 | echo "MSYS doesn't directly provide the right compiler(s);" |
| 106 | echo "switch to a MinGW shell." |
| 107 | exit 1 |
| 108 | fi |
| 109 | |
Mehrdad Afshari | 3bfa56f | 2017-06-27 11:36:45 -0700 | [diff] [blame] | 110 | ROOT=$(pwd) |
Masood Malekghassemi | 586e383 | 2016-06-03 19:29:12 -0700 | [diff] [blame] | 111 | export CFLAGS="-I$ROOT/include -std=gnu99 -fno-wrapv $CFLAGS" |
Masood Malekghassemi | d00241e | 2015-12-03 16:42:36 -0800 | [diff] [blame] | 112 | export GRPC_PYTHON_BUILD_WITH_CYTHON=1 |
Muxi Yan | 6f74893 | 2016-08-02 10:10:28 -0700 | [diff] [blame] | 113 | export LANG=en_US.UTF-8 |
Masood Malekghassemi | 68a94c8 | 2016-03-09 11:45:07 -0800 | [diff] [blame] | 114 | |
Masood Malekghassemi | 0bd13ed | 2016-06-13 19:22:43 -0700 | [diff] [blame] | 115 | # Default python on the host to fall back to when instantiating e.g. the |
| 116 | # virtualenv. |
| 117 | HOST_PYTHON=${HOST_PYTHON:-python} |
| 118 | |
Masood Malekghassemi | fbf15e4 | 2016-06-07 19:13:11 -0700 | [diff] [blame] | 119 | # If ccache is available on Linux, use it. |
Mehrdad Afshari | 3bfa56f | 2017-06-27 11:36:45 -0700 | [diff] [blame] | 120 | if [ "$(is_linux)" ]; then |
Masood Malekghassemi | 3b5b206 | 2016-06-02 20:27:20 -0700 | [diff] [blame] | 121 | # We're not on Darwin (Mac OS X) |
| 122 | if [ -x "$(command -v ccache)" ]; then |
| 123 | if [ -x "$(command -v gcc)" ]; then |
| 124 | export CC='ccache gcc' |
| 125 | elif [ -x "$(command -v clang)" ]; then |
| 126 | export CC='ccache clang' |
| 127 | fi |
| 128 | fi |
| 129 | fi |
| 130 | |
Masood Malekghassemi | fbf15e4 | 2016-06-07 19:13:11 -0700 | [diff] [blame] | 131 | ############################ |
| 132 | # Perform build operations # |
| 133 | ############################ |
Masood Malekghassemi | 3b5b206 | 2016-06-02 20:27:20 -0700 | [diff] [blame] | 134 | |
Masood Malekghassemi | 0bd13ed | 2016-06-13 19:22:43 -0700 | [diff] [blame] | 135 | # Instnatiate the virtualenv, preferring to do so from the relevant python |
| 136 | # version. Even if these commands fail (e.g. on Windows due to name conflicts) |
| 137 | # it's possible that the virtualenv is still usable and we trust the tester to |
| 138 | # be able to 'figure it out' instead of us e.g. doing potentially expensive and |
| 139 | # unnecessary error recovery by `rm -rf`ing the virtualenv. |
Mehrdad Afshari | 3bfa56f | 2017-06-27 11:36:45 -0700 | [diff] [blame] | 140 | ($PYTHON -m virtualenv "$VENV" || |
| 141 | $HOST_PYTHON -m virtualenv -p "$PYTHON" "$VENV" || |
Masood Malekghassemi | 0bd13ed | 2016-06-13 19:22:43 -0700 | [diff] [blame] | 142 | true) |
Mehrdad Afshari | 3bfa56f | 2017-06-27 11:36:45 -0700 | [diff] [blame] | 143 | VENV_PYTHON=$(script_realpath "$VENV/$VENV_RELATIVE_PYTHON") |
Masood Malekghassemi | eaa7d20 | 2015-12-07 14:38:53 -0800 | [diff] [blame] | 144 | |
Masood Malekghassemi | 3b5b206 | 2016-06-02 20:27:20 -0700 | [diff] [blame] | 145 | # pip-installs the directory specified. Used because on MSYS the vanilla Windows |
| 146 | # Python gets confused when parsing paths. |
| 147 | pip_install_dir() { |
Mehrdad Afshari | 3bfa56f | 2017-06-27 11:36:45 -0700 | [diff] [blame] | 148 | PWD=$(pwd) |
| 149 | cd "$1" |
| 150 | ($VENV_PYTHON setup.py build_ext -c "$TOOLCHAIN" || true) |
Masood Malekghassemi | e9fee3c | 2016-12-14 17:36:50 -0800 | [diff] [blame] | 151 | $VENV_PYTHON -m pip install --no-deps . |
Mehrdad Afshari | 3bfa56f | 2017-06-27 11:36:45 -0700 | [diff] [blame] | 152 | cd "$PWD" |
Masood Malekghassemi | 3b5b206 | 2016-06-02 20:27:20 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Nathaniel Manista | 8bba3bf | 2017-06-26 16:33:25 +0000 | [diff] [blame] | 155 | $VENV_PYTHON -m pip install --upgrade pip==9.0.1 |
Masood Malekghassemi | dfd5199 | 2016-12-05 16:22:30 -0800 | [diff] [blame] | 156 | $VENV_PYTHON -m pip install setuptools |
Masood Malekghassemi | 3b5b206 | 2016-06-02 20:27:20 -0700 | [diff] [blame] | 157 | $VENV_PYTHON -m pip install cython |
Masood Malekghassemi | e9fee3c | 2016-12-14 17:36:50 -0800 | [diff] [blame] | 158 | $VENV_PYTHON -m pip install six enum34 protobuf futures |
Mehrdad Afshari | 3bfa56f | 2017-06-27 11:36:45 -0700 | [diff] [blame] | 159 | pip_install_dir "$ROOT" |
Masood Malekghassemi | e9fee3c | 2016-12-14 17:36:50 -0800 | [diff] [blame] | 160 | |
Mehrdad Afshari | 3bfa56f | 2017-06-27 11:36:45 -0700 | [diff] [blame] | 161 | $VENV_PYTHON "$ROOT/tools/distrib/python/make_grpcio_tools.py" |
| 162 | pip_install_dir "$ROOT/tools/distrib/python/grpcio_tools" |
Masood Malekghassemi | aff6936 | 2016-09-21 15:10:36 -0700 | [diff] [blame] | 163 | |
| 164 | # Build/install health checking |
Mehrdad Afshari | 3bfa56f | 2017-06-27 11:36:45 -0700 | [diff] [blame] | 165 | $VENV_PYTHON "$ROOT/src/python/grpcio_health_checking/setup.py" preprocess |
| 166 | $VENV_PYTHON "$ROOT/src/python/grpcio_health_checking/setup.py" build_package_protos |
| 167 | pip_install_dir "$ROOT/src/python/grpcio_health_checking" |
Masood Malekghassemi | aff6936 | 2016-09-21 15:10:36 -0700 | [diff] [blame] | 168 | |
| 169 | # Build/install reflection |
Mehrdad Afshari | 3bfa56f | 2017-06-27 11:36:45 -0700 | [diff] [blame] | 170 | $VENV_PYTHON "$ROOT/src/python/grpcio_reflection/setup.py" preprocess |
| 171 | $VENV_PYTHON "$ROOT/src/python/grpcio_reflection/setup.py" build_package_protos |
| 172 | pip_install_dir "$ROOT/src/python/grpcio_reflection" |
Masood Malekghassemi | aff6936 | 2016-09-21 15:10:36 -0700 | [diff] [blame] | 173 | |
Nathaniel Manista | 69b7231 | 2017-07-21 03:18:11 +0000 | [diff] [blame] | 174 | # Install testing |
Mehrdad Afshari | 3bfa56f | 2017-06-27 11:36:45 -0700 | [diff] [blame] | 175 | pip_install_dir "$ROOT/src/python/grpcio_testing" |
Nathaniel Manista | 69b7231 | 2017-07-21 03:18:11 +0000 | [diff] [blame] | 176 | |
Masood Malekghassemi | aff6936 | 2016-09-21 15:10:36 -0700 | [diff] [blame] | 177 | # Build/install tests |
Ken Payson | 937d96d | 2017-05-10 17:01:38 -0700 | [diff] [blame] | 178 | $VENV_PYTHON -m pip install coverage==4.4 oauth2client==4.1.0 \ |
| 179 | google-auth==1.0.0 requests==2.14.2 |
Mehrdad Afshari | 3bfa56f | 2017-06-27 11:36:45 -0700 | [diff] [blame] | 180 | $VENV_PYTHON "$ROOT/src/python/grpcio_tests/setup.py" preprocess |
| 181 | $VENV_PYTHON "$ROOT/src/python/grpcio_tests/setup.py" build_package_protos |
| 182 | pip_install_dir "$ROOT/src/python/grpcio_tests" |