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