# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=binutils
pkgver=2.44
pkgrel=2
pkgdesc="Tools necessary to build programs"
url="https://www.gnu.org/software/binutils/"
makedepends_build="bison flex texinfo"
makedepends_host="zlib-dev"
arch="all"
license="GPL-2.0-or-later AND LGPL-2.1-or-later AND BSD-3-Clause"
subpackages="$pkgname-dev $pkgname-doc"
source="https://ftp.gnu.org/gnu/binutils/binutils-with-gold-$pkgver.tar.xz
	0001-Revert-PR25882-.gnu.attributes-are-not-checked-for-s.patch
	binutils-ld-fix-static-linking.patch
	CVE-2025-1153.patch
	CVE-2025-3198.patch
	"
builddir="$srcdir/$pkgname-with-gold-$pkgver"
options="!check"

if [ -z "$BOOTSTRAP" ]; then
	makedepends_host="$makedepends_host jansson-dev zstd-dev"
	_noboot_configure="--enable-jansson --with-zstd"
fi

_gold_configure="--disable-gold"
if [ "$CHOST" = "$CBUILD" ] && [ "$CBUILD" = "$CTARGET" ] && [ "$CTARGET_ARCH" != "riscv64" ] && [ "$CTARGET_ARCH" != "loongarch64" ]; then
	subpackages="$subpackages $pkgname-gold"
	_gold_configure="--enable-gold"
fi

if [ "$CHOST" != "$CTARGET" ]; then
	pkgname="$pkgname-$CTARGET_ARCH"
	subpackages=""
	sonameprefix="$pkgname:"
fi

# secfixes:
#   2.44-r2:
#     - CVE-2025-1153
#     - CVE-2025-3198
#   2.44-r0:
#     - CVE-2025-0840
#   2.40-r10:
#     - CVE-2023-1972
#   2.40-r0:
#     - CVE-2023-1579
#   2.39-r2:
#     - CVE-2022-38533
#   2.39-r0:
#     - CVE-2022-38126
#   2.35.2-r1:
#     - CVE-2021-3487
#   2.32-r0:
#     - CVE-2018-19931
#     - CVE-2018-19932
#     - CVE-2018-20002
#     - CVE-2018-20712
#   2.28-r1:
#     - CVE-2017-7614

build() {
	local _sysroot=/
	local _cross_configure="--enable-install-libiberty --enable-shared --enable-plugins"
	local _arch_configure=""

	if [ "$CHOST" != "$CTARGET" ]; then
		_sysroot="$CBUILDROOT"
		_cross_configure="--disable-install-libiberty --disable-plugins"
	fi

	if [ "$CTARGET_ARCH" = "x86_64" ]; then
		_arch_configure="--enable-targets=x86_64-pep"
	fi

	CFLAGS="$CFLAGS -O2" \
	CXXFLAGS="$CXXFLAGS -O2" \
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--target=$CTARGET \
		--with-build-sysroot="$CBUILDROOT" \
		--with-sysroot=$_sysroot \
		--prefix=/usr \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info \
		--sysconfdir=/etc \
		$_arch_configure \
		$_cross_configure \
		$_gold_configure \
		$_noboot_configure \
		--disable-gdb \
		--disable-gprofng \
		--disable-multilib \
		--disable-nls \
		--disable-werror \
		--enable-64-bit-bfd \
		--enable-colored-disassembly \
		--enable-default-execstack=no \
		--enable-default-hash-style=gnu \
		--enable-deterministic-archives \
		--enable-ld=default \
		--enable-new-dtags \
		--enable-relro \
		--enable-threads \
		--with-bugurl="https://gitlab.alpinelinux.org/alpine/aports/-/issues" \
		--with-mmap \
		--with-pic \
		--with-system-zlib
	make
}

package() {
	make install DESTDIR="$pkgdir"
	if [ -d "$pkgdir"/usr/lib64 ]; then
		mv "$pkgdir"/usr/lib64/* "$pkgdir"/usr/lib/
		rmdir "$pkgdir"/usr/lib64
	fi
	if [ "$CHOST" != "$CTARGET" ]; then
		# creating cross tools: remove any files that would conflict
		# with the native tools, or other cross tools
		rm -r "${pkgdir:?}"/usr/share "${pkgdir:?}"/usr/lib
	fi
}

gold() {
	pkgdesc="GNU binutils - gold linker (deprecated)"

	amove usr/bin/ld.gold
	amove usr/$CTARGET/bin/ld.gold
}

sha512sums="
1d8688f1b6f1bc64cfd1f82e3af05b8ace1d709d4fa819acb4d12d74e1b69b4314d98fb4281dacc9b5fa51ff7370d0249f540ec016fd137aa701502d72426301  binutils-with-gold-2.44.tar.xz
70ec22bd72ef6dddecfd970613387dd4a8cdc8730dd3cbf03d5a0c3a7c4d839383167bb06dad21bf7c235329fd44b5dc4aefe762f68544f17155cf002bf1be4a  0001-Revert-PR25882-.gnu.attributes-are-not-checked-for-s.patch
ecee33b0e435aa704af1c334e560f201638ff79e199aa11ed78a72f7c9b46f85fbb227af5748e735fd681d1965fcc42ac81b0c8824e540430ce0c706c81e8b49  binutils-ld-fix-static-linking.patch
48f718ea70b394ac24087fded3634b6cb6b99f6082e95952a02ea7551cf56bef24bcd8ec5877c4e228ea8a750d52117d977bb461f07b5ac84521a15c2a7418df  CVE-2025-1153.patch
0b529cb91c83bb24df02042832e383cd18bbd70947e7db16c76b33c2a13b11c238e647d1bfd79d7c477fe739567972567da202c598d0aae59e231f9cad86ca2e  CVE-2025-3198.patch
"
