一个扩展setuptools以支持二进制扩展的Python库。
项目描述
Milksnake
Milksnake是setuptools的一个扩展,允许您以最便携的方式在Python wheel中分发动态链接库。
它提供了一个钩子来调用您的构建过程,然后获取生成的动态链接库。
为什么?
已经存在其他项目可以让Python和本地库协同工作,但这个项目是不同的。与其他构建Python扩展模块的项目不同,此项目的目标是构建常规的本地库,然后在运行时通过CFFI加载。为什么不直接使用CFFI?因为CFFI的setuptools支持本身无法正确处理此类wheel(它不提供构建和正确标记共享库wheel的方式),并且它不提供调用外部构建过程(如makefile、cargo构建rust二进制文件等)的好方法。
特别是,您可能只需要两个Linux wheel,一个mac wheel,很快还将有一个Windows wheel,无论您想要针对多少个Python解释器。
支持什么?
- 平台:Linux、Mac、Windows
- setuptools命令:
bdist_wheel
、build
、build_ext
、develop
pip install --editable .
- 通用wheel(
PACKAGE-py2.py3-none-PLATFORM.whl
);如果包还包含一些链接到libpython的东西,可以在setup()
中使用milksnake_universal=False
来禁用它。
如何?
此示例展示了如何使用它构建一个rust项目
这是一个setup.py
文件的样子
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
]
)
然后您需要一个包含Rust库(crate类型为cdylib
)和example/
python包的rust/
文件夹。
示例example/__init__.py
文件
from example._native import ffi, lib
def test():
return lib.a_function_from_rust()
以及rust/src/lib.rs
#[no_mangle]
pub unsafe extern "C" fn a_function_from_rust() -> i32 {
42
}
以及rust/Cargo.toml
[package]
name = "example"
version = "0.1.0"
build = "build.rs"
[lib]
name = "example"
crate-type = ["cdylib"]
[build-dependencies]
cbindgen = "0.4"
最后是build.rs文件
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");
}
项目详情
下载文件
下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。
源代码分发
milksnake-0.1.6.tar.gz (10.5 kB 查看哈希值)
构建分发
milksnake-0.1.6-py2.py3-none-any.whl (11.1 kB 查看哈希值)
关闭
milksnake-0.1.6.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 0198f8932b4e136c29c0d0d490ff1bac03f82c3a7b2ee6f666e3683b64314fd9 |
|
MD5 | 58e06ff52117122412efb579da3e9803 |
|
BLAKE2b-256 | 379c100deced3999e748500dda3027e2a19b0074199ba27cdc5a6988d22919b8 |
关闭
milksnake-0.1.6-py2.py3-none-any.whl 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 31e3eafaf2a48e177bb4b2dacef2c7ae8c5b2147a19c6d626209b819490e6f1d |
|
MD5 | c375b5c0a964b07406c883f2f19cf21d |
|
BLAKE2b-256 | 6d5b1688cbd7244f039a2c1a762e246f04f7fc3eff07932776ac9944da3ea208 |