# Contributor: Sergei Lukin <sergej.lukin@gmail.com>
# Contributor: Sören Tempel <soeren+alpine@soeren-tempel.net>
# Contributor: Pierre-Gilas MILLON <pgmillon@gmail.com>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=libgit2
pkgver=1.7.2
pkgrel=0
pkgdesc="linkable library for Git"
url="https://libgit2.org/"
arch="all"
license="GPL-2.0-only WITH GCC-exception-2.0"
depends_dev="curl-dev libssh2-dev http-parser-dev"
makedepends_host="$depends_dev zlib-dev openssl-dev>3 pcre2-dev"
makedepends_build="python3 cmake samurai"
subpackages="$pkgname-dbg $pkgname-static $pkgname-dev $pkgname-tools $pkgname-tests::noarch"
options="net"
source="$pkgname-$pkgver.tar.gz::https://github.com/libgit2/libgit2/archive/v$pkgver.tar.gz
	fix-util-tests.patch
	missing-header.patch
	no-online-test.patch
	"

# secfixes:
#   1.7.2-r0:
#     - CVE-2024-24577
#     - CVE-2024-24575
#   1.4.4-r0:
#     - CVE-2022-29187
#     - CVE-2022-24765
#   0.28.4-r0:
#     - CVE-2019-1348
#     - CVE-2019-1349
#     - CVE-2019-1350
#     - CVE-2019-1351
#     - CVE-2019-1352
#     - CVE-2019-1353
#     - CVE-2019-1354
#     - CVE-2019-1387
#   0.27.4-r0:
#     - CVE-2018-15501
#   0.27.3-r0:
#     - CVE-2018-10887
#     - CVE-2018-10888
#   0.25.1-r0:
#     - CVE-2016-10128
#     - CVE-2016-10129
#     - CVE-2016-10130
#   0.24.3-r0:
#     - CVE-2016-8568
#     - CVE-2016-8569

build() {
	CFLAGS="$CFLAGS -flto=auto" \
	cmake -B build-shared -G Ninja \
		-DCMAKE_BUILD_TYPE=None \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=lib \
		-DUSE_HTTP_PARSER=system \
		-DUSE_HTTPS=OpenSSL \
		-DREGEX_BACKEND=pcre2 \
		-DUSE_BUNDLED_ZLIB=OFF \
		-DUSE_SSH=ON
	cmake --build build-shared

	cmake -B build-static -G Ninja \
		-DCMAKE_BUILD_TYPE=None \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=lib \
		-DUSE_HTTP_PARSER=system \
		-DUSE_HTTPS=OpenSSL \
		-DREGEX_BACKEND=pcre2 \
		-DUSE_BUNDLED_ZLIB=OFF \
		-DBUILD_TESTS=OFF \
		-DBUILD_SHARED_LIBS=OFF
	cmake --build build-static
}

check() {
	cd "$builddir"/build-shared
	# Test 'offline' fails on s390x, aarch64 and ppc64le
	ctest -E 'offline|online' --output-on-failure
}

package() {
	DESTDIR="$pkgdir" cmake --install build-static
	DESTDIR="$pkgdir" cmake --install build-shared

	mkdir -p "$pkgdir"/usr/src/$pkgname/
	cp -a tests "$pkgdir"/usr/src/$pkgname/tests
}

tools() {
	pkgdesc="$pkgdesc (tools)"

	amove usr/bin
}

tests() {
	pkgdesc="$pkgdesc (tests)"

	amove usr/src/$pkgname
}

sha512sums="
825737e4a1991fba50ea535f15b0e560ebe76ead752e04aeba36925b944d0da77fe9826a70980a1aa3d0bf9afbedfab79dd92e799c9252931384c89ebec9b012  libgit2-1.7.2.tar.gz
e2409f8e446c5742099787dedae12c5f4933ca95f3841e7ee571b85d6a862645defcfd38bb7b6607ff6e9f25544b9d5e0bb434aad0a7af6a2d22dcf27f2d29c7  fix-util-tests.patch
e116b403d1b8365e86837d15e6864220744e675d1ab06e5c310fd387a791e89a6627df7f45c656786361f6fc74e3d47457258aa0c3069d630b4cb1a2b32ac29f  missing-header.patch
0d4e3aa518a0a114b7b57244c5968c0d43d3ffc90e9b605e72886288a7d42b3a63d9665229e37ba2176d82f29f2eda8d40aba990c9b8688212d2b8c68465e1fc  no-online-test.patch
"
