Skip to main content

Automatic FFI bindings generator for polyglot projects

Project description

Polyglot FFI

Automatic FFI bindings generator for polyglot projects

PyPI version Python Build codecov Documentation

Stop writing FFI boilerplate. Start building amazing things.

Full Documentation | Quickstart | API Reference | Type Mapping

What is Polyglot FFI?

Polyglot FFI automatically generates complete Foreign Function Interface (FFI) bindings between programming languages. Write your OCaml interface once, and get type-safe, memory-safe bindings for Python (and soon Rust, Go, etc.) instantly.

The Problem

Building multi-language projects requires writing:

  • 50+ lines of OCaml ctypes boilerplate
  • 30+ lines of C stubs with tricky memory management
  • 20+ lines of Python ctypes configuration
  • Plus: Dune configs, debugging, memory leaks...

The Solution

polyglot-ffi generate crypto.mli

Done! All 100+ lines generated automatically.

Why Polyglot FFI?

Zero Boilerplate - One command generates OCaml ctypes declarations, C wrappers, Python modules, build configs, type conversions, and error handling.

Type Safe - Preserves type information with Python type hints, OCaml type constraints, C type declarations, and compile-time checking.

Memory Safe - Proper memory management with CAMLparam/CAMLreturn macros, no memory leaks, and GC-safe conversions.

Quick Start

Initialize a new project:

polyglot-ffi init my-crypto-lib
cd my-crypto-lib

Note on project names: You can use hyphens in project names (like my-crypto-lib). The tool automatically converts them to underscores for generated code to ensure compatibility with OCaml, C, and Python naming requirements.

Write your OCaml interface:

(* src/my-crypto-lib.mli *)
val greet : string -> string
val add : int -> int -> int

Generate bindings:

polyglot-ffi generate src/my-crypto-lib.mli

Implement your OCaml functions:

(* src/my-crypto-lib.ml *)
let greet name = "Hello, " ^ name ^ "!"
let add x y = x + y

let () =
  Callback.register "greet" greet;
  Callback.register "add" add

Use from Python:

from generated.my_crypto_lib_py import greet, add

print(greet("World"))  # Hello, World!
print(add(2, 3))       # 5

Complex types supported: Records, variants (Result, Option), lists, tuples, and more. See the Type Mapping docs for details.

Installation

pip install polyglot-ffi

Verify installation:

polyglot-ffi --version

To upgrade:

pip install --upgrade polyglot-ffi

See the full installation guide for virtual environments, shell completion, and troubleshooting.

Prerequisites for Building Generated Bindings

If you want to build and use the generated OCaml bindings (not just generate them), you'll need:

# Install OCaml and required libraries
opam install dune ctypes ctypes-foreign

Note: polyglot-ffi itself is a Python tool and doesn't require OCaml. OCaml dependencies are only needed if you want to compile the generated bindings.

Features

  • Automatic Code Generation - One command generates OCaml ctypes, C wrappers, Python modules, and build configs
  • Rich Type Support - Primitives, records, variants (Result, Option), lists, tuples, and nested types
  • Type Safety - Full Python type hints and OCaml type preservation
  • Memory Safety - Proper GC integration, no memory leaks
  • Watch Mode - Auto-regenerate bindings on file changes
  • Project Validation - Built-in dependency and configuration checking
  • Zero Runtime Overhead - All generation happens at build time

Roadmap

  • Rust target support
  • Go target support
  • Bidirectional bindings (call Python from OCaml)
  • Plugin system for custom type mappings

Use Cases

  • Cryptography - OCaml for correctness, Python for integration
  • Data Processing - OCaml for logic, Python for data science
  • Financial Systems - OCaml for algorithms, Python for reporting
  • ML Infrastructure - OCaml for pipelines, Python for training

CLI Reference

polyglot-ffi init my-project              # Initialize new project
polyglot-ffi generate src/module.mli      # Generate bindings
polyglot-ffi watch                        # Auto-regenerate on changes
polyglot-ffi check                        # Validate configuration
polyglot-ffi clean                        # Remove generated files
polyglot-ffi --help                       # Get help

Run any command with --help for full options. See the CLI documentation for detailed usage.

Documentation

Contributing & Community

We welcome contributions! See CONTRIBUTING.md for development setup, testing requirements, and PR process. Look for good-first-issue labels to get started.

Get in touch:

License

MIT License - See LICENSE for details.

Acknowledgments

Built with inspiration from PyO3, OCaml-Ctypes, and SWIG.

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

polyglot_ffi-0.5.1.tar.gz (44.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

polyglot_ffi-0.5.1-py3-none-any.whl (50.8 kB view details)

Uploaded Python 3

File details

Details for the file polyglot_ffi-0.5.1.tar.gz.

File metadata

  • Download URL: polyglot_ffi-0.5.1.tar.gz
  • Upload date:
  • Size: 44.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for polyglot_ffi-0.5.1.tar.gz
Algorithm Hash digest
SHA256 d11f8637d0c0e8c8c5772fc880bb511ea2a2d2a5780c3f84934cbe16456a525b
MD5 82bccb59cbc6ddd7ead61abdfee3e480
BLAKE2b-256 1dc93cfd483e3562d5b58296f1a00b2aa154842805f7836176038d1181dab7dc

See more details on using hashes here.

Provenance

The following attestation bundles were made for polyglot_ffi-0.5.1.tar.gz:

Publisher: release.yml on chizy7/polyglot-ffi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file polyglot_ffi-0.5.1-py3-none-any.whl.

File metadata

  • Download URL: polyglot_ffi-0.5.1-py3-none-any.whl
  • Upload date:
  • Size: 50.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for polyglot_ffi-0.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c616336bd882cac66c4010f4a4c16f55f42a177c2c403fcce5a30cafaa467ca2
MD5 56f2557c6394045d33570802a5fd030d
BLAKE2b-256 105f5fdf31c3e5ecb4b573e6dfe0d213d12b35763107b7daf19350eb458c5f8a

See more details on using hashes here.

Provenance

The following attestation bundles were made for polyglot_ffi-0.5.1-py3-none-any.whl:

Publisher: release.yml on chizy7/polyglot-ffi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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