Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pip install configuration settings #21

Merged
merged 2 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ ndarray = "0.15.6"
numpy = "0.20"
pyo3 = { version = "0.20", features = ["extension-module", "multiple-pymethods"] }
pyo3-log = "0.9"
tucanos = { git = "https://github.com/tucanos/tucanos.git", rev = "f94ef7d" }
tucanos = { git = "https://github.com/tucanos/tucanos.git", rev = "ca44516" }

[features]
default = ["parry"]
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ Python bindings to [tucanos](https://github.com/tucanos/tucanos.git)
pip install git+https://github.com/tucanos/pytucanos.git
```

To install tucanos in debug mode:

```bash
pip install -C debug=true git+https://github.com/tucanos/pytucanos.git
```

To install Tucanos with [libmeshb](https://github.com/LoicMarechal/libMeshb) support:

```bash
pip install -C meshb=true git+https://github.com/tucanos/pytucanos.git
```

# Benchmarks

## `.meshb/.solb` I/O
Expand Down
55 changes: 55 additions & 0 deletions _custom_build/backend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# adapted from https://github.com/python-pillow/Pillow/blob/main/_custom_build/backend.py
# see also https://peps.python.org/pep-0517/#in-tree-build-backends
import sys
from setuptools.build_meta import build_wheel, build_editable

FEATURES = ["meshb", "nlopt"]


def update_argv(config_settings):
if config_settings:
flags = []
if config_settings.get("debug", "false").lower() == "true":
flags += ["--debug"]
for feature in FEATURES:
if config_settings.get(feature, "false").lower() == "true":
flags += ["--features=%s" % feature]
if flags:
sys.argv = sys.argv[:1] + ["build_rust"] + flags + sys.argv[1:]


backend_class = build_wheel.__self__.__class__


class _CustomBuildMetaBackend(backend_class):
def run_setup(self, setup_script="setup.py"):
update_argv(self.config_settings)
return super().run_setup(setup_script)

def build_wheel(
self, wheel_directory, config_settings=None, metadata_directory=None
):
self.config_settings = config_settings
return super().build_wheel(wheel_directory, config_settings, metadata_directory)


build_wheel = _CustomBuildMetaBackend().build_wheel

backend_class = build_editable.__self__.__class__


class _CustomBuildMetaBackend(backend_class):
def run_setup(self, setup_script="setup.py"):
update_argv(self.config_settings)
return super().run_setup(setup_script)

def build_editable(
self, wheel_directory, config_settings=None, metadata_directory=None
):
self.config_settings = config_settings
return super().build_editable(
wheel_directory, config_settings, metadata_directory
)


build_editable = _CustomBuildMetaBackend().build_editable
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
[build-system]
requires = ["setuptools", "wheel", "setuptools-rust"]
build-backend = "backend"
backend-path = ["_custom_build"]
25 changes: 21 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
import setuptools
from setuptools_rust import Binding, RustExtension
from setuptools_rust import Binding, RustExtension, build_rust
import sys


class BuildRustCommand(build_rust):
user_options = build_rust.user_options + [
("features=", None, "Value for cargo --features")
]

def initialize_options(self):
super().initialize_options()
self.features = None

def finalize_options(self):
super().finalize_options()
ext = self.distribution.rust_extensions[0]
ext.debug = self.debug
ext.release = not self.debug
if self.features:
ext.features = self.features.split(",")

features = []

setuptools.setup(
name="pytucanos",
Expand All @@ -12,10 +30,9 @@
RustExtension(
"pytucanos._pytucanos",
binding=Binding.PyO3,
features=features,
debug=False,
)
],
cmdclass={"build_rust": BuildRustCommand},
# rust extensions are not zip safe, just like C-extensions.
zip_safe=False,
)
Loading