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 59function inside_venv() { 60 if [[ -n "${VIRTUAL_ENV}" ]]; then 61 echo true 62 fi 63} 64 65# Associated virtual environment name for the given python command. 66function venv() { 67 $1 -c "import sys; print('py{}{}'.format(*sys.version_info[:2]))" 68} 69 70# Path to python executable within a virtual environment depending on the 71# system. 72function venv_relative_python() { 73 if [ "$(is_mingw)" ]; then 74 echo 'Scripts/python.exe' 75 else 76 echo 'bin/python' 77 fi 78} 79 80# Toolchain to use depending on the system. 81function toolchain() { 82 if [ "$(is_mingw)" ]; then 83 echo 'mingw32' 84 else 85 echo 'unix' 86 fi 87} 88 89#################### 90# Script Arguments # 91#################### 92 93PYTHON=${1:-python2.7} 94VENV=${2:-$(venv "$PYTHON")} 95VENV_RELATIVE_PYTHON=${3:-$(venv_relative_python)} 96TOOLCHAIN=${4:-$(toolchain)} 97 98if [ "$(is_msys)" ]; then 99 echo "MSYS doesn't directly provide the right compiler(s);" 100 echo "switch to a MinGW shell." 101 exit 1 102fi 103 104ROOT=$(pwd) 105export CFLAGS="-I$ROOT/include -fno-wrapv $CFLAGS" 106export GRPC_PYTHON_BUILD_WITH_CYTHON=1 107export LANG=en_US.UTF-8 108 109# Allow build_ext to build C/C++ files in parallel 110# by enabling a monkeypatch. It speeds up the build a lot. 111DEFAULT_PARALLEL_JOBS=$(nproc) || DEFAULT_PARALLEL_JOBS=4 112export GRPC_PYTHON_BUILD_EXT_COMPILER_JOBS=${GRPC_PYTHON_BUILD_EXT_COMPILER_JOBS:-$DEFAULT_PARALLEL_JOBS} 113 114# activate ccache if desired 115# shellcheck disable=SC1091 116source tools/internal_ci/helper_scripts/prepare_ccache_symlinks_rc 117 118############################ 119# Perform build operations # 120############################ 121 122if [[ "$(inside_venv)" ]]; then 123 VENV_PYTHON="$PYTHON" 124else 125 # Instantiate the virtualenv from the Python version passed in. 126 $PYTHON -m pip install --user virtualenv==20.25.0 127 # Skip wheel and setuptools and manually install later. Otherwise we might 128 # not find cython module while building grpcio. 129 $PYTHON -m virtualenv --no-wheel --no-setuptools "$VENV" 130 VENV_PYTHON="$(pwd)/$VENV/$VENV_RELATIVE_PYTHON" 131fi 132 133pip_install() { 134 $VENV_PYTHON -m pip install "$@" 135} 136 137pip_install --upgrade pip 138pip_install --upgrade wheel 139pip_install --upgrade setuptools==66.1.0 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 151pip_install_dir_and_deps() { 152 PWD=$(pwd) 153 cd "$1" 154 ($VENV_PYTHON setup.py build_ext -c "$TOOLCHAIN" || true) 155 $VENV_PYTHON -m pip install . 156 cd "$PWD" 157} 158 159pip_install -U gevent 160 161pip_install --upgrade 'cython<3.0.0rc1' 162pip_install --upgrade six 'protobuf>=4.21.3rc1,!=4.22.0.*' 163 164if [ "$("$VENV_PYTHON" -c "import sys; print(sys.version_info[0])")" == "2" ] 165then 166 pip_install --upgrade futures enum34 167fi 168 169pip_install_dir "$ROOT" 170 171$VENV_PYTHON "$ROOT/tools/distrib/python/make_grpcio_tools.py" 172pip_install_dir_and_deps "$ROOT/tools/distrib/python/grpcio_tools" 173 174# Build/install Observability 175# Observability does not support Windows and MacOS. 176if [ "$(is_mingw)" ] || [ "$(is_darwin)" ]; then 177 echo "Skip building grpcio_observability for Windows or MacOS" 178else 179 $VENV_PYTHON "$ROOT/src/python/grpcio_observability/make_grpcio_observability.py" 180 pip_install_dir_and_deps "$ROOT/src/python/grpcio_observability" 181fi 182 183# Build/install Channelz 184$VENV_PYTHON "$ROOT/src/python/grpcio_channelz/setup.py" preprocess 185$VENV_PYTHON "$ROOT/src/python/grpcio_channelz/setup.py" build_package_protos 186pip_install_dir "$ROOT/src/python/grpcio_channelz" 187 188# Build/install health checking 189$VENV_PYTHON "$ROOT/src/python/grpcio_health_checking/setup.py" preprocess 190$VENV_PYTHON "$ROOT/src/python/grpcio_health_checking/setup.py" build_package_protos 191pip_install_dir "$ROOT/src/python/grpcio_health_checking" 192 193# Build/install reflection 194$VENV_PYTHON "$ROOT/src/python/grpcio_reflection/setup.py" preprocess 195$VENV_PYTHON "$ROOT/src/python/grpcio_reflection/setup.py" build_package_protos 196pip_install_dir "$ROOT/src/python/grpcio_reflection" 197 198# Build/install status proto mapping 199$VENV_PYTHON "$ROOT/src/python/grpcio_status/setup.py" preprocess 200$VENV_PYTHON "$ROOT/src/python/grpcio_status/setup.py" build_package_protos 201pip_install_dir "$ROOT/src/python/grpcio_status" 202 203 204# Build/install status proto mapping 205# build.py is invoked as part of generate_projects.sh 206pip_install_dir "$ROOT/tools/distrib/python/xds_protos" 207 208# Build/install csds 209pip_install_dir "$ROOT/src/python/grpcio_csds" 210 211# Build/install admin 212pip_install_dir "$ROOT/src/python/grpcio_admin" 213 214# Install testing 215pip_install_dir "$ROOT/src/python/grpcio_testing" 216 217# Build/install tests 218pip_install coverage==7.2.0 oauth2client==4.1.0 \ 219 google-auth>=1.35.0 requests==2.31.0 \ 220 googleapis-common-protos>=1.5.5 rsa==4.0 absl-py==1.4.0 \ 221 opentelemetry-sdk==1.21.0 222$VENV_PYTHON "$ROOT/src/python/grpcio_tests/setup.py" preprocess 223$VENV_PYTHON "$ROOT/src/python/grpcio_tests/setup.py" build_package_protos 224pip_install_dir "$ROOT/src/python/grpcio_tests" 225