# POWER Maintainer: Alexander Baldeck <alex.bldck@gmail.com>
# Maintainer: Jan Alexander Steffens (heftig) <heftig@archlinux.org>

pkgname=js115
pkgver=115.16.1
pkgrel=1
pkgdesc="JavaScript interpreter and libraries - Version 115"
url="https://spidermonkey.dev/"
arch=(x86_64 powerpc64le powerpc64 powerpc riscv64)
license=(MPL-2.0)
depends=(
  gcc-libs
  gtk3
  libpulse
  libxss
  libxt
  readline
  sh
  zlib
)
makedepends=(
  alsa-lib
  autoconf2.13
  cbindgen
  clang
  libpulse
  lld
  llvm
  nodejs
  python
  rust
  unzip
  zip
)
makedepends_x86_64=(lld nasm yasm)
checkdepends=(
  git
  mercurial
)
options=(!lto)
_relver=${pkgver}esr
source=(
  https://archive.mozilla.org/pub/firefox/releases/$_relver/source/firefox-$_relver.source.tar.xz{,.asc}
  sqlite-ppc.patch
  sandbox-fork.patch
  big-endian-image-decoders.patch
  fix-webrtc-glibcisms.patch
  ppc32-fix-build.patch
  power9-jit-744146.diff
  power9-jit-744147.diff
  power9-jit-744148.diff
  power9-jit-744150.diff
  power9-jit-744151.diff
  power9-jit-744152.diff
  power9-jit-744153.diff
  power9-jit-744154.diff
  power9-jit-744155.diff
)
sha256sums=('c3bb5a210738ca0f8cd26aa53fc4a07560db079c06cd0421cf35af4035029367'
            'SKIP'
            '5f0ca8811b4f4967b311270ccf0f5e1aca5917bec37eb54f2b241d0da1641a30'
            'ef9bd794b322482ec7ba217da0cc7cf9e079e05e27bdb37b241b9e16551295ad'
            '4eb95f37dfe07175a30d89b6b0a22a59adbc0bd211fedf975e450f8b407c6e45'
            '0b33247a87160cc2d80827c1c3ae6ca915f4ea72ecc41be9c496ff402b3a5252'
            'bb8aba9cb52ac497bb3f32c5b8d2fa44152b518913bc868d56bbbad90679e484'
            '7b98f766b632aab9c1247ecad0575b722fd8ff57ced3f5d8a9d2c8da6f6353d4'
            '75c96962ae0f1fffdd3e051bede12e7e0cdf2051c849cc7b03d462883a32bb39'
            'df2758a662f54270bf762e3afcb66b0ba287a56caf7d51202853ac6df81f4b1f'
            '76d8c2440c7c3acd4bd2542477f4ea2a67066e7642710d6f1c65c933ca042159'
            '05096b194379c68c8a8d8509d00dfb641859f52040c3771b337eca2ef1899f50'
            '3e2dfb0fe1602e8eba8d031a6477e07e112b6ed392a119c04bef07b5bf36739d'
            'd97aee7fd15ac27fe192eb3e412d3b0fb70427fed440e5b04525a487a55416b2'
            'abd67a5612bcb73a1fdf68753a076793deae609c26ff3adf21060cda610c363a'
            '6031f88d231b6f18c1ff482e364b3830b382b88365e11419092dcd2b81cb2817')
b2sums=('e54beb7036c89283fec40bbff1087cfdba5b1040d4655f7469aa4743d158bb2749dbeda15f950e47b42bd30c31e248c2ca690ac920007c04cd810bbda5abbdbd'
        'SKIP'
        '2100b761662aae4fa168081b78c287f7361c8c04304e192e29567d9e7e700940563f07030230d8b7ee6a997f4c98348a1500b2cbc98111dec073d174077baa14'
        '14eb4e04155cfc814aa0c9102152753cf647edf62d1ebfb95ddc1e6e00d32557418d1cd584dc143f2bf6c5bf5fe2f343dc9f65936848f242ad39788b232f4395'
        '5cbf300eb5706ccdc891c02076dc2607b12e218ba88963fde8fe1c3ae6ac2ea090d56b46cff4b4b4b02a8a3dbe6873c60f79088474f31db5f82189800eaeceaa'
        'a9e707ec00ea7dd55bd2cacd6f7fa94998d30ee471a8b0f7067fcca3b7456457ee9caa5f340192c9cf2e7ebae8d60ab8ed75d45e469bcb957c1a99706d8c1dd5'
        'b459f9264e7d1a431619d3e0a020f9cc22e273df75b512915f231e1c1fb2e55d5a8f5d2aedf1d23076b8a2f785918064014503b270656a7b56f021295eb2b730'
        '78a24d3978c542550e4af443862b0783736245c677569a2ef789936bd6a4146f720d21bb176b08b7b13855c1708358bc7906cf0689d6aa39f7e9814bda230647'
        '2e9ff759e7e3dbcdae1761e1d01ba7f97c8b9618de4a245ee49e15acc618d00427d21a38f262190805f2850d3d0358824354bf4d4b0b87508390e86b20b288bf'
        'a9fc1ae9c93625ec22c58f14c37e4f429601a11eeb58f7c8884e3ce58c486e6f11a1298b89568453215601bfa4a08549b6067db8ee15c054a2086cbe307ab519'
        '0a962b3fd6fedd73b18f9fab9a31d15a7c18979e4d1121a8e040e59543e7d6d8b29b4bec65c1e91ada0d20c79eb7ef74fb7316cd38a308f29275fa499153af34'
        '15e4a9f901810eb07d372ced410e3dd5fe5d2af430afb53850f0c55a187d5e764acbfccaa9213ee36d36fc9a7a0b8b8f105b2c4690e116c74b0a2a4c07be1d0b'
        'cbda0ce9105f01d5061b05b7a08286ae793aba8050a4a67a9768aae3990e8140bd003193d84abc237f1d0e3e24ee2d02729fed483b18e9f4e1b64c680f4d1816'
        '79027817bedc9ef1d3ea29441c4a1f9101b90fd273a474c86028c4e831323099188a19dfd13f77c0c85b762aa0daf62f6693db3b416151198a45c8d82ec6d1c7'
        '3166ab38322be04632a4b502107ef4197d1ef31cef155204cc2dbcc837a172090f04edd22c996ff0e212cb872568a44e82c8872c9f6a37e34ea910591403e927'
        'b34de2082ddc56b60667c2f809771ac2ad90cb556b843ea465e06fc2f1921070799ab2c9d20ccd779667518195370a9a29a3d86a295c1034fc987e19cbee963e')
validpgpkeys=(
  # Mozilla Software Releases <release@mozilla.com>
  # https://blog.mozilla.org/security/2023/05/11/updated-gpg-key-for-signing-firefox-releases/
  14F26682D0916CDD81E37B6D61B7B526D98F0353
)

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

prepare() {
  mkdir mozbuild
  cd firefox-$pkgver

  patch -Np1 -i ${srcdir}/sqlite-ppc.patch
  patch -Np1 -i ${srcdir}/sandbox-fork.patch
  patch -Np1 -i ${srcdir}/big-endian-image-decoders.patch
  patch -Np1 -i ${srcdir}/fix-webrtc-glibcisms.patch
  patch -Np1 -i ${srcdir}/ppc32-fix-build.patch

  patch -Np1 -i ${srcdir}/power9-jit-744146.diff
  patch -Np1 -i ${srcdir}/power9-jit-744147.diff
  patch -Np1 -i ${srcdir}/power9-jit-744148.diff
  patch -Np1 -i ${srcdir}/power9-jit-744150.diff
  patch -Np1 -i ${srcdir}/power9-jit-744151.diff
  patch -Np1 -i ${srcdir}/power9-jit-744152.diff
  patch -Np1 -i ${srcdir}/power9-jit-744153.diff
  patch -Np1 -i ${srcdir}/power9-jit-744154.diff
  patch -Np1 -i ${srcdir}/power9-jit-744155.diff

  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 --enable-hardening
ac_add_options --enable-optimize
ac_add_options --enable-rust-simd
ac_add_options --disable-bootstrap
ac_add_options --disable-debug
ac_add_options --disable-jemalloc
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

case "${CARCH}" in
  powerpc64le)
    echo 'ac_add_options --enable-jit' >> ../mozconfig
    echo 'ac_add_options --enable-lto=full' >> ../mozconfig
    echo 'ac_add_options --enable-linker=bfd' >> ../mozconfig
    #echo 'ac_add_options --without-wasm-sandboxed-libraries' >> ../mozconfig
  ;;
  x86_64)
    echo 'ac_add_options --enable-jit' >> ../mozconfig
    echo 'ac_add_options --enable-linker=lld' >> ../mozconfig
  ;;
  *)
    echo 'ac_add_options --disable-jit' >> ../mozconfig
    echo 'ac_add_options --enable-lto=full' >> ../mozconfig
    echo 'ac_add_options --enable-linker=bfd' >> ../mozconfig
  ;;
esac

}

build() {
  cd firefox-$pkgver

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

  export MOZ_BUILD_DATE=$(head -1 sourcestamp.txt)
  export RUSTFLAGS="-C debuginfo=1"
  export RUSTC_OPT_LEVEL=2

  # 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}"

  # Greatly reduce size of relocation tables
  # https://gitlab.archlinux.org/archlinux/rfcs/-/blob/master/rfcs/0023-pack-relative-relocs.rst
  LDFLAGS+=" -Wl,-z,pack-relative-relocs"
 
  # LTO needs more open files
  ulimit -n 4096

  case "${CARCH}" in
    powerpc*)
      export CC=${CHOST}-gcc
      export CXX=${CHOST}-g++
    ;;
  esac

  case "${CARCH}" in
    x86_64)
  # Do 3-tier PGO
  echo "Building instrumented JS..."
  cat >.mozconfig ../mozconfig - <<END
ac_add_options --enable-profile-generate
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-lto=full
ac_add_options --enable-profile-use
ac_add_options --with-pgo-profile-path=${PWD@Q}/merged.profdata
END
    ;;
  esac
  ./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:
