build_artifact_python.sh 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #!/bin/bash
  2. # Copyright 2016 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. cd "$(dirname "$0")/../../.."
  17. export GRPC_PYTHON_BUILD_WITH_CYTHON=1
  18. export PYTHON=${PYTHON:-python}
  19. export AUDITWHEEL=${AUDITWHEEL:-auditwheel}
  20. # activate ccache if desired
  21. # shellcheck disable=SC1091
  22. source tools/internal_ci/helper_scripts/prepare_ccache_symlinks_rc
  23. # Needed for building binary distribution wheels -- bdist_wheel
  24. "${PYTHON}" -m pip install --upgrade wheel
  25. if [ "$GRPC_SKIP_PIP_CYTHON_UPGRADE" == "" ]
  26. then
  27. # Install Cython to avoid source wheel build failure.
  28. # This only needs to be done when not running under docker (=on MacOS)
  29. # since the docker images used for building python wheels
  30. # already have a new-enough version of cython pre-installed.
  31. # Any installation step is a potential source of breakages,
  32. # so we are trying to perform as few download-and-install operations
  33. # as possible.
  34. "${PYTHON}" -m pip install --upgrade cython
  35. fi
  36. # Allow build_ext to build C/C++ files in parallel
  37. # by enabling a monkeypatch. It speeds up the build a lot.
  38. # Use externally provided GRPC_PYTHON_BUILD_EXT_COMPILER_JOBS value if set.
  39. export GRPC_PYTHON_BUILD_EXT_COMPILER_JOBS=${GRPC_PYTHON_BUILD_EXT_COMPILER_JOBS:-2}
  40. mkdir -p "${ARTIFACTS_OUT}"
  41. ARTIFACT_DIR="$PWD/${ARTIFACTS_OUT}"
  42. # check whether we are crosscompiling. AUDITWHEEL_ARCH is set by the dockcross docker image.
  43. if [ "$AUDITWHEEL_ARCH" == "aarch64" ]
  44. then
  45. # when crosscompiling for aarch64, --plat-name needs to be set explicitly
  46. # to end up with correctly named wheel file
  47. # the value should be manylinuxABC_ARCH and dockcross docker image
  48. # conveniently provides the value in the AUDITWHEEL_PLAT env
  49. WHEEL_PLAT_NAME_FLAG="--plat-name=$AUDITWHEEL_PLAT"
  50. # override the value of EXT_SUFFIX to make sure the crosscompiled .so files in the wheel have the correct filename suffix
  51. GRPC_PYTHON_OVERRIDE_EXT_SUFFIX="$(${PYTHON} -c 'import sysconfig; print(sysconfig.get_config_var("EXT_SUFFIX").replace("-x86_64-linux-gnu.so", "-aarch64-linux-gnu.so"))')"
  52. export GRPC_PYTHON_OVERRIDE_EXT_SUFFIX
  53. # since we're crosscompiling, we need to explicitly choose the right platform for boringssl assembly optimizations
  54. export GRPC_BUILD_OVERRIDE_BORING_SSL_ASM_PLATFORM="linux-aarch64"
  55. fi
  56. # check whether we are crosscompiling. AUDITWHEEL_ARCH is set by the dockcross docker image.
  57. if [ "$AUDITWHEEL_ARCH" == "armv7l" ]
  58. then
  59. # when crosscompiling for arm, --plat-name needs to be set explicitly
  60. # to end up with correctly named wheel file
  61. # our dockcross-based docker image onveniently provides the value in the AUDITWHEEL_PLAT env
  62. WHEEL_PLAT_NAME_FLAG="--plat-name=$AUDITWHEEL_PLAT"
  63. # override the value of EXT_SUFFIX to make sure the crosscompiled .so files in the wheel have the correct filename suffix
  64. GRPC_PYTHON_OVERRIDE_EXT_SUFFIX="$(${PYTHON} -c 'import sysconfig; print(sysconfig.get_config_var("EXT_SUFFIX").replace("-x86_64-linux-gnu.so", "-arm-linux-gnueabihf.so"))')"
  65. export GRPC_PYTHON_OVERRIDE_EXT_SUFFIX
  66. # since we're crosscompiling, we need to explicitly choose the right platform for boringssl assembly optimizations
  67. export GRPC_BUILD_OVERRIDE_BORING_SSL_ASM_PLATFORM="linux-arm"
  68. fi
  69. # Build the source distribution first because MANIFEST.in cannot override
  70. # exclusion of built shared objects among package resources (for some
  71. # inexplicable reason).
  72. ${SETARCH_CMD} "${PYTHON}" setup.py sdist
  73. # Wheel has a bug where directories don't get excluded.
  74. # https://bitbucket.org/pypa/wheel/issues/99/cannot-exclude-directory
  75. # shellcheck disable=SC2086
  76. ${SETARCH_CMD} "${PYTHON}" setup.py bdist_wheel $WHEEL_PLAT_NAME_FLAG
  77. GRPCIO_STRIP_TEMPDIR=$(mktemp -d)
  78. GRPCIO_TAR_GZ_LIST=( dist/grpcio-*.tar.gz )
  79. GRPCIO_TAR_GZ=${GRPCIO_TAR_GZ_LIST[0]}
  80. GRPCIO_STRIPPED_TAR_GZ=$(mktemp -t "TAR_GZ_XXXXXXXXXX")
  81. clean_non_source_files() {
  82. ( cd "$1"
  83. find . -type f \
  84. | grep -v '\.c$' | grep -v '\.cc$' | grep -v '\.cpp$' \
  85. | grep -v '\.h$' | grep -v '\.hh$' | grep -v '\.inc$' \
  86. | grep -v '\.s$' | grep -v '\.py$' | grep -v '\.hpp$' \
  87. | grep -v '\.S$' | grep -v '\.asm$' \
  88. | while read -r file; do
  89. rm -f "$file" || true
  90. done
  91. find . -type d -empty -delete
  92. )
  93. }
  94. tar xzf "${GRPCIO_TAR_GZ}" -C "${GRPCIO_STRIP_TEMPDIR}"
  95. ( cd "${GRPCIO_STRIP_TEMPDIR}"
  96. find . -type d -name .git -exec rm -fr {} \; || true
  97. for dir in */third_party/*; do
  98. clean_non_source_files "${dir}" || true
  99. done
  100. tar czf "${GRPCIO_STRIPPED_TAR_GZ}" -- *
  101. )
  102. mv "${GRPCIO_STRIPPED_TAR_GZ}" "${GRPCIO_TAR_GZ}"
  103. # Build gRPC tools package distribution
  104. "${PYTHON}" tools/distrib/python/make_grpcio_tools.py
  105. # Build gRPC tools package source distribution
  106. ${SETARCH_CMD} "${PYTHON}" tools/distrib/python/grpcio_tools/setup.py sdist
  107. # Build gRPC tools package binary distribution
  108. # shellcheck disable=SC2086
  109. ${SETARCH_CMD} "${PYTHON}" tools/distrib/python/grpcio_tools/setup.py bdist_wheel $WHEEL_PLAT_NAME_FLAG
  110. # run twine check before auditwheel, because auditwheel puts the repaired wheels into
  111. # the artifacts output dir.
  112. if [ "$GRPC_SKIP_TWINE_CHECK" == "" ]
  113. then
  114. # Ensure the generated artifacts are valid.
  115. # TODO(jtattermusch): avoid the need for always re-installing virtualenv and twine
  116. "${PYTHON}" -m pip install virtualenv
  117. "${PYTHON}" -m virtualenv venv || { "${PYTHON}" -m pip install virtualenv==16.7.9 && "${PYTHON}" -m virtualenv venv; }
  118. venv/bin/python -m pip install "twine<=2.0"
  119. venv/bin/python -m twine check dist/* tools/distrib/python/grpcio_tools/dist/*
  120. rm -rf venv/
  121. fi
  122. if [ "$GRPC_RUN_AUDITWHEEL_REPAIR" != "" ]
  123. then
  124. for wheel in dist/*.whl; do
  125. "${AUDITWHEEL}" show "$wheel" | tee /dev/stderr | grep -E -w "$AUDITWHEEL_PLAT"
  126. "${AUDITWHEEL}" repair "$wheel" --strip --wheel-dir "$ARTIFACT_DIR"
  127. rm "$wheel"
  128. done
  129. for wheel in tools/distrib/python/grpcio_tools/dist/*.whl; do
  130. "${AUDITWHEEL}" show "$wheel" | tee /dev/stderr | grep -E -w "$AUDITWHEEL_PLAT"
  131. "${AUDITWHEEL}" repair "$wheel" --strip --wheel-dir "$ARTIFACT_DIR"
  132. rm "$wheel"
  133. done
  134. else
  135. cp -r dist/*.whl "$ARTIFACT_DIR"
  136. cp -r tools/distrib/python/grpcio_tools/dist/*.whl "$ARTIFACT_DIR"
  137. fi
  138. # grpcio and grpcio-tools wheels have already been copied to artifact_dir
  139. # by "auditwheel repair", now copy the .tar.gz source archives as well.
  140. cp -r dist/*.tar.gz "$ARTIFACT_DIR"
  141. cp -r tools/distrib/python/grpcio_tools/dist/*.tar.gz "$ARTIFACT_DIR"
  142. # We need to use the built grpcio-tools/grpcio to compile the health proto
  143. # Wheels are not supported by setup_requires/dependency_links, so we
  144. # manually install the dependency. Note we should only do this if we
  145. # are in a docker image or in a virtualenv.
  146. if [ "$GRPC_BUILD_GRPCIO_TOOLS_DEPENDENTS" != "" ]
  147. then
  148. "${PYTHON}" -m pip install -rrequirements.txt
  149. if [ "$("$PYTHON" -c "import sys; print(sys.version_info[0])")" == "2" ]
  150. then
  151. "${PYTHON}" -m pip install futures>=2.2.0 enum34>=1.0.4
  152. fi
  153. "${PYTHON}" -m pip install grpcio --no-index --find-links "file://$ARTIFACT_DIR/"
  154. "${PYTHON}" -m pip install grpcio-tools --no-index --find-links "file://$ARTIFACT_DIR/"
  155. # Note(lidiz) setuptools's "sdist" command creates a source tarball, which
  156. # demands an extra step of building the wheel. The building step is merely ran
  157. # through setup.py, but we can optimize it with "bdist_wheel" command, which
  158. # skips the wheel building step.
  159. # Build grpcio_testing source distribution
  160. ${SETARCH_CMD} "${PYTHON}" src/python/grpcio_testing/setup.py preprocess \
  161. sdist bdist_wheel
  162. cp -r src/python/grpcio_testing/dist/* "$ARTIFACT_DIR"
  163. # Build grpcio_channelz source distribution
  164. ${SETARCH_CMD} "${PYTHON}" src/python/grpcio_channelz/setup.py \
  165. preprocess build_package_protos sdist bdist_wheel
  166. cp -r src/python/grpcio_channelz/dist/* "$ARTIFACT_DIR"
  167. # Build grpcio_health_checking source distribution
  168. ${SETARCH_CMD} "${PYTHON}" src/python/grpcio_health_checking/setup.py \
  169. preprocess build_package_protos sdist bdist_wheel
  170. cp -r src/python/grpcio_health_checking/dist/* "$ARTIFACT_DIR"
  171. # Build grpcio_reflection source distribution
  172. ${SETARCH_CMD} "${PYTHON}" src/python/grpcio_reflection/setup.py \
  173. preprocess build_package_protos sdist bdist_wheel
  174. cp -r src/python/grpcio_reflection/dist/* "$ARTIFACT_DIR"
  175. # Build grpcio_status source distribution
  176. ${SETARCH_CMD} "${PYTHON}" src/python/grpcio_status/setup.py \
  177. preprocess sdist bdist_wheel
  178. cp -r src/python/grpcio_status/dist/* "$ARTIFACT_DIR"
  179. # Build grpcio_csds source distribution
  180. ${SETARCH_CMD} "${PYTHON}" src/python/grpcio_csds/setup.py \
  181. sdist bdist_wheel
  182. cp -r src/python/grpcio_csds/dist/* "$ARTIFACT_DIR"
  183. # Build grpcio_admin source distribution and it needs the cutting-edge version
  184. # of Channelz and CSDS to be installed.
  185. "${PYTHON}" -m pip install --upgrade xds-protos==0.0.8
  186. "${PYTHON}" -m pip install grpcio-channelz --no-index --find-links "file://$ARTIFACT_DIR/"
  187. "${PYTHON}" -m pip install grpcio-csds --no-index --find-links "file://$ARTIFACT_DIR/"
  188. ${SETARCH_CMD} "${PYTHON}" src/python/grpcio_admin/setup.py \
  189. sdist bdist_wheel
  190. cp -r src/python/grpcio_admin/dist/* "$ARTIFACT_DIR"
  191. fi