# Maintainer: Alexey Pavlov <alexpux@gmail.com>

pkgbase=libffi
pkgname=('libffi' 'libffi-devel')
pkgver=3.3
pkgrel=1
pkgdesc="Portable, high level programming interface to various calling conventions"
arch=('i686' 'x86_64')
url="https://sourceware.org/libffi/"
groups=('libraries')
license=('MIT')
depends=()
checkdepends=('dejagnu')
#options=('!strip' 'debug')
install=libffi.install
source=(https://sourceware.org/pub/libffi/libffi-${pkgver}.tar.gz
        libffi-3.3-msysize.patch)
sha256sums=('72fba7922703ddfa7a028d513ac15a85c8d54c8d67f55fa5a4802885dc652056'
            '8fbdab06b3c42561060d73aae2c1cfdc535797e96f53411eab0fe44869203246')

prepare() {
  cd ${pkgname}-${pkgver}
  patch -p1 -i ${srcdir}/libffi-3.3-msysize.patch

  autoreconf -fi
}

build() {
  cd ${pkgname}-${pkgver}
  # Perform the build outside of srcdir so that
  # srcdir is not simplified to '.' as relative
  # paths in debug information are not ideal.
  [[ -d build ]] && rm -rf build
  mkdir build && cd build
  local -a extra_config
  if check_option "debug" "y"; then
    extra_config+=("--enable-debug")
  fi
  ${srcdir}/${pkgname}-${pkgver}/configure \
    --prefix=/usr \
    --srcdir=${srcdir}/${pkgname}-${pkgver} \
    --build=${CHOST} \
    --enable-static \
    --enable-shared \
    "${extra_config[@]}"
  make
  make DESTDIR="${srcdir}/dest" install
}

check() {
  cd ${pkgname}-${pkgver}/build
  make check
}

package_libffi() {
  mkdir -p ${pkgdir}/usr/bin
  cp -f ${srcdir}/dest/usr/bin/*.dll ${pkgdir}/usr/bin/
  cp -rf ${srcdir}/dest/usr/share ${pkgdir}/usr/
  install -Dm644 ${srcdir}/${pkgname}-${pkgver}/LICENSE "${pkgdir}"/usr/share/licenses/${pkgname}/LICENSE
}

package_libffi-devel() {
  pkgdesc="Libffi headers and libraries"
  groups=('development')
  if check_option "debug" "y"; then
    options=('staticlibs' '!strip' 'debug')
  else
    options=('staticlibs' '!strip' 'debug')
  fi
  depends=("libffi=${pkgver}")

  mkdir -p ${pkgdir}/usr
  cp -rf ${srcdir}/dest/usr/include ${pkgdir}/usr/
  cp -rf ${srcdir}/dest/usr/lib ${pkgdir}/usr/
}
