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 16set -ex 17 18cd "$(dirname "$0")/../../.." 19 20export GRPC_PYTHON_BUILD_WITH_CYTHON=1 21export PYTHON=${PYTHON:-python} 22export PIP=${PIP:-pip} 23export AUDITWHEEL=${AUDITWHEEL:-auditwheel} 24 25# Install Cython to avoid source wheel build failure. 26"${PIP}" install --upgrade cython 27 28# Allow build_ext to build C/C++ files in parallel 29# by enabling a monkeypatch. It speeds up the build a lot. 30# Use externally provided GRPC_PYTHON_BUILD_EXT_COMPILER_JOBS value if set. 31export GRPC_PYTHON_BUILD_EXT_COMPILER_JOBS=${GRPC_PYTHON_BUILD_EXT_COMPILER_JOBS:-2} 32 33mkdir -p "${ARTIFACTS_OUT}" 34ARTIFACT_DIR="$PWD/${ARTIFACTS_OUT}" 35 36# Build the source distribution first because MANIFEST.in cannot override 37# exclusion of built shared objects among package resources (for some 38# inexplicable reason). 39${SETARCH_CMD} "${PYTHON}" setup.py sdist 40 41# Wheel has a bug where directories don't get excluded. 42# https://bitbucket.org/pypa/wheel/issues/99/cannot-exclude-directory 43${SETARCH_CMD} "${PYTHON}" setup.py bdist_wheel 44 45GRPCIO_STRIP_TEMPDIR=$(mktemp -d) 46GRPCIO_TAR_GZ_LIST=( dist/grpcio-*.tar.gz ) 47GRPCIO_TAR_GZ=${GRPCIO_TAR_GZ_LIST[0]} 48GRPCIO_STRIPPED_TAR_GZ=$(mktemp -t "XXXXXXXXXX.tar.gz") 49 50clean_non_source_files() { 51( cd "$1" 52 find . -type f \ 53 | grep -v '\.c$' | grep -v '\.cc$' | grep -v '\.cpp$' \ 54 | grep -v '\.h$' | grep -v '\.hh$' | grep -v '\.inc$' \ 55 | grep -v '\.s$' | grep -v '\.py$' | grep -v '\.hpp$' \ 56 | while read -r file; do 57 rm -f "$file" || true 58 done 59 find . -type d -empty -delete 60) 61} 62 63tar xzf "${GRPCIO_TAR_GZ}" -C "${GRPCIO_STRIP_TEMPDIR}" 64( cd "${GRPCIO_STRIP_TEMPDIR}" 65 find . -type d -name .git -exec rm -fr {} \; || true 66 for dir in */third_party/*; do 67 clean_non_source_files "${dir}" || true 68 done 69 tar czf "${GRPCIO_STRIPPED_TAR_GZ}" -- * 70) 71mv "${GRPCIO_STRIPPED_TAR_GZ}" "${GRPCIO_TAR_GZ}" 72 73# Build gRPC tools package distribution 74"${PYTHON}" tools/distrib/python/make_grpcio_tools.py 75 76# Build gRPC tools package source distribution 77${SETARCH_CMD} "${PYTHON}" tools/distrib/python/grpcio_tools/setup.py sdist 78 79# Build gRPC tools package binary distribution 80${SETARCH_CMD} "${PYTHON}" tools/distrib/python/grpcio_tools/setup.py bdist_wheel 81 82if [ "$GRPC_BUILD_MANYLINUX_WHEEL" != "" ] 83then 84 for wheel in dist/*.whl; do 85 "${AUDITWHEEL}" show "$wheel" | tee /dev/stderr | grep -E -w "$AUDITWHEEL_PLAT" 86 "${AUDITWHEEL}" repair "$wheel" -w "$ARTIFACT_DIR" 87 rm "$wheel" 88 done 89 for wheel in tools/distrib/python/grpcio_tools/dist/*.whl; do 90 "${AUDITWHEEL}" show "$wheel" | tee /dev/stderr | grep -E -w "$AUDITWHEEL_PLAT" 91 "${AUDITWHEEL}" repair "$wheel" -w "$ARTIFACT_DIR" 92 rm "$wheel" 93 done 94fi 95 96# We need to use the built grpcio-tools/grpcio to compile the health proto 97# Wheels are not supported by setup_requires/dependency_links, so we 98# manually install the dependency. Note we should only do this if we 99# are in a docker image or in a virtualenv. 100if [ "$GRPC_BUILD_GRPCIO_TOOLS_DEPENDENTS" != "" ] 101then 102 "${PIP}" install -rrequirements.txt 103 104 if [ "$("$PYTHON" -c "import sys; print(sys.version_info[0])")" == "2" ] 105 then 106 "${PIP}" install futures>=2.2.0 107 fi 108 109 "${PIP}" install grpcio --no-index --find-links "file://$ARTIFACT_DIR/" 110 "${PIP}" install grpcio-tools --no-index --find-links "file://$ARTIFACT_DIR/" 111 112 # Build grpcio_testing source distribution 113 ${SETARCH_CMD} "${PYTHON}" src/python/grpcio_testing/setup.py preprocess \ 114 sdist 115 cp -r src/python/grpcio_testing/dist/* "$ARTIFACT_DIR" 116 117 # Build grpcio_channelz source distribution 118 ${SETARCH_CMD} "${PYTHON}" src/python/grpcio_channelz/setup.py \ 119 preprocess build_package_protos sdist 120 cp -r src/python/grpcio_channelz/dist/* "$ARTIFACT_DIR" 121 122 # Build grpcio_health_checking source distribution 123 ${SETARCH_CMD} "${PYTHON}" src/python/grpcio_health_checking/setup.py \ 124 preprocess build_package_protos sdist 125 cp -r src/python/grpcio_health_checking/dist/* "$ARTIFACT_DIR" 126 127 # Build grpcio_reflection source distribution 128 ${SETARCH_CMD} "${PYTHON}" src/python/grpcio_reflection/setup.py \ 129 preprocess build_package_protos sdist 130 cp -r src/python/grpcio_reflection/dist/* "$ARTIFACT_DIR" 131 132 # Build grpcio_status source distribution 133 ${SETARCH_CMD} "${PYTHON}" src/python/grpcio_status/setup.py \ 134 preprocess sdist 135 cp -r src/python/grpcio_status/dist/* "$ARTIFACT_DIR" 136fi 137 138# Ensure the generated artifacts are valid. 139"${PYTHON}" -m virtualenv venv || { "${PYTHON}" -m pip install virtualenv==16.7.9 && "${PYTHON}" -m virtualenv venv; } 140venv/bin/python -m pip install "twine<=2.0" 141venv/bin/python -m twine check dist/* tools/distrib/python/grpcio_tools/dist/* 142rm -rf venv/ 143 144cp -r dist/* "$ARTIFACT_DIR" 145cp -r tools/distrib/python/grpcio_tools/dist/* "$ARTIFACT_DIR" 146