Nathaniel Manista | 840615e | 2015-01-22 20:31:47 +0000 | [diff] [blame] | 1 | #!/bin/bash |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 2 | # Copyright 2015, Google Inc. |
Craig Tiller | 8342881 | 2015-02-16 12:13:57 -0800 | [diff] [blame] | 3 | # All rights reserved. |
| 4 | # |
| 5 | # Redistribution and use in source and binary forms, with or without |
| 6 | # modification, are permitted provided that the following conditions are |
| 7 | # met: |
| 8 | # |
| 9 | # * Redistributions of source code must retain the above copyright |
| 10 | # notice, this list of conditions and the following disclaimer. |
| 11 | # * Redistributions in binary form must reproduce the above |
| 12 | # copyright notice, this list of conditions and the following disclaimer |
| 13 | # in the documentation and/or other materials provided with the |
| 14 | # distribution. |
| 15 | # * Neither the name of Google Inc. nor the names of its |
| 16 | # contributors may be used to endorse or promote products derived from |
| 17 | # this software without specific prior written permission. |
| 18 | # |
| 19 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Nathaniel Manista | 840615e | 2015-01-22 20:31:47 +0000 | [diff] [blame] | 30 | |
| 31 | set -ex |
| 32 | |
| 33 | # change to grpc repo root |
| 34 | cd $(dirname $0)/../.. |
| 35 | |
Masood Malekghassemi | fbf15e4 | 2016-06-07 19:13:11 -0700 | [diff] [blame] | 36 | ########################## |
| 37 | # Portability operations # |
| 38 | ########################## |
| 39 | |
| 40 | PLATFORM=`uname -s` |
| 41 | |
Masood Malekghassemi | cad0908 | 2016-10-26 15:08:13 -0700 | [diff] [blame] | 42 | function is_msys() { |
| 43 | if [ "${PLATFORM/MSYS}" != "$PLATFORM" ]; then |
| 44 | echo true |
| 45 | else |
| 46 | exit 1 |
| 47 | fi |
| 48 | } |
| 49 | |
Masood Malekghassemi | fbf15e4 | 2016-06-07 19:13:11 -0700 | [diff] [blame] | 50 | function is_mingw() { |
| 51 | if [ "${PLATFORM/MINGW}" != "$PLATFORM" ]; then |
| 52 | echo true |
| 53 | else |
| 54 | exit 1 |
| 55 | fi |
| 56 | } |
| 57 | |
| 58 | function is_darwin() { |
| 59 | if [ "${PLATFORM/Darwin}" != "$PLATFORM" ]; then |
| 60 | echo true |
| 61 | else |
| 62 | exit 1 |
| 63 | fi |
| 64 | } |
| 65 | |
| 66 | function is_linux() { |
| 67 | if [ "${PLATFORM/Linux}" != "$PLATFORM" ]; then |
| 68 | echo true |
| 69 | else |
| 70 | exit 1 |
| 71 | fi |
| 72 | } |
| 73 | |
| 74 | # Associated virtual environment name for the given python command. |
| 75 | function venv() { |
| 76 | $1 -c "import sys; print('py{}{}'.format(*sys.version_info[:2]))" |
| 77 | } |
| 78 | |
| 79 | # Path to python executable within a virtual environment depending on the |
| 80 | # system. |
| 81 | function venv_relative_python() { |
| 82 | if [ $(is_mingw) ]; then |
| 83 | echo 'Scripts/python.exe' |
| 84 | else |
| 85 | echo 'bin/python' |
| 86 | fi |
| 87 | } |
| 88 | |
| 89 | # Distutils toolchain to use depending on the system. |
| 90 | function toolchain() { |
| 91 | if [ $(is_mingw) ]; then |
| 92 | echo 'mingw32' |
| 93 | else |
| 94 | echo 'unix' |
| 95 | fi |
| 96 | } |
| 97 | |
| 98 | # Command to invoke the linux command `realpath` or equivalent. |
| 99 | function script_realpath() { |
| 100 | # Find `realpath` |
| 101 | if [ -x "$(command -v realpath)" ]; then |
| 102 | realpath "$@" |
| 103 | elif [ -x "$(command -v grealpath)" ]; then |
| 104 | grealpath "$@" |
| 105 | else |
| 106 | exit 1 |
| 107 | fi |
| 108 | } |
| 109 | |
| 110 | #################### |
| 111 | # Script Arguments # |
| 112 | #################### |
| 113 | |
Masood Malekghassemi | 3b5b206 | 2016-06-02 20:27:20 -0700 | [diff] [blame] | 114 | PYTHON=${1:-python2.7} |
Masood Malekghassemi | fbf15e4 | 2016-06-07 19:13:11 -0700 | [diff] [blame] | 115 | VENV=${2:-$(venv $PYTHON)} |
| 116 | VENV_RELATIVE_PYTHON=${3:-$(venv_relative_python)} |
| 117 | TOOLCHAIN=${4:-$(toolchain)} |
Jan Tattermusch | 825471c | 2016-04-25 16:52:25 -0700 | [diff] [blame] | 118 | |
Masood Malekghassemi | cad0908 | 2016-10-26 15:08:13 -0700 | [diff] [blame] | 119 | if [ $(is_msys) ]; then |
| 120 | echo "MSYS doesn't directly provide the right compiler(s);" |
| 121 | echo "switch to a MinGW shell." |
| 122 | exit 1 |
| 123 | fi |
| 124 | |
Masood Malekghassemi | 2b84162 | 2015-07-28 17:39:02 -0700 | [diff] [blame] | 125 | ROOT=`pwd` |
Masood Malekghassemi | 586e383 | 2016-06-03 19:29:12 -0700 | [diff] [blame] | 126 | export CFLAGS="-I$ROOT/include -std=gnu99 -fno-wrapv $CFLAGS" |
Masood Malekghassemi | d00241e | 2015-12-03 16:42:36 -0800 | [diff] [blame] | 127 | export GRPC_PYTHON_BUILD_WITH_CYTHON=1 |
Muxi Yan | 6f74893 | 2016-08-02 10:10:28 -0700 | [diff] [blame] | 128 | export LANG=en_US.UTF-8 |
Masood Malekghassemi | 68a94c8 | 2016-03-09 11:45:07 -0800 | [diff] [blame] | 129 | |
Masood Malekghassemi | 0bd13ed | 2016-06-13 19:22:43 -0700 | [diff] [blame] | 130 | # Default python on the host to fall back to when instantiating e.g. the |
| 131 | # virtualenv. |
| 132 | HOST_PYTHON=${HOST_PYTHON:-python} |
| 133 | |
Masood Malekghassemi | fbf15e4 | 2016-06-07 19:13:11 -0700 | [diff] [blame] | 134 | # If ccache is available on Linux, use it. |
| 135 | if [ $(is_linux) ]; then |
Masood Malekghassemi | 3b5b206 | 2016-06-02 20:27:20 -0700 | [diff] [blame] | 136 | # We're not on Darwin (Mac OS X) |
| 137 | if [ -x "$(command -v ccache)" ]; then |
| 138 | if [ -x "$(command -v gcc)" ]; then |
| 139 | export CC='ccache gcc' |
| 140 | elif [ -x "$(command -v clang)" ]; then |
| 141 | export CC='ccache clang' |
| 142 | fi |
| 143 | fi |
| 144 | fi |
| 145 | |
Masood Malekghassemi | fbf15e4 | 2016-06-07 19:13:11 -0700 | [diff] [blame] | 146 | ############################ |
| 147 | # Perform build operations # |
| 148 | ############################ |
Masood Malekghassemi | 3b5b206 | 2016-06-02 20:27:20 -0700 | [diff] [blame] | 149 | |
Masood Malekghassemi | 0bd13ed | 2016-06-13 19:22:43 -0700 | [diff] [blame] | 150 | # Instnatiate the virtualenv, preferring to do so from the relevant python |
| 151 | # version. Even if these commands fail (e.g. on Windows due to name conflicts) |
| 152 | # it's possible that the virtualenv is still usable and we trust the tester to |
| 153 | # be able to 'figure it out' instead of us e.g. doing potentially expensive and |
| 154 | # unnecessary error recovery by `rm -rf`ing the virtualenv. |
| 155 | ($PYTHON -m virtualenv $VENV || |
| 156 | $HOST_PYTHON -m virtualenv -p $PYTHON $VENV || |
| 157 | true) |
Masood Malekghassemi | fbf15e4 | 2016-06-07 19:13:11 -0700 | [diff] [blame] | 158 | VENV_PYTHON=`script_realpath -s "$VENV/$VENV_RELATIVE_PYTHON"` |
Masood Malekghassemi | eaa7d20 | 2015-12-07 14:38:53 -0800 | [diff] [blame] | 159 | |
Masood Malekghassemi | 3b5b206 | 2016-06-02 20:27:20 -0700 | [diff] [blame] | 160 | # pip-installs the directory specified. Used because on MSYS the vanilla Windows |
| 161 | # Python gets confused when parsing paths. |
| 162 | pip_install_dir() { |
| 163 | PWD=`pwd` |
| 164 | cd $1 |
| 165 | ($VENV_PYTHON setup.py build_ext -c $TOOLCHAIN || true) |
| 166 | # install the dependencies |
| 167 | $VENV_PYTHON -m pip install --upgrade . |
| 168 | # ensure that we've reinstalled the test packages |
| 169 | $VENV_PYTHON -m pip install --upgrade --force-reinstall --no-deps . |
| 170 | cd $PWD |
| 171 | } |
| 172 | |
Ken Payson | 6f156b7 | 2016-08-01 17:49:52 -0700 | [diff] [blame] | 173 | $VENV_PYTHON -m pip install --upgrade pip |
| 174 | # TODO(https://github.com/pypa/setuptools/issues/709) get the latest setuptools |
| 175 | $VENV_PYTHON -m pip install setuptools==25.1.1 |
Masood Malekghassemi | 3b5b206 | 2016-06-02 20:27:20 -0700 | [diff] [blame] | 176 | $VENV_PYTHON -m pip install cython |
| 177 | pip_install_dir $ROOT |
| 178 | $VENV_PYTHON $ROOT/tools/distrib/python/make_grpcio_tools.py |
| 179 | pip_install_dir $ROOT/tools/distrib/python/grpcio_tools |
| 180 | # TODO(atash) figure out namespace packages and grpcio-tools and auditwheel |
| 181 | # etc... |
| 182 | pip_install_dir $ROOT |
Masood Malekghassemi | aff6936 | 2016-09-21 15:10:36 -0700 | [diff] [blame] | 183 | |
| 184 | # Build/install health checking |
Masood Malekghassemi | 3b5b206 | 2016-06-02 20:27:20 -0700 | [diff] [blame] | 185 | $VENV_PYTHON $ROOT/src/python/grpcio_health_checking/setup.py preprocess |
Ken Payson | dd24c1e | 2016-07-13 14:18:33 -0700 | [diff] [blame] | 186 | $VENV_PYTHON $ROOT/src/python/grpcio_health_checking/setup.py build_package_protos |
Masood Malekghassemi | 3b5b206 | 2016-06-02 20:27:20 -0700 | [diff] [blame] | 187 | pip_install_dir $ROOT/src/python/grpcio_health_checking |
Masood Malekghassemi | aff6936 | 2016-09-21 15:10:36 -0700 | [diff] [blame] | 188 | |
| 189 | # Build/install reflection |
| 190 | $VENV_PYTHON $ROOT/src/python/grpcio_reflection/setup.py preprocess |
| 191 | $VENV_PYTHON $ROOT/src/python/grpcio_reflection/setup.py build_package_protos |
| 192 | pip_install_dir $ROOT/src/python/grpcio_reflection |
| 193 | |
| 194 | # Build/install tests |
Masood Malekghassemi | 3b5b206 | 2016-06-02 20:27:20 -0700 | [diff] [blame] | 195 | $VENV_PYTHON $ROOT/src/python/grpcio_tests/setup.py preprocess |
Ken Payson | dd24c1e | 2016-07-13 14:18:33 -0700 | [diff] [blame] | 196 | $VENV_PYTHON $ROOT/src/python/grpcio_tests/setup.py build_package_protos |
Masood Malekghassemi | 3b5b206 | 2016-06-02 20:27:20 -0700 | [diff] [blame] | 197 | pip_install_dir $ROOT/src/python/grpcio_tests |