build.rs

Overview

The build.rs file is a Rust build script responsible for configuring the compilation and linking process of the SQLite3 or SQLCipher library bindings for the Rust crate. It manages the environment detection, conditional compilation, binding generation, and compilation of bundled or system-provided SQLite3/SQLCipher libraries. The script adjusts its behavior based on Cargo features, environment variables, and target platforms, allowing flexible integration scenarios including static linking, dynamic linking, bundled source compilation, and loadable extensions.


Detailed Explanations

Top-Level Functions

fn win_target() -> bool

fn android_target() -> bool

fn is_compiler(compiler_name: &str) -> bool

fn copy_bindings<T: AsRef<Path>>(dir: &str, bindgen_name: &str, out_path: T)


fn main()


Modules

build_bundled


build_linked


bindings Module

Two variants are conditionally compiled depending on the buildtime_bindgen feature:

Without buildtime_bindgen

With buildtime_bindgen


loadable_extension Module


HeaderLocation Enum

Variant

Description

FromEnvironment

Header path derived from environment variables, e.g., ${PREFIX}_INCLUDE_DIR/sqlite3.h.

Wrapper

Uses a wrapper header file included with the crate (wrapper.h or wrapper_ext.h).

FromPath(String)

Specifies a custom header file path as a string, optionally with an appended filename.


Environment Prefix and Library Name Functions


Important Implementation Details & Algorithms


Interaction with Other Parts of the System


Usage Examples

Building with Bundled SQLite

To build with SQLite bundled and compiled from source, enable the bundled feature in Cargo. The build script will:

cargo build --features bundled

Building with SQLCipher and OpenSSL

To build with SQLCipher support that links against system OpenSSL, set environment variables and features accordingly:

export SQLCIPHER_INCLUDE_DIR=/usr/local/include
export SQLCIPHER_LIB_DIR=/usr/local/lib
cargo build --features sqlcipher

If OpenSSL is not found, building will panic unless the vendored OpenSSL feature is enabled.

Using Loadable Extensions

When building with the loadable_extension feature, the build script generates bindings for SQLite extension APIs and ensures safe runtime initialization of the SQLite API pointers.


Visual Diagram: Flowchart of build.rs Main Build Logic

flowchart TD
A["Start: main()"] --> B{Feature "in_gecko"?}
B -- Yes --> C[Copy bundled bindings]
C --> Z[End]
B -- No --> D[Print rerun-if-env-changed for PKG_CONFIG]
D --> E{Use pkg-config or loadable_extension?}
E -- Yes --> F[Call build_linked::main]
E -- No --> G{Feature "sqlcipher" and not "bundled-sqlcipher"?}
G -- Yes --> H{Feature "bundled" or Windows + "bundled-windows"?}
H -- Yes --> I[Print warning about feature override]
I --> F
H -- No --> F
G -- No --> J{Feature "bundled" or Windows + "bundled-windows" or "bundled-sqlcipher"?}
J -- Yes --> K[Call build_bundled::main]
J -- No --> F
F --> Z[End]
K --> Z

Summary of Key Entities

Entity

Description

main

Entry point deciding build strategy based on features/env.

build_bundled::main

Configures and compiles bundled SQLite/SQLCipher C sources.

build_linked::main

Handles linking to system-provided SQLite/SQLCipher libs.

bindings module

Generates or copies Rust FFI bindings for SQLite APIs.

loadable_extension

Generates Rust wrappers for SQLite extension API macros.

HeaderLocation enum

Represents header file location for binding generation.

copy_bindings

Utility to copy prebuilt binding files.

win_target/android_target/is_compiler

Helpers for environment and target detection.


References