blob: b786c479f3660fea4597b3254ae5446c6601b996 [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
42function is_mingw() {
43 if [ "${PLATFORM/MINGW}" != "$PLATFORM" ]; then
44 echo true
45 else
46 exit 1
47 fi
48}
49
50function is_darwin() {
51 if [ "${PLATFORM/Darwin}" != "$PLATFORM" ]; then
52 echo true
53 else
54 exit 1
55 fi
56}
57
58function 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.
67function 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.
73function 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.
82function 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.
91function 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 Malekghassemi3b5b2062016-06-02 20:27:20 -0700106PYTHON=${1:-python2.7}
Masood Malekghassemifbf15e42016-06-07 19:13:11 -0700107VENV=${2:-$(venv $PYTHON)}
108VENV_RELATIVE_PYTHON=${3:-$(venv_relative_python)}
109TOOLCHAIN=${4:-$(toolchain)}
Jan Tattermusch825471c2016-04-25 16:52:25 -0700110
Masood Malekghassemi2b841622015-07-28 17:39:02 -0700111ROOT=`pwd`
Masood Malekghassemi586e3832016-06-03 19:29:12 -0700112export CFLAGS="-I$ROOT/include -std=gnu99 -fno-wrapv $CFLAGS"
Masood Malekghassemid00241e2015-12-03 16:42:36 -0800113export GRPC_PYTHON_BUILD_WITH_CYTHON=1
Muxi Yan6f748932016-08-02 10:10:28 -0700114export LANG=en_US.UTF-8
Masood Malekghassemi68a94c82016-03-09 11:45:07 -0800115
Masood Malekghassemi0bd13ed2016-06-13 19:22:43 -0700116# Default python on the host to fall back to when instantiating e.g. the
117# virtualenv.
118HOST_PYTHON=${HOST_PYTHON:-python}
119
Masood Malekghassemifbf15e42016-06-07 19:13:11 -0700120# If ccache is available on Linux, use it.
121if [ $(is_linux) ]; then
Masood Malekghassemi3b5b2062016-06-02 20:27:20 -0700122 # 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
130fi
131
Masood Malekghassemifbf15e42016-06-07 19:13:11 -0700132############################
133# Perform build operations #
134############################
Masood Malekghassemi3b5b2062016-06-02 20:27:20 -0700135
Masood Malekghassemi0bd13ed2016-06-13 19:22:43 -0700136# 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 Malekghassemifbf15e42016-06-07 19:13:11 -0700144VENV_PYTHON=`script_realpath -s "$VENV/$VENV_RELATIVE_PYTHON"`
Masood Malekghassemieaa7d202015-12-07 14:38:53 -0800145
Masood Malekghassemi3b5b2062016-06-02 20:27:20 -0700146# pip-installs the directory specified. Used because on MSYS the vanilla Windows
147# Python gets confused when parsing paths.
148pip_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 Payson6f156b72016-08-01 17:49:52 -0700159$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 Malekghassemi3b5b2062016-06-02 20:27:20 -0700162$VENV_PYTHON -m pip install cython
163pip_install_dir $ROOT
164$VENV_PYTHON $ROOT/tools/distrib/python/make_grpcio_tools.py
165pip_install_dir $ROOT/tools/distrib/python/grpcio_tools
166# TODO(atash) figure out namespace packages and grpcio-tools and auditwheel
167# etc...
168pip_install_dir $ROOT
169$VENV_PYTHON $ROOT/src/python/grpcio_health_checking/setup.py preprocess
Ken Paysondd24c1e2016-07-13 14:18:33 -0700170$VENV_PYTHON $ROOT/src/python/grpcio_health_checking/setup.py build_package_protos
Masood Malekghassemi3b5b2062016-06-02 20:27:20 -0700171pip_install_dir $ROOT/src/python/grpcio_health_checking
172$VENV_PYTHON $ROOT/src/python/grpcio_tests/setup.py preprocess
Ken Paysondd24c1e2016-07-13 14:18:33 -0700173$VENV_PYTHON $ROOT/src/python/grpcio_tests/setup.py build_package_protos
Masood Malekghassemi3b5b2062016-06-02 20:27:20 -0700174pip_install_dir $ROOT/src/python/grpcio_tests