#!/bin/sh
#
# Copyright (c) 2019 David Demelier <markand@malikania.fr>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

PKGNAME=elfutils
PKGVERSION=0.176
PKGREVISION=1
PKGLICENSE="GPLv2+ GPLv3+ LGPLv3+"
PKGSUMMARY="utilities to inspect ELF files"
PKGDOWNLOAD="https://sourceware.org/$PKGNAME/ftp/$PKGVERSION/$PKGNAME-$PKGVERSION.tar.bz2"
PKGDEPENDS="lib/zlib lib/musl-fts lib/musl-obstack"

: ${CHOST:=$(uname -m)-linux-musl}
: ${CBUILD:=$(uname -m)-linux-musl}
: ${CC:=gcc}
: ${CFLAGS:=-O2 -Wno-error}
: ${LDFLAGS:=}
: ${LIBS:=}

if [ "$(uname -m)" = "x86_64" ]; then
	CFLAGS="-fPIC $CFLAGS"
fi

build()
{
	rm -rf $PKGNAME-$PKGVERSION
	tar xvaf $PKGNAME-$PKGVERSION.tar.bz2
	pushd $PKGNAME-$PKGVERSION

	# credits to void linux and alpine linux.
	# https://github.com/void-linux/void-packages/tree/master/srcpkgs/elfutils
	# https://git.alpinelinux.org/aports/tree/main/elfutils/
	patch -p0 < ../musl.patch
	autoreconf -fi
	CC="$CC" \
	CFLAGS="$CFLAGS" \
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--program-prefix="eu-" \
		--disable-nls
	make
	make install DESTDIR=$DESTDIR
	rm -f $DESTDIR/usr/lib/lib{asm,dw,ebl,elf}.a

	popd
	rm -rf $PKGNAME-$PKGVERSION
}
