Skip to main content

A python library that extends setuptools for binary extensions.

Project description

Milksnake

Milksnake is an extension for setuptools that allows you to distribute dynamic linked libraries in Python wheels in the most portable way imaginable.

It gives you a hook to invoke your own build process and to then take the resulting dynamic linked library.

Why?

There are already other projects that make Python and native libraries play along but this one is different. Unlike other projects that build Python extension modules the goal of this project is to build regular native libraries that are then loaded with CFFI at runtime. Why not just use CFFI? Because CFFI's setuptools support alone does not properly work with such wheels (it does not provide a way to build and properly tag wheels for shared libraries) and it does not provide a good way to invoke an external build process (like a makefile, cargo to build rust binaries etc.)

In particular you will most likely only need two wheels for Linux, one for macs and soon one for Windows independently of how many Python interpreters you want to target.

What is supported?

  • Platforms: Linux, Mac, Windows
  • setuptools commands: bdist_wheel, build, build_ext, develop
  • pip install --editable .
  • Universal wheels (PACKAGE-py2.py3-none-PLATFORM.whl); this can be disabled with milksnake_universal=False in setup() in case the package also contains stuff that does link against libpython.

How?

This example shows how to build a rust project with it:

This is what a setup.py file looks like:

from setuptools import setup

def build_native(spec):
    # build an example rust library
    build = spec.add_external_build(
        cmd=['cargo', 'build', '--release'],
        path='./rust'
    )

    spec.add_cffi_module(
        module_path='example._native',
        dylib=lambda: build.find_dylib('example', in_path='target/release'),
        header_filename=lambda: build.find_header('example.h', in_path='target'),
        rtld_flags=['NOW', 'NODELETE']
    )

setup(
    name='example',
    version='0.0.1',
    packages=['example'],
    zip_safe=False,
    platforms='any',
    setup_requires=['milksnake'],
    install_requires=['milksnake'],
    milksnake_tasks=[
        build_native
    ]
)

You then need a rust/ folder that has a Rust library (with a crate type of cdylib) and a example/ python package.

Example example/__init__.py file:

from example._native import ffi, lib


def test():
    return lib.a_function_from_rust()

And a rust/src/lib.rs:

#[no_mangle]
pub unsafe extern "C" fn a_function_from_rust() -> i32 {
    42
}

And the rust/Cargo.toml:

[package]
name = "example"
version = "0.1.0"
build = "build.rs"

[lib]
name = "example"
crate-type = ["cdylib"]

[build-dependencies]
cbindgen = "0.19"

And finally the build.rs file:

extern crate cbindgen;

use std::env;

fn main() {
    let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
    let mut config: cbindgen::Config = Default::default();
    config.language = cbindgen::Language::C;
    cbindgen::generate_with_config(&crate_dir, config)
      .unwrap()
      .write_to_file("target/example.h");
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyromilksnakex-0.1.8.tar.gz (9.7 kB view details)

Uploaded Source

Built Distribution

pyromilksnakex-0.1.8-py2.py3-none-any.whl (11.0 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file pyromilksnakex-0.1.8.tar.gz.

File metadata

  • Download URL: pyromilksnakex-0.1.8.tar.gz
  • Upload date:
  • Size: 9.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.1

File hashes

Hashes for pyromilksnakex-0.1.8.tar.gz
Algorithm Hash digest
SHA256 cc68e15a8907675d0e3875d517733611d216b28074806c4d932602cfa943288e
MD5 57c0d8794d6f015eb4a501d8c3182816
BLAKE2b-256 e1876d0209eae397c62af3406ea364f7acad382d8b8faf137b7eb5614809fc37

See more details on using hashes here.

File details

Details for the file pyromilksnakex-0.1.8-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for pyromilksnakex-0.1.8-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 b1523e0a72e64c6ce3b95492de9f2768df82fdf7be5024319d30c41240948d7c
MD5 67e1ab77c0ee5bff716b07a6cf751ed8
BLAKE2b-256 cf887068bf261fad217888ef0c6652812ac6fdc2d133cdb58334ad4070a557c2

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page