# Maintainer: PktSurf <smlinux@pktsurf.in>
app=firefox
version=115.5.0
build=1sml
homepage="http://www.mozilla.org/projects/firefox/"
download="https://archive.mozilla.org/pub/firefox/releases/$version/source/firefox-"$version"esr.source.tar.xz'"
desc="Web browser from mozilla.org based on Gecko engine"
requires="zip unzip yasm libevent alsa-lib libpng icu hunspell python3 diffutils llvm imake libxt gtk3 dbus-glib ffmpeg clang nodejs mozilla-nss lld"

prepbuilddir() {
  mkandenterbuilddir
  rm -rf $app-$version

  tar xf $srcdir/$app-"$version"esr.source.tar.?z
  cd $app-$version
  fixbuilddirpermissions

  # We require gcc 9+ if we have to use the lld linker.

  # I don't know of a proper way to point this project to the GCC headers
  # location that the toolchain comes with. So I've put all the GCC headers
  # in /usr/local/include

  # In case of firefox, old habits die hard. This thing still depends on autoconf 2.13
  mkdir -p AC213
  tar xf "$srcdir"/autoconf-2.13.tar.gz
  cd autoconf-2.13
  ./configure --prefix="$PWD"/../AC213 --program-suffix=-2.13
  make && make install
  cd ..
  export PATH="$PWD"/AC213/bin:"$PATH"

  applypatch $srcdir/audio-lfs64.patch
  applypatch $srcdir/disable-moz-stackwalk.patch
  applypatch $srcdir/esr-metainfo.patch
  applypatch $srcdir/fix-fortify-system-wrappers.patch
  applypatch $srcdir/fix-rust-target.patch
  applypatch $srcdir/fix-webrtc-glibcisms.patch
  #applypatch $srcdir/icu74.patch
  applypatch $srcdir/lfs64.patch
  applypatch $srcdir/no-ccache-stats.patch
  applypatch $srcdir/python-deps.patch
  applypatch $srcdir/rust-lto-thin.patch
  applypatch $srcdir/sandbox-fork.patch
  applypatch $srcdir/sandbox-largefile.patch
  applypatch $srcdir/sandbox-sched_setscheduler.patch
  applypatch $srcdir/symboltable.patch
  applypatch $srcdir/isnan.patch

  cp $srcdir/stab.h toolkit/crashreporter/google-breakpad/src/

  clear_vendor_checksums() {
    sed -i 's/\("files":{\)[^}]*/\1/' third_party/rust/$1/.cargo-checksum.json
  }

  clear_vendor_checksums audio_thread_priority

}

build() {
  export SHELL=/bin/sh 
  export BUILD_OFFICIAL=1 
  export MOZILLA_OFFICIAL=1
  export MACH_BUILD_PYTHON_NATIVE_PACKAGE_SOURCE=none
  export USE_SHORT_LIBNAME=1
  export RUST_TARGET="$arch-unknown-linux-musl"

  # Thanks slackware. This reduces memory consumption issues when rust objects are compiled
  export RUSTFLAGS="-Cdebuginfo=0"
  
  # Point bindgen to the right toolchain
  export BINDGEN_CFLAGS="--sysroot="/" --target="$arch-linux-musl""
  
  # Cause cargo to respect MAKEFLAGS
  export CARGO_BUILD_JOBS="${MAKEFLAGS/-j/}"
  
  cd build

  export CC CXX

  ../mach configure \
    --prefix="" \
    --libdir=/lib \
    --enable-audio-backends=alsa \
    --enable-dbus \
    --enable-hardening \
    --enable-official-branding \
    --enable-release \
    --enable-application=browser \
    --enable-default-toolkit=cairo-gtk3 \
    --disable-necko-wifi \
    --disable-strip \
    --disable-install-strip \
    --disable-tests \
    --disable-updater \
    --disable-rust-simd \
    --disable-crashreporter \
    --disable-jemalloc \
    --enable-optimize="-O2" \
    --disable-profiling \
    --enable-ffmpeg \
    --with-system-zlib \
    --allow-addon-sideload \
    --enable-linker=lld \
    --disable-cargo-incremental \
    --without-wasm-sandboxed-libraries \
    --with-clang-path=/bin/clang \
    --with-libclang-path=/lib

  # For some reason, firefox compiler is not able to find critical GCC headers
  # for our supported architectures when using a custom toolchain like skarnet's.
  # They are mostly located in $arch-linux-musl-$version/include/c++/$version.
  # The compiler looks in /usr/local/include, so let's populate this directory
  # with our own headers
  #mkdir -p /local/include
  #tar -xf $srcdir/aarch64-8.2.0-headers.tar.gz -C /local/include

  ../mach build $MAKEFLAGS
  DESTDIR="$pkg" ../mach install

  # sabotage linux does a cleanup of the headers and the /bin/firefox
  # script, replacing it with another script that has LD_LIBRARY_PATH set
  # for correct detection of libs at runtime, so we follow suit

  rm -rf $pkg/include $pkg/lib/firefox-devel-$version
  rm -f $pkg/bin/firefox
  install -Dm 755 $srcdir/firefox $pkg/bin/firefox

  install -Dm 644 $srcdir/mozicon128.png $pkg/share/pixmaps/firefox.png
  install -Dm 644 $srcdir/firefox.desktop $pkg/share/applications/firefox.desktop
  install -Dm 644 $srcdir/vendor.js $pkg/lib/firefox/browser/defaults/preferences/firefox-branding.js

  mkfinalpkg
}

sha512sums="
602584f4c77b7a554aaa068eda5409b68eb0b3229e9c224bffb91c83c4314d25de15bd560a323626ff78f6df339c79e1ef8938c54b78ecadf4dc75c5241290ad  autoconf-2.13.tar.gz
5ee722884cd545cf5146f414526b4547286625f4f5996a409d7f64f115633fb7eb74d202e82f175fd5b2d24cce88deee70020fcb284055fcdea3d39da182074e  firefox-115.5.0esr.source.tar.xz
3e0501ae7a650346c667dfdc0ae0ca286084f22e89ab2ac671cc0d7315673dc5b6dcb9f9882f6f39d26e9a31e57f7a0fd53d6b805e520224e22b8976850e2eb8  audio-lfs64.patch
454ea3263cabce099accbdc47aaf83be26a19f8b5a4568c01a7ef0384601cf8315efd86cd917f9c8bf419c2c845db89a905f3ff9a8eb0c8e41042e93aa96a85c  disable-moz-stackwalk.patch
f7b3b45ba04d05d17439d009bf0c9f27881e126f424e2257552338a0c1e3771ee1289c044babcb0920f62af62873a268c0cf524e1d35711e6dc8b808ca5e9f26  esr-metainfo.patch
2f4f15974d52de4bb273b62a332d13620945d284bbc6fe6bd0a1f58ff7388443bc1d3bf9c82cc31a8527aad92b0cd3a1bc41d0af5e1800e0dcbd7033e58ffd71  fix-fortify-system-wrappers.patch
cd68b89e29e5f6379fbd5679db27b9a5ef70ea65e51c0d0a8137e1f1fd210e35a8cfb047798e9549bc7275606d7ec5c8d8af1335d29da4699db7acd8bc7ff556  fix-rust-target.patch
305c874fdea3096e9c4c6aa6520ac64bb1c347c4b59db8360096646593fe684c3b5377874d91cecd33d56d1410b4714fbdea2b514923723ecbeff79d51265d9b  fix-webrtc-glibcisms.patch
afabea91b328c5a68eaa20f9099ac7b2d0e7f2423e816b05ed168bdd326a5684fa02de08bf05c6033e9b888f02775d1b0443a00329b7a632ee399122a391c13a  icu74.patch
528cf76e8b00a8c12b99afb2c9bb102aca095b7eab9d224139e36d9dad533fd441d636a950e3c5e36626a5fb9b49fb601601712ce0b30de9d8968385c42340e5  isnan.patch
5fa9382c692e4bd6a2634308f24a6526fd12a60a2563d2090056d43a60505df3ec9881bbf54562e69394467529b3b0dc45955afca46ed329af03cea074fff070  lfs64.patch
c0437a6753f3f350968fa12d250efdfe1bea77baf0e4c06b072b5cc9e78c774dbf4506bc536337030d349fb3ba4460097b75b0c7c5b8fb2d39d8b0a392948936  no-ccache-stats.patch
ff3618223bba2d7b877a16451256e67d973c5f4e24027cb5a7da4c0a6f6a028a1eb51d3cbcddae71bca584a4c13cb485950b86022ea26d4e25ae3fdf3ce5d910  python-deps.patch
1c6918dd6655d3a1251bfd4af2e1c561cbb00d540a883b4c1ebf7f5de530d754d9ac07b4b5f56cdab6c511d25c8910ec94043f5733e97501a67abffe1bafaeb1  rust-lto-thin.patch
2518f2fc75b5db30058e0735f47d60fdf1e7adfaeee4b33fb2afb1bd9a616ce943fd88f4404d0802d4083703f4acf1d5ad42377218d025bc768807fbaf7e1609  sandbox-fork.patch
b7d0a6126bdf6c0569f80aabf5b37ed2c7a35712eb8a0404a2d85381552f5555d4f97d213ea26cec6a45dc2785f22439376ed5f8e78b4fd664ef0223307b333e  sandbox-largefile.patch
94433c5ffdbe579c456d95c5f053f61fcbab2f652fa90bc69dcc27d9a1507a8e5c677adeadae9a7a75cc9a55184c1040737f4dfd10b279c088ef016561e6f135  sandbox-sched_setscheduler.patch
7c8584c39c8d3d2c8b0bd430fea6d835359580419e6676d06d9f5973c43222de3f314d9f42ee6a492544af5882596dba20373d8fcad9c82d0ce454aa40066e9f  symboltable.patch
0b3f1e4b9fdc868e4738b5c81fd6c6128ce8885b260affcb9a65ff9d164d7232626ce1291aaea70132b3e3124f5e13fef4d39326b8e7173e362a823722a85127  stab.h
"