Skip to content

Commit bf0777a

Browse files
committed
Add support for custom cargo profiles
1 parent 32213c6 commit bf0777a

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
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

setuptools_rust/build.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,18 @@ def build_extension(
221221
# Find the shared library that cargo hopefully produced and copy
222222
# it into the build directory as if it were produced by build_ext.
223223

224-
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)
225236
dylib_paths = []
226237

227238
if ext._uses_exec_binding():
@@ -462,7 +473,9 @@ def _cargo_args(
462473
args.extend(["--target", target_triple])
463474

464475
if release:
465-
args.append("--release")
476+
profile = ext.get_cargo_profile()
477+
if not profile:
478+
args.append("--release")
466479

467480
if quiet:
468481
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:

0 commit comments

Comments
 (0)