develop


Overview

The `develop` file is a **shell script** designed to automate the build and installation process of the Rust-based Python extension module within this project. Its main purpose is to prepare an optimized development build of the Rust crate, package it as a Python wheel using `maturin`, and install that wheel into the active Python environment.

This script sets up a carefully controlled build environment with specific compiler and linker flags to enable LLVM bitcode generation, linker plugin LTO optimizations, and multi-threaded Rust MIR optimizations. It also configures environment variables to bypass certain PyO3 safety/version checks to speed up the build in trusted environments.

The workflow encapsulated by this script simplifies and standardizes the complex process of building, packaging, and installing the Rust extension for developers iterating on the project.


Detailed Explanation

Script Type and Shebang

#!/bin/sh -e

Step-by-Step Breakdown

1. Clean Previous Build Artifacts

rm -f target/wheels/*

2. Export PyO3 Build Environment Variables

export UNSAFE_PYO3_BUILD_FREE_THREADED=1
export UNSAFE_PYO3_SKIP_VERSION_CHECK=1

3. Set Compiler and Linker Environment Variables

export CC="${CC:-clang}"
export LD="${LD:-lld}"
export TARGET="${TARGET:-x86_64-unknown-linux-gnu}"
export CARGO_TARGET_DIR="${CARGO_TARGET_DIR:-target}"

These variables allow customization of the build toolchain and target platform.

4. Print Compiler and Linker Info

echo "CC: ${CC}, LD: ${LD}, LD_LIBRARY_PATH: ${LD_LIBRARY_PATH}"

5. Set Compilation and Linking Flags

export CFLAGS="-O2 -fstrict-aliasing -fno-plt -emit-llvm"
export LDFLAGS="-fuse-ld=${LD} -Wl,-plugin-opt=also-emit-llvm -Wl,--as-needed -Wl,-zrelro,-znow"
export RUSTFLAGS="-C linker=${CC} -C link-arg=-fuse-ld=${LD} -C linker-plugin-lto -C lto=fat -C link-arg=-Wl,-zrelro,-znow -Z mir-opt-level=4 -Z threads=8"

These flags produce an optimized build with LLVM bitcode and advanced link-time optimizations, improving performance and binary size.

6. Remove Existing Wheels from Cargo Target Directory

rm -f ${CARGO_TARGET_DIR}/wheels/*.whl

7. Build Python Wheel Using maturin

maturin build --target="${TARGET}" "$@"

8. Install the Built Wheel Using pip

uv pip install --link-mode=copy ${CARGO_TARGET_DIR}/wheels/*.whl

Usage Example

To build and install the Rust Python extension in development mode with default target:

./develop

To specify additional `maturin` build flags, such as enabling features or setting profiles:

./develop --release --features=some_feature

To change the target platform or compiler:

TARGET=aarch64-unknown-linux-gnu CC=aarch64-linux-gnu-gcc ./develop

Important Implementation Details


Interaction with Other Parts of the System


Visual Diagram: Workflow of develop Script

flowchart TD
    CleanOldWheels[Remove old wheels from target/wheels]
    ExportEnv[Export PyO3 and toolchain environment variables]
    ShowEnv[Print CC, LD, LD_LIBRARY_PATH]
    CleanCargoWheels[Remove old wheels from Cargo target directory]
    BuildWheel[maturin build with target and args]
    InstallWheel[Install wheel using pip]
    
    CleanOldWheels --> ExportEnv --> ShowEnv --> CleanCargoWheels --> BuildWheel --> InstallWheel

Summary

The `develop` script is a critical build automation tool enabling developers to efficiently compile, package, and install the Rust-based Python extension during development cycles. By setting precise environment variables for compilers, linkers, and Rust optimization flags, it ensures builds are optimized with LLVM bitcode emission and linker plugin LTO. Integration with `maturin` abstracts complexity in Rust-Python packaging, while the script’s cleaning steps guarantee fresh builds. Overall, this script encapsulates a complex multi-toolchain build process into a simple, repeatable command, facilitating rapid development and testing.


Appendix: Environment Variables Summary

Variable

Description

Default Value

`UNSAFE_PYO3_BUILD_FREE_THREADED`

Disables PyO3 safety check for free-threaded build

`1` (enabled)

`UNSAFE_PYO3_SKIP_VERSION_CHECK`

Skips PyO3 Python version checks

`1` (enabled)

`CC`

C compiler

`clang`

`LD`

Linker

`lld`

`TARGET`

Rust compilation target triple

`x86_64-unknown-linux-gnu`

`CARGO_TARGET_DIR`

Cargo output directory

`target`

`CFLAGS`

C compiler flags

`-O2 -fstrict-aliasing -fno-plt -emit-llvm`

`LDFLAGS`

Linker flags

-fuse-ld=lld -Wl,-plugin-opt=also-emit-llvm -Wl,--as-needed -Wl,-zrelro,-znow

`RUSTFLAGS`

Rust compiler flags

See detailed flags above


If you need further details on how this script fits into the overall build system or on the options available in `maturin`, please let me know!