rules_pyo3

Rules

Functions

pyo3_toolchain

load("@rules_pyo3//pyo3:defs.bzl", "pyo3_toolchain")

pyo3_toolchain(name)

Define a toolchain which generates config data for the PyO3 for producing extension modules on any target platform.

Note that this toolchain expects the pyo3 crate to be built with the following features:

When using rules_rust's crate_universe, this data can be plubmed into the target using the following snippet.

annotations = {
    "pyo3-build-config": [
        crate.annotation(
            build_script_data = [
                "@rules_pyo3//pyo3:current_pyo3_toolchain",
            ],
            build_script_env = {
                "PYO3_CROSS": "$(PYO3_CROSS)",
                "PYO3_CROSS_LIB_DIR": "$(PYO3_CROSS_LIB_DIR)",
                "PYO3_CROSS_PYTHON_IMPLEMENTATION": "$(PYO3_CROSS_PYTHON_IMPLEMENTATION)",
                "PYO3_CROSS_PYTHON_VERSION": "$(PYO3_CROSS_PYTHON_VERSION)",
                "PYO3_NO_PYTHON": "$(PYO3_NO_PYTHON)",
                "PYO3_PYTHON": "$(PYO3_PYTHON)",
            },
            build_script_toolchains = [
                "@rules_pyo3//pyo3:current_pyo3_toolchain",
            ],
        ),
    ],
    "pyo3-ffi": [
        crate.annotation(
            build_script_data = [
                "@rules_pyo3//pyo3:current_pyo3_toolchain",
            ],
            build_script_env = {
                "PYO3_CROSS": "$(PYO3_CROSS)",
                "PYO3_CROSS_LIB_DIR": "$(PYO3_CROSS_LIB_DIR)",
                "PYO3_CROSS_PYTHON_IMPLEMENTATION": "$(PYO3_CROSS_PYTHON_IMPLEMENTATION)",
                "PYO3_CROSS_PYTHON_VERSION": "$(PYO3_CROSS_PYTHON_VERSION)",
                "PYO3_NO_PYTHON": "$(PYO3_NO_PYTHON)",
                "PYO3_PYTHON": "$(PYO3_PYTHON)",
            },
            build_script_toolchains = [
                "@rules_pyo3//pyo3:current_pyo3_toolchain",
            ],
        ),
    ],
},

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired

rust_pyo3_toolchain

load("@rules_pyo3//pyo3:defs.bzl", "rust_pyo3_toolchain")

rust_pyo3_toolchain(name, pyo3)

Define a toolchain for PyO3 Rust dependencies which power internal rules.

This toolchain is how the rules know which version of pyo3 to link against.

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
pyo3The PyO3 library.Labelrequired

pyo3_extension

load("@rules_pyo3//pyo3:defs.bzl", "pyo3_extension")

pyo3_extension(name, srcs, aliases, compile_data, crate_features, crate_root, data, deps, edition,
               imports, proc_macro_deps, rustc_env, rustc_env_files, rustc_flags, version,
               compilation_mode, **kwargs)

Define a PyO3 python extension module.

This target is consumed just as a py_library would be.

PARAMETERS

NameDescriptionDefault Value
nameThe name of the target.none
srcsList of Rust .rs source files used to build the library. For more details see rust_shared_library.none
aliasesRemap crates to a new name or moniker for linkage to this target. For more details see rust_shared_library.{}
compile_dataList of files used by this rule at compile time. For more details see rust_shared_library.[]
crate_featuresList of features to enable for this crate. For more details see rust_shared_library.[]
crate_rootThe file that will be passed to rustc to be used for building this crate. For more details see rust_shared_library.None
dataList of files used by this rule at compile time and runtime. For more details see rust_shared_library.[]
depsList of other libraries to be linked to this library target. For more details see rust_shared_library.[]
editionThe rust edition to use for this crate. Defaults to the edition specified in the rust_toolchain. For more details see rust_shared_library.None
importsList of import directories to be added to the PYTHONPATH. For more details see py_library.imports.[]
proc_macro_depsList of rust_proc_macro targets used to help build this library target. For more details see rust_shared_library.[]
rustc_envDictionary of additional "key": "value" environment variables to set for rustc. For more details see rust_shared_library.{}
rustc_env_filesFiles containing additional environment variables to set for rustc. For more details see rust_shared_library.[]
rustc_flagsList of compiler flags passed to rustc. For more details see rust_shared_library.[]
versionA version to inject in the cargo environment variable. For more details see rust_shared_library.None
compilation_modeThe compilation_mode value to build the extension for. If set to "current", the current configuration will be used."opt"
kwargsAdditional keyword arguments.none