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; }
Determines the directory where the script resides.
Changes the working directory to the script location to ensure relative paths work correctly.
Exits with an error message if the directory change fails.
Cleaning Previous Build Artifacts
cargo clean -p libsqlite3-sys
Cleans the build output specifically for the
libsqlite3-syspackage to avoid conflicts from stale artifacts.
Directory Preparation
TARGET_DIR="$SCRIPT_DIR/../target"
export SQLITE3_LIB_DIR="$SCRIPT_DIR/sqlite3"
mkdir -p "$TARGET_DIR" "$SQLITE3_LIB_DIR"
Defines environment variables for the build target directory and the SQLite3 library directory.
Creates necessary directories for the build and source files.
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"
These lines, currently disabled, describe fetching the SQLite amalgamation source code from the official SQLite website.
Extracts
sqlite3.c,sqlite3.h, andsqlite3ext.hfiles into the SQLite3 library directory for compilation and binding generation.The comment implies manual toggling for updates or initial setup.
Environment Variables for Build
export SQLITE3_INCLUDE_DIR="$SQLITE3_LIB_DIR"
Sets the SQLite3 include directory environment variable for the build tools.
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" \;
Removes any existing Rust binding file generated by bindgen for
sqlite3.h.Updates Cargo dependencies quietly.
Removes any existing
bindgen.rsfiles from the target directory to avoid conflicts.Builds the
libsqlite3-syscrate with environment variableLIBSQLITE3_SYS_BUNDLING=1and featuresbuildtime_bindgenandsessionenabled, disabling default features.Moves the newly generated
bindgen.rsfile to a versioned Rust source file in the SQLite3 directory for explicit use.
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"
SQLite3 extension header
sqlite3ext.his patched to replaceva_listwithvoid*because stable Rust currently does not supportva_listin FFI bindings.Removes any existing extension binding Rust file.
Removes existing
bindgen.rsfiles again to avoid conflicts.Builds the crate enabling the
loadable_extensionfeature alongsidebuildtime_bindgen.Moves the generated bindings to a separate Rust source file for SQLite3 extensions.
Removes the backup of the patched header file.
Final Output Message
printf ' \e[35;1mFinished\e[0m bundled sqlite3 tests\n'
Prints a colored message signaling the completion of the bundled SQLite3 build and binding regeneration process.
Implementation Details and Algorithms
Uses standard shell scripting techniques for directory management, environment variable setup, and conditional execution.
Employs
cargocommands with specific features toggled and environment variables set to control Rust crate compilation.Utilizes
bindgenimplicitly through the Rust build, which generates Rust FFI code from C header files (sqlite3.handsqlite3ext.h).Applies a workaround for the unsupported
va_listtype in Rust by patching the C header before binding generation.The script is idempotent and removes previous artifacts to ensure clean and consistent builds.
Commented-out sections indicate preparation for automated downloading of SQLite sources, which can be enabled if needed.
Interaction with Other System Components
Operates closely with the Rust build system (
cargo) and thelibsqlite3-syscrate, which provides low-level bindings to SQLite3.Relies on the presence of SQLite3 amalgamation source files (
sqlite3.c,sqlite3.h,sqlite3ext.h) in thesqlite3directory.Interacts with the file system to store generated binding files for use by Rust source code in the project.
The generated Rust binding files (
bindgen_bundled_version.rsandbindgen_bundled_version_ext.rs) are likely included or referenced by higher-level Rust modules that provide SQLite3 functionality.The script may be integrated into build or CI pipelines to ensure that SQLite3 bindings are always current.
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.