|
| 1 | +# Setuptools plugin for Rust extensions support |
| 2 | + |
| 3 | +[](https://travis-ci.org/PyO3/setuptools-rust) |
| 4 | +[](https://badge.fury.io/py/setuptools-rust) |
| 5 | +[](https://github.com/ambv/black) |
| 6 | + |
| 7 | +** You might want to check out [pyo3-pack](https://github.com/PyO3/pyo3-pack), the successor to this project, which allows to develop, build and upload without any configuration ** |
| 8 | + |
| 9 | +Setuptools helpers for rust Python extensions implemented with [PyO3 |
| 10 | +python binding](https://github.com/PyO3/pyo3). |
| 11 | + |
| 12 | +Compile and distribute Python extensions written in rust as easily as if |
| 13 | +they were written in C. |
| 14 | + |
| 15 | +## Example |
| 16 | + |
| 17 | +For a complete example, see |
| 18 | +[word-count](https://github.com/PyO3/pyo3/tree/master/examples/word-count). |
| 19 | + |
| 20 | +### setup.py |
| 21 | + |
| 22 | +```python |
| 23 | +from setuptools import setup |
| 24 | +from setuptools_rust import Binding, RustExtension |
| 25 | + |
| 26 | +setup(name='hello-rust', |
| 27 | + version='1.0', |
| 28 | + rust_extensions=[RustExtension('hello_rust.helloworld', |
| 29 | + 'Cargo.toml', binding=Binding.PyO3)], |
| 30 | + packages=['hello_rust'], |
| 31 | + # rust extensions are not zip safe, just like C-extensions. |
| 32 | + zip_safe=False |
| 33 | +) |
| 34 | +``` |
| 35 | + |
| 36 | +You can use same commands as for c-extensions. For example: |
| 37 | + |
| 38 | +``` |
| 39 | +>>> python ./setup.py develop |
| 40 | +running develop |
| 41 | +running egg_info |
| 42 | +writing hello-rust.egg-info/PKG-INFO |
| 43 | +writing top-level names to hello_rust.egg-info/top_level.txt |
| 44 | +writing dependency_links to hello_rust.egg-info/dependency_links.txt |
| 45 | +reading manifest file 'hello_rust.egg-info/SOURCES.txt' |
| 46 | +writing manifest file 'hello_rust.egg-info/SOURCES.txt' |
| 47 | +running build_ext |
| 48 | +running build_rust |
| 49 | +cargo build --manifest-path extensions/Cargo.toml --features python3 |
| 50 | + Finished debug [unoptimized + debuginfo] target(s) in 0.0 secs |
| 51 | +
|
| 52 | +Creating /.../lib/python3.6/site-packages/hello_rust.egg-link (link to .) |
| 53 | +
|
| 54 | +Installed hello_rust |
| 55 | +Processing dependencies for hello_rust==1.0 |
| 56 | +Finished processing dependencies for hello_rust==1.0 |
| 57 | +``` |
| 58 | + |
| 59 | +Or you can use commands like bdist\_wheel (after installing wheel) or |
| 60 | +bdist\_egg. |
| 61 | + |
| 62 | +You can build manylinux1 binary wheels using Docker, e.g. with the |
| 63 | +build-wheels.sh |
| 64 | +script: |
| 65 | + |
| 66 | +```bash |
| 67 | +docker run --rm -v `pwd`:/io quay.io/pypa/manylinux1_x86_64 /io/build-wheels.sh |
| 68 | +``` |
| 69 | + |
| 70 | +## RustExtension |
| 71 | + |
| 72 | +You can define rust extension with RustExtension class: |
| 73 | + |
| 74 | +RustExtension(name, path, args=None, features=None, |
| 75 | +rust\_version=None, quiet=False, debug=False) |
| 76 | + |
| 77 | +The class for creating rust extensions. |
| 78 | + |
| 79 | + - param str name |
| 80 | + the full name of the extension, including any packages -- ie. |
| 81 | + *not* a filename or pathname, but Python dotted name. It is |
| 82 | + possible to specify multiple binaries, if extension uses |
| 83 | + Binsing.Exec binding mode. In that case first argument has to be |
| 84 | + dictionary. Keys of the dictionary corresponds to compiled rust |
| 85 | + binaries and values are full name of the executable inside python |
| 86 | + package. |
| 87 | + |
| 88 | + - param str path |
| 89 | + path to the Cargo.toml manifest file |
| 90 | + |
| 91 | + - param \[str\] args |
| 92 | + a list of extra argumenents to be passed to cargo. |
| 93 | + |
| 94 | + - param \[str\] features |
| 95 | + a list of features to also build |
| 96 | + |
| 97 | + - param \[str\] rustc\_flags |
| 98 | + A list of arguments to pass to rustc, e.g. cargo rustc --features |
| 99 | + \<features\> \<args\> -- \<rustc\_flags\> |
| 100 | + |
| 101 | + - param str rust\_version |
| 102 | + sematic version of rust compiler version -- for example |
| 103 | + *\>1.14,\<1.16*, default is None |
| 104 | + |
| 105 | + - param bool quiet |
| 106 | + Does not echo cargo's output. default is False |
| 107 | + |
| 108 | + - param bool debug |
| 109 | + Controls whether --debug or --release is passed to cargo. If set |
| 110 | + to None then build type is auto-detect. Inplace build is debug |
| 111 | + build otherwise release. Default: None |
| 112 | + |
| 113 | + - param int binding |
| 114 | + Controls which python binding is in use. Binding.PyO3 uses PyO3 |
| 115 | + Binding.RustCPython uses rust-cpython Binding.NoBinding uses no |
| 116 | + binding. Binding.Exec build executable. |
| 117 | + |
| 118 | + - param int strip |
| 119 | + Strip symbols from final file. Does nothing for debug build. |
| 120 | + Strip.No - do not strip symbols (default) Strip.Debug - strip |
| 121 | + debug symbols Strip.All - strip all symbols |
| 122 | + |
| 123 | + - param bool script |
| 124 | + Generate console script for executable if Binding.Exec is used. |
| 125 | + |
| 126 | + - param bool native |
| 127 | + Build extension or executable with "-C target-cpu=native" |
| 128 | + |
| 129 | + - param bool optional |
| 130 | + if it is true, a build failure in the extension will not abort the |
| 131 | + build process, but instead simply not install the failing |
| 132 | + extension. |
| 133 | + |
| 134 | +## Commands |
| 135 | + |
| 136 | + - build - Standard build command builds all rust extensions. |
| 137 | + - build\_rust - Command builds all rust extensions. |
| 138 | + - clean - Standard clean command executes cargo clean for all rust |
| 139 | + extensions. |
| 140 | + - check - Standard check command executes cargo check for all rust |
| 141 | + extensions. |
| 142 | + - tomlgen\_rust - Automatically generate a Cargo.toml manifest based |
| 143 | + on Python package metadata. See the [example |
| 144 | + project](https://github.com/PyO3/setuptools-rust/tree/master/example_tomlgen) |
| 145 | + on GitHub for more information about this command. |
0 commit comments