# Maintainer: Jan Alexander Steffens (heftig) <jan.steffens@gmail.com>

# ALARM: Kevin Mihelich <kevin@archlinuxarm.org>
#  - use tarball source
#  - remove -flto from CFLAGS
#  - remove --enable-gold

pkgname=js52
pkgver=52.9.0
pkgrel=2
_ffver=${pkgver%%.*}
pkgdesc="JavaScript interpreter and libraries - Version $_ffver"
arch=(x86_64)
url="https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey"
license=(MPL)
depends=(gcc-libs readline zlib sh)
makedepends=(python2 zip autoconf2.13 mercurial)
_repo=https://hg.mozilla.org/mozilla-unified
source=("https://hg.mozilla.org/mozilla-unified/archive/FIREFOX_${pkgver//./_}esr_RELEASE.tar.gz"
        mozjs52-copy-headers.patch
        mozjs52-disable-mozglue.patch
        mozjs52-fix-soname.patch
        mozjs52-include-configure-script.patch)
sha256sums=('9603829c4ca04ec1c6eae74071b92f7ff61be842e5b554246a725d8a2ac1db32'
            'c5b3a88e4d10ef149aba6fc48d431db54ff266323fa22a5d549028fd794423cc'
            '5a84f02521f37de873991dd360a4c4bfdbdd2fb4a218e11be73f9cbbf02050e8'
            '728456fd9e66b69d6e0688c75e50091dc56735004ecf5f649212a83fe3087df1'
            'd91a89acd88bfc747a255050757a0c17139bf5c3508c2e1c3c6bb2056786a344')

prepare() {
  cd mozilla-unified-FIREFOX_${pkgver//./_}esr_RELEASE
  mkdir obj

  # https://salsa.debian.org/gnome-team/mozjs52/tree/debian/master/debian/patches
  patch -Np1 -i ../mozjs52-fix-soname.patch
  patch -Np1 -i ../mozjs52-copy-headers.patch
  patch -Np1 -i ../mozjs52-disable-mozglue.patch
  patch -Np1 -i ../mozjs52-include-configure-script.patch
}

build() {
  local configure_args=(
    --prefix=/usr
    --disable-debug
    --disable-debug-symbols
    --disable-strip
    --enable-optimize="-O2"
    --enable-pie
    --enable-posix-nspr-emulation
    --enable-readline
    --enable-release
    --enable-shared-js
    --enable-tests
    --with-intl-api
    --with-system-zlib
    --without-system-icu
  )

  cd mozilla-unified-FIREFOX_${pkgver//./_}esr_RELEASE/obj

  unset CPPFLAGS
  CFLAGS+=' -fno-delete-null-pointer-checks -fno-strict-aliasing -fno-tree-vrp'
  CXXFLAGS+=' -fno-delete-null-pointer-checks -fno-strict-aliasing -fno-tree-vrp'
  export CC=gcc CXX=g++ PYTHON=/usr/bin/python2

  sh ../js/src/configure "${configure_args[@]}"
  make
}

check() {
  local jstests_extra_args=(
    --format=none
    --exclude-random
  ) jittest_extra_args=(
    --format=none
    --timeout 300
  ) jittest_test_args=(
    basic
  )

  cd mozilla-unified/obj
  make -C js/src check-jstests check-jit-test \
    JSTESTS_EXTRA_ARGS="${jstests_extra_args[*]}" \
    JITTEST_EXTRA_ARGS="${jittest_extra_args[*]}" \
    JITTEST_TEST_ARGS="${jittest_test_args[*]}"
}

package() {
  cd mozilla-unified-FIREFOX_${pkgver//./_}esr_RELEASE/obj
  make DESTDIR="$pkgdir" install
  rm "$pkgdir"/usr/lib/*.ajs
  find "$pkgdir"/usr/{lib/pkgconfig,include} -type f -exec chmod -c a-x {} +
}

# vim:set ts=2 sw=2 et: