Skip to content

Commit 87096ac

Browse files
committed
Add RustBin
1 parent e447c01 commit 87096ac

File tree

4 files changed

+64
-14
lines changed

4 files changed

+64
-14
lines changed

examples/hello-world/setup.py

Lines changed: 2 additions & 4 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(
9+
RustBin(
1010
"hello-world",
11-
binding=Binding.Exec,
12-
script=True,
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: 4 additions & 4 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,9 +313,7 @@ def install_extension(
313313
ext_path += exe
314314

315315
os.makedirs(os.path.dirname(ext_path), exist_ok=True)
316-
if "." in module_name:
317-
ext.install_script(module_name.split(".")[-1], ext_path)
318-
else:
316+
if isinstance(ext, RustBin):
319317
executable_name = module_name
320318
if exe is not None:
321319
executable_name += exe
@@ -325,6 +323,8 @@ def install_extension(
325323
)
326324
os.makedirs(scripts_dir, exist_ok=True)
327325
ext_path = os.path.join(scripts_dir, executable_name)
326+
else:
327+
ext.install_script(module_name.split(".")[-1], ext_path)
328328
else:
329329
ext_path = self.get_dylib_ext_path(ext, module_name)
330330
os.makedirs(os.path.dirname(ext_path), exist_ok=True)

setuptools_rust/extension.py

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,9 @@ def entry_points(self) -> List[str]:
195195
entry_points = []
196196
if self.script and self.binding == Binding.Exec:
197197
for executable, mod in self.target.items():
198-
if "." in mod:
199-
base_mod, name = mod.rsplit(".")
200-
script = "%s=%s.%s:run" % (name, base_mod, _script_name(executable))
201-
entry_points.append(script)
198+
base_mod, name = mod.rsplit(".")
199+
script = "%s=%s.%s:run" % (name, base_mod, _script_name(executable))
200+
entry_points.append(script)
202201

203202
return entry_points
204203

@@ -231,6 +230,59 @@ def _uses_exec_binding(self) -> bool:
231230
return self.binding == Binding.Exec
232231

233232

233+
class RustBin(RustExtension):
234+
"""Used to define a Rust binary and its build configuration.
235+
236+
Args:
237+
target: Rust binary target name.
238+
path: Path to the ``Cargo.toml`` manifest file.
239+
args: A list of extra arguments to be passed to Cargo. For example,
240+
``args=["--no-default-features"]`` will disable the default
241+
features listed in ``Cargo.toml``.
242+
features: A list of Cargo features to also build.
243+
rustc_flags: A list of additional flags passed to rustc.
244+
rust_version: Minimum Rust compiler version required for this
245+
extension.
246+
quiet: Suppress Cargo's output.
247+
debug: Controls whether ``--debug`` or ``--release`` is passed to
248+
Cargo. If set to `None` (the default) then build type is
249+
automatic: ``inplace`` build will be a debug build, ``install``
250+
and ``wheel`` builds will be release.
251+
strip: Strip symbols from final file. Does nothing for debug build.
252+
native: Build extension or executable with ``--target-cpu=native``.
253+
"""
254+
255+
def __init__(
256+
self,
257+
target: str,
258+
path: str = "Cargo.toml",
259+
args: Optional[List[str]] = None,
260+
features: Optional[List[str]] = None,
261+
rustc_flags: Optional[List[str]] = None,
262+
rust_version: Optional[str] = None,
263+
quiet: bool = False,
264+
debug: Optional[bool] = None,
265+
strip: Strip = Strip.No,
266+
native: bool = False,
267+
):
268+
super().__init__(
269+
target,
270+
path,
271+
args,
272+
features,
273+
rustc_flags,
274+
rust_version,
275+
quiet,
276+
debug,
277+
binding=Binding.Exec,
278+
strip=strip,
279+
native=native,
280+
)
281+
282+
def entry_points(self) -> List[str]:
283+
return []
284+
285+
234286
_CargoMetadata = NewType("_CargoMetadata", Dict[str, Any])
235287

236288

0 commit comments

Comments
 (0)