# Contributor: qaqland <qaq@qaq.land>
# Maintainer: qaqland <qaq@qaq.land>
pkgname=xmake
pkgver=2.9.9
pkgrel=0
pkgdesc="A cross-platform build utility based on Lua"
url="https://xmake.io/"
arch="all"
license="Apache-2.0"
depends="bash"
makedepends="
	grep
	libsv-dev
	libtbox-dev
	linux-headers
	lua5.4-dev
	lz4-dev
	ncurses-dev
	readline-dev
	xxhash-dev
	"
subpackages="
	$pkgname-doc
	$pkgname-bash-completion
	$pkgname-zsh-completion
	$pkgname-fish-completion
	"

# xmake use patched lua-cjson, external will cause test fail
_lua_cjson=ddcecf3b24b71421e7b4a2962f1fbcc0297e0c1e

source="
	$pkgname-$pkgver.tar.gz::https://github.com/xmake-io/xmake/archive/refs/tags/v$pkgver.tar.gz
	$pkgname-core-lua-cjson-$_lua_cjson.tar.gz::https://github.com/xmake-io/xmake-core-lua-cjson/archive/$_lua_cjson.tar.gz
	01-pkgconfig-libsv.patch
	02-system-include.patch
	03-internal-lua-cjson.patch
	"

prepare() {
	default_prepare

	# only test c here
	find tests/projects/ -mindepth 1 -maxdepth 1 ! -path "tests/projects/c" -exec rm -r {} \;

	# remove online tests
	rm -rf tests/actions/install
	rm -rf tests/actions/package/localpkg
	rm -rf tests/apis/namespace/package
	rm -rf tests/projects/c/library_with_cmakelists

	mv -Tv "$srcdir"/xmake-core-lua-cjson-$_lua_cjson core/src/lua-cjson/lua-cjson

	# xmake "source tbox/xmake.sh" even use external libtbox
	mkdir -p core/src/tbox/tbox/src
	touch core/src/tbox/tbox/src/xmake.sh
}

build() {
	# this configure is not from autotools but github.com/xmake-io/xmake.sh
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--external=yes \
		--kind=shared
	make
}

check() {
	cp build/xmake xmake/xmake
	cp scripts/xrepo.sh xmake/xrepo
	export PATH="$PATH:$builddir/xmake" CC="gcc" CXX="g++"
	xmake lua -v -D tests/run.lua
	xrepo --version
}

package() {
	make DESTDIR="$pkgdir" install

	install -Dm644 scripts/man/*1 -t "$pkgdir"/usr/share/man/man1/

	install -Dm644 xmake/scripts/completions/register-completions.bash \
		"$pkgdir"/usr/share/bash-completion/completions/xmake
	install -Dm644 xmake/scripts/completions/register-completions.zsh \
		"$pkgdir"/usr/share/zsh/site-functions/_xmake
	install -Dm644 xmake/scripts/completions/register-completions.fish \
		"$pkgdir"/usr/share/fish/vendor_completions.d/xmake.fish
}

sha512sums="
bf6477198614ebb9b349faa6c6e721e0278514fc1d86a5f1c0ee434c2d7fa1c96c1fff8413d8cb842e24ac55a9d6e5b7c9ed581c141fba1405e78a56f8e99f87  xmake-2.9.9.tar.gz
3f1caa76b501eadc00d55c8a9e332576d7719007d5f3a1bd5d3f1609b01917d9206849454a9d2adb1d60963191e53a89c6ea5a1996851af5ff6bedc708df88a9  xmake-core-lua-cjson-ddcecf3b24b71421e7b4a2962f1fbcc0297e0c1e.tar.gz
26131e4a84ff159db4a75c3095e0b0f04ad909ecb00296f2e2e10a8db5b01a53603ad2067a56b4b76453a527269b8f77dd24435b87940e929ed6a904aa064aa7  01-pkgconfig-libsv.patch
735560bb8a148818b3c1efeac612c2873c473fd6727e749de78053442fda09919e51292f1aae71e49e5b22ab432da0a981d2d7505bd6e25ae02e4b40b6084279  02-system-include.patch
1df3a3ddf30209556715939e57783f230a56bbaf03a3ded839718c6639b3de0e1161056dcd76f25827cee72d6765973d1e8b112121482cad0050a8eac86dfa79  03-internal-lua-cjson.patch
"
