• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2# Copyright 2021 The 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# ARGUMENTS
19# $1 - python version in "3.X.Y" format
20# $2 - python tags (as in manylinx images) e.g. "/opt/python/cp37-cp37m"
21PYTHON_VERSION="$1"
22PYTHON_RELEASE="$2"
23PYTHON_PREFIX="$3"
24
25curl -O "https://www.python.org/ftp/python/${PYTHON_VERSION}/Python-${PYTHON_RELEASE}.tar.xz"
26tar -xf "Python-${PYTHON_RELEASE}.tar.xz"
27pushd "Python-${PYTHON_RELEASE}"
28
29# In this step, we are building python that can run on the architecture where the build runs (x64).
30# Since the CC, CXX and other env vars are set by default to point to the crosscompilation toolchain,
31# we explicitly unset them to end up with x64 python binaries.
32(unset AS AR CC CPP CXX LD && ./configure --prefix="${PYTHON_PREFIX}" && make -j 4 && make install)
33
34# When building the crosscompiled native extension, python will automatically add its include directory
35# that contains "Python.h" and other headers. But since we are going to be crosscompiling
36# the wheels, we need the pyconfig.h that corresponds to the target architecture.
37# Since pyconfig.h is the only generated header and python itself (once built) doesn't
38# really need this header anymore, we simply generate a new pyconfig.h using our crosscompilation
39# toolchain and overwrite the current ("wrong") version in the python's include directory.
40./configure && cp pyconfig.h "${PYTHON_PREFIX}"/include/python*
41
42popd
43# remove the build directory to decrease the overall docker image size
44rm -rf "Python-${PYTHON_VERSION}"
45
46# install cython and wheel
47"${PYTHON_PREFIX}/bin/python3" -m pip install --upgrade 'cython<4.0.0rc1' wheel
48