Skip to content

Commit 4f62bc0

Browse files
committed
Metadata in pyproject.toml
1 parent d80ba5b commit 4f62bc0

File tree

5 files changed

+95
-43
lines changed

5 files changed

+95
-43
lines changed
File renamed without changes.
File renamed without changes.

README.md

Lines changed: 53 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,110 @@
11
# Python C++ extension
2+
23
A template for a standalone C++ library with dependencies managed by
34
[vcpkg](https://github.com/microsoft/vcpkg) accessible through Python using
45
[pybind11](https://github.com/pybind/pybind11).
56

67
## Why should I use this template?
7-
- You want to write a C++ library that can be accessed through Python.
8-
- You want to use `cmake` to build your C++ code.
9-
- You want to use `pybind11` to expose your C++ library as a Python module.
10-
- You want to use some C++ dependencies and manage them with `vcpkg`. Otherwise
8+
9+
* You want to write a C++ library that can be accessed through Python.
10+
* You want to use `cmake` to build your C++ code.
11+
* You want to use `pybind11` to expose your C++ library as a Python module.
12+
* You want to use some C++ dependencies and manage them with `vcpkg`. Otherwise
1113
you should check other [scikit-build sample
1214
projects](https://github.com/scikit-build/scikit-build-sample-projects).
13-
- You are not specially concerned about build and install optimizations, it is
15+
* You are not specially concerned about build and install optimizations, it is
1416
not a problem if they are long running.
1517

1618
If you want to distribute your extension using `pip` or `conda` and you mind
1719
that your users take a long time to install it, then it might be better to
1820
distribute some built binaries instead of optimizing the build process. This
1921
template might still be useful for you as it has a
20-
[release](.github/workflows/release.yml) workflow for building python wheels
21-
with [cibuildwheel](https://github.com/pypa/cibuildwheel) and distributing them
22-
to [PyPI](https://pypi.org/).
22+
[CD](.github/workflows/cd.yml) workflow for building python wheels
23+
with [cibuildwheel](https://github.com/pypa/cibuildwheel) and uploading them to
24+
[PyPI](https://pypi.org/).
2325

2426
## Example usage
2527

2628
### Create a clean Python virtual environment
29+
2730
```
2831
python -m venv venv
2932
```
33+
3034
Activate it on Windows
35+
3136
```
3237
.\venv\Scripts\activate
3338
```
39+
3440
otherwise
41+
3542
```
3643
source ./venv/bin/activate
3744
```
3845

3946
### Install this project
47+
4048
```
4149
pip install git+https://github.com/esdandreu/python-extension-cpp
4250
```
43-
It will take a while to build as it will build the C++ dependencies as well,
51+
52+
It will take a while to build as it will build the C++ dependencies as well,
4453
but it will work. It is definitely not the most optimal way of installing a
4554
package as we are installing as well the `vcpkg` package manager and building
4655
from source dependencies that might as well be installed on the system. But
4756
this allows a fast development environment where adding or removing C++
4857
dependencies should be easy.
4958

50-
Alternatively, you can install the package from the binaries distributed in [PyPI](https://pypi.org/project/example-python-extension-cpp/).
59+
Alternatively, you can install the package from the binaries distributed in
60+
[PyPI](https://pypi.org/project/example-python-extension-cpp/) using the
61+
[continuous deployment](#cicd) workflow.
62+
5163
```
5264
pip install example-python-extension-cpp
5365
```
5466

5567
### Test that the C++ code is working in the Python package
68+
5669
Our simple project contains a `add` function that adds two numbers together.
70+
5771
```
5872
python -c "import my_python_api; print(my_python_api.add(1, 2))"
5973
```
6074

6175
It also makes use of the C++ library
6276
[fftw3](https://github.com/FFTW/fftw3.git) that is available through `vcpkg`
77+
6378
in order to perform a Fast Fourier Transform over a generated signal, printing
6479
its results.
80+
6581
```
6682
python -c "import my_python_api; my_python_api.hello_fft()"
6783
```
6884

6985
## Setup
86+
7087
### Install the requirements
7188
Install [vcpkg](https://github.com/microsoft/vcpkg) requirements with the
7289
addition of `cmake` and Python. It could be summarized as:
73-
- [git](https://git-scm.com/downloads)
74-
- Build tools ([Visual
90+
* [git](https://git-scm.com/downloads)
91+
* Build tools ([Visual
7592
Studio](https://docs.microsoft.com/en-us/visualstudio/install/install-visual-studio)
7693
on Windows or `gcc` on Linux for example)
77-
- [cmake](#cmake)
78-
- Python. Make sure to have development tools installed (`python3.X-dev` on
94+
* [cmake](#cmake)
95+
* Python. Make sure to have development tools installed (`python3.X-dev` on
7996
Linux, being `X` your version of Python).
8097

8198
If running on a clean linux environment (like a container or Windows Subsystem
8299
for Linux) you will need to install some additional tools as it is stated in
83-
`vcpkg`.
100+
`vcpkg` .
101+
84102
```
85103
sudo apt-get install build-essential curl zip unzip tar pkg-config libssl-dev python3-dev
86104
```
105+
87106
#### CMake
107+
88108
Follow the [official instructions](https://cmake.org/install/).
89109

90110
The required `cmake` version is quite high, if you are using a Linux
@@ -95,10 +115,11 @@ line](https://askubuntu.com/a/865294).
95115

96116
Make sure that when you run `cmake --version` the output is `3.21` or higher.
97117
The reason for this is that we are using some of the `3.21` features to install
98-
runtime dependencies (managed with `vcpkg`) together with our project so they
118+
runtime dependencies (managed with `vcpkg` ) together with our project so they
99119
are available to Python when using its API.
100120

101121
#### Formatters
122+
102123
This project uses `clang-format` to format the C++ code. There is a
103124
`.clang-format` file with options that I personally like. Download
104125
`clang-format` as part of `LLVM` from the official [release
@@ -109,40 +130,48 @@ I als recommend using `yapf` to format python code.
109130
### Clone this repository with `vcpkg`
110131

111132
Cone this repository with `vcpkg` as a submodule and navigate into it.
133+
112134
```
113135
git clone --recursive git@github.com:esdandreu/python-extension-cpp.git
114136
cd python-extension-cpp
115137
```
116138

117139
Bootstrap `vcpkg` in Windows. Make sure you have [installed the
118140
prerequisites](https://github.com/microsoft/vcpkg).
141+
119142
```
120143
.\vcpkg\bootstrap-vcpkg.bat
121144
```
122145

123146
Or in Linux/MacOS. Make sure you have [installed developer
124147
tools](https://github.com/microsoft/vcpkg)
148+
125149
```
126150
./vcpkg/bootstrap-vcpkg.sh
127151
```
128152

129153
## Building
130154

131155
### Build locally with CMake
156+
132157
Navigate to the root of the repository and create a build directory.
158+
133159
```
134160
mkdir build
135161
```
136162

137-
Configure `cmake` to use `vcpkg`.
163+
Configure `cmake` to use `vcpkg` .
164+
138165
```
139166
cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE="$pwd/vcpkg/scripts/buildsystems/vcpkg.cmake"
140167
```
141168

142169
Build the project.
170+
143171
```
144172
cmake --build build
145173
```
174+
146175
### Build locally with Python
147176

148177
It is recommended to use a [clean virtual
@@ -158,11 +187,11 @@ pip install scikit-build git
158187

159188
Install the repository. By adding `[test]` to our install command we can
160189
install additionally the test dependencies.
190+
161191
```
162192
pip install .[test]
163193
```
164194

165-
166195
## Testing
167196

168197
### Test the C++ library with Google Test
@@ -180,16 +209,16 @@ pytest
180209
## CI/CD
181210

182211
This template contains a continuous integration workflow that builds and tests
183-
the C++ library and the python extension
184-
[test.yml](.github/workflows/test.yml).
212+
the C++ library and the python extension [ci.yml](.github/workflows/ci.yml).
185213

186214
It also contains a continuous deployment workflow that builds wheels and source
187215
distributions for the python extension, then creates a github release with it
188216
and uploads it to [PyPI](https://pypi.org/):
189-
[release.yml](.github/workflows/release.yml). That workflow requires a
190-
repository secret named `PYPI_TOKEN` with a PyPI API token. is activated when
191-
pushing a version tag to the repository:
217+
[cd.yml](.github/workflows/cd.yml). That workflow requires a repository secret
218+
named `PYPI_TOKEN` with a PyPI API token. is activated when pushing a version
219+
tag to the repository:
220+
192221
```
193222
git tag -a v0.0.1 -m "First release"
194223
git push origin --tags
195-
```
224+
```

pyproject.toml

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,32 @@ requires = [
77
"pybind11",
88
"GitPython",
99
]
10-
1110
build-backend = "setuptools.build_meta"
1211

12+
[project]
13+
# https://setuptools.pypa.io/en/latest/userguide/pyproject_config.html
14+
# https://peps.python.org/pep-0621/
15+
name = "example-python-extension-cpp"
16+
authors = [
17+
{ name = "Andreu Gimenez", email = "esdandreu@gmail.com" },
18+
]
19+
description = "A minimal C++ extension using pybind11 and vcpkg"
20+
readme = "README.md"
21+
license = {text = "BSD 3-Clause License"}
22+
requires-python = ">=3.7"
23+
keywords = ["example", "vcpkg", "pybind11", "scikit-build", "cpp"]
24+
dynamic = ["version"]
25+
26+
[project.urls]
27+
homepage = "https://github.com/esdandreu/python-extension-cpp"
28+
repository = "https://github.com/esdandreu/python-extension-cpp"
29+
30+
[project.optional-dependencies]
31+
# When adding `[test]` to your `pip install` command you can install the extra
32+
# dependencies associated with testing. Example `pip install .[test]`
33+
# https://setuptools.pypa.io/en/latest/userguide/dependency_management.html#optional-dependencies
34+
test = ["pytest"]
35+
1336
[tool.pytest.ini_options]
1437
minversion = "6.0"
1538
testpaths = [

setup.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
"""Build setup
2+
3+
Setup the build environment for a C++ python extension module with `vcpkg` as
4+
C++ package manager. It uses `scikit-build` to build the C++ extension module
5+
and `GitPython` to pull `vcpkg` when it is defined as a submodule (as it is
6+
recommended).
7+
8+
It is recommended to set the package metadata in the file `pyproject.toml`
9+
minimizing the amount of package specific configuration in this file.
10+
11+
Raises:
12+
RuntimeError: If `vcpkg` is not a submodule of the repository.
13+
"""
14+
115
import warnings
216
import json
317
import sys
@@ -55,8 +69,7 @@
5569
PROJECT_VERSION_STRING = vcpkg_json["version-string"]
5670
# A different name can be specified here in order to upload to PyPI with a
5771
# project name different than the module name.
58-
# PROJECT_NAME = vcpkg_json["name"]
59-
PROJECT_NAME = 'example-python-extension-cpp'
72+
PROJECT_NAME = vcpkg_json["name"]
6073

6174
# scikit-build will take care of puting our compiled C++ library together with
6275
# our python package so it can access it. The name of the python package will
@@ -75,13 +88,9 @@
7588
)
7689

7790
setup(
78-
# Python package information, can be edited
79-
name=PROJECT_NAME,
80-
version=PROJECT_VERSION_STRING,
81-
description="A minimal C++ extension using pybind11 and vcpkg",
82-
author="Andreu Gimenez",
83-
license="MIT",
84-
# Python package information is defined above
91+
# Package metadata, comment out if it is provided in `pyproject.toml`.
92+
# name=PROJECT_NAME, # Use the name defined in `vcpkg.json`
93+
version=PROJECT_VERSION_STRING, # Set version from `vcpkg.json`
8594
packages=packages,
8695
package_dir={"": python_packages_root},
8796
cmake_install_dir=python_packages_root + "/" + packages[0],
@@ -94,13 +103,4 @@
94103
"-DBUILD_PYTHON_API=ON",
95104
"-DBUILD_TESTS=OFF",
96105
],
97-
# Extra setuptools keywords:
98-
# https://setuptools.pypa.io/en/latest/userguide/keywords.html
99-
python_requires=">=3.7",
100-
# When adding `[test]` to your `pip install` command you can install the
101-
# extra dependencies associated with testing. Example `pip install .[test]`
102-
# https://setuptools.pypa.io/en/latest/userguide/dependency_management.html#optional-dependencies
103-
extras_require={"test": ["pytest"]},
104-
# Necessary to publish to PyPi with the README.md as the long description
105-
long_description_content_type="text/markdown",
106106
)

0 commit comments

Comments
 (0)