blob: fb884ad1668c0e942ae9d3e06d7fff0b1059f166 [file] [log] [blame]
Nathaniel Manista840615e2015-01-22 20:31:47 +00001#!/bin/bash
Craig Tiller6169d5f2016-03-31 07:46:18 -07002# Copyright 2015, Google Inc.
Craig Tiller83428812015-02-16 12:13:57 -08003# 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 Manista840615e2015-01-22 20:31:47 +000030
31set -ex
32
33# change to grpc repo root
34cd $(dirname $0)/../..
35
Masood Malekghassemifbf15e42016-06-07 19:13:11 -070036##########################
37# Portability operations #
38##########################
39
40PLATFORM=`uname -s`
41
Masood Malekghassemicad09082016-10-26 15:08:13 -070042function is_msys() {
43 if [ "${PLATFORM/MSYS}" != "$PLATFORM" ]; then
44 echo true
45 else
46 exit 1
47 fi
48}
49
Masood Malekghassemifbf15e42016-06-07 19:13:11 -070050function is_mingw() {
51 if [ "${PLATFORM/MINGW}" != "$PLATFORM" ]; then
52 echo true
53 else
54 exit 1
55 fi
56}
57
58function is_darwin() {
59 if [ "${PLATFORM/Darwin}" != "$PLATFORM" ]; then
60 echo true
61 else
62 exit 1
63 fi
64}
65
66function 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.
75function 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.
81function 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.
90function 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.
99function 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 Malekghassemi3b5b2062016-06-02 20:27:20 -0700114PYTHON=${1:-python2.7}
Masood Malekghassemifbf15e42016-06-07 19:13:11 -0700115VENV=${2:-$(venv $PYTHON)}
116VENV_RELATIVE_PYTHON=${3:-$(venv_relative_python)}
117TOOLCHAIN=${4:-$(toolchain)}
Jan Tattermusch825471c2016-04-25 16:52:25 -0700118
Masood Malekghassemicad09082016-10-26 15:08:13 -0700119if [ $(is_msys) ]; then
120 echo "MSYS doesn't directly provide the right compiler(s);"
121 echo "switch to a MinGW shell."
122 exit 1
123fi
124
Masood Malekghassemi2b841622015-07-28 17:39:02 -0700125ROOT=`pwd`
Masood Malekghassemi586e3832016-06-03 19:29:12 -0700126export CFLAGS="-I$ROOT/include -std=gnu99 -fno-wrapv $CFLAGS"
Masood Malekghassemid00241e2015-12-03 16:42:36 -0800127export GRPC_PYTHON_BUILD_WITH_CYTHON=1
Muxi Yan6f748932016-08-02 10:10:28 -0700128export LANG=en_US.UTF-8
Masood Malekghassemi68a94c82016-03-09 11:45:07 -0800129
Masood Malekghassemi0bd13ed2016-06-13 19:22:43 -0700130# Default python on the host to fall back to when instantiating e.g. the
131# virtualenv.
132HOST_PYTHON=${HOST_PYTHON:-python}
133
Masood Malekghassemifbf15e42016-06-07 19:13:11 -0700134# If ccache is available on Linux, use it.
135if [ $(is_linux) ]; then
Masood Malekghassemi3b5b2062016-06-02 20:27:20 -0700136 # 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
144fi
145
Masood Malekghassemifbf15e42016-06-07 19:13:11 -0700146############################
147# Perform build operations #
148############################
Masood Malekghassemi3b5b2062016-06-02 20:27:20 -0700149
Masood Malekghassemi0bd13ed2016-06-13 19:22:43 -0700150# 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 Malekghassemifbf15e42016-06-07 19:13:11 -0700158VENV_PYTHON=`script_realpath -s "$VENV/$VENV_RELATIVE_PYTHON"`
Masood Malekghassemieaa7d202015-12-07 14:38:53 -0800159
Masood Malekghassemi3b5b2062016-06-02 20:27:20 -0700160# pip-installs the directory specified. Used because on MSYS the vanilla Windows
161# Python gets confused when parsing paths.
162pip_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 Payson6f156b72016-08-01 17:49:52 -0700173$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 Malekghassemi3b5b2062016-06-02 20:27:20 -0700176$VENV_PYTHON -m pip install cython
177pip_install_dir $ROOT
178$VENV_PYTHON $ROOT/tools/distrib/python/make_grpcio_tools.py
179pip_install_dir $ROOT/tools/distrib/python/grpcio_tools
180# TODO(atash) figure out namespace packages and grpcio-tools and auditwheel
181# etc...
182pip_install_dir $ROOT
Masood Malekghassemiaff69362016-09-21 15:10:36 -0700183
184# Build/install health checking
Masood Malekghassemi3b5b2062016-06-02 20:27:20 -0700185$VENV_PYTHON $ROOT/src/python/grpcio_health_checking/setup.py preprocess
Ken Paysondd24c1e2016-07-13 14:18:33 -0700186$VENV_PYTHON $ROOT/src/python/grpcio_health_checking/setup.py build_package_protos
Masood Malekghassemi3b5b2062016-06-02 20:27:20 -0700187pip_install_dir $ROOT/src/python/grpcio_health_checking
Masood Malekghassemiaff69362016-09-21 15:10:36 -0700188
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
192pip_install_dir $ROOT/src/python/grpcio_reflection
193
194# Build/install tests
Masood Malekghassemi3b5b2062016-06-02 20:27:20 -0700195$VENV_PYTHON $ROOT/src/python/grpcio_tests/setup.py preprocess
Ken Paysondd24c1e2016-07-13 14:18:33 -0700196$VENV_PYTHON $ROOT/src/python/grpcio_tests/setup.py build_package_protos
Masood Malekghassemi3b5b2062016-06-02 20:27:20 -0700197pip_install_dir $ROOT/src/python/grpcio_tests