| 1 | #!/usr/bin/env bash |
| 2 | # Builds SQLite from its amalgamation into a prefix: <prefix>/lib/libsqlite3.a, |
| 3 | # <prefix>/include/sqlite3.h and a pkg-config file, which the sqlite3 opam |
| 4 | # package then finds. The source tarball is downloaded once into .cache/src, |
| 5 | # checked against sqlite.org's SHA3-256, and kept by make clean-deps. |
| 6 | # tools/sqlite.sh fetch (on the host) |
| 7 | # tools/sqlite.sh build <prefix> (with the target's C compiler) |
| 8 | set -euo pipefail |
| 9 | app=$(cd "$(dirname "$0")/.." && pwd) |
| 10 | version=3540100 |
| 11 | sha3=b6ced585c750916b1052667239a12c25a7ff34e7f06e1e6e01ecb0427657d9c4 |
| 12 | tarball=$app/.cache/src/sqlite-autoconf-$version.tar.gz |
| 13 | |
| 14 | case $1 in |
| 15 | fetch) |
| 16 | [ -f "$tarball" ] && exit 0 |
| 17 | mkdir -p "$(dirname "$tarball")" |
| 18 | curl -sSfo "$tarball.part" "https://sqlite.org/2026/sqlite-autoconf-$version.tar.gz" |
| 19 | got=$(python3 -c 'import hashlib,sys; print(hashlib.sha3_256(open(sys.argv[1],"rb").read()).hexdigest())' "$tarball.part") |
| 20 | [ "$got" = "$sha3" ] || { echo "sqlite: SHA3-256 mismatch: $got" >&2; rm -f "$tarball.part"; exit 1; } |
| 21 | mv "$tarball.part" "$tarball" |
| 22 | ;; |
| 23 | build) |
| 24 | prefix=$2 |
| 25 | [ -f "$prefix/lib/libsqlite3.a" ] && exit 0 |
| 26 | rm -rf "$prefix" && mkdir -p "$prefix/build" "$prefix/lib/pkgconfig" "$prefix/include" |
| 27 | tar xzf "$tarball" -C "$prefix/build" --strip-components=1 \ |
| 28 | "sqlite-autoconf-$version/sqlite3.c" "sqlite-autoconf-$version/sqlite3.h" "sqlite-autoconf-$version/sqlite3ext.h" |
| 29 | # -fPIC: the x86 build also links it into the OCaml stubs' shared library. |
| 30 | cc -c -O2 -fPIC -DSQLITE_THREADSAFE=1 -DSQLITE_DQS=0 -DSQLITE_DEFAULT_MEMSTATUS=0 \ |
| 31 | -DSQLITE_OMIT_DEPRECATED -DSQLITE_OMIT_SHARED_CACHE \ |
| 32 | -o "$prefix/build/sqlite3.o" "$prefix/build/sqlite3.c" |
| 33 | ar rcs "$prefix/lib/libsqlite3.a" "$prefix/build/sqlite3.o" |
| 34 | cp "$prefix/build/sqlite3.h" "$prefix/build/sqlite3ext.h" "$prefix/include/" |
| 35 | rm -rf "$prefix/build" |
| 36 | cat > "$prefix/lib/pkgconfig/sqlite3.pc" <<EOS |
| 37 | Name: SQLite |
| 38 | Description: SQL database engine, built by tools/sqlite.sh |
| 39 | Version: ${version:0:1}.${version:1:2}.${version:4:1} |
| 40 | Cflags: -I$prefix/include |
| 41 | Libs: -L$prefix/lib -lsqlite3 |
| 42 | Libs.private: -lm -lpthread |
| 43 | EOS |
| 44 | ;; |
| 45 | esac |
| 46 | |