Skip to content

Commit cbe486a

Browse files
authored
Merge pull request #29 from althonos/master
Find extensions and automatically generate Cargo.toml manifests
2 parents 2911759 + 45b0e53 commit cbe486a

File tree

13 files changed

+500
-10
lines changed

13 files changed

+500
-10
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ install:
2121
- pip install -e .
2222

2323
script:
24-
- cd example && python setup.py develop
24+
- cd example && python setup.py develop && cd ..
25+
- cd example_tomlgen && python setup.py tomlgen_rust -w && cd ..

CHANGES.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
CHANGES
22
^^^^^^^
33

4+
0.9.0 (2018-03-07)
5+
------------------
6+
7+
- Find inplace extensions and automatically generate ``Cargo.toml`` manifests #29
8+
9+
410
0.8.4 (2018-02-27)
511
------------------
612

example_tomlgen/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
build/
2+
Cargo.toml
3+
Cargo.lock

example_tomlgen/README.rst

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
`example_tomlgen`
2+
=================
3+
4+
An example extensions with automatically generated ``Cargo.toml`` manifest
5+
files. Simply run ``python setup.py tomlgen_rust`` to generate the following
6+
files:
7+
8+
* ``Cargo.toml`` for ``hello.english``
9+
10+
.. code-block:: toml
11+
12+
[package]
13+
name = "hello.english"
14+
version = "0.1.0"
15+
authors = ["Martin Larralde <[email protected]>"]
16+
publish = false
17+
18+
[lib]
19+
crate-type = ["cdylib"]
20+
name = "hello_english"
21+
path = "lib.rs"
22+
23+
[dependencies]
24+
pyo3 = { version = "*", features = ["extension-module"] }
25+
english-lint = "*"
26+
27+
28+
* ``Cargo.toml`` for ``hello.french``
29+
30+
.. code-block:: toml
31+
32+
[package]
33+
name = "hello.french"
34+
version = "0.1.0"
35+
authors = ["Martin Larralde <[email protected]>"]
36+
publish = false
37+
38+
[lib]
39+
crate-type = ["cdylib"]
40+
name = "hello_french"
41+
path = "lib.rs"
42+
43+
[dependencies]
44+
pyo3 = { version = "*", features = ["extension-module"] }
45+
46+
47+
Metadata
48+
--------
49+
50+
The package name will be generated from the position of the extension within
51+
the Python package. The same version is used as the one declared in ``setup.py``
52+
or ``setup.cfg``.
53+
54+
The authors list is generated after the ``author`` and ``author_email`` options
55+
from ``setup.py`` / ``setup.cfg``, but can also be overriden using the
56+
``authors`` key in the ``[tomlgen_rust]`` section of ``setup.cfg``:
57+
58+
.. code-block:: ini
59+
60+
[tomlgen_rust]
61+
authors =
62+
Jane Doe <[email protected]>
63+
John Doe <[email protected]>
64+
65+
The library name is a slugified variant of the extension package name, to
66+
avoid name collisions within the build directory.
67+
68+
As a safety, ``publish = false`` is added to the ``[package]`` section
69+
(you wouldn't publish an automatically generated package, *would you ?!*).
70+
71+
72+
Options
73+
-------
74+
75+
Use ``--force`` (or add ``force = true`` to the ``[tomlgen_rust]`` section of
76+
``setup.cfg``) to force generating a manifest even when one already exists.
77+
78+
Use ``--create-workspace`` to create a virtual manifest at the root of your
79+
project (next to the ``setup.py`` file) which registers all of the extensions.
80+
This way, generic ``cargo`` commands can be run without leaving the root of
81+
the project.
82+
83+
If ``--create-workspace`` is enable, a `.cargo/config` file will also be
84+
created to force ``cargo`` to build to the temporary build directory. Use
85+
``--no-config`` to disable.
86+
87+
88+
Dependencies
89+
------------
90+
91+
To specify dependencies for all extensions, add them to the
92+
``[tomlgen_rust.dependencies]`` section of your setuptools configuration file
93+
(``setup.cfg``), as you would normally in your ``Cargo.toml`` file. Here is
94+
probably a good place to add ``pyo3`` as a dependency.
95+
96+
To specify per-extension dependency, create a section for each extension
97+
(``[tomlgen_rust.dependencies.<DOTTEDPATH>]``, where ``<DOTTEDPATH>`` is the
98+
complete Python path to the extension (e.g. ``hello.english``). Extension
99+
specific dependencies are added *after* global dependencies.
100+
101+
*Note that, since all projects are built in the same directory, you can also
102+
declare all dependencies in the* ``[tomlgen_rust.dependencies]``, *as they will
103+
be built only once anyway*.
104+
105+
106+
Automatic generation at each build
107+
----------------------------------
108+
109+
If you intend to regenerate manifests everytime the library is built, you can
110+
add ``Cargo.toml`` and ``Cargo.lock`` to your ``.gitignore`` file.
111+
112+
Then, make sure ``tomlgen_rust`` is run before ``build_rust`` everytime by
113+
adding aliases to your ``setup.cfg`` file:
114+
115+
.. code-block:: ini
116+
117+
[aliases]
118+
build_rust = tomlgen_rust -f build_rust
119+
clean_rust = tomlgen_rust -f clean_rust
120+
build = tomlgen_rust -f build
121+
clean = clean_rust -f clean

example_tomlgen/hello/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# coding: utf-8
2+
3+
from . import french
4+
from . import english

example_tomlgen/hello/english/lib.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#![feature(proc_macro)]
2+
3+
extern crate pyo3;
4+
5+
use pyo3::prelude::*;
6+
7+
/// Module documentation string
8+
#[py::modinit(english)]
9+
fn init(py: Python, m: &PyModule) -> PyResult<()> {
10+
11+
#[pyfn(m, "hello")]
12+
fn hello(_py: Python) -> PyResult<()> {
13+
println!("Hello, world!");
14+
Ok(())
15+
}
16+
17+
Ok(())
18+
}

example_tomlgen/hello/french/lib.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#![feature(proc_macro)]
2+
3+
extern crate pyo3;
4+
5+
use pyo3::prelude::*;
6+
7+
/// Module documentation string
8+
#[py::modinit(french)]
9+
fn init(py: Python, m: &PyModule) -> PyResult<()> {
10+
11+
#[pyfn(m, "hello")]
12+
fn hello(_py: Python) -> PyResult<()> {
13+
println!("Bonjour, monde!");
14+
Ok(())
15+
}
16+
17+
Ok(())
18+
}

example_tomlgen/setup.cfg

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[metadata]
2+
author_email = "[email protected]"
3+
description = Example Python/Rust project with automatic Cargo.toml generation
4+
long-description = file: README.rst
5+
6+
[options]
7+
zip_safe = false
8+
9+
[tomlgen_rust]
10+
create_workspace = true
11+
12+
[tomlgen_rust.dependencies]
13+
pyo3 = { version = "*", features = ["extension-module"] }
14+
15+
[tomlgen_rust.dependencies.hello.english]
16+
english-lint = "*"
17+
18+
[aliases]
19+
build_rust = tomlgen_rust build_rust
20+
clean_rust = tomlgen_rust clean_rust
21+
build = tomlgen_rust build
22+
clean = clean_rust clean

example_tomlgen/setup.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env python
2+
# coding: utf-8
3+
4+
import setuptools
5+
import setuptools_rust as rust
6+
7+
8+
setuptools.setup(
9+
name='hello-rust',
10+
version='0.1.0',
11+
author="Martin Larralde",
12+
13+
# Find all inplace extensions
14+
rust_extensions=rust.find_rust_extensions(
15+
binding=rust.Binding.PyO3,
16+
strip=rust.Strip.Debug,
17+
),
18+
19+
# specify setup dependencies
20+
setup_requires=[
21+
'setuptools',
22+
'setuptools_rust',
23+
],
24+
25+
# rust extensions are not zip safe, just like C-extensions.
26+
zip_safe=False
27+
)

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,6 @@
3838
build_ext=setuptools_rust:build_ext
3939
build_rust=setuptools_rust:build_rust
4040
test_rust=setuptools_rust:test_rust
41+
tomlgen_rust=setuptools_rust:tomlgen_rust
4142
"""
4243
)

0 commit comments

Comments
 (0)