build_python.sh 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. set -ex
  16. # change to grpc repo root
  17. cd "$(dirname "$0")/../../.."
  18. ##########################
  19. # Portability operations #
  20. ##########################
  21. PLATFORM=$(uname -s)
  22. function is_msys() {
  23. if [ "${PLATFORM/MSYS}" != "$PLATFORM" ]; then
  24. echo true
  25. else
  26. exit 1
  27. fi
  28. }
  29. function is_mingw() {
  30. if [ "${PLATFORM/MINGW}" != "$PLATFORM" ]; then
  31. echo true
  32. else
  33. exit 1
  34. fi
  35. }
  36. function is_darwin() {
  37. if [ "${PLATFORM/Darwin}" != "$PLATFORM" ]; then
  38. echo true
  39. else
  40. exit 1
  41. fi
  42. }
  43. function is_linux() {
  44. if [ "${PLATFORM/Linux}" != "$PLATFORM" ]; then
  45. echo true
  46. else
  47. exit 1
  48. fi
  49. }
  50. function inside_venv() {
  51. if [[ -n "${VIRTUAL_ENV}" ]]; then
  52. echo true
  53. fi
  54. }
  55. # Associated virtual environment name for the given python command.
  56. function venv() {
  57. $1 -c "import sys; print('py{}{}'.format(*sys.version_info[:2]))"
  58. }
  59. # Path to python executable within a virtual environment depending on the
  60. # system.
  61. function venv_relative_python() {
  62. if [ "$(is_mingw)" ]; then
  63. echo 'Scripts/python.exe'
  64. else
  65. echo 'bin/python'
  66. fi
  67. }
  68. # Distutils toolchain to use depending on the system.
  69. function toolchain() {
  70. if [ "$(is_mingw)" ]; then
  71. echo 'mingw32'
  72. else
  73. echo 'unix'
  74. fi
  75. }
  76. # TODO(jtattermusch): this adds dependency on grealpath on mac
  77. # (brew install coreutils) for little reason.
  78. # Command to invoke the linux command `realpath` or equivalent.
  79. function script_realpath() {
  80. # Find `realpath`
  81. if [ -x "$(command -v realpath)" ]; then
  82. realpath "$@"
  83. elif [ -x "$(command -v grealpath)" ]; then
  84. grealpath "$@"
  85. else
  86. exit 1
  87. fi
  88. }
  89. ####################
  90. # Script Arguments #
  91. ####################
  92. PYTHON=${1:-python2.7}
  93. VENV=${2:-$(venv "$PYTHON")}
  94. VENV_RELATIVE_PYTHON=${3:-$(venv_relative_python)}
  95. TOOLCHAIN=${4:-$(toolchain)}
  96. if [ "$(is_msys)" ]; then
  97. echo "MSYS doesn't directly provide the right compiler(s);"
  98. echo "switch to a MinGW shell."
  99. exit 1
  100. fi
  101. ROOT=$(pwd)
  102. export CFLAGS="-I$ROOT/include -std=gnu99 -fno-wrapv $CFLAGS"
  103. export GRPC_PYTHON_BUILD_WITH_CYTHON=1
  104. export LANG=en_US.UTF-8
  105. # Allow build_ext to build C/C++ files in parallel
  106. # by enabling a monkeypatch. It speeds up the build a lot.
  107. DEFAULT_PARALLEL_JOBS=$(nproc) || DEFAULT_PARALLEL_JOBS=4
  108. export GRPC_PYTHON_BUILD_EXT_COMPILER_JOBS=${GRPC_PYTHON_BUILD_EXT_COMPILER_JOBS:-$DEFAULT_PARALLEL_JOBS}
  109. # activate ccache if desired
  110. # shellcheck disable=SC1091
  111. source tools/internal_ci/helper_scripts/prepare_ccache_symlinks_rc
  112. ############################
  113. # Perform build operations #
  114. ############################
  115. if [[ "$(inside_venv)" ]]; then
  116. VENV_PYTHON="$PYTHON"
  117. else
  118. # Instantiate the virtualenv from the Python version passed in.
  119. $PYTHON -m pip install --user virtualenv==16.7.9
  120. $PYTHON -m virtualenv "$VENV"
  121. VENV_PYTHON=$(script_realpath "$VENV/$VENV_RELATIVE_PYTHON")
  122. fi
  123. # On library/version/platforms combo that do not have a binary
  124. # published, we may end up building a dependency from source. In that
  125. # case, several of our build environment variables may disrupt the
  126. # third-party build process. This function pipes through only the
  127. # minimal environment necessary.
  128. pip_install() {
  129. /usr/bin/env -i PATH="$PATH" "$VENV_PYTHON" -m pip install "$@"
  130. }
  131. # Pin setuptools to < 60.0.0 to restore the distutil installation, see:
  132. # https://github.com/pypa/setuptools/pull/2896
  133. export SETUPTOOLS_USE_DISTUTILS=stdlib
  134. pip_install --upgrade pip==21.3.1
  135. pip_install --upgrade setuptools==59.6.0
  136. # pip-installs the directory specified. Used because on MSYS the vanilla Windows
  137. # Python gets confused when parsing paths.
  138. pip_install_dir() {
  139. PWD=$(pwd)
  140. cd "$1"
  141. ($VENV_PYTHON setup.py build_ext -c "$TOOLCHAIN" || true)
  142. $VENV_PYTHON -m pip install --no-deps .
  143. cd "$PWD"
  144. }
  145. # Install gevent
  146. if [[ "$VENV" == "py36" ]]; then
  147. # TODO(https://github.com/grpc/grpc/issues/15411) unpin this
  148. pip_install gevent==1.3.b1
  149. else
  150. pip_install -U gevent
  151. fi
  152. pip_install --upgrade cython
  153. pip_install --upgrade six protobuf
  154. if [ "$("$VENV_PYTHON" -c "import sys; print(sys.version_info[0])")" == "2" ]
  155. then
  156. pip_install --upgrade futures enum34
  157. fi
  158. pip_install_dir "$ROOT"
  159. $VENV_PYTHON "$ROOT/tools/distrib/python/make_grpcio_tools.py"
  160. pip_install_dir "$ROOT/tools/distrib/python/grpcio_tools"
  161. # Build/install Channelz
  162. $VENV_PYTHON "$ROOT/src/python/grpcio_channelz/setup.py" preprocess
  163. $VENV_PYTHON "$ROOT/src/python/grpcio_channelz/setup.py" build_package_protos
  164. pip_install_dir "$ROOT/src/python/grpcio_channelz"
  165. # Build/install health checking
  166. $VENV_PYTHON "$ROOT/src/python/grpcio_health_checking/setup.py" preprocess
  167. $VENV_PYTHON "$ROOT/src/python/grpcio_health_checking/setup.py" build_package_protos
  168. pip_install_dir "$ROOT/src/python/grpcio_health_checking"
  169. # Build/install reflection
  170. $VENV_PYTHON "$ROOT/src/python/grpcio_reflection/setup.py" preprocess
  171. $VENV_PYTHON "$ROOT/src/python/grpcio_reflection/setup.py" build_package_protos
  172. pip_install_dir "$ROOT/src/python/grpcio_reflection"
  173. # Build/install status proto mapping
  174. $VENV_PYTHON "$ROOT/src/python/grpcio_status/setup.py" preprocess
  175. $VENV_PYTHON "$ROOT/src/python/grpcio_status/setup.py" build_package_protos
  176. pip_install_dir "$ROOT/src/python/grpcio_status"
  177. # Build/install csds
  178. pip_install_dir "$ROOT/src/python/grpcio_csds"
  179. # Build/install admin
  180. pip_install_dir "$ROOT/src/python/grpcio_admin"
  181. # Install testing
  182. pip_install_dir "$ROOT/src/python/grpcio_testing"
  183. # Build/install tests
  184. pip_install coverage==4.4 oauth2client==4.1.0 \
  185. google-auth>=1.17.2 requests==2.14.2 \
  186. googleapis-common-protos>=1.5.5 rsa==4.0
  187. $VENV_PYTHON "$ROOT/src/python/grpcio_tests/setup.py" preprocess
  188. $VENV_PYTHON "$ROOT/src/python/grpcio_tests/setup.py" build_package_protos
  189. pip_install_dir "$ROOT/src/python/grpcio_tests"