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 | |
| 42 | function is_mingw() { |
| 43 | if [ "${PLATFORM/MINGW}" != "$PLATFORM" ]; then |
| 44 | echo true |
| 45 | else |
| 46 | exit 1 |
| 47 | fi |
| 48 | } |
| 49 | |
| 50 | function is_darwin() { |
| 51 | if [ "${PLATFORM/Darwin}" != "$PLATFORM" ]; then |
| 52 | echo true |
| 53 | else |
| 54 | exit 1 |
| 55 | fi |
| 56 | } |
| 57 | |
| 58 | function is_linux() { |
| 59 | if [ "${PLATFORM/Linux}" != "$PLATFORM" ]; then |
| 60 | echo true |
| 61 | else |
| 62 | exit 1 |
| 63 | fi |
| 64 | } |
| 65 | |
| 66 | # Associated virtual environment name for the given python command. |
| 67 | function venv() { |
| 68 | $1 -c "import sys; print('py{}{}'.format(*sys.version_info[:2]))" |
| 69 | } |
| 70 | |
| 71 | # Path to python executable within a virtual environment depending on the |
| 72 | # system. |
| 73 | function venv_relative_python() { |
| 74 | if [ $(is_mingw) ]; then |
| 75 | echo 'Scripts/python.exe' |
| 76 | else |
| 77 | echo 'bin/python' |
| 78 | fi |
| 79 | } |
| 80 | |
| 81 | # Distutils toolchain to use depending on the system. |
| 82 | function toolchain() { |
| 83 | if [ $(is_mingw) ]; then |
| 84 | echo 'mingw32' |
| 85 | else |
| 86 | echo 'unix' |
| 87 | fi |
| 88 | } |
| 89 | |
| 90 | # Command to invoke the linux command `realpath` or equivalent. |
| 91 | function script_realpath() { |
| 92 | # Find `realpath` |
| 93 | if [ -x "$(command -v realpath)" ]; then |
| 94 | realpath "$@" |
| 95 | elif [ -x "$(command -v grealpath)" ]; then |
| 96 | grealpath "$@" |
| 97 | else |
| 98 | exit 1 |
| 99 | fi |
| 100 | } |
| 101 | |
| 102 | #################### |
| 103 | # Script Arguments # |
| 104 | #################### |
| 105 | |
Masood Malekghassemi | 3b5b206 | 2016-06-02 20:27:20 -0700 | [diff] [blame] | 106 | PYTHON=${1:-python2.7} |
Masood Malekghassemi | fbf15e4 | 2016-06-07 19:13:11 -0700 | [diff] [blame] | 107 | VENV=${2:-$(venv $PYTHON)} |
| 108 | VENV_RELATIVE_PYTHON=${3:-$(venv_relative_python)} |
| 109 | TOOLCHAIN=${4:-$(toolchain)} |
Jan Tattermusch | 825471c | 2016-04-25 16:52:25 -0700 | [diff] [blame] | 110 | |
Masood Malekghassemi | 2b84162 | 2015-07-28 17:39:02 -0700 | [diff] [blame] | 111 | ROOT=`pwd` |
Masood Malekghassemi | 586e383 | 2016-06-03 19:29:12 -0700 | [diff] [blame] | 112 | export CFLAGS="-I$ROOT/include -std=gnu99 -fno-wrapv $CFLAGS" |
Masood Malekghassemi | d00241e | 2015-12-03 16:42:36 -0800 | [diff] [blame] | 113 | export GRPC_PYTHON_BUILD_WITH_CYTHON=1 |
Muxi Yan | 6f74893 | 2016-08-02 10:10:28 -0700 | [diff] [blame] | 114 | export LANG=en_US.UTF-8 |
Masood Malekghassemi | 68a94c8 | 2016-03-09 11:45:07 -0800 | [diff] [blame] | 115 | |
Masood Malekghassemi | 0bd13ed | 2016-06-13 19:22:43 -0700 | [diff] [blame] | 116 | # Default python on the host to fall back to when instantiating e.g. the |
| 117 | # virtualenv. |
| 118 | HOST_PYTHON=${HOST_PYTHON:-python} |
| 119 | |
Masood Malekghassemi | fbf15e4 | 2016-06-07 19:13:11 -0700 | [diff] [blame] | 120 | # If ccache is available on Linux, use it. |
| 121 | if [ $(is_linux) ]; then |
Masood Malekghassemi | 3b5b206 | 2016-06-02 20:27:20 -0700 | [diff] [blame] | 122 | # We're not on Darwin (Mac OS X) |
| 123 | if [ -x "$(command -v ccache)" ]; then |
| 124 | if [ -x "$(command -v gcc)" ]; then |
| 125 | export CC='ccache gcc' |
| 126 | elif [ -x "$(command -v clang)" ]; then |
| 127 | export CC='ccache clang' |
| 128 | fi |
| 129 | fi |
| 130 | fi |
| 131 | |
Masood Malekghassemi | fbf15e4 | 2016-06-07 19:13:11 -0700 | [diff] [blame] | 132 | ############################ |
| 133 | # Perform build operations # |
| 134 | ############################ |
Masood Malekghassemi | 3b5b206 | 2016-06-02 20:27:20 -0700 | [diff] [blame] | 135 | |
Masood Malekghassemi | 0bd13ed | 2016-06-13 19:22:43 -0700 | [diff] [blame] | 136 | # Instnatiate the virtualenv, preferring to do so from the relevant python |
| 137 | # version. Even if these commands fail (e.g. on Windows due to name conflicts) |
| 138 | # it's possible that the virtualenv is still usable and we trust the tester to |
| 139 | # be able to 'figure it out' instead of us e.g. doing potentially expensive and |
| 140 | # unnecessary error recovery by `rm -rf`ing the virtualenv. |
| 141 | ($PYTHON -m virtualenv $VENV || |
| 142 | $HOST_PYTHON -m virtualenv -p $PYTHON $VENV || |
| 143 | true) |
Masood Malekghassemi | fbf15e4 | 2016-06-07 19:13:11 -0700 | [diff] [blame] | 144 | VENV_PYTHON=`script_realpath -s "$VENV/$VENV_RELATIVE_PYTHON"` |
Masood Malekghassemi | eaa7d20 | 2015-12-07 14:38:53 -0800 | [diff] [blame] | 145 | |
Masood Malekghassemi | 3b5b206 | 2016-06-02 20:27:20 -0700 | [diff] [blame] | 146 | # pip-installs the directory specified. Used because on MSYS the vanilla Windows |
| 147 | # Python gets confused when parsing paths. |
| 148 | pip_install_dir() { |
| 149 | PWD=`pwd` |
| 150 | cd $1 |
| 151 | ($VENV_PYTHON setup.py build_ext -c $TOOLCHAIN || true) |
| 152 | # install the dependencies |
| 153 | $VENV_PYTHON -m pip install --upgrade . |
| 154 | # ensure that we've reinstalled the test packages |
| 155 | $VENV_PYTHON -m pip install --upgrade --force-reinstall --no-deps . |
| 156 | cd $PWD |
| 157 | } |
| 158 | |
Ken Payson | 6f156b7 | 2016-08-01 17:49:52 -0700 | [diff] [blame] | 159 | $VENV_PYTHON -m pip install --upgrade pip |
| 160 | # TODO(https://github.com/pypa/setuptools/issues/709) get the latest setuptools |
| 161 | $VENV_PYTHON -m pip install setuptools==25.1.1 |
Masood Malekghassemi | 3b5b206 | 2016-06-02 20:27:20 -0700 | [diff] [blame] | 162 | $VENV_PYTHON -m pip install cython |
| 163 | pip_install_dir $ROOT |
| 164 | $VENV_PYTHON $ROOT/tools/distrib/python/make_grpcio_tools.py |
| 165 | pip_install_dir $ROOT/tools/distrib/python/grpcio_tools |
| 166 | # TODO(atash) figure out namespace packages and grpcio-tools and auditwheel |
| 167 | # etc... |
| 168 | pip_install_dir $ROOT |
| 169 | $VENV_PYTHON $ROOT/src/python/grpcio_health_checking/setup.py preprocess |
Ken Payson | dd24c1e | 2016-07-13 14:18:33 -0700 | [diff] [blame] | 170 | $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] | 171 | pip_install_dir $ROOT/src/python/grpcio_health_checking |
| 172 | $VENV_PYTHON $ROOT/src/python/grpcio_tests/setup.py preprocess |
Ken Payson | dd24c1e | 2016-07-13 14:18:33 -0700 | [diff] [blame] | 173 | $VENV_PYTHON $ROOT/src/python/grpcio_tests/setup.py build_package_protos |
Masood Malekghassemi | 3b5b206 | 2016-06-02 20:27:20 -0700 | [diff] [blame] | 174 | pip_install_dir $ROOT/src/python/grpcio_tests |