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

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

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}"

Build Wheel with maturin

maturin build --profile=dev --target=${TARGET} --features="${ORJSON_FEATURES}" --interpreter "${PYTHON}"

This step produces a wheel file compatible with the Python environment.


Install the Wheel

uv pip install ${CARGO_TARGET_DIR}/wheels/*.whl

Run Tests

pytest -v test

Setup Cargo Configuration

mkdir .cargo
cp ci/config.toml .cargo

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:

  1. Clean previous build artifacts.

  2. Set environment variables for building.

  3. Build the Rust Python extension wheel for the target.

  4. Install the wheel.

  5. Run tests to verify the build.

  6. Prepare Cargo configuration for future incremental builds.


Important Implementation Details


Interaction with Other Parts of the System


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

maturin build --profile=dev ...

Build Python wheel with Rust dev profile

`pip install wheel`

Install the built Python extension

`pytest -v test`

Run test suite with verbose output

mkdir .cargo && cp ci/config.toml .cargo

Setup Cargo config for incremental build


End of Documentation for debug