blob: b809fe012f5c6a8b66535b16f2ffddd9c250199c [file] [log] [blame]
Nathaniel Manista840615e2015-01-22 20:31:47 +00001#!/bin/bash
Jan Tattermusch7897ae92017-06-07 22:57:36 +02002# Copyright 2015 gRPC authors.
Craig Tiller83428812015-02-16 12:13:57 -08003#
Jan Tattermusch7897ae92017-06-07 22:57:36 +02004# 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 Tiller83428812015-02-16 12:13:57 -08007#
Jan Tattermusch7897ae92017-06-07 22:57:36 +02008# http://www.apache.org/licenses/LICENSE-2.0
Craig Tiller83428812015-02-16 12:13:57 -08009#
Jan Tattermusch7897ae92017-06-07 22:57:36 +020010# 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 Manista840615e2015-01-22 20:31:47 +000015
16set -ex
17
18# change to grpc repo root
Mehrdad Afshari3bfa56f2017-06-27 11:36:45 -070019cd "$(dirname "$0")/../../.."
Nathaniel Manista840615e2015-01-22 20:31:47 +000020
Masood Malekghassemifbf15e42016-06-07 19:13:11 -070021##########################
22# Portability operations #
23##########################
24
Mehrdad Afshari3bfa56f2017-06-27 11:36:45 -070025PLATFORM=$(uname -s)
Masood Malekghassemifbf15e42016-06-07 19:13:11 -070026
Masood Malekghassemicad09082016-10-26 15:08:13 -070027function is_msys() {
28 if [ "${PLATFORM/MSYS}" != "$PLATFORM" ]; then
29 echo true
30 else
31 exit 1
32 fi
33}
34
Masood Malekghassemifbf15e42016-06-07 19:13:11 -070035function 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() {
Mehrdad Afshari3bfa56f2017-06-27 11:36:45 -070067 if [ "$(is_mingw)" ]; then
Masood Malekghassemifbf15e42016-06-07 19:13:11 -070068 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() {
Mehrdad Afshari3bfa56f2017-06-27 11:36:45 -070076 if [ "$(is_mingw)" ]; then
Masood Malekghassemifbf15e42016-06-07 19:13:11 -070077 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
Masood Malekghassemi3b5b2062016-06-02 20:27:20 -070099PYTHON=${1:-python2.7}
Mehrdad Afshari3bfa56f2017-06-27 11:36:45 -0700100VENV=${2:-$(venv "$PYTHON")}
Masood Malekghassemifbf15e42016-06-07 19:13:11 -0700101VENV_RELATIVE_PYTHON=${3:-$(venv_relative_python)}
102TOOLCHAIN=${4:-$(toolchain)}
Jan Tattermusch825471c2016-04-25 16:52:25 -0700103
Mehrdad Afshari3bfa56f2017-06-27 11:36:45 -0700104if [ "$(is_msys)" ]; then
Masood Malekghassemicad09082016-10-26 15:08:13 -0700105 echo "MSYS doesn't directly provide the right compiler(s);"
106 echo "switch to a MinGW shell."
107 exit 1
108fi
109
Mehrdad Afshari3bfa56f2017-06-27 11:36:45 -0700110ROOT=$(pwd)
Masood Malekghassemi586e3832016-06-03 19:29:12 -0700111export CFLAGS="-I$ROOT/include -std=gnu99 -fno-wrapv $CFLAGS"
Masood Malekghassemid00241e2015-12-03 16:42:36 -0800112export GRPC_PYTHON_BUILD_WITH_CYTHON=1
Muxi Yan6f748932016-08-02 10:10:28 -0700113export LANG=en_US.UTF-8
Masood Malekghassemi68a94c82016-03-09 11:45:07 -0800114
Masood Malekghassemi0bd13ed2016-06-13 19:22:43 -0700115# Default python on the host to fall back to when instantiating e.g. the
116# virtualenv.
117HOST_PYTHON=${HOST_PYTHON:-python}
118
Masood Malekghassemifbf15e42016-06-07 19:13:11 -0700119# If ccache is available on Linux, use it.
Mehrdad Afshari3bfa56f2017-06-27 11:36:45 -0700120if [ "$(is_linux)" ]; then
Masood Malekghassemi3b5b2062016-06-02 20:27:20 -0700121 # 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
129fi
130
Masood Malekghassemifbf15e42016-06-07 19:13:11 -0700131############################
132# Perform build operations #
133############################
Masood Malekghassemi3b5b2062016-06-02 20:27:20 -0700134
Masood Malekghassemi0bd13ed2016-06-13 19:22:43 -0700135# 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 Afshari3bfa56f2017-06-27 11:36:45 -0700140($PYTHON -m virtualenv "$VENV" ||
141 $HOST_PYTHON -m virtualenv -p "$PYTHON" "$VENV" ||
Masood Malekghassemi0bd13ed2016-06-13 19:22:43 -0700142 true)
Mehrdad Afshari3bfa56f2017-06-27 11:36:45 -0700143VENV_PYTHON=$(script_realpath "$VENV/$VENV_RELATIVE_PYTHON")
Masood Malekghassemieaa7d202015-12-07 14:38:53 -0800144
Masood Malekghassemi3b5b2062016-06-02 20:27:20 -0700145# pip-installs the directory specified. Used because on MSYS the vanilla Windows
146# Python gets confused when parsing paths.
147pip_install_dir() {
Mehrdad Afshari3bfa56f2017-06-27 11:36:45 -0700148 PWD=$(pwd)
149 cd "$1"
150 ($VENV_PYTHON setup.py build_ext -c "$TOOLCHAIN" || true)
Masood Malekghassemie9fee3c2016-12-14 17:36:50 -0800151 $VENV_PYTHON -m pip install --no-deps .
Mehrdad Afshari3bfa56f2017-06-27 11:36:45 -0700152 cd "$PWD"
Masood Malekghassemi3b5b2062016-06-02 20:27:20 -0700153}
154
Nathaniel Manista8bba3bf2017-06-26 16:33:25 +0000155$VENV_PYTHON -m pip install --upgrade pip==9.0.1
Masood Malekghassemidfd51992016-12-05 16:22:30 -0800156$VENV_PYTHON -m pip install setuptools
Masood Malekghassemi3b5b2062016-06-02 20:27:20 -0700157$VENV_PYTHON -m pip install cython
Masood Malekghassemie9fee3c2016-12-14 17:36:50 -0800158$VENV_PYTHON -m pip install six enum34 protobuf futures
Mehrdad Afshari3bfa56f2017-06-27 11:36:45 -0700159pip_install_dir "$ROOT"
Masood Malekghassemie9fee3c2016-12-14 17:36:50 -0800160
Mehrdad Afshari3bfa56f2017-06-27 11:36:45 -0700161$VENV_PYTHON "$ROOT/tools/distrib/python/make_grpcio_tools.py"
162pip_install_dir "$ROOT/tools/distrib/python/grpcio_tools"
Masood Malekghassemiaff69362016-09-21 15:10:36 -0700163
164# Build/install health checking
Mehrdad Afshari3bfa56f2017-06-27 11:36:45 -0700165$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
167pip_install_dir "$ROOT/src/python/grpcio_health_checking"
Masood Malekghassemiaff69362016-09-21 15:10:36 -0700168
169# Build/install reflection
Mehrdad Afshari3bfa56f2017-06-27 11:36:45 -0700170$VENV_PYTHON "$ROOT/src/python/grpcio_reflection/setup.py" preprocess
171$VENV_PYTHON "$ROOT/src/python/grpcio_reflection/setup.py" build_package_protos
172pip_install_dir "$ROOT/src/python/grpcio_reflection"
Masood Malekghassemiaff69362016-09-21 15:10:36 -0700173
Nathaniel Manista69b72312017-07-21 03:18:11 +0000174# Install testing
Mehrdad Afshari3bfa56f2017-06-27 11:36:45 -0700175pip_install_dir "$ROOT/src/python/grpcio_testing"
Nathaniel Manista69b72312017-07-21 03:18:11 +0000176
Masood Malekghassemiaff69362016-09-21 15:10:36 -0700177# Build/install tests
Ken Payson937d96d2017-05-10 17:01:38 -0700178$VENV_PYTHON -m pip install coverage==4.4 oauth2client==4.1.0 \
179 google-auth==1.0.0 requests==2.14.2
Mehrdad Afshari3bfa56f2017-06-27 11:36:45 -0700180$VENV_PYTHON "$ROOT/src/python/grpcio_tests/setup.py" preprocess
181$VENV_PYTHON "$ROOT/src/python/grpcio_tests/setup.py" build_package_protos
182pip_install_dir "$ROOT/src/python/grpcio_tests"