# Maintainer: jazztickets <amF6enRpY2tldHNAZ21haWwuY29tCg==>
# Contributor: Andrew Sun <adsun701 at gmail dot com>
# Contributor: napa3um <napa3um at gmail dot com>
# Contributor: Filip Brcic <brcha at gna dot org>
# Contributor: Martchus <martchus at gmx dot net>

pkgname=mingw-w64-clang-aarch64-sqlite
_amalgamationver=3490100
_snapshotver=202502212246
pkgver=3.49.1
pkgrel=2
pkgdesc="A C library that implements an SQL database engine (mingw-w64-clang-aarch64)"
arch=('any')
groups=(mingw-w64-clang-aarch64)
depends=('mingw-w64-clang-aarch64-crt')
makedepends=('mingw-w64-clang-aarch64-configure' 'mingw-w64-clang-aarch64-pdcurses' 'mingw-w64-clang-aarch64-readline')
options=('!strip' '!buildflags' 'staticlibs')
license=('custom:Public Domain')
url="https://www.sqlite.org/"
source=("https://www.sqlite.org/2025/sqlite-autoconf-${_amalgamationver}.tar.gz")
[[ $_snapshotver ]] && source=("https://wasm-testing.sqlite.org/tmp/sqlite-snapshot-$_snapshotver.tar.gz")
sha256sums=('e785fe384655a9304603f81cb29db8570578cfefb5199e5230937151c5d8b0f5')

_architectures="aarch64-w64-mingw32"
_cflags=(
	-fexceptions
	-DSQLITE_ENABLE_COLUMN_METADATA=1
	-DSQLITE_ENABLE_UNLOCK_NOTIFY
	-DSQLITE_ENABLE_DBSTAT_VTAB=1
	-DSQLITE_ENABLE_FTS3_TOKENIZER=1
	-DSQLITE_ENABLE_FTS3_PARENTHESIS
	-DSQLITE_SECURE_DELETE
	-DSQLITE_ENABLE_STMTVTAB
	-DSQLITE_ENABLE_STAT4
	-DSQLITE_MAX_VARIABLE_NUMBER=250000
	-DSQLITE_MAX_EXPR_DEPTH=10000
	-DSQLITE_ENABLE_MATH_FUNCTIONS
	-DSQLITE_USE_MALLOC_H=1
	-DSQLITE_USE_MSIZE=1
	-DSQLITE_DISABLE_DIRSYNC=1
	-fno-strict-aliasing
)

build() {
  export USE_COMPILER_WRAPPERS=1
	CFLAGS+=" ${_cflags[*]}" CXXFLAGS+=" ${_cflags[*]}"

	[[ $_snapshotver ]] && \
		cd "${srcdir}/sqlite-snapshot-${_snapshotver}" || \
		cd "${srcdir}/sqlite-autoconf-${_amalgamationver}"

	for _arch in ${_architectures}; do
    source mingw-clang-env $_arch
		mkdir -p build-${_arch} && pushd build-${_arch}

		# remove '--target=...' from mingw's configure
		bash <(sed 's/--target[^ ]* //' $(command -v "${_arch}-configure")) \
			--fts3 \
			--fts4 \
			--fts5 \
			--rtree \
			--session \
			--out-implib \
			--dll-basename \
			--disable-load-extension

		make
		popd
	done
}

package() {
  export USE_COMPILER_WRAPPERS=1
	[[ $_snapshotver ]] && \
		cd "${srcdir}/sqlite-snapshot-${_snapshotver}" || \
		cd "${srcdir}/sqlite-autoconf-${_amalgamationver}"
	for _arch in ${_architectures}; do
    source mingw-clang-env $_arch
		pushd build-${_arch}
		make DESTDIR="${pkgdir}" install
		rm -r "${pkgdir}/usr/${_arch}/share"
		#mv "$pkgdir"/usr/${_arch}/lib/*.dll "$pkgdir"/usr/${_arch}/bin/
		$STRIP --strip-unneeded "$pkgdir"/usr/${_arch}/bin/*.exe
		$STRIP --strip-unneeded "$pkgdir"/usr/${_arch}/bin/*.dll
		$STRIP -g "$pkgdir"/usr/${_arch}/lib/*.a
		popd
	done
}
