app=firefox
version=91.11.0
build=1sml
homepage="http://www.mozilla.org/projects/firefox/"
download="https://archive.mozilla.org/pub/firefox/releases/91.11.0esr/source/firefox-91.11.0esr.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"

build() {
  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/allow-custom-rust-vendor.patch
  applypatch $srcdir/avoid-redefinition.patch
  applypatch $srcdir/disable-moz-stackwalk.patch
  applypatch $srcdir/disable-neon-in-aom.patch
  applypatch $srcdir/fix-arm-opus-include.patch
  applypatch $srcdir/fix-fortify-system-wrappers.patch
  applypatch $srcdir/fix-tools.patch
  applypatch $srcdir/fix-webrtc-glibcisms.patch
  applypatch $srcdir/mallinfo.patch
  applypatch $srcdir/sandbox-fork.patch
  applypatch $srcdir/sandbox-largefile.patch
  applypatch $srcdir/sandbox-sched_setscheduler.patch
  applypatch $srcdir/symboltable.patch

  export SHELL=/bin/sh 
  export BUILD_OFFICIAL=1 
  export MOZILLA_OFFICIAL=1
  export MACH_USE_SYSTEM_PYTHON=1
  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"

  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
  clear_vendor_checksums target-lexicon-0.9.0

  unset MAKEFLAGS
  export BINDGEN_CFLAGS="--sysroot="/" --target="$arch-linux-musl""

  # Go easy on the pi
  if [ "$arch" = "aarch64" ] ; then 
    MAKEFLAGS="-j2"
  fi

  cd build

  ../mach configure \
    --prefix="" \
    --libdir=/lib \
    --enable-alsa \
    --enable-dbus \
    --enable-hardening \
    --enable-official-branding \
    --enable-release \
    --enable-application=browser \
    --enable-default-toolkit=cairo-gtk3 \
    --disable-pulseaudio \
    --disable-necko-wifi \
    --disable-strip \
    --disable-install-strip \
    --disable-tests \
    --disable-updater \
    --disable-rust-simd \
    --disable-crashreporter \
    --disable-gold \
    --disable-jemalloc \
    --enable-optimize="-O2" \
    --disable-profiling \
    --enable-ffmpeg \
    --enable-system-ffi \
    --enable-system-pixman \
    --with-system-icu \
    --with-system-jpeg \
    --with-system-libevent \
    --with-system-libvpx \
    --with-system-zlib \
    --with-system-pixman \
    --with-system-webp \
    --with-system-nss \
    --with-system-nspr \
    --with-clang-path=/bin/clang \
    --with-libclang-path=/lib \
    --allow-addon-sideload \
    --enable-linker=lld

  # 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
bff3a399c03bd1cdaaec0b6963b1558aa35b6338b6c02042ffd65fec0aedd344d01718692e881332f5f352c32da15ba09a20a09ee072200b47ae840bc0585a96  firefox-91.11.0esr.source.tar.xz
4e584621145cf8add069c6dac18e805b3274a1ee402d84e924df2341f7d3c5be261a93ef51283bacbd606f47fbdc628c4323ecc31efc5b403b8d224b18dc278f  allow-custom-rust-vendor.patch
b1cb2db3122634f66d2bae7066e76f2dcd455c464e021db4de3b0a08314df95cb667846081682db549dd2af8a00831cabe44a2420c66cdfb5e3b5fa7e6bd21d3  avoid-redefinition.patch
454ea3263cabce099accbdc47aaf83be26a19f8b5a4568c01a7ef0384601cf8315efd86cd917f9c8bf419c2c845db89a905f3ff9a8eb0c8e41042e93aa96a85c  disable-moz-stackwalk.patch
55eab1a02e19a19a1ee0e36b11097ab48a44200e07e543d91469967206854f39709c7c0bc31855559528e64642d610868140e9533f1c0e3bebc953353c142fa8  disable-neon-in-aom.patch
b88b312ec14330351fe2d3aef0aef9fa0d533f46dae7ddd1288aa79fc06e9c247147769369a11387988fa0685836135bbfc24000f30d263173361b9c0c5c3c39  fix-arm-opus-include.patch
2f4f15974d52de4bb273b62a332d13620945d284bbc6fe6bd0a1f58ff7388443bc1d3bf9c82cc31a8527aad92b0cd3a1bc41d0af5e1800e0dcbd7033e58ffd71  fix-fortify-system-wrappers.patch
a763228d0742be7323c7b2dafff173d48e261307372ed4445a8f1d583f0cebaf125b46d0abb1daa44bb03b1dafa8026d5215f5378f58b09b674f6f380ff5777f  fix-tools.patch
47c2c2428c3598a42f6241705179642b3378a86ace39c8c3cbef4954e6d220b42e6c76f3d71731d65f67ce2c8597259122ac44bbd45e20993bb8bc70c0c8a010  fix-webrtc-glibcisms.patch
551676b1b2d5c34c6e66cdf52781e5f94807df1155dca5fc89b45936140368b40b374e209f18c085fd1c2034764d7eb64bd95ec8cd513f9bded43cd692cc9059  mallinfo.patch
2518f2fc75b5db30058e0735f47d60fdf1e7adfaeee4b33fb2afb1bd9a616ce943fd88f4404d0802d4083703f4acf1d5ad42377218d025bc768807fbaf7e1609  sandbox-fork.patch
36ce3c2f97e4b53a627d3dba48a34921eb3fe7303524b8fe59033f3d159ea48bc90869fb555b1774b532f31e5b967fbf76d0305743f462cd9036f43cba7da044  sandbox-largefile.patch
db26757b2ebf9f567962e32294b4ae48b3a5d0378a7589dfe650fe3a179ff58befbab5082981c68e1c25fb9e56b2db1e4e510d4bca17c3e3aedbf9a2f21806eb  sandbox-sched_setscheduler.patch
7c8584c39c8d3d2c8b0bd430fea6d835359580419e6676d06d9f5973c43222de3f314d9f42ee6a492544af5882596dba20373d8fcad9c82d0ce454aa40066e9f  symboltable.patch
0b3f1e4b9fdc868e4738b5c81fd6c6128ce8885b260affcb9a65ff9d164d7232626ce1291aaea70132b3e3124f5e13fef4d39326b8e7173e362a823722a85127  stab.h
"
