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

_realname=libvpx
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.10.0
pkgrel=1
pkgdesc="The VP8/VP9 Codec SDK (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
license=('BSD')
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-yasm")
url="https://www.webmproject.org/"
source=(${_realname}-${pkgver}.tar.gz::https://github.com/webmproject/libvpx/archive/v${pkgver}.tar.gz
        0001-enable-shared-on.mingw.patch
        0003-cross-with-native-binutils.mingw.patch
        0005-fix-exports.mingw.patch)
options=('strip' 'staticlibs')
sha256sums=('85803ccbdbdd7a3b03d930187cb055f1353596969c1f92ebec2db839fa4f834a'
            'f37a24525725cad215d73c538c0b06343b043f2b07553e7dc25835bd68af0695'
            'fabe7d4a756b33016947f741b1b5d8cbbf9aefd8862240645d0ed7bf7b4a3739'
            '7c511b5df935278da2cba32b03b90582f360ab6786a5e32028bc82025193fabd')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  msg2 "Applying $1"
  patch -Np1 -i "${srcdir}"/$1
}

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f $_fname ]; then
      rm -rf $_fname
    fi
  done
}
# =========================================== #


prepare() {
  cd ${srcdir}/${_realname}-${pkgver}

  apply_patch_with_msg 0001-enable-shared-on.mingw.patch
  apply_patch_with_msg 0003-cross-with-native-binutils.mingw.patch
  apply_patch_with_msg 0005-fix-exports.mingw.patch
}

build() {
  if [ ${MINGW_CHOST} = "i686-w64-mingw32" ]; then
    _targetarch="x86-win32-gcc"
    EXTRA_CFLAGS="-mstackrealign"
  else
    _targetarch="x86_64-win64-gcc"
  fi
  mkdir -p ${srcdir}/build-${MINGW_CHOST}
  cd "${srcdir}/build-${MINGW_CHOST}"
  CFLAGS="$EXTRA_CFLAGS $CFLAGS -fno-asynchronous-unwind-tables" \
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --target=${_targetarch} \
    --enable-vp8 \
    --enable-vp9 \
    --enable-vp9-highbitdepth \
    --enable-vp9-temporal-denoising \
    --enable-runtime-cpu-detect \
    --enable-postproc \
    --enable-pic \
    --enable-shared \
    --enable-static \
    --enable-experimental \
    --disable-examples \
    --disable-docs \
    --disable-install-docs \
    --disable-install-srcs \
    --disable-unit-tests

  for _ff in *.mk; do
    sed -i "s/HAVE_GNU_STRIP=yes/HAVE_GNU_STRIP=no/g" $_ff
  done
  make #-j1
}

#These tests seem to take a lot of time
#check() {
#  cd "${srcdir}/build-$MINGW_CHOST"
#  make test
#}

package() {
  cd "${srcdir}/build-$MINGW_CHOST"
  make DIST_DIR="${pkgdir}${MINGW_PREFIX}" install
}
