Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased
### Added
- Add support for `kebab-case` executable names. [#205](https://github.com/PyO3/setuptools-rust/pull/205)
- Add support for custom cargo profiles. [#216](https://github.com/PyO3/setuptools-rust/pull/216)

## 1.1.2 (2021-12-05)
### Changed
Expand Down
4 changes: 4 additions & 0 deletions examples/hello-world/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

[profile.release-lto]
inherits = "release"
lto = true
1 change: 1 addition & 0 deletions examples/hello-world/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
{"hello-world": "hello_world.hello-world"},
binding=Binding.Exec,
script=True,
args=["--profile", "release-lto"],
)
],
# rust extensions are not zip safe, just like C-extensions.
Expand Down
23 changes: 17 additions & 6 deletions setuptools_rust/build.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import glob
import json
import os
import platform
import shutil
Expand All @@ -15,15 +14,14 @@
DistutilsPlatformError,
)
from distutils.sysconfig import get_config_var
from subprocess import check_output
from typing import Dict, List, NamedTuple, Optional, Union, cast
from typing import Dict, List, NamedTuple, Optional, cast

from setuptools.command.build_ext import build_ext as CommandBuildExt
from setuptools.command.build_ext import get_abi3_suffix
from typing_extensions import Literal

from .command import RustCommand
from .extension import Binding, RustExtension, Strip
from .extension import RustExtension, Strip
from .utils import (
PyLimitedApi,
binding_features,
Expand Down Expand Up @@ -223,7 +221,18 @@ def build_extension(
# Find the shared library that cargo hopefully produced and copy
# it into the build directory as if it were produced by build_ext.

artifacts_dir = os.path.join(target_dir, "debug" if debug else "release")
profile = ext.get_cargo_profile()
if profile:
# https://doc.rust-lang.org/cargo/reference/profiles.html
if profile in {"dev", "test"}:
profile_dir = "debug"
elif profile == "bench":
profile_dir = "release"
else:
profile_dir = profile
else:
profile_dir = "debug" if debug else "release"
artifacts_dir = os.path.join(target_dir, profile_dir)
dylib_paths = []

if ext._uses_exec_binding():
Expand Down Expand Up @@ -464,7 +473,9 @@ def _cargo_args(
args.extend(["--target", target_triple])

if release:
args.append("--release")
profile = ext.get_cargo_profile()
if not profile:
args.append("--release")

if quiet:
args.append("-q")
Expand Down
20 changes: 20 additions & 0 deletions setuptools_rust/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,26 @@ def get_rust_version(self) -> Optional[SimpleSpec]: # type: ignore[no-any-unimp
"Can not parse rust compiler version: %s", self.rust_version
)

def get_cargo_profile(self) -> Optional[str]:
args = self.args or []
try:
index = args.index("--profile")
return args[index + 1]
except ValueError:
pass
except IndexError:
raise DistutilsSetupError("Can not parse cargo profile from %s", args)

# Handle `--profile=<profile>`
profile_args = [p for p in args if p.startswith("--profile=")]
if profile_args:
profile = profile_args[0].split("=", 1)[1]
if not profile:
raise DistutilsSetupError("Can not parse cargo profile from %s", args)
return profile
else:
return None

def entry_points(self) -> List[str]:
entry_points = []
if self.script and self.binding == Binding.Exec:
Expand Down
2 changes: 1 addition & 1 deletion setuptools_rust/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import subprocess
from distutils.errors import DistutilsPlatformError
from typing import List, Optional, Set, Tuple, Union
from typing import List, Optional, Set, Tuple

from semantic_version import Version
from typing_extensions import Literal
Expand Down