debug
Overview
The `debug` file is a shell script designed to automate the build, installation, and testing process of a Rust-based Python extension module during the development and debugging phase. It configures environment variables, cleans previous build artifacts, compiles the Rust crate into a Python wheel using `maturin`, installs the wheel, runs Python tests, and sets up Cargo configuration for incremental builds.
This script facilitates a smooth and consistent workflow for developers who want to build and debug the Rust extension with unwind panic semantics, enabling easier debugging and testing of the module in a Python environment.
Detailed Explanation
Script Shebang and Safety Settings
#!/usr/bin/env bash
set -eou pipefail
#!/usr/bin/env bash: Specifies that the script should be run withbash.set -eou pipefail: Enables strict error handling:-e: Exit immediately if any command fails.-o nounset(-u): Treat unset variables as an error.-o pipefail: The pipeline returns the exit status of the last failed command.
This ensures the script stops on errors and avoids subtle bugs.
Cleaning Previous Build Artifacts
rm -rf .cargo
rm -f ${CARGO_TARGET_DIR}/wheels/*.whl
Deletes the
.cargodirectory and any previously built wheel files inside the configured Cargo target directory.Ensures a clean build environment by removing stale configuration and build artifacts.
Environment Variable Exports
export UNSAFE_PYO3_BUILD_FREE_THREADED=1
export UNSAFE_PYO3_SKIP_VERSION_CHECK=1
export CC="${CC:-clang}"
export LD="${LD:-lld}"
export TARGET="${TARGET:-x86_64-unknown-linux-gnu}"
export CARGO_TARGET_DIR="${CARGO_TARGET_DIR:-target}"
export ORJSON_FEATURES="${ORJSON_FEATURES:-yyjson}"
export CFLAGS="-Os -fstrict-aliasing"
export RUSTFLAGS="-C panic=unwind -C linker=${CC} -C link-arg=-fuse-ld=${LD}"
PyO3 Build Flags:
UNSAFE_PYO3_BUILD_FREE_THREADED=1: Allows PyO3 to build with free-threaded mode, enabling parallelism.UNSAFE_PYO3_SKIP_VERSION_CHECK=1: Skips PyO3 version compatibility checks to speed up the build.
Compiler and Linker:
CC: Sets the C compiler, defaulting toclang.LD: Sets the linker, defaulting tolld.TARGET: The compilation target triple, defaulting tox86_64-unknown-linux-gnu.CARGO_TARGET_DIR: Directory where Cargo places build artifacts, defaulting totarget.
Feature Flags:
ORJSON_FEATURES: Features passed to the Rust build, defaulting toyyjson(enables the embedded JSON parser C library).
Compiler Flags:
CFLAGS: Compiler flags for C code (-Osfor size optimization,-fstrict-aliasingfor strict aliasing optimization).
Rust Compiler Flags:
RUSTFLAGS: Configures Rust compilation:-C panic=unwind: Enables unwinding on panic to allow stack traces and debugging.-C linker=${CC}: Uses the specified C compiler as the linker.-C link-arg=-fuse-ld=${LD}: Passes linker argument to use the specified linker.
Build Wheel with maturin
maturin build --profile=dev --target=${TARGET} --features="${ORJSON_FEATURES}" --interpreter "${PYTHON}"
Uses
maturinto build a Python wheel (.whl) from the Rust crate.Parameters:
--profile=dev: Builds using the development profile with debug info and no optimizations.--target=${TARGET}: Cross-compiles for the specified target.--features="${ORJSON_FEATURES}": Enables specified Cargo features (yyjsonby default).--interpreter "${PYTHON}": Specifies the Python interpreter to build against (e.g.,/usr/bin/python3).
This step produces a wheel file compatible with the Python environment.
Install the Wheel
uv pip install ${CARGO_TARGET_DIR}/wheels/*.whl
Installs the built wheel using
pip.The prefix
uvis likely an alias or shell function forunbufferedor similar, ensuring real-time output (not standard; may require environment-specific definition).Installs the Python extension module into the active Python environment.
Run Tests
pytest -v test
Runs the test suite located in the
testdirectory usingpytest.-vflag ensures verbose output.Validates that the installed extension module behaves as expected.
Setup Cargo Configuration
mkdir .cargo
cp ci/config.toml .cargo
Creates a
.cargodirectory if it doesn't exist.Copies a Cargo configuration file from
ci/config.tomlto.cargo/.This configuration may contain settings that affect incremental compilation, feature flags, compiler options, or platform-specific overrides, ensuring consistent builds.
Usage Example
Assuming you have the environment variables `PYTHON` (path to Python interpreter) and optionally `CC`, `LD`, `TARGET` set, you can run:
./debug
This will:
Clean previous build artifacts.
Set environment variables for building.
Build the Rust Python extension wheel for the target.
Install the wheel.
Run tests to verify the build.
Prepare Cargo configuration for future incremental builds.
Important Implementation Details
Strict Error Handling: The script fails fast on errors, avoiding partial or inconsistent builds.
Environment Variable Defaults: Uses parameter expansion to allow overriding of compiler, linker, target, and features without modifying the script.
Panic Strategy: Uses
panic=unwindinRUSTFLAGSfor better debugging support (stack unwinding).Integration with maturin: Leverages maturin to build and package Rust extensions as Python wheels seamlessly.
Test Automation: Runs
pytestimmediately after installation to verify build correctness.Cargo Configuration Management: Copies a CI-specific Cargo config, likely to maintain consistent build options across environments.
Interaction with Other Parts of the System
Cargo and Rust Build System: This script relies on Cargo and Rust toolchains to compile the Rust crate.
maturin: Bridges Rust compilation with Python packaging, producing wheels compatible with Python.Python Environment: The wheel is installed into the Python interpreter specified by the
PYTHONenvironment variable.CI Configuration (
ci/config.toml): The Cargo config file copied at the end ensures consistent build behavior, especially in CI or developer machines.Testing Framework (
pytest): Validates correctness of the built extension module.Build Artifacts Location (
${CARGO_TARGET_DIR}): The script assumes wheels are placed under this directory.
Workflow Diagram
flowchart TD
Start[Start Script]
Clean[Remove .cargo and Wheels]
SetEnv[Set Environment Variables]
Build[Build Wheel with maturin]
Install[Install Wheel via pip]
Test[Run pytest Tests]
SetupCfg[Copy Cargo Config]
End[Script Complete]
Start --> Clean --> SetEnv --> Build --> Install --> Test --> SetupCfg --> End
Summary
The `debug` script is a developer-focused automation tool for building and testing the Rust-based Python extension in a debug-friendly manner. It ensures a clean build environment, sets appropriate environment variables for compilation, builds the extension using maturin, installs it into Python, runs automated tests, and configures Cargo for consistent incremental builds. This script is essential for debugging, validating, and iterating on the Rust extension during active development.
It abstracts away the complexity of configuring the Rust build flags, Python interpreter selection, and testing into a single command, improving developer productivity and build consistency.
Quick Reference
Command/Step | Description |
|---|---|
`rm -rf .cargo` | Remove Cargo config directory |
`rm -f ${CARGO_TARGET_DIR}/wheels/*.whl` | Remove previous wheels |
Environment variables | Configure compilers, targets, features |
Build Python wheel with Rust dev profile | |
`pip install wheel` | Install the built Python extension |
`pytest -v test` | Run test suite with verbose output |
Setup Cargo config for incremental build |