Downloading and building
Installation guide for the Python bindings.
Building of Python bindings is a similar process to building Magnum itself with an additional step involving Python setuptools. Minimal set of tools and libraries required for building is:
- C++ compiler with good C++11 support. Compilers which are tested to have everything needed are GCC >= 4.8.1, Clang >= 3.3 and MSVC >= 2015. On Windows you can also use MinGW-w64.
- CMake >= 3.1
- Corrade and Magnum installed as described in their docs
- Python >= 3.5 and pybind11
Prepared packages
ArchLinux packages
In package/archlinux
there is a development package, similar to the ones
in Magnum itself. They allow you to build and install the package directly from
the source tree.
git clone https://github.com/mosra/magnum-bindings && cd magnum-bindings cd package/archlinux makepkg -fp PKGBUILD
The PKGBUILD also contains a check()
function which will run all unit
tests before packaging. That might sometimes fail or take too long, pass
--nocheck
to makepkg
to skip that.
Once built, install the package using pacman
:
sudo pacman -U magnum-bindings-*.pkg.tar.zst
Homebrew formulas for macOS
macOS Homebrew formulas building the latest Git revision
are in the package/homebrew
directory. Either use the *.rb
files
directly or use the tap at https://github.com/mosra/homebrew-magnum. This will
install the latest stable version of Magnum Bindings with all its dependencies:
brew install mosra/magnum/magnum-bindings
But often you may want to install the latest Git revision of all Magnum projects instead:
brew install --HEAD mosra/magnum/corrade brew install --HEAD mosra/magnum/magnum brew install --HEAD mosra/magnum/magnum-bindings # If already installed, use the following to upgrade, in the same order brew upgrade --fetch-HEAD mosra/magnum/corrade brew upgrade --fetch-HEAD mosra/magnum/magnum brew upgrade --fetch-HEAD mosra/magnum/magnum-bindings
When installing from the *.rb
files you need to install the
Corrade and
Magnum Homebrew packages first. If you want to
pass additional flags to CMake or setup.py
or enable / disable additional
features, edit the *.rb
file.
There are also Homebrew packages for Magnum Plugins, Magnum Integration, Magnum Extras and Magnum Examples.
Manual build
The source is available on GitHub at https://github.com/mosra/magnum-bindings. Clone the repository with your favorite IDE or Git GUI, download currrent snapshot as a compressed archive or use the command line:
git clone https://github.com/mosra/magnum-bindings.git
Assuming a Unix-based OS, the first step is to build the native libraries. The
bindings will be generated for all Corrade and Magnum libraries that are found,
ignoring the ones which aren’t. If Corrade, Magnum and pybind11 are not in a
default location known to CMake, add their path to CMAKE_PREFIX_PATH
.
mkdir build && cd build cmake .. \ -DMAGNUM_WITH_PYTHON=ON make
Note that pybind11 compilation is quite time- and memory-hungry, so you might
not want to run the build on all cores on memory-constrained systems. In the
build directory, CMake will create the desired Python package layout, meaning
the bindings can be used directly if you cd
into build/src/python/magnum
.
For installing into a system-wide location, CMake generates a setup.py
containing location of all built libraries for use with Python setuptools:
cd build/src/python/magnum python setup.py install # or python3, sudo might be needed
Static build
In case Corrade or Magnum is built with CORRADE_BUILD_STATIC / MAGNUM_BUILD_STATIC, the corresponding bindings are compiled into a single dynamic module instead of one module per Corrade/Magnum library.
In this case, similarly to linking static plugins to Magnum’s own command-line
utilities, you can use the MAGNUM_PYTHON_BINDINGS_STATIC_PLUGINS
CMake
variable to link static plugins to the Python module, assuming Magnum, Magnum
Plugins and Magnum Bindings are all CMake subprojects. It’s a
semicolon-separated list of existing CMake targets, for example
Magnum::AnyImageImporter;MagnumPlugins::StbImageImporter
.
On Unix platforms, Python by default loads the modules in isolated namespaces.
That’s a good thing to do from a security point of view, nevertheless with
static builds of Corrade and Magnum it will result in globals duplicated across
the Corrade and Magnum modules (and also any other Python modules that
use Magnum natively inside) unable to see each other in order to deduplicate
themselves, causing strange issues. To solve that, the
MAGNUM_BUILD_PYTHON_BINDINGS_RTLD_GLOBAL
CMake option, which is enabled by
default on static builds on Unix platforms, overrides Python’s module loading
code to not load them in isolated namespaces using sys.setdlopenflags().
The override then happens inside import corrade
and is in effect for the
rest of the interpreter lifetime, to affect also any potential other modules
depending on Magnum loaded later. See also
CORRADE_BUILD_STATIC_UNIQUE_GLOBALS and
MAGNUM_BUILD_STATIC_UNIQUE_GLOBALS in Corrade and Magnum for more
information.
Running unit tests
Essential functionality of the bindings is tested using Python’s builtin
unittest
module. The tests currently assume a CMake build directory with
all binaries already built located in a build/
directory in project root,
running them is then a matter of:
cd src/python/magnum python -m unittest
For code coverage, coverage.py is used.
Get it via pip
or as a system package.
pip install coverage # sudo might be needed
Running the unit tests with coverage enabled is then a matter of executing the
following commands, the resulting HTML overview is located in
htmlcov/index.html
:
cd src/python/magnum coverage run -m unittest coverage html
Continuous Integration
In package/ci/
there is a circleci.yml
file that compiles and tests the
bindings on Linux GCC 4.8 + CMake 3.5 and on macOS, online at
https://circleci.com/gh/mosra/magnum-bindings. For Windows there is an
appveyor.yml
testing on Windows with MSVC, online at
https://ci.appveyor.com/project/mosra/magnum-bindings. Code coverage for both
the C++ bindings code and Python side is reported to
https://codecov.io/gh/mosra/magnum-bindings.