GitHub - matthew-brett/multibuild: Machinery for building and testing Python Wheels for Linux, OSX and (less flexibly) Windows. (original) (raw)

Utilities for building on Travis CI and AppVeyor

Update: Uploads, Rackspace, Anaconda

The original Multibuild default was to upload wheels to a Rackspace container, where Rackspace kindly donated the hosting to the Scikit-learn team. We had a URL pointing to the Rackspace container: http://wheels.scipy.org.

Rackspace finally stopped subsidizing this container, and the Rackspace ofhttp://wheels.scipy.org is no more. Some projects using Multibuild have moved to using https://anaconda.org/scipy-wheels-nightly/ for weekly uploads andhttps://anaconda.org/multibuild-wheels-staging for staging wheels to PyPI.

Uploads to Anaconda

If you want to upload to Anaconda, and you don't need the extra storage space for nightly builds that Anaconda kindly donates to Numpy, Scipy etc, then you can do this with your own Anaconda organization.

See https://github.com/MacPython/nipy-wheels for a simple example.

where secure: is the text suggested from "Encrypt" above, and nipy is your Anaconda organization.
Finally, add a clause like the following to the end of your appveyor.yml file:
on_success:

Upload the generated wheel package to Anaconda.org

There's a simple example of these steps applied athttps://github.com/MacPython/nipy-wheels.

Here is the NumPy code (for running on Travis CI) to upload to Anaconda:https://github.com/MacPython/numpy-wheels/blob/master/.travis.yml#L99

For projects housed under the MacPython GitHub organization, you have access to Anaconda upload tokens via the "Organization Secrets"https://github.com/MacPython/numexpr-wheels/settings/secrets . You can use these to move to GitHub Actions (they provide x86 machines for Windows, Linux and Mac). Otherwise we (please raise an issue here) will need to negotiate getting you tokens, or you can make your own, as above.

Use Github for uploads

Another option is to use GitHub for staging --- as do Cython for Travis CI andfor AppVeyor.

Introduction

A set of scripts to automate builds of macOS and Manylinux1 wheels on theTravis CI infrastructure, and also Windows wheels on the AppVeyor infrastructure.

The Travis CI scripts are designed to build and test:

You can currently build and test against Pythons 2.7, 3.5, 3.6, 3.7, 3.8 and 3.9

The small innovation here is that you can test against Linux 32-bit builds, both wide and narrow Unicode Python 2 builds, which was not easy on the default Travis CI configurations.

The AppVeyor setup is designed to build and test:

You can currently build and test against Pythons 2.7, 3.5, 3.6, 3.7, 3.8, 3.9

How does it work?

Multibuild is a series of bash scripts that define bash functions to build and test wheels.

Configuration is by overriding the default build function, and defining a test function.

The bash scripts are layered, in the sense that they are composed of a number of scripts which are sourced in sequence, each one potentially overriding previous ones.

macOS

The following bash scripts are sourced in this order:

multibuild/common_utils.sh multibuild/osx_utils.sh env_vars.sh multibuild/configure_build.sh multibuild/library_builders.sh config.sh

See multibuild/travis_osx_steps.sh

The macOS build / test phases run on the macOS VM started by Travis CI. Therefore any environment variable defined in .travis.yml or the bash shell scripts listed above are available for your build and test.

Build options are controlled mainly by the following environment variables:

In most cases it's best to rely on the defaults for MB_PYTHON_OSX_VER andPLAT, rather than setting them explicitly. Examples of exceptions to this guideline include:

The build_wheel function builds the wheel, and install_runfunction installs and tests it. Look in multibuild/common_utils.sh for default definitions of these functions. See below for more details, many of which are common to macOS and Linux.

Manylinux

The build phase is in a Manylinux Docker container, but the test phase is in a clean container.

Build phase

Specify the Manylinux version to build for with the MB_ML_VER environment variable. The default version is 1. Versions that are currently valid are:

The environment variable specified which Manylinux docker container you are building in.

The PLAT environment variable can be one of

The default is x86_64. Only x86_64 and i686 are valid on manylinux1 and manylinux2010.

multibuild/travis_linux_steps.sh defines the build_wheel function, which starts up the Manylinux1 Docker container to run a wrapper scriptmultibuild/docker_build_wrap.sh, that (within the container) sources the following bash scripts:

multibuild/common_utils.sh multibuild/manylinux_utils.sh env_vars.sh multibuild/configure_build.sh multibuild/library_builders.sh config.sh

See docker_build_wrap.sh to review the order of script sourcing.

See the definition of build_multilinux inmultibuild/travis_linux_steps.sh for the environment variables passed from Travis CI to the Manylinux1 container.

Once in the container, after sourcing the scripts above, the wrapper runs the real build_wheel function, which now comes (by default) frommultibuild/common_utils.sh.

Test phase

Specify the version to test with the DOCKER_TEST_IMAGE environment variable. The default version is dependent on PLAT:

Other valid values are any in https://hub.docker.com/orgs/multibuild/repositories, using the correct platform code. Alternatively, you can use the substitution pattern multibuild/xenial_{PLAT} in the .travis.yml file.

See multibuild/docker_test_wrap.sh.

multibuild/travis_linux_steps.sh defines the install_run function, which starts up the testing Docker container with the wrapper scriptmultibuild/docker_test_wrap.sh. The wrapper script sources the following bash scripts:

multibuild/common_utils.sh config.sh

See docker_test_wrap.sh for script source order.

See install_run in multibuild/travis_linux_steps.sh for the environment variables passed into the container.

It then (in the container) runs the real install_run command, which comes (by default) from multibuild/common_utils.sh.

Standard build and test functions

The standard build command is build_wheel. This is a bash function. By default the function that is run on macOS, and in the Manylinux container for the build phase, is defined in multibuild/common_utils.sh. You can override the default function in the project config.sh file (see below).

If you are building a wheel from PyPI, rather than from a source repository, you can use the build_index_wheel command, again defined inmultibuild/common_utils.sh.

Typically, you can get away with leaving the default build_wheel /build_index_wheel functions to do their thing, but you may need to define a pre_build function in config.sh. The default build_wheel andbuild_index_wheel functions will call the pre_build function, if defined, before building the wheel, so pre_build is a good place to build any required libraries.

The standard test command is the bash function install_run. The version run on macOS and in the Linux testing container is also defined inmultibuild/common_utils.sh. Typically, you do not override this function, but you in that case you will need to define a run_tests function, to run your tests, returning a non-zero error code for failure. The defaultinstall_run implementation calls the run_tests function, which you will likely define in config.sh. See the examples below for examples of less and more complicated builds, where the complicated builds override more of the default implementations.

To use these scripts

You will likely prefer "language: generic" for travis configuration,

rather than, say "language: python". Multibuild doesn't use

Travis-provided Python but rather installs and uses its own, where the

Python version is set from the MB_PYTHON_VERSION variable. You can still

specify a language here if you need it for some unrelated logic and you

can't use Multibuild-provided Python or other software present on a

builder.

language: generic

For CPython macOS builds only, the minimum supported macOS version and

architectures of any C extensions in the wheel are set with the variable

MB_PYTHON_OSX_VER: 10.9 (64-bit only) or 10.6 (64/32-bit dual arch). By

default this is set to the highest available for the Python version selected

using MB_PYTHON_VERSION. You should only need to set this explicitly if you

are building a 10.6 dual-arch build for a CPython version where both a 10.9 and

10.6 build are available (for example, 2.7 or 3.7).

All PyPy macOS builds are 64-bit only.

Required in Linux to invoke docker ourselves

services: docker

Host distribution. This is the distribution from which we run the build

and test containers, via docker.

dist: xenial

osx image that enables building Apple silicon libraries

osx_image: xcode12.2
matrix:
include:
- os: linux
env: MB_PYTHON_VERSION=2.7
- os: linux
env:
- MB_PYTHON_VERSION=2.7
- UNICODE_WIDTH=16
- os: linux
env:
- MB_PYTHON_VERSION=2.7
- PLAT=i686
- os: linux
env:
- MB_PYTHON_VERSION=2.7
- PLAT=i686
- UNICODE_WIDTH=16
- os: linux
env:
- MB_PYTHON_VERSION=3.5
- os: linux
env:
- MB_PYTHON_VERSION=3.5
- PLAT=i686
- os: linux
env:
- MB_PYTHON_VERSION=3.6
- os: linux
env:
- MB_PYTHON_VERSION=3.6
- PLAT=i686
- os: osx
env:
- MB_PYTHON_VERSION=2.7
- MB_PYTHON_OSX_VER=10.6
- os: osx
env:
- MB_PYTHON_VERSION=2.7
- os: osx
env:
- MB_PYTHON_VERSION=3.5
- os: osx
env:
- MB_PYTHON_VERSION=3.6
- os: osx
env:
- MB_PYTHON_VERSION=3.7
- MB_PYTHON_OSX_VER=10.6
- os: osx
env:
- MB_PYTHON_VERSION=3.7
- os: osx
env:
- MB_PYTHON_VERSION=3.8
- os: osx
env:
- MB_PYTHON_VERSION=3.9
- PLAT="universal2"
- os: osx
env:
- MB_PYTHON_VERSION=3.9
- os: osx
language: generic
env:
- MB_PYTHON_VERSION=pypy-5.7
before_install:
- source multibuild/common_utils.sh
- source multibuild/travis_steps.sh
- before_install
install:
# Maybe get and clean and patch source
- clean_code REPODIRREPO_DIR REPODIRBUILD_COMMIT
- build_wheel REPODIRREPO_DIR REPODIRPLAT
script:
- install_run $PLAT
after_success:
# Here you should put the code to upload your wheels
# See the Uploads section for more details.
The example above is for a project building from a Git submodule. If you aren't building from a submodule, but want to use pip to build from a source archive on https://pypi.org or similar, replace the first few lines of the .travis.yml file with something like:
env:
global:
# Instead of REPO_DIR, BUILD_COMMIT
- PROJECT_SPEC="tornado==4.1.1"
then your install section could look something like this:
install:
- build_index_wheel $PROJECT_SPEC

Define custom utilities

Test for macOS with [ -n "$IS_MACOS" ]

function pre_build {
# Any stuff that you need to do before you start building the wheels
# Runs in the root directory of this repository.
:
}
function run_tests {
# Runs tests on installed distribution from an empty directory
python --version
python -c 'import sys; import yourpackage; sys.exit(yourpackage.test())'
}
Optionally you can specify a different location for config.sh file with the $CONFIG_PATH environment variable.

If your project depends on NumPy, you will want to build against the earliest NumPy that your project supports - see forward, backward NumPy compatibility. See the astropy-wheels Travis file for an example specifying NumPy build and test dependencies.

Here are some simple example projects:

Less simple projects where there are some serious build dependencies, and / or macOS / Linux differences:

Multibuild development

The main multibuild repository is always athttps://github.com/matthew-brett/multibuild

We try to keep the master branch stable and do testing and development in the devel branch. From time to time we merge devel into master.

In practice, you can check out the newest commit from devel that works for you, then stay at it until you need newer features.