• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2# Copyright 2015 gRPC authors.
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.
15
16set -ex
17
18# change to grpc repo root
19cd "$(dirname "$0")/../../.."
20
21##########################
22# Portability operations #
23##########################
24
25PLATFORM=$(uname -s)
26
27function is_msys() {
28  if [ "${PLATFORM/MSYS}" != "$PLATFORM" ]; then
29    echo true
30  else
31    exit 1
32  fi
33}
34
35function is_mingw() {
36  if [ "${PLATFORM/MINGW}" != "$PLATFORM" ]; then
37    echo true
38  else
39    exit 1
40  fi
41}
42
43function is_darwin() {
44  if [ "${PLATFORM/Darwin}" != "$PLATFORM" ]; then
45    echo true
46  else
47    exit 1
48  fi
49}
50
51function 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.
60function 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.
66function venv_relative_python() {
67  if [ "$(is_mingw)" ]; then
68    echo 'Scripts/python.exe'
69  else
70    echo 'bin/python'
71  fi
72}
73
74# Distutils toolchain to use depending on the system.
75function toolchain() {
76  if [ "$(is_mingw)" ]; then
77    echo 'mingw32'
78  else
79    echo 'unix'
80  fi
81}
82
83# Command to invoke the linux command `realpath` or equivalent.
84function 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
99PYTHON=${1:-python2.7}
100VENV=${2:-$(venv "$PYTHON")}
101VENV_RELATIVE_PYTHON=${3:-$(venv_relative_python)}
102TOOLCHAIN=${4:-$(toolchain)}
103
104if [ "$(is_msys)" ]; then
105  echo "MSYS doesn't directly provide the right compiler(s);"
106  echo "switch to a MinGW shell."
107  exit 1
108fi
109
110ROOT=$(pwd)
111export CFLAGS="-I$ROOT/include -std=gnu99 -fno-wrapv $CFLAGS"
112export GRPC_PYTHON_BUILD_WITH_CYTHON=1
113export LANG=en_US.UTF-8
114
115# If ccache is available on Linux, use it.
116if [ "$(is_linux)" ]; then
117  # We're not on Darwin (Mac OS X)
118  if [ -x "$(command -v ccache)" ]; then
119    if [ -x "$(command -v gcc)" ]; then
120      export CC='ccache gcc'
121    elif [ -x "$(command -v clang)" ]; then
122      export CC='ccache clang'
123    fi
124  fi
125fi
126
127############################
128# Perform build operations #
129############################
130
131# Instantiate the virtualenv from the Python version passed in.
132$PYTHON -m pip install --user virtualenv
133$PYTHON -m virtualenv "$VENV"
134VENV_PYTHON=$(script_realpath "$VENV/$VENV_RELATIVE_PYTHON")
135
136# See https://github.com/grpc/grpc/issues/14815 for more context. We cannot rely
137# on pip to upgrade itself because if pip is too old, it may not have the required
138# TLS version to run `pip install`.
139curl https://bootstrap.pypa.io/get-pip.py | $VENV_PYTHON
140
141# pip-installs the directory specified. Used because on MSYS the vanilla Windows
142# Python gets confused when parsing paths.
143pip_install_dir() {
144  PWD=$(pwd)
145  cd "$1"
146  ($VENV_PYTHON setup.py build_ext -c "$TOOLCHAIN" || true)
147  $VENV_PYTHON -m pip install --no-deps .
148  cd "$PWD"
149}
150
151case "$VENV" in
152  *gevent*)
153  # TODO(https://github.com/grpc/grpc/issues/15411) unpin this
154  $VENV_PYTHON -m pip install gevent==1.3.b1
155  ;;
156esac
157
158$VENV_PYTHON -m pip install --upgrade pip==10.0.1
159$VENV_PYTHON -m pip install setuptools
160$VENV_PYTHON -m pip install cython
161$VENV_PYTHON -m pip install six enum34 protobuf
162
163if [ "$("$VENV_PYTHON" -c "import sys; print(sys.version_info[0])")" == "2" ]
164then
165  $VENV_PYTHON -m pip install futures
166fi
167
168pip_install_dir "$ROOT"
169
170$VENV_PYTHON "$ROOT/tools/distrib/python/make_grpcio_tools.py"
171pip_install_dir "$ROOT/tools/distrib/python/grpcio_tools"
172
173# Build/install health checking
174$VENV_PYTHON "$ROOT/src/python/grpcio_health_checking/setup.py" preprocess
175$VENV_PYTHON "$ROOT/src/python/grpcio_health_checking/setup.py" build_package_protos
176pip_install_dir "$ROOT/src/python/grpcio_health_checking"
177
178# Build/install reflection
179$VENV_PYTHON "$ROOT/src/python/grpcio_reflection/setup.py" preprocess
180$VENV_PYTHON "$ROOT/src/python/grpcio_reflection/setup.py" build_package_protos
181pip_install_dir "$ROOT/src/python/grpcio_reflection"
182
183# Install testing
184pip_install_dir "$ROOT/src/python/grpcio_testing"
185
186# Build/install tests
187$VENV_PYTHON -m pip install coverage==4.4 oauth2client==4.1.0 \
188                            google-auth==1.0.0 requests==2.14.2
189$VENV_PYTHON "$ROOT/src/python/grpcio_tests/setup.py" preprocess
190$VENV_PYTHON "$ROOT/src/python/grpcio_tests/setup.py" build_package_protos
191pip_install_dir "$ROOT/src/python/grpcio_tests"
192