upgrade.sh

Overview

upgrade.sh is a shell script designed to automate the preparation and building of SQLite3 bindings within a Rust project environment. It handles cleaning previous build artifacts, setting up directories, managing SQLite3 source files, and regenerating Rust FFI (Foreign Function Interface) bindings using bindgen. The script ensures a consistent and up-to-date build environment for the libsqlite3-sys Rust crate by orchestrating the downloading, modification, and compilation steps required for bundling SQLite3 with Rust.

This script is primarily used during development or build-time to maintain the native SQLite3 bindings in sync with the Rust project, facilitating the use of SQLite3 features through Rust.


Detailed Explanation of Workflow and Key Steps

Environment Setup and Navigation

SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
echo "$SCRIPT_DIR"
cd "$SCRIPT_DIR" || { echo "fatal error" >&2; exit 1; }

Cleaning Previous Build Artifacts

cargo clean -p libsqlite3-sys

Directory Preparation

TARGET_DIR="$SCRIPT_DIR/../target"
export SQLITE3_LIB_DIR="$SCRIPT_DIR/sqlite3"
mkdir -p "$TARGET_DIR" "$SQLITE3_LIB_DIR"

SQLite3 Amalgamation Handling (Commented Out)

# SQLITE=sqlite-amalgamation-3500400
# curl -O https://sqlite.org/2025/$SQLITE.zip
# unzip -p "$SQLITE.zip" "$SQLITE/sqlite3.c" > "$SQLITE3_LIB_DIR/sqlite3.c"
# unzip -p "$SQLITE.zip" "$SQLITE/sqlite3.h" > "$SQLITE3_LIB_DIR/sqlite3.h"
# unzip -p "$SQLITE.zip" "$SQLITE/sqlite3ext.h" > "$SQLITE3_LIB_DIR/sqlite3ext.h"
# rm -f "$SQLITE.zip"

Environment Variables for Build

export SQLITE3_INCLUDE_DIR="$SQLITE3_LIB_DIR"

Bindgen Regeneration for sqlite3.h

rm -f "$SQLITE3_LIB_DIR/bindgen_bundled_version.rs"
cargo update --quiet
find "$TARGET_DIR" -type f -name bindgen.rs -exec rm {} \;
env LIBSQLITE3_SYS_BUNDLING=1 cargo build --features "buildtime_bindgen session" --no-default-features
find "$TARGET_DIR" -type f -name bindgen.rs -exec mv {} "$SQLITE3_LIB_DIR/bindgen_bundled_version.rs" \;

Bindgen Regeneration for sqlite3ext.h

sed -i.bk -e 's/va_list/void*/' "$SQLITE3_LIB_DIR/sqlite3ext.h"
rm -f "$SQLITE3_LIB_DIR/bindgen_bundled_version_ext.rs"
find "$TARGET_DIR" -type f -name bindgen.rs -exec rm {} \;
env LIBSQLITE3_SYS_BUNDLING=1 cargo build --features "buildtime_bindgen loadable_extension" --no-default-features
find "$TARGET_DIR" -type f -name bindgen.rs -exec mv {} "$SQLITE3_LIB_DIR/bindgen_bundled_version_ext.rs" \;
rm -f "$SQLITE3_LIB_DIR/sqlite3ext.h.bk"

Final Output Message

printf '    \e[35;1mFinished\e[0m bundled sqlite3 tests\n'

Implementation Details and Algorithms


Interaction with Other System Components


Usage Example

Run the script from its containing directory or invoke it directly:

./upgrade.sh

The script will clean previous build artifacts, prepare directories, regenerate bindings, and output a completion message. If the SQLite amalgamation files need to be updated, uncomment and adjust the download section accordingly.


Mermaid Diagram: Flowchart of upgrade.sh Workflow

flowchart TD
A[Start: Determine Script Directory] --> B[Change to Script Directory]
B --> C[Clean libsqlite3-sys Build Artifacts]
C --> D[Prepare TARGET_DIR and SQLITE3_LIB_DIR]
D --> E{Download SQLite Amalgamation?}
E -- No --> F[Set SQLITE3_INCLUDE_DIR]
E -- Yes --> G[Download & Extract SQLite Source Files]
G --> F
F --> H[Remove Old sqlite3 Bindings]
H --> I[Cargo Update]
I --> J[Remove Old bindgen.rs Files]
J --> K[Build with buildtime_bindgen & session Features]
K --> L[Move bindgen.rs to bindgen_bundled_version.rs]
L --> M["Patch sqlite3ext.h (Replace va_list)"]
M --> N[Remove Old sqlite3ext Bindings]
N --> O[Remove Old bindgen.rs Files]
O --> P[Build with buildtime_bindgen & loadable_extension Features]
P --> Q[Move bindgen.rs to bindgen_bundled_version_ext.rs]
Q --> R[Cleanup Patch Backup]
R --> S[Print Finished Message]
S --> T[End]

This documentation references topics related to Rust FFI and Bindgen and SQLite3 Integration for further understanding of binding generation and native library management.