1#!/bin/sh -e
2
3SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
4echo "$SCRIPT_DIR"
5cd "$SCRIPT_DIR" || { echo "fatal error" >&2; exit 1; }
6cargo clean -p libsqlite3-sys
7TARGET_DIR="$SCRIPT_DIR/../target"
8export SQLITE3_LIB_DIR="$SCRIPT_DIR/sqlite3"
9mkdir -p "$TARGET_DIR" "$SQLITE3_LIB_DIR"
10
11# Download and extract amalgamation
12SQLITE=sqlite-amalgamation-3460000
13curl -O https://sqlite.org/2024/$SQLITE.zip
14unzip -p "$SQLITE.zip" "$SQLITE/sqlite3.c" > "$SQLITE3_LIB_DIR/sqlite3.c"
15unzip -p "$SQLITE.zip" "$SQLITE/sqlite3.h" > "$SQLITE3_LIB_DIR/sqlite3.h"
16unzip -p "$SQLITE.zip" "$SQLITE/sqlite3ext.h" > "$SQLITE3_LIB_DIR/sqlite3ext.h"
17rm -f "$SQLITE.zip"
18
19export SQLITE3_INCLUDE_DIR="$SQLITE3_LIB_DIR"
20# Regenerate bindgen file for sqlite3.h
21rm -f "$SQLITE3_LIB_DIR/bindgen_bundled_version.rs"
22cargo update --quiet
23# Just to make sure there is only one bindgen.rs file in target dir
24find "$TARGET_DIR" -type f -name bindgen.rs -exec rm {} \;
25env LIBSQLITE3_SYS_BUNDLING=1 cargo build --features "buildtime_bindgen session" --no-default-features
26find "$TARGET_DIR" -type f -name bindgen.rs -exec mv {} "$SQLITE3_LIB_DIR/bindgen_bundled_version.rs" \;
27
28# Regenerate bindgen file for sqlite3ext.h
29# some sqlite3_api_routines fields are function pointers with va_list arg but currently stable Rust doesn't support this type.
30# FIXME how to generate portable bindings without :
31sed -i.bk -e 's/va_list/void*/' "$SQLITE3_LIB_DIR/sqlite3ext.h"
32rm -f "$SQLITE3_LIB_DIR/bindgen_bundled_version_ext.rs"
33find "$TARGET_DIR" -type f -name bindgen.rs -exec rm {} \;
34env LIBSQLITE3_SYS_BUNDLING=1 cargo build --features "buildtime_bindgen loadable_extension" --no-default-features
35find "$TARGET_DIR" -type f -name bindgen.rs -exec mv {} "$SQLITE3_LIB_DIR/bindgen_bundled_version_ext.rs" \;
36git checkout "$SQLITE3_LIB_DIR/sqlite3ext.h"
37rm -f "$SQLITE3_LIB_DIR/sqlite3ext.h.bk"
38
39# Sanity checks
40cd "$SCRIPT_DIR/.." || { echo "fatal error" >&2; exit 1; }
41cargo update --quiet
42cargo test --features "backup blob chrono functions limits load_extension serde_json trace vtab bundled"
43printf '    \e[35;1mFinished\e[0m bundled sqlite3 tests\n'
44