Skip to content

Commit 24de6c1

Browse files
authored
Expose default build profile as QISKIT_BUILD_PROFILE (#15242)
We previously have read an undocumented environment variable `RUST_DEBUG` as part of `setup.py` and used this to force debug mode in `pip install .`. This instead formalises the system into a `QISKIT_BUILD_PROFILE` environment variable with the values `release` and `debug`, which can (and do) publicly document. For convenience, we still read `RUST_DEBUG` as a fallback.
1 parent 97bca22 commit 24de6c1

File tree

4 files changed

+44
-13
lines changed

4 files changed

+44
-13
lines changed

CONTRIBUTING.md

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,24 @@ If you use Rustup, it will automatically install the correct Rust version
126126
currently used by the project.
127127

128128
Once you have a Rust compiler installed, you can rely on the normal Python
129-
build/install steps to install Qiskit. This means you just run
130-
`pip install .` in your local git clone to build and install Qiskit.
131-
132-
Do note that if you do use develop mode/editable install (via `python setup.py develop` or `pip install -e .`) the Rust extension will be built in debug mode
133-
without any optimizations enabled. This will result in poor runtime performance.
134-
If you'd like to use an editable install with an optimized binary you can
135-
run `python setup.py build_rust --release --inplace` after you install in
136-
editable mode to recompile the rust extensions in release mode.
129+
build/install steps to install Qiskit. This means you run `pip install .` or
130+
`pip install -e .` in your local git clone to build and install Qiskit.
131+
Note that changes to Rust files will not be reflected in an editable install
132+
until you recompile. You can recompile the Rust components of an editable
133+
install by running
134+
```
135+
python setup.py build_rust --inplace [--release | --debug]
136+
```
137+
Modifications to Rust files will not take effect until the Rust extension module
138+
is recompiled with the above command.
139+
140+
By default, `pip install .` will build the Rust components in "release" mode
141+
and `pip install -e .` (or `python setup.py build_rust --inplace`) will build
142+
them in "debug" mode, without optimizations. Debug mode will have poor
143+
runtime performance. You can set the environment variable `QISKIT_BUILD_PROFILE`
144+
to `release` or `debug` to control the default. The `--release`/`--debug` flag
145+
to `build_rust` overrides this default.
146+
137147

138148
Note that in order to run `python setup.py ...` commands you need to have the
139149
build dependency packages, which are listed in the `pyproject.toml` file under
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
build:
3+
- |
4+
The build system now reads the environment variable ``QISKIT_BUILD_PROFILE`` to
5+
choose whether to build in ``release`` or ``debug`` modes. If this is not set, ``pip install .``
6+
will (continue to) default to release mode, and ``pip install -e .`` to debug mode.

setup.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"The Qiskit setup file."
1414

1515
import os
16+
import warnings
1617
from setuptools import setup
1718
from setuptools_rust import Binding, RustExtension
1819

@@ -26,9 +27,23 @@
2627
# unergonomic to do otherwise.
2728

2829

29-
# If RUST_DEBUG is set, force compiling in debug mode. Else, use the default behavior of whether
30-
# it's an editable installation.
31-
rust_debug = True if os.getenv("RUST_DEBUG") == "1" else None
30+
# Check for a default build profile from the environment (`--release` or `--debug` flags to
31+
# `build_rust` override this default). If not present, we also check if `RUST_DEBUG=1` for
32+
# convenience, since we did that (undocumented) until Qiskit 2.4.
33+
if (build_profile := os.getenv("QISKIT_BUILD_PROFILE", None)) is None:
34+
rust_debug = os.getenv("RUST_DEBUG", None) == "1" or None
35+
else:
36+
match build_profile.lower():
37+
case "debug":
38+
rust_debug = True
39+
case "release":
40+
rust_debug = False
41+
case _:
42+
warnings.warn(
43+
f"QISKIT_BUILD_PROFILE set to unknown value '{build_profile}'."
44+
" Valid values are 'debug' and 'release'."
45+
)
46+
rust_debug = None
3247

3348
# If QISKIT_NO_CACHE_GATES is set then don't enable any features while building
3449
#

tox.ini

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ commands = python tools/run_cargo_test.py
4444
# still run fast enough.
4545
[testenv:.pkg-rust]
4646
setenv =
47-
RUST_DEBUG=1
47+
QISKIT_BUILD_PROFILE=debug
4848

4949
[testenv:lint]
5050
basepython = python3
@@ -104,7 +104,7 @@ allowlist_externals =
104104
doxygen
105105
setenv =
106106
{[testenv]setenv}
107-
RUST_DEBUG=1 # Faster to compile.
107+
QISKIT_BUILD_PROFILE=debug
108108
commands_pre =
109109
{[testenv]install_command} -r{toxinidir}/requirements-optional.txt -r{toxinidir}/requirements-dev.txt
110110
commands =

0 commit comments

Comments
 (0)