Skip to content

Commit 2e908ab

Browse files
authored
Merge pull request #216 from messense/allow-cargo-profile
Add support for custom cargo profiles
2 parents 9ad1bb0 + 1151bed commit 2e908ab

File tree

6 files changed

+44
-7
lines changed

6 files changed

+44
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Unreleased
44
### Added
55
- Add support for `kebab-case` executable names. [#205](https://github.com/PyO3/setuptools-rust/pull/205)
6+
- Add support for custom cargo profiles. [#216](https://github.com/PyO3/setuptools-rust/pull/216)
67

78
## 1.1.2 (2021-12-05)
89
### Changed

examples/hello-world/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@ edition = "2018"
66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77

88
[dependencies]
9+
10+
[profile.release-lto]
11+
inherits = "release"
12+
lto = true

examples/hello-world/setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
{"hello-world": "hello_world.hello-world"},
1111
binding=Binding.Exec,
1212
script=True,
13+
args=["--profile", "release-lto"],
1314
)
1415
],
1516
# rust extensions are not zip safe, just like C-extensions.

setuptools_rust/build.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import glob
2-
import json
32
import os
43
import platform
54
import shutil
@@ -15,15 +14,14 @@
1514
DistutilsPlatformError,
1615
)
1716
from distutils.sysconfig import get_config_var
18-
from subprocess import check_output
19-
from typing import Dict, List, NamedTuple, Optional, Union, cast
17+
from typing import Dict, List, NamedTuple, Optional, cast
2018

2119
from setuptools.command.build_ext import build_ext as CommandBuildExt
2220
from setuptools.command.build_ext import get_abi3_suffix
2321
from typing_extensions import Literal
2422

2523
from .command import RustCommand
26-
from .extension import Binding, RustExtension, Strip
24+
from .extension import RustExtension, Strip
2725
from .utils import (
2826
PyLimitedApi,
2927
binding_features,
@@ -223,7 +221,18 @@ def build_extension(
223221
# Find the shared library that cargo hopefully produced and copy
224222
# it into the build directory as if it were produced by build_ext.
225223

226-
artifacts_dir = os.path.join(target_dir, "debug" if debug else "release")
224+
profile = ext.get_cargo_profile()
225+
if profile:
226+
# https://doc.rust-lang.org/cargo/reference/profiles.html
227+
if profile in {"dev", "test"}:
228+
profile_dir = "debug"
229+
elif profile == "bench":
230+
profile_dir = "release"
231+
else:
232+
profile_dir = profile
233+
else:
234+
profile_dir = "debug" if debug else "release"
235+
artifacts_dir = os.path.join(target_dir, profile_dir)
227236
dylib_paths = []
228237

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

466475
if release:
467-
args.append("--release")
476+
profile = ext.get_cargo_profile()
477+
if not profile:
478+
args.append("--release")
468479

469480
if quiet:
470481
args.append("-q")

setuptools_rust/extension.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,26 @@ def get_rust_version(self) -> Optional[SimpleSpec]: # type: ignore[no-any-unimp
170170
"Can not parse rust compiler version: %s", self.rust_version
171171
)
172172

173+
def get_cargo_profile(self) -> Optional[str]:
174+
args = self.args or []
175+
try:
176+
index = args.index("--profile")
177+
return args[index + 1]
178+
except ValueError:
179+
pass
180+
except IndexError:
181+
raise DistutilsSetupError("Can not parse cargo profile from %s", args)
182+
183+
# Handle `--profile=<profile>`
184+
profile_args = [p for p in args if p.startswith("--profile=")]
185+
if profile_args:
186+
profile = profile_args[0].split("=", 1)[1]
187+
if not profile:
188+
raise DistutilsSetupError("Can not parse cargo profile from %s", args)
189+
return profile
190+
else:
191+
return None
192+
173193
def entry_points(self) -> List[str]:
174194
entry_points = []
175195
if self.script and self.binding == Binding.Exec:

setuptools_rust/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import os
22
import subprocess
33
from distutils.errors import DistutilsPlatformError
4-
from typing import List, Optional, Set, Tuple, Union
4+
from typing import List, Optional, Set, Tuple
55

66
from semantic_version import Version
77
from typing_extensions import Literal

0 commit comments

Comments
 (0)