# Maintainer: Jan Alexander Steffens (heftig) <heftig@archlinux.org>

# ALARM: Kevin Mihelich <kevin@archlinuxarm.org>
#  - configure with --disable-lto, --disable-rust-simd

pkgname=js91
pkgver=91.13.0
pkgrel=2
pkgdesc="JavaScript interpreter and libraries - Version 91"
url="https://spidermonkey.dev/"
arch=(x86_64)
license=(MPL-2.0)
depends=(
  gcc-libs
  readline
  sh
  zlib
)
makedepends=(
  autoconf2.13
  clang
  lld
  llvm
  python
  python-setuptools
  rustup
  zip
)
checkdepends=(
  git
  mercurial
)
options=(!lto)
_relver=${pkgver}esr
source=(
  https://archive.mozilla.org/pub/firefox/releases/$_relver/source/firefox-$_relver.source.tar.xz{,.asc}
  0001-Bug-1769631-Remove-U-from-mode-parameters-for-variou.patch
)
validpgpkeys=(
  # Mozilla Software Releases <release@mozilla.com>
  # https://blog.mozilla.org/security/2023/05/11/updated-gpg-key-for-signing-firefox-releases/
  14F26682D0916CDD81E37B6D61B7B526D98F0353
)
sha256sums=('53be2bcde0b5ee3ec106bd8ba06b8ae95e7d489c484e881dfbe5360e4c920762'
            'SKIP'
            '1b6fcec05714c0fbc52549059d570dddd79302ca787dec9983b046d54fa7a6ff')
b2sums=('75d0daa512b3a2d41974a0169778be9ef19a100de2bd382add9831860ca93976585a41e760b6a7ec753268fb78f9f61049780fa7961834248bc8157cfdcc2827'
        'SKIP'
        '216206c7f0356267f4f29880b8b8e8e388f9dd483a56cd427de57756d64ef26ea6d6b36e33ca96dbc54c45bf78bbf2b9a666aa8b2029fa28d07dc41342101158')

# Make sure the duplication between bin and lib is found
COMPRESSZST+=(--long)

prepare() {
  # packed_simd no longer builds with 1.63.0
  rustup toolchain update --profile minimal 1.62.1
  rustup default 1.62.1

  mkdir mozbuild
  cd firefox-$pkgver

  # Python 3.11
  patch -Np1 -i ../0001-Bug-1769631-Remove-U-from-mode-parameters-for-variou.patch

  cat >../mozconfig <<END
ac_add_options --enable-application=js
mk_add_options MOZ_OBJDIR=${PWD@Q}/obj

ac_add_options --prefix=/usr
ac_add_options --enable-release
ac_add_options --disable-lto
ac_add_options --enable-hardening
ac_add_options --enable-optimize
ac_add_options --disable-rust-simd
ac_add_options --enable-linker=lld
ac_add_options --disable-bootstrap
ac_add_options --disable-debug
ac_add_options --disable-jemalloc
ac_add_options --disable-new-pass-manager
ac_add_options --disable-strip

# System libraries
ac_add_options --with-system-zlib
ac_add_options --without-system-icu

# Features
ac_add_options --enable-readline
ac_add_options --enable-shared-js
ac_add_options --enable-tests
ac_add_options --with-intl-api
END
}

build() {
  cd firefox-$pkgver

  export MACH_USE_SYSTEM_PYTHON=1
  export MOZBUILD_STATE_PATH="$srcdir/mozbuild"
  export MOZ_BUILD_DATE="$(date -u${SOURCE_DATE_EPOCH:+d @$SOURCE_DATE_EPOCH} +%Y%m%d%H%M%S)"
  export MOZ_NOSPAM=1

  # malloc_usable_size is used in various parts of the codebase
  CFLAGS="${CFLAGS/_FORTIFY_SOURCE=3/_FORTIFY_SOURCE=2}"
  CXXFLAGS="${CXXFLAGS/_FORTIFY_SOURCE=3/_FORTIFY_SOURCE=2}"

  # Do 3-tier PGO
  echo "Building instrumented JS..."
  cat >.mozconfig ../mozconfig - <<END
ac_add_options --enable-profile-generate=cross
END
  ./mach build

  echo "Profiling instrumented JS..."
  (
    local js="$PWD/obj/dist/bin/js"
    export LLVM_PROFILE_FILE="$PWD/js-%p-%m.profraw"

    cd js/src/octane
    "$js" run.js

    cd ../../../third_party/webkit/PerformanceTests/ARES-6
    "$js" cli.js

    cd ../SunSpider/sunspider-0.9.1
    "$js" sunspider-standalone-driver.js
  )

  llvm-profdata merge -o merged.profdata *.profraw

  stat -c "Profile data found (%s bytes)" merged.profdata
  test -s merged.profdata

  echo "Removing instrumented JS..."
  ./mach clobber

  echo "Building optimized JS..."
  cat >.mozconfig ../mozconfig - <<END
ac_add_options --enable-profile-use=cross
ac_add_options --with-pgo-profile-path=${PWD@Q}/merged.profdata
END
  ./mach build
}

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

  cd firefox-$pkgver/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 firefox-$pkgver/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 sw=2 sts=-1 et: