Skip to content

Commit 3590d1a

Browse files
authored
Merge pull request #248 from messense/data-scripts
Add support for packaging binaries in scripts data directory
2 parents 6347ad4 + db3ccff commit 3590d1a

File tree

6 files changed

+91
-10
lines changed

6 files changed

+91
-10
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ jobs:
253253
toolchain: stable
254254
override: true
255255
- name: Install cross
256-
run: cargo install cross
256+
run: cargo install --version 0.2.1 cross
257257
- name: Build package
258258
run: pip install -e .
259259
- name: Build wheel using cross

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77

88
### Added
99
- Add `cargo_manifest_args` to support locked, frozen and offline builds. [#234](https://github.com/PyO3/setuptools-rust/pull/234)
10+
- Add `RustBin` for packaging binaries in scripts data directory. [#248](https://github.com/PyO3/setuptools-rust/pull/248)
11+
12+
### Changed
13+
- `Exec` binding `RustExtension` with `script=True` is deprecated in favor of `RustBin`. [#248](https://github.com/PyO3/setuptools-rust/pull/248)
1014

1115
### Fixed
1216
- If the sysconfig for `BLDSHARED` has no flags, `setuptools-rust` won't crash anymore. [#241](https://github.com/PyO3/setuptools-rust/pull/241)

examples/hello-world/setup.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
from setuptools import setup
22

3-
from setuptools_rust import Binding, RustExtension
3+
from setuptools_rust import RustBin
44

55
setup(
66
name="hello-world",
77
version="1.0",
88
rust_extensions=[
9-
RustExtension(
10-
{"hello-world": "hello_world.hello-world"},
11-
binding=Binding.Exec,
12-
script=True,
9+
RustBin(
10+
"hello-world",
1311
args=["--profile", "release-lto"],
1412
)
1513
],

setuptools_rust/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from .build import build_rust
22
from .clean import clean_rust
3-
from .extension import Binding, RustExtension, Strip
3+
from .extension import Binding, RustBin, RustExtension, Strip
44
from .version import version as __version__
55

6-
__all__ = ("Binding", "RustExtension", "Strip", "build_rust", "clean_rust")
6+
__all__ = ("Binding", "RustBin", "RustExtension", "Strip", "build_rust", "clean_rust")

setuptools_rust/build.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from typing_extensions import Literal
2222

2323
from .command import RustCommand
24-
from .extension import RustExtension, Strip
24+
from .extension import RustBin, RustExtension, Strip
2525
from .utils import (
2626
PyLimitedApi,
2727
binding_features,
@@ -313,7 +313,18 @@ def install_extension(
313313
ext_path += exe
314314

315315
os.makedirs(os.path.dirname(ext_path), exist_ok=True)
316-
ext.install_script(module_name.split(".")[-1], ext_path)
316+
if isinstance(ext, RustBin):
317+
executable_name = module_name
318+
if exe is not None:
319+
executable_name += exe
320+
wheel = self.get_finalized_command("bdist_wheel")
321+
scripts_dir = os.path.join(
322+
build_ext.build_lib, wheel.data_dir, "scripts" # type: ignore[attr-defined]
323+
)
324+
os.makedirs(scripts_dir, exist_ok=True)
325+
ext_path = os.path.join(scripts_dir, executable_name)
326+
else:
327+
ext.install_script(module_name.split(".")[-1], ext_path)
317328
else:
318329
ext_path = self.get_dylib_ext_path(ext, module_name)
319330
os.makedirs(os.path.dirname(ext_path), exist_ok=True)

setuptools_rust/extension.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import json
22
import os
33
import re
4+
import warnings
45
import subprocess
56
from distutils.errors import DistutilsSetupError
67
from enum import IntEnum, auto
@@ -160,6 +161,12 @@ def __init__(
160161

161162
self._cargo_metadata: Optional[_CargoMetadata] = None
162163

164+
if binding == Binding.Exec and script:
165+
warnings.warn(
166+
"'Binding.Exec' with 'script=True' is deprecated, use 'RustBin' instead.",
167+
DeprecationWarning,
168+
)
169+
163170
def get_lib_name(self) -> str:
164171
"""Parse Cargo.toml to get the name of the shared library."""
165172
metadata = self._metadata()
@@ -240,6 +247,67 @@ def _uses_exec_binding(self) -> bool:
240247
return self.binding == Binding.Exec
241248

242249

250+
class RustBin(RustExtension):
251+
"""Used to define a Rust binary and its build configuration.
252+
253+
Args:
254+
target: Rust binary target name.
255+
path: Path to the ``Cargo.toml`` manifest file.
256+
args: A list of extra arguments to be passed to Cargo. For example,
257+
``args=["--no-default-features"]`` will disable the default
258+
features listed in ``Cargo.toml``.
259+
cargo_manifest_args: A list of extra arguments to be passed to Cargo.
260+
These arguments will be passed to every ``cargo`` command, not just
261+
``cargo build``. For valid options, see
262+
`the Cargo Book <https://doc.rust-lang.org/cargo/commands/cargo-build.html#manifest-options>`_.
263+
For example, ``cargo_manifest_args=["--locked"]`` will require
264+
``Cargo.lock`` files are up to date.
265+
features: A list of Cargo features to also build.
266+
rustc_flags: A list of additional flags passed to rustc.
267+
rust_version: Minimum Rust compiler version required for this
268+
extension.
269+
quiet: Suppress Cargo's output.
270+
debug: Controls whether ``--debug`` or ``--release`` is passed to
271+
Cargo. If set to `None` (the default) then build type is
272+
automatic: ``inplace`` build will be a debug build, ``install``
273+
and ``wheel`` builds will be release.
274+
strip: Strip symbols from final file. Does nothing for debug build.
275+
native: Build extension or executable with ``--target-cpu=native``.
276+
"""
277+
278+
def __init__(
279+
self,
280+
target: str,
281+
path: str = "Cargo.toml",
282+
args: Optional[List[str]] = None,
283+
cargo_manifest_args: Optional[List[str]] = None,
284+
features: Optional[List[str]] = None,
285+
rustc_flags: Optional[List[str]] = None,
286+
rust_version: Optional[str] = None,
287+
quiet: bool = False,
288+
debug: Optional[bool] = None,
289+
strip: Strip = Strip.No,
290+
native: bool = False,
291+
):
292+
super().__init__(
293+
target,
294+
path,
295+
args,
296+
cargo_manifest_args,
297+
features,
298+
rustc_flags,
299+
rust_version,
300+
quiet,
301+
debug,
302+
binding=Binding.Exec,
303+
strip=strip,
304+
native=native,
305+
)
306+
307+
def entry_points(self) -> List[str]:
308+
return []
309+
310+
243311
_CargoMetadata = NewType("_CargoMetadata", Dict[str, Any])
244312

245313

0 commit comments

Comments
 (0)