# Contributor: Pedro Filipe <xpecex@outlook.com>
# Contributor: Stefan Wagner <stw@bit-strickerei.de>
# Maintainer: Marian Buschsieweke <marian.buschsieweke@posteo.net>
pkgname=gcc-avr
pkgver=15.1.0
pkgrel=0
pkgdesc="The GNU Compiler Collection for AVR targets"
url="https://gcc.gnu.org/"
depends="binutils-avr"
makedepends="
	bash
	gawk
	gmp-dev
	isl-dev
	linux-headers
	mpc1-dev
	mpfr-dev
	zlib-dev
	"
arch="all"
license="GPL-3.0-or-later"
subpackages="$pkgname-doc"
source="
	https://mirrors.kernel.org/gnu/gcc/gcc-$pkgver/gcc-$pkgver.tar.xz

	fix-no-system-headers.patch
	"

builddir="$srcdir/gcc-$pkgver"
_workingdir="$builddir/build"

build() {
	mkdir "$_workingdir"
	cd "$_workingdir"

	export CFLAGS_FOR_TARGET='-pipe -Os -ffunction-sections -fdata-sections'
	export CXXFLAGS_FOR_TARGET='-pipe -Os -ffunction-sections -fdata-sections -fno-use-cxa-atexit'

	# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100431
	export CFLAGS="$CFLAGS -Wno-error=format-security"
	export CXXFLAGS="$CXXFLAGS -Wno-error=format-security"
	export CPPFLAGS="$CPPFLAGS -Wno-error=format-security"

	"$builddir"/configure \
		--target=avr \
		--prefix=/usr \
		--with-sysroot=/usr/avr \
		--with-native-system-header-dir=/include \
		--with-headers=/usr/avr/include \
		--with-python-dir=share/gcc-avr \
		--libexecdir=/usr/lib \
		--infodir=/deleteme/info \
		--htmldir=/deleteme/html \
		--pdfdir=/deleteme/pdf \
		--enable-languages=c,c++ \
		--disable-__cxa_atexit \
		--disable-install-libiberty \
		--disable-libssp \
		--disable-libstdcxx-pch \
		--disable-libunwind-exceptions \
		--disable-linker-build-id \
		--disable-nls \
		--disable-werror \
		--with-gnu-as \
		--with-gnu-ld \
		--with-system-zlib \
		--with-isl \
		--with-avrlibc \
		--with-pkgversion='Alpine Linux'
	make
}

package() {
	cd "$_workingdir"
	make install DESTDIR="$pkgdir"
	rm -rf "$pkgdir"/usr/share/man/man7
	rm -f "$pkgdir"/usr/lib/libcc1.*

	# remove unneeded doc
	rm -rf "$subpkgdir"/deleteme
	find "$pkgdir"/usr/lib -type f -name "*.a" -exec /usr/bin/avr-strip --strip-debug '{}' \;
}

sha512sums="
ddd35ca6c653dffa88f7c7ef9ee4cd806e156e0f3b30f4d63e75a8363361285cd566ee73127734cde6a934611de815bee3e32e24bfd2e0ab9f7ff35c929821c1  gcc-15.1.0.tar.xz
99bbe52c3c411e5a5af47a2539524f036190ab34de2eff762554c65936ee9ed713b91fc605bcbc5f11e740580356748d40c520d915687a77ea81ae3ef31bfb0d  fix-no-system-headers.patch
"
