Skip to content
Snippets Groups Projects
Commit fbf15e43 authored by Masood Malekghassemi's avatar Masood Malekghassemi
Browse files

Make build_python.sh script smarter

Now reasonable defaults are auto-detected by platform (and by specific
Python implementation).
parent 768b1db4
No related branches found
No related tags found
No related merge requests found
...@@ -33,11 +33,80 @@ set -ex ...@@ -33,11 +33,80 @@ set -ex
# change to grpc repo root # change to grpc repo root
cd $(dirname $0)/../.. cd $(dirname $0)/../..
# Arguments ##########################
# Portability operations #
##########################
PLATFORM=`uname -s`
function is_mingw() {
if [ "${PLATFORM/MINGW}" != "$PLATFORM" ]; then
echo true
else
exit 1
fi
}
function is_darwin() {
if [ "${PLATFORM/Darwin}" != "$PLATFORM" ]; then
echo true
else
exit 1
fi
}
function is_linux() {
if [ "${PLATFORM/Linux}" != "$PLATFORM" ]; then
echo true
else
exit 1
fi
}
# Associated virtual environment name for the given python command.
function venv() {
$1 -c "import sys; print('py{}{}'.format(*sys.version_info[:2]))"
}
# Path to python executable within a virtual environment depending on the
# system.
function venv_relative_python() {
if [ $(is_mingw) ]; then
echo 'Scripts/python.exe'
else
echo 'bin/python'
fi
}
# Distutils toolchain to use depending on the system.
function toolchain() {
if [ $(is_mingw) ]; then
echo 'mingw32'
else
echo 'unix'
fi
}
# Command to invoke the linux command `realpath` or equivalent.
function script_realpath() {
# Find `realpath`
if [ -x "$(command -v realpath)" ]; then
realpath "$@"
elif [ -x "$(command -v grealpath)" ]; then
grealpath "$@"
else
exit 1
fi
}
####################
# Script Arguments #
####################
PYTHON=${1:-python2.7} PYTHON=${1:-python2.7}
VENV=${2:-py27} VENV=${2:-$(venv $PYTHON)}
VENV_RELATIVE_PYTHON=${3:-bin/python} VENV_RELATIVE_PYTHON=${3:-$(venv_relative_python)}
TOOLCHAIN=${4:-unix} TOOLCHAIN=${4:-$(toolchain)}
ROOT=`pwd` ROOT=`pwd`
export CFLAGS="-I$ROOT/include -std=gnu99 -fno-wrapv $CFLAGS" export CFLAGS="-I$ROOT/include -std=gnu99 -fno-wrapv $CFLAGS"
...@@ -47,11 +116,8 @@ export GRPC_PYTHON_BUILD_WITH_CYTHON=1 ...@@ -47,11 +116,8 @@ export GRPC_PYTHON_BUILD_WITH_CYTHON=1
# virtualenv. # virtualenv.
HOST_PYTHON=${HOST_PYTHON:-python} HOST_PYTHON=${HOST_PYTHON:-python}
# If ccache is available, use it... unless we're on Mac, then all hell breaks # If ccache is available on Linux, use it.
# loose because Python does hacky things to support other hacky things done to if [ $(is_linux) ]; then
# hacky things on Mac OS X
PLATFORM=`uname -s`
if [ "${PLATFORM/Darwin}" = "$PLATFORM" ]; then
# We're not on Darwin (Mac OS X) # We're not on Darwin (Mac OS X)
if [ -x "$(command -v ccache)" ]; then if [ -x "$(command -v ccache)" ]; then
if [ -x "$(command -v gcc)" ]; then if [ -x "$(command -v gcc)" ]; then
...@@ -63,7 +129,7 @@ if [ "${PLATFORM/Darwin}" = "$PLATFORM" ]; then ...@@ -63,7 +129,7 @@ if [ "${PLATFORM/Darwin}" = "$PLATFORM" ]; then
fi fi
# TODO(atash) consider conceptualizing MinGW as a first-class platform and move # TODO(atash) consider conceptualizing MinGW as a first-class platform and move
# these flags into our `setup.py`s # these flags into our `setup.py`s
if [ "${PLATFORM/MINGW}" != "$PLATFORM" ]; then if [ $(is_mingw) ]; then
# We're on MinGW, and our CFLAGS and LDFLAGS will be eaten by the void. Use # We're on MinGW, and our CFLAGS and LDFLAGS will be eaten by the void. Use
# our work-around environment variables instead. # our work-around environment variables instead.
PYTHON_MSVCR=`$PYTHON -c "from distutils.cygwinccompiler import get_msvcr; print(get_msvcr()[0])"` PYTHON_MSVCR=`$PYTHON -c "from distutils.cygwinccompiler import get_msvcr; print(get_msvcr()[0])"`
...@@ -75,15 +141,9 @@ if [ "${PLATFORM/MINGW}" != "$PLATFORM" ]; then ...@@ -75,15 +141,9 @@ if [ "${PLATFORM/MINGW}" != "$PLATFORM" ]; then
export GRPC_PYTHON_CFLAGS="-frtti -std=c++11 $GRPC_PYTHON_CFLAGS" export GRPC_PYTHON_CFLAGS="-frtti -std=c++11 $GRPC_PYTHON_CFLAGS"
fi fi
# Find `realpath` ############################
if [ -x "$(command -v realpath)" ]; then # Perform build operations #
export REALPATH=realpath ############################
elif [ -x "$(command -v grealpath)" ]; then
export REALPATH=grealpath
else
echo 'Couldn'"'"'t find `realpath` or `grealpath`'
exit 1
fi
# Instnatiate the virtualenv, preferring to do so from the relevant python # Instnatiate the virtualenv, preferring to do so from the relevant python
# version. Even if these commands fail (e.g. on Windows due to name conflicts) # version. Even if these commands fail (e.g. on Windows due to name conflicts)
...@@ -93,7 +153,7 @@ fi ...@@ -93,7 +153,7 @@ fi
($PYTHON -m virtualenv $VENV || ($PYTHON -m virtualenv $VENV ||
$HOST_PYTHON -m virtualenv -p $PYTHON $VENV || $HOST_PYTHON -m virtualenv -p $PYTHON $VENV ||
true) true)
VENV_PYTHON=`$REALPATH -s "$VENV/$VENV_RELATIVE_PYTHON"` VENV_PYTHON=`script_realpath -s "$VENV/$VENV_RELATIVE_PYTHON"`
# pip-installs the directory specified. Used because on MSYS the vanilla Windows # pip-installs the directory specified. Used because on MSYS the vanilla Windows
# Python gets confused when parsing paths. # Python gets confused when parsing paths.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment