# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=abuild
pkgver=3.14.1
_ver=${pkgver%_git*}
pkgrel=4
pkgdesc="Script to build Alpine Packages"
url="https://gitlab.alpinelinux.org/alpine/abuild/"
arch="all"
license="GPL-2.0-only"
depends="
	apk-tools>=2.0.7-r1
	cmd:getcap
	fakeroot
	musl-utils
	lzip
	openssl>3
	patch
	pkgconf
	scanelf
	tar
	"
makedepends_build="pkgconfig scdoc"
makedepends_host="openssl-dev>3 zlib-dev"
makedepends="$makedepends_host $makedepends_build"
checkdepends="cmd:setcap kyua git"
install="$pkgname.pre-install $pkgname.pre-upgrade"
subpackages="
	apkbuild-cpan:cpan:noarch
	apkbuild-gem-resolver:gems:noarch
	apkbuild-pypi:pypi:noarch
	abuild-rootbld:_rootbld:noarch
	abuild-sudo:_sudo
	$pkgname-doc
	"
options="suid"
pkggroups="abuild"
source="https://gitlab.alpinelinux.org/alpine/abuild/-/archive/$pkgver/abuild-$pkgver.tar.gz
	tar-arguments.patch
	0001-abuild-add-option-C-DIR-to-allow-change-directory.patch
	"
builddir="$srcdir"/abuild-$pkgver

build() {
	make VERSION="$pkgver-r$pkgrel"
}

check() {
	make check
}

package() {
	make install VERSION="$pkgver-r$pkgrel" DESTDIR="$pkgdir"

	install -m 644 abuild.conf "$pkgdir"/etc/abuild.conf

	case "$CARCH" in
	aarch64|x86*|ppc64le)
		# binutils only supports it here
		cat >>"$pkgdir"/usr/share/abuild/default.conf <<-EOF

		# binutils ld.bfd supports this on this architecture; default to it
		export RUSTFLAGS="\${RUSTFLAGS-} -Clink-arg=-Wl,-z,pack-relative-relocs"
		export LDFLAGS="\$LDFLAGS -Wl,-z,pack-relative-relocs"
		EOF
	esac

	case "$CARCH" in
	x86_64)
		# https://lists.alpinelinux.org/~alpine/devel/%3C1628515011.zujvcn248v.none%40localhost%3E
		# note that this is x86-exclusive. on other architectures, this is pretty much always bad
		# https://github.com/rust-lang/rust/pull/106380
		cat >>"$pkgdir"/usr/share/abuild/default.conf <<-EOF

		# -fno-plt has very slight improvements to general code size and speed on x86-only,
		# for the common system dynamic linking case
		export CFLAGS="\$CFLAGS -fno-plt"
		export CXXFLAGS="\$CXXFLAGS -fno-plt"
		EOF
		;;
	esac

	install -d -m 775 -g abuild "$pkgdir"/var/cache/distfiles
}

cpan() {
	pkgdesc="Script to generate perl APKBUILD from CPAN"
	depends="perl perl-libwww perl-json perl-module-build perl-module-build-tiny
		perl-lwp-protocol-https"

	amove usr/bin/apkbuild-cpan
}

gems() {
	pkgdesc="APKBUILD dependency resolver for RubyGems"
	depends="ruby ruby-augeas"

	amove usr/bin/apkbuild-gem-resolver
}

pypi() {
	pkgdesc="Script to generate python3 APKBUILD from PYPI"
	depends="perl perl-libwww perl-json perl-module-build-tiny perl-lwp-protocol-https
	perl-ipc-system-simple"

	amove usr/bin/apkbuild-pypi
}

_rootbld() {
	pkgdesc="Build packages in chroot"
	depends="abuild bubblewrap cmd:envsubst git"
	mkdir -p "$subpkgdir"
}

_sudo() {
	pkgdesc="Simple privilege elevation tools for building packages"
	depends=""
	install_if="$pkgname=$pkgver-r$pkgrel"

	amove usr/bin/abuild-sudo usr/bin/abuild-apk usr/bin/abuild-adduser \
		usr/bin/abuild-addgroup
}

sha512sums="
49e243114933f3013884f0014fc8e5f142ff146de3d9ddb62e5ad98689cb1bf2bc8172da0331943f44b106dca4fd6fcdd917f5439e82facbb97eccc0f3459f37  abuild-3.14.1.tar.gz
824e936eed3699e8b18ad0c9a2462ea2ec0995111edbb595188b29a2e728b7ec9a4c445fba706d36c9954a6d349bb92b4eff0f20715758051f14065208a9778f  tar-arguments.patch
4b43ed25d97593996b4154ef6f2e4253ddfb1b8cc85fba18c5dd15e01c9ee8fbb8b3db80203455a1120f779615d834e79c600d40e26ee97f64ff56fe28c44d46  0001-abuild-add-option-C-DIR-to-allow-change-directory.patch
"
