_pkgname=alsa-lib
pkgname=static-compat-$_pkgname
pkgver=1.2.10
pkgrel=3
pkgdesc="An alternative implementation of Linux sound support"
arch=(x86_64)
url="https://www.alsa-project.org"
license=(LGPL-2.1-or-later)
depends=('gcc-libs-static-compat')
makedepends=('static-compat-configure')
source=(
  https://www.alsa-project.org/files/pub/lib/$_pkgname-$pkgver.tar.bz2{,.sig}
  $_pkgname-1.2.10-config_header.patch::https://github.com/alsa-project/alsa-lib/commit/0e3dfb9f705ca78be34cd70fd59d67c431e29cc7.patch
)
sha512sums=('4ccbd1dc5a612044571c26290923009e4c3f7959b30a5d0bed47daa68bbefaff9059c4f0fa3bc16f22c1eed2d36f079139369f40243da5921ae4de02a4541939'
            'SKIP'
            '4c10050fb92da9c61e08eb742b60245314362bf8b7f5c34d5edade2f0bcaa0930c1d30267db0a95de2391c01a4aa59f5565423a1c41e42471f3dc9614b4d9b8a')
b2sums=('b2e4f8431e61f5bb56b2b5d124e67d5a68bbca3c647bebfa93f5e5ff092ec9ef3f6cb6315801fcd93e21151784814ff238d357313b8b44f32d4e7c9ee565388f'
        'SKIP'
        '73ce802cfcefed592d3820e4e8fdc2cfcb0666e60e07ede87f5581acf751e2c7552bc9347c1075552337047ec4c2cb64dbc92433d4e4b2e52cf6cad36f2679f2')
validpgpkeys=('F04DF50737AC1A884C4B3D718380596DA6E59C91') # ALSA Release Team (Package Signing Key v1) <release@alsa-project.org>

prepare() {
  source static-compat-environment
  # fix crashes with 32bit applications: https://bugs.archlinux.org/task/79628
  patch -Np1 -d $_pkgname-$pkgver -i ../$_pkgname-1.2.10-config_header.patch
  cd $_pkgname-$pkgver
  autoreconf -fiv
}

build() {
  source static-compat-environment
  # -flto=auto is not supported: https://github.com/alsa-project/alsa-lib/issues/110
  CFLAGS+=" -flto-partition=none"
  cd $_pkgname-$pkgver
  static-compat-configure --prefix=/usr --disable-shared --enable-static --without-debug
  # prevent excessive overlinking due to libtool
  sed -i -e 's/ -shared / -Wl,-O1,--as-needed\0/g' libtool
  make
}

check() {
  source static-compat-environment
  export LD_LIBRARY_PATH="$_pkgname-$pkgver/src/.libs/:$LD_LIBRARY_PATH"
  make -k check -C $_pkgname-$pkgver || true
}

package() {
  source static-compat-environment
  make DESTDIR="$pkgdir" install -C $_pkgname-$pkgver
  rm -r $pkgdir/usr/{bin,share/alsa,share/aclocal}
}
