Skip to main content

build_ext for Pyodide

Project description

exodide: build_ext for Pyodide

PyPI

1. Overview

Warning This project is still under development.

Pyodide is a WebAssembly variant of CPython. By using it, we can run Python code inside web browser.

Although we can run most of pure-Python packages on Pyodide, however, available C/C++-extension packages are limited to builtin packages.

The motivation of this project (exodide) is providing C/C++-extension builder for Pyodide, and enables users to run your own custom C/C++-extension packages on Pyodide.

2. Usage

2.1 Requirement Summary

  • 2.3: Build
  • 2.4: Run
    • Pyodide v0.21.0a2
  • 2.5: Inspect

2.2 Install exodide

You can install exodide from PyPI

  • pip install exodide
    • Only exodide
  • pip install exodide[build]
    • With wheel
  • pip install exodide[inspect]
    • With NumPy
  • pip install exodide[all]
    • With NumPy and wheel

Since this repository doesn't contains patched headers, you cannot install directly from GitHub like pip install git+https://github.com/ymd-h/exodide. Please read 4. Build exodide.

2.3 Build with exodide

The followings are reuired;

  • Emscripten v3.1.13
  • C/C++ extension package source working on Python 3.10
  • wheel (pip install exodide[build] install it, too.)

2.3.1 Method 1: Main Usage

from setuptools import setup
from exodide import build

# omit

setup(
    # omit
    cmdclass=build.cmdclass(), # {'build': build.build, 'build_ext': build.build_ext}
)

then CC=emcc CXX=em++ python setup.py bdist_wheel.

2.3.2 Method 2: Fine tuning for Power User

If your package has special build flow, you might need to call exodide internal API.

All internal buil API are implemented at exodide.build module.

  • system_include() -> str: System include directory of host Python
  • exodide_include() -> List[str]: Include directories inside exodide package.
  • adjust_include(include: List[str]) -> List[str]: Adjust include directories
    • Internally system_include() and exodide_include() are used
  • exodide_links() -> List[str]: Linker arguments
  • exodide_unsupported_links() -> List[str]: Unsupported linker arguments
  • exodide_platform_tag() -> str: Platform name tag for wheel

2.3.3 Method 3: Quick Usage

Under Consideration

2.3.4 Notes

Pyodide doesn't provide all the functionalities of CPython, so that you might need to modify your package. You can detect Emscripten compiler by __EMSCRIPTEN__ macro (ref).

#ifdef __EMSCRIPTEN__
// Code for Pyodide
#else
// Code for Others
#endif

2.4 Install extension package to Pyodide

Warning exodide doesn't check any wheel compatibility, so that user must take care.

Pyodide v0.21a2 is required.

const pyodide = await loadPyodide();

await pyodide.runPythonAsync(`
import micropip
micropip.install("exodide")

from exodide.install import fetch_install

await fetch_install("example.com/your-package.whl")

import your_package
# omit
`);

2.5 Inspect Shared Object (for Debugging)

Fot inspect, numpy is also required, and you can install with pip install exodide[inspect].

python3 -m exodide.inspect your-package/your-module.so

Currently, exodide.inspect module prints dylink / dylink.0 custom section, which are used for metadata of Wasm dynamic link.

3. LICENSEs

We utilize other projects and these codes obey their original lisences. We distribute patched header files of CPython and NumPy, too.

4. Build exodide

git clone --recursive --depth 1 https://github.com/ymd-h/exodide.git
cd exodide

make

pip install .

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

exodide-0.0.5-py3-none-any.whl (349.9 kB view hashes)

Uploaded Python 3

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