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# When we mount and reuse the existing repo from host machine inside docker 90# container, the `tools/bazel.rc` file is shared to the docker container and 91# the Bazel override written to `bazel.rc` from tools/.../grpc_build_submodule_at_head.sh 92# (outside docker container) forces bazel to look for the same host location 93# inside the docker container, which doesn't exist. 94# Hence overriding it again with the working directory inside the container 95# should solve this issue 96BAZEL_DEP_PATH="$(pwd)/third_party/protobuf" 97BAZEL_DEP_NAME="com_google_protobuf" 98echo "bazel override_repository is set for ${BAZEL_DEP_NAME} to ${BAZEL_DEP_PATH}" 99echo "build --override_repository=${BAZEL_DEP_NAME}=${BAZEL_DEP_PATH}" >> "tools/bazel.rc" 100echo "query --override_repository=${BAZEL_DEP_NAME}=${BAZEL_DEP_PATH}" >> "tools/bazel.rc" 101 102#################### 103# Script Arguments # 104#################### 105 106PYTHON=${1:-python2.7} 107VENV=${2:-$(venv "$PYTHON")} 108VENV_RELATIVE_PYTHON=${3:-$(venv_relative_python)} 109TOOLCHAIN=${4:-$(toolchain)} 110 111if [ "$(is_msys)" ]; then 112 echo "MSYS doesn't directly provide the right compiler(s);" 113 echo "switch to a MinGW shell." 114 exit 1 115fi 116 117ROOT=$(pwd) 118export CFLAGS="-I$ROOT/include -fno-wrapv $CFLAGS" 119export GRPC_PYTHON_BUILD_WITH_CYTHON=1 120export LANG=en_US.UTF-8 121 122# Allow build_ext to build C/C++ files in parallel 123# by enabling a monkeypatch. It speeds up the build a lot. 124DEFAULT_PARALLEL_JOBS=$(nproc) || DEFAULT_PARALLEL_JOBS=4 125export GRPC_PYTHON_BUILD_EXT_COMPILER_JOBS=${GRPC_PYTHON_BUILD_EXT_COMPILER_JOBS:-$DEFAULT_PARALLEL_JOBS} 126 127# activate ccache if desired 128# shellcheck disable=SC1091 129source tools/internal_ci/helper_scripts/prepare_ccache_symlinks_rc 130 131############################ 132# Perform build operations # 133############################ 134 135if [[ "$(inside_venv)" ]]; then 136 VENV_PYTHON="$PYTHON" 137else 138 # Instantiate the virtualenv from the Python version passed in. 139 $PYTHON -m pip install --user virtualenv==20.25.0 140 # Skip wheel and setuptools and manually install later. Otherwise we might 141 # not find cython module while building grpcio. 142 $PYTHON -m virtualenv --no-wheel --no-setuptools "$VENV" 143 VENV_PYTHON="$(pwd)/$VENV/$VENV_RELATIVE_PYTHON" 144fi 145 146pip_install() { 147 $VENV_PYTHON -m pip install "$@" 148} 149 150pip_install --upgrade pip 151pip_install --upgrade wheel 152pip_install --upgrade setuptools==70.1.1 153 154# pip-installs the directory specified. Used because on MSYS the vanilla Windows 155# Python gets confused when parsing paths. 156pip_install_dir() { 157 PWD=$(pwd) 158 cd "$1" 159 ($VENV_PYTHON setup.py build_ext -c "$TOOLCHAIN" || true) 160 $VENV_PYTHON -m pip install --no-deps . 161 cd "$PWD" 162} 163 164pip_install_dir_and_deps() { 165 PWD=$(pwd) 166 cd "$1" 167 ($VENV_PYTHON setup.py build_ext -c "$TOOLCHAIN" || true) 168 $VENV_PYTHON -m pip install . 169 cd "$PWD" 170} 171 172pip_install -U gevent 173 174pip_install --upgrade 'cython>=3.0.0' 175pip_install --upgrade six 'protobuf>=5.26.1,<6.0dev' 176 177if [ "$("$VENV_PYTHON" -c "import sys; print(sys.version_info[0])")" == "2" ] 178then 179 pip_install --upgrade futures enum34 180fi 181 182pip_install_dir "$ROOT" 183 184$VENV_PYTHON "$ROOT/tools/distrib/python/make_grpcio_tools.py" 185pip_install_dir_and_deps "$ROOT/tools/distrib/python/grpcio_tools" 186 187# Build/install Observability 188# Observability does not support Windows and MacOS. 189if [ "$(is_mingw)" ] || [ "$(is_darwin)" ]; then 190 echo "Skip building grpcio_observability for Windows or MacOS" 191else 192 $VENV_PYTHON "$ROOT/src/python/grpcio_observability/make_grpcio_observability.py" 193 pip_install_dir_and_deps "$ROOT/src/python/grpcio_observability" 194 pip_install_dir_and_deps "$ROOT/src/python/grpcio_csm_observability" 195fi 196 197# Build/install Channelz 198$VENV_PYTHON "$ROOT/src/python/grpcio_channelz/setup.py" preprocess 199$VENV_PYTHON "$ROOT/src/python/grpcio_channelz/setup.py" build_package_protos 200pip_install_dir "$ROOT/src/python/grpcio_channelz" 201 202# Build/install health checking 203$VENV_PYTHON "$ROOT/src/python/grpcio_health_checking/setup.py" preprocess 204$VENV_PYTHON "$ROOT/src/python/grpcio_health_checking/setup.py" build_package_protos 205pip_install_dir "$ROOT/src/python/grpcio_health_checking" 206 207# Build/install reflection 208$VENV_PYTHON "$ROOT/src/python/grpcio_reflection/setup.py" preprocess 209$VENV_PYTHON "$ROOT/src/python/grpcio_reflection/setup.py" build_package_protos 210pip_install_dir "$ROOT/src/python/grpcio_reflection" 211 212# Build/install status proto mapping 213$VENV_PYTHON "$ROOT/src/python/grpcio_status/setup.py" preprocess 214$VENV_PYTHON "$ROOT/src/python/grpcio_status/setup.py" build_package_protos 215pip_install_dir "$ROOT/src/python/grpcio_status" 216 217 218# Build/install status proto mapping 219# build.py is invoked as part of generate_projects.sh 220pip_install_dir "$ROOT/tools/distrib/python/xds_protos" 221 222# Build/install csds 223pip_install_dir "$ROOT/src/python/grpcio_csds" 224 225# Build/install admin 226pip_install_dir "$ROOT/src/python/grpcio_admin" 227 228# Install testing 229pip_install_dir "$ROOT/src/python/grpcio_testing" 230 231# Build/install tests 232# shellcheck disable=SC2261 233pip_install coverage==7.2.0 oauth2client==4.1.0 \ 234 google-auth>=1.35.0 requests==2.31.0 \ 235 rsa==4.0 absl-py==1.4.0 \ 236 opentelemetry-sdk==1.21.0 237$VENV_PYTHON "$ROOT/src/python/grpcio_tests/setup.py" preprocess 238$VENV_PYTHON "$ROOT/src/python/grpcio_tests/setup.py" build_package_protos 239pip_install_dir "$ROOT/src/python/grpcio_tests" 240