diff --git a/tools/docker/build-manylinux1.sh b/tools/docker/build-manylinux1.sh index d6e57c6f7e..3a29b1a037 100755 --- a/tools/docker/build-manylinux1.sh +++ b/tools/docker/build-manylinux1.sh @@ -1,18 +1,79 @@ #!/bin/bash -# Build all the wheel artifacts for the platforms supported by manylinux1 -# and export them to the specified location. +# Build all the wheel artifacts for the platforms supported by manylinux1 and +# export them to the specified location. # # Arguments: # -# $1 the build process root directory. If not specified as argument, -# the env var BUILD_ROOT will be used. If not specified at all, a -# default location will be used. +# $1 the build process root directory. If not specified as argument, the +# env var BUILD_ROOT will be used. If not specified at all, a default +# location will be used. # -# $2 the artifacts export directory. If not specified as argument, -# the env var EXPORT_ROOT will be used. If not specified at all, a -# default location will be used. +# $2 the artifacts export directory. If not specified as argument, the env +# var EXPORT_ROOT will be used. If not specified at all, a default location +# will be used. set -e + +function contains_element () { + # Look for the presence of an element in an array. Echoes '0' if found, + # '1' otherwise. + # Arguments: + # $1 the element to be searched + # $2 the array to search into + local e match="$1" + shift + for e; do [[ "$e" == "$match" ]] && echo '0' && return; done + echo '1' && return +} + + +function export_manylinux_wheel { + # Build all the wheel artifacts assuming that the current 'python' command + # is the target interpreter. Please note that in order to have or-tools + # correctly detect the target python version, this function should be run in + # a virtualenv. + # Arguments: + # $1 the or-tools sources root directory + # $2 the artifacts export directory + if [ "$#" -ne 2 ]; then + echo "build_pypi_archives called with an illegal number of parameters" + exit 1 # TODO return error and check it outside + fi + local src_root="$1" + local export_root="$2" + # Build python bindings + cd "$src_root" + # We need to force this target, otherwise the protobuf stub will be missing + # (for the makefile, it exists even if previously generated for another + # platform) + make -B install_python_modules # regenerates Makefile.local + make python + make test_python + make pypi_archive + # Build and repair wheels + cd temp-python*/ortools + python setup.py bdist_wheel + cd dist + auditwheel repair ./*.whl -w "$export_root" +} + + +function test_installed { + # Run all the specified test scripts using the current environment. + # Arguments: + # $@ the test files to be tested + local testfiles=("${@}") + cd "$(mktemp -d)" # ensure we are not importing something from $PWD + for testfile in "${testfiles[@]}" + do + python "$testfile" + done +} + + +############################################################################### +# Setup + if [ -n "$1" ]; then BUILD_ROOT="$1"; fi if [ -n "$2" ]; then EXPORT_ROOT="$2"; fi @@ -31,15 +92,15 @@ fi SRC_ROOT="${BUILD_ROOT}/or-tools" # Platforms to be ignored. -# The build of Python 2.6.x bindings is known to be broken -# (and 2.6 itself is deprecated). +# The build of Python 2.6.x bindings is known to be broken (and 2.6 itself is +# deprecated). SKIP_PLATFORMS=( cp26-cp26m cp26-cp26mu ) -# Python scripts to be used as tests for the installed wheel. -# This list of files has been taken from 'test_python' make target. +# Python scripts to be used as tests for the installed wheel. This list of files +# has been taken from the 'test_python' make target. TESTS=( "${SRC_ROOT}/examples/python/hidato_table.py" "${SRC_ROOT}/examples/python/tsp.py" @@ -54,62 +115,12 @@ TESTS=( echo "BUILD_ROOT=${BUILD_ROOT}" echo "SRC_ROOT=${SRC_ROOT}" echo "EXPORT_ROOT=${EXPORT_ROOT}" -echo "SKIP_PLATFORMS=${SKIP_PLATFORMS[@]}" -echo "TESTS=${TESTS[@]}" +echo "SKIP_PLATFORMS=(${SKIP_PLATFORMS[*]})" +echo "TESTS=(${TESTS[*]})" -################################################################################ - -function contains_element () { - # Look for the presence of an element in an array. Echoes '0' if found, - # '1' otherwise. - # Arguments: - # $1 the element to be searched - # $2 the array to search into - local e match="$1" - shift - for e; do [[ "$e" == "$match" ]] && echo '0' && return; done - echo '1' && return -} - -function export_manylinux_wheel { - # Build all the wheel artifacts assuming that the current 'python' - # command is the target interpreter. Please note that in order to - # have or-tools correctly detect the target python version, this - # function should be run in a virtualenv. - # Arguments: - # $1 the or-tools sources root directory - # $2 the artifacts export directory - if [ "$#" -ne 2 ]; then - echo "build_pypi_archives called with an illegal number of parameters" - exit 1 # TODO return error and check it outside - fi - _SRC_ROOT="$1" - _EXPORT_ROOT="$2" - # Build - cd "$_SRC_ROOT" - # We need to force this target, otherwise the protobuf stub will be missing - # (for the makefile, it exists even if previously generated for another - # platform) - make -B install_python_modules # regenerates Makefile.local - make python - make test_python - make pypi_archive - # Build and repair wheels - cd temp-python*/ortools - python setup.py bdist_wheel - cd dist - auditwheel repair *.whl -w "$_EXPORT_ROOT" -} - -function test_installed { - cd $(mktemp -d) # ensure we are not importing something from $PWD - for testfile in "${TESTS[@]}" - do - python "$testfile" - done -} ############################################################################### +# Main mkdir -p "${BUILD_ROOT}" mkdir -p "${EXPORT_ROOT}" @@ -131,27 +142,25 @@ fi # TODO remove all patching, write Makefile targets for manylinux -# Patch makefile, remove offending command -# (the setup.py-e file doesn't exist) +# Patch makefile, remove offending command (the setup.py-e file doesn't exist) cd "$SRC_ROOT" -sed -i -e 's=-rm $(PYPI_ARCHIVE_TEMP_DIR)/ortools/setup.py-e==g' makefiles/Makefile.python.mk +sed -i -e "s=-rm \$(PYPI_ARCHIVE_TEMP_DIR)/ortools/setup.py-e==g" makefiles/Makefile.python.mk -# Patch makefile, remove manual libortools.so grafting -# and RPATH setting. All of this stuff will be carried -# out by auditwheel, otherwise we would end up with a +# Patch makefile, remove manual libortools.so grafting and RPATH setting. All of +# this stuff will be carried out by auditwheel, otherwise we would end up with a # broken manylinux wheel file cd "$SRC_ROOT" -sed -i -e 's=cp lib/libortools=#=g' makefiles/Makefile.python.mk -sed -i -e 's=tools/fix_python_libraries_on_linux.sh=#=g' makefiles/Makefile.python.mk +sed -i -e "s=cp lib/libortools=#=g" makefiles/Makefile.python.mk +sed -i -e "s=tools/fix_python_libraries_on_linux.sh=#=g" makefiles/Makefile.python.mk -# For each python platform provided by manylinux, -# build, export and test artifacts. +# For each python platform provided by manylinux, build, export and test +# artifacts. for PYROOT in /opt/python/* do PYTAG=$(basename "$PYROOT") # Check for platforms to be skipped SKIP=$(contains_element "$PYTAG" "${SKIP_PLATFORMS[@]}") - if [ $SKIP -eq '0' ] + if [ "$SKIP" -eq '0' ] then echo "skipping deprecated platform $PYTAG" continue @@ -182,8 +191,8 @@ do source "${BUILD_ROOT}/${PYTAG}-test/bin/activate" pip install -U pip setuptools wheel six # Install wheel and run tests - pip install --no-cache-dir $WHEEL_FILE - test_installed + pip install --no-cache-dir "$WHEEL_FILE" + test_installed "${TESTS[@]}" # Restore environment deactivate done