# disable LTO as it has little benefit and uses too many resources
# don't compile with clang, use gcc toolchain (clang has issues on IA32)
# disable SIMD (SSE2 for i686)
# set correct compiler and toochain tools
if [ "${CARCH}" = "pentium4" ]; then
	source+=('mozconfig-pentium4.patch')
	sha256sums+=('c8b5376c4ee3f7dec4c9fe2b81434df4e2f5dd7caa99c8bf4371aec3c32233af')
	b2sums+=('6d1f48b81a430ca39e8ea8de6ffc44010c18344b0f3b1d282f7f6d6e124823f2152267c593c1d86f049a0d8a80591b20e27b3e7c2ca29f6b29b68ef4333e4828')
eval "$(
  declare -f prepare | \
    sed '
      $ i patch -d .. -Np1 -i "$srcdir/mozconfig-pentium4.patch"
    '
)"
fi
if [ "${CARCH}" = "i686" ]; then
	source+=('mozconfig-i686.patch')
	sha256sums+=('a0e274593c8d3587c4a669e22c480997cce24789f1dff78fb0da0f1d43083d68')
	b2sums+=('7f6a89042ab4a4c7c6727056029d0e20411fa63c6b857a0a75c95c0544e81bea565d8cb0daba0c775861d45486ca9ab5375ec564ed567bd330f72a5e1d657ea3')
eval "$(
  declare -f prepare | \
    sed '
      $ i patch -d .. -Np1 -i "$srcdir/mozconfig-i686.patch"
    '
)"
fi	

# try hard to tell ld and rust not to use too much memory (no lto, no debug info, etc.)
eval "$(
  declare -f build | \
    sed '
      2 a export RUSTFLAGS+=" -Cdebuginfo=0 -Clto=off"
      2 a export LDFLAGS+=" -Wl,--no-keep-memory -Wl,--reduce-memory-overheads -Wl,--max-cache-size=16384000 "
    '
  declare -f prepare | \
    sed '
      $ i export MOZ_SOURCE_CHANGESET="DEVEDITION_${pkgver//./_}_RELEASE"
    '
)"

# do not build a first firefox, run it in headless X just to produce
# some profiling data and build it again. the penalty of using a 32-bit
# build is high enough, so optimizing based on profiling data will not
# be that helpfull IMHO.
eval "$(
  declare -f build | \
    sed '
      /.\/mach package/,/.\/mach build/d
    '
)"

# use locally cached version of firefox sources
# TODO: think about how to put this into helper functions
for ((i=0; i<${#source[@]}; i++)); do
  infos=$(
    printf '%s\n' "${source[${i}]}" | \
      sed -n '
        s@^\(\([^:]\+\)::\)\?\(git\|hg\)+\([^?#]\+\)\(?signed\)\?#\(\(tag\|commit\)=\S\+\)$@\3 \2 \4 \5 \6@
        T
        p
      '
  )
  if [ -n "${infos}" ]; then
    source[${i}]=$(
      type="${infos%% *}"
      infos="${infos#* }"
      if [ -n "${infos%% *}" ]; then
        prefix="${infos%% *}"
      else
        prefix=''
      fi
      infos="${infos#* }"
      repo="${infos%% *}"
      repo64=$(
        printf '%s' "${repo}" | \
          base64 -w0 | \
          sed 's/=/%3D/g'
      )
      infos="${infos#* }"
      if [ "${infos%% *}" = '?signed' ]; then
        key_check=$(
          printf '&valid_keys='
          printf '%s,' "${validpgpkeys[@]}" | \
            sed 's/,$//'
        )
      else
        key_check=''
      fi
      infos="${infos#* }"
      if [ -z "${prefix}" ]; then
        prefix="${repo%.git}"
        prefix="${prefix##*/}"
      fi
      prefix_64=$(
        printf '%s/' "${prefix}" | \
          base64 -w0 | \
          sed 's/=/%3D/g'
      )

      printf '%s-%s.tar.gz::https://archive-server.archlinux32.org/?t=%s&p=%s&r=%s%s&%s\n' \
        "${prefix}" \
        "${pkgver}" \
        "${type}" \
        "${prefix_64}" \
        "${repo64}" \
        "${key_check}" \
        "${infos}"
    )
  fi
done

# from VoidLinux, avoid excessive debug symbols in rust leading
# to out-of-memory situations
eval "$(
  declare -f build | \
    sed '
      5 a sed -i "s/debug_info = '\''2'\''/debug_info = '\''0'\''/" build/moz.configure/toolchain.configure
    '
)"

# don't kill build slaves by auto-guessing available CPU cores
eval "$(
  declare -f build | \
    sed '
      2 a export MOZ_MAKE_FLAGS=-j2
    '
  declare -f package | \
    sed '
      2 a export MOZ_MAKE_FLAGS=-j2
    '
)"

# test failure in rust code (complaining about network functions) when PGO is used,
# see https://bugzilla.mozilla.org/show_bug.cgi?id=1565757
source+=('rust-static-disable-network-test-on-static-libraries.patch')
sha256sums+=('10c5276eab2e87f400a6ec15d7ffbef3b0407ee888dea36f1128927ca55b9041')
b2sums+=('009789d3bb93ba418929019135804fc3de7de161e5be8efba7fc75646fbde395720be5b7a7d3f5d1671f459fdd2944dd14aca306bbf1c975436d4f7765d62d15')
eval "$(
  declare -f prepare | \
    sed '
      $ i patch -p1 -i "$srcdir/rust-static-disable-network-test-on-static-libraries.patch"
    '
)"

# gcc and cross PGO are not best friends, disable PGO
eval "$(
  declare -f build | \
    sed '
      /--enable-profile-generate=cross/d
      /--with-pgo-profile-path=/d
      /--with-pgo-jarlog=/d
    '
)"

# readelf: Error: Unable to seek to 0x801db328 for section headers
source+=('avoid-libxul-OOM-python-check.patch')
sha256sums+=('2f0c81a38c4578f68f5456b618fe84a78974072821488173eb55e0e72287e353')
b2sums+=('97035e44cd1deae7bb2422c81eec7294feb51f43f460b4d7ddba083e1d8a48d265a36ca43cf1d9dee49b01ee6df0c76e0f6916cc73cb7ad9caa1c235a59da0e4')
eval "$(
  declare -f prepare | \
    sed '
      $ i patch -p1 -i "$srcdir/avoid-libxul-OOM-python-check.patch"
    '
)"

# libxul.so cannot find it's libraries
eval "$(
  declare -f package | \
    sed '
      /}/ i \
        install -dm 755 "${pkgdir}/etc/ld.so.conf.d" \
        echo "/usr/lib/${pkgname}" > "${pkgdir}"/etc/ld.so.conf.d/${pkgname}.conf \
    '
)"

# do not do profiling: it doesn't find libraries:
# "XPCOMGlueLoad error for file /build/firefox/src/firefox-70.0/obj-i686-pc-linux-gnu/dist/firefox/libxul.so:
#libmozsandbox.so: cannot open shared object file: No such file or directory
# Couldn't load XPCOM.
# ..or it will for sure run out of memory
eval "$(
  declare -f build | \
    sed '
      /msg2 "Profiling instrumented browser..."/,/.\/mach build/d
    '
)"

# libvpx has some hard-coded compiler flags for MMX, SSE, SSE2, use the correct one
# per CARCH (75.0 uses an intrisic _mm_empty now, which required the corresponding
# architecture flag to be preset - before it was merely embedding some assembly
# code with EMMS
if [ "${CARCH}" = "i686" ]; then
  CFLAGS="$CFLAGS -mmmx"
  CXXFLAGS="$CXXFLAGS -mmmx"
fi	

# do not build and phone back symbols, fails anyway with:
# "panic occurred at library/alloc/src/raw_vec.rs:537: capacity overflow\n" in
# dump_syms (see also FS32#174)
eval "$(
  declare -f build | \
    sed '
      /echo.*symbol archive/,/mach buildsymbols/{s/\(.*\)/#\1/g}
    '
)"

eval "$(
  declare -f package | \
    sed '
      /export SOCORRO_SYMBOL_UPLOAD_TOKEN_FILE/,/fi/{s/\(.*\)/#\1/g}
    '
)"

# 214:49.30 /build/firefox/src/firefox-99.0.1/modules/fdlibm/src/math_private.h:34:21: error: conflicting
# declaration ?typedef __double_t double_t?
# and
# typedef __float_t   float_t;
# /usr/include/math.h:169:21: note: previous declaration as ?typedef long double float_t?
# typedef long double float_t;
source+=('firefox-111.0.1-fdlibm.patch')
sha256sums+=('ed3bb281697af7c4353a34067ffb4b18a971d40757bef2d6af3c8bf2d28d42d1')
b2sums+=('05c7c9727201971650df4ca2e00539767fc9e159539835c641fccab5ec9577a7294e8f62f5d0670158316e5467ea121c03a36fd4ba153e1d60f3402f0430548d')
eval "$(
  declare -f prepare | \
    sed '
      $ i patch -p1 -i "$srcdir/firefox-111.0.1-fdlibm.patch"
    '
)"

# disable stripping in mozconfig.<arch> (insists to use llvm-strip which runs
# out of memory on libxul.so). Now 2.5 GB can be stripped to 166 MB, so we do
# that with the normal 'strip' from binutils after 'mach install'
eval "$(
  declare -f package | \
    sed '
      /.*mach.*install/ a \
        strip "$pkgdir/usr/lib/$pkgname/libxul.so"
    '
)"

# from Voidlinux, enable atomics in JIT for i686
source+=('fix-i686-build-moz-1792159.patch')
sha256sums+=('2fb39374fd3d80eea9e346032a2a4b2bc2e357dee7380855b24bcf19b1335d06')
b2sums+=('d20ce3eff595f85df86eaa0dfb665fc356f8987117a771f76adc4ac12046a7e82b0af182fa99f87ea1362a5026c9d0216c7b714649fef0c7294c61c8e8f4d790')
eval "$(
  declare -f prepare | \
    sed '
      $ i patch -p1 -i "$srcdir/fix-i686-build-moz-1792159.patch"
    '
)"

# from Parabola (thanks go to bill-auger)
source+=('fix-i686-xsimd-incomplete.patch')
sha256sums+=('c3ce181fbb0142055aa6dd17f3cda2ba6a1e54d7a689a8c6e9cce76aa40e6544')
b2sums+=('3e6fd4cebe0fd4c250ccf5b04e12b93e5f4cc2d139301316c987cc07a57b4b6b49b830d745520a82f5ac59da8d17f728a3ecce4dccf21804a2d6a256a6f76472')
eval "$(
  declare -f prepare | \
    sed '
      $ i patch -p1 -i "$srcdir/fix-i686-xsimd-incomplete.patch"
    '
)"