File PKGBUILD of Package firefox

# $Id$
# Maintainer : Figue <ffigue@gmail.com>
# Contributor : Ionut Biru <ibiru@archlinux.org>
# Contributor: Jakub Schmidtke <sjakub@gmail.com>

pkgname=firefox
pkgver=128.0
pkgrel=1
pkgdesc="Standalone web browser from mozilla.org"
arch=(x86_64)
license=(MPL GPL LGPL)
url="https://www.mozilla.org/firefox/"
depends=(gtk3 libxt mime-types dbus alsa-lib at-spi2-core bash cairo ffmpeg
         fontconfig freetype2 gcc-libs gdk-pixbuf2 glib2 glibc hicolor-icon-theme
         libpulse libx11 libxcb libxcomposite libxdamage libxext libxfixes
         libxrandr libxss nspr nss pango ttf-font)
makedepends=(unzip zip diffutils yasm mesa imake inetutils xorg-server-xvfb
             rust clang llvm jack python nodejs lld mold
             cbindgen nasm obs-service-source_validator obs-build
             wasi-compiler-rt wasi-libc wasi-libc++ wasi-libc++abi)
optdepends=('networkmanager: Location detection via available WiFi networks'
            'libnotify: Notification integration'
            'speech-dispatcher: Text-to-Speech'
            'xdg-desktop-portal: Screensharing with Wayland')
provides=(firefox)
conflicts=(firefox)
options=(!emptydirs !lto !makeflags)
source=(https://archive.mozilla.org/pub/firefox/releases/$pkgver/source/firefox-$pkgver.source.tar.xz{,.asc}
        firefox.desktop identity-icons-brand.svg
        build-pgo.patch
        nopgo.patch lto.patch)
sha256sums=('65271ffefb235ea1e162a081f2074a0f06fce27b2f613f573c126ba8eef95172'
            'SKIP'
            '298eae9de76ec53182f38d5c549d0379569916eebf62149f9d7f4a7edef36abf'
            'a9b8b4a0a1f4a7b4af77d5fc70c2686d624038909263c795ecc81e0aec7711e9'
            '695c87f6c40e55d6f51a6ee5bf4c11587e529b65999b72a8df834d4eac172d33'
            '7f2e71d4c3eb8fce295fbc3ce6ec1fb84aa8d0875e8d90ba6cc7b289c33608a9'
            'bd626174f12ba90b888b393f3287f668e9e23911fc17b76d0691da8f899ed949')
#validpgpkeys=('BBBEBDBB24C6F355')
validpgpkeys=('14F26682D0916CDD81E37B6D61B7B526D98F0353') # Mozilla Software Releases <release@mozilla.com>

# Google API keys (see http://www.chromium.org/developers/how-tos/api-keys)
# Note: These are for Arch Linux use ONLY. For your own distribution, please
# get your own set of keys. Feel free to contact foutrelis@archlinux.org for
# more information.
_google_api_key=AIzaSyDwr302FpOSkGRpLlUpPThNTDPbXcIn_FM

# Mozilla API keys (see https://location.services.mozilla.com/api)
# Note: These are for Arch Linux use ONLY. For your own distribution, please
# get your own set of keys. Feel free to contact heftig@archlinux.org for
# more information.
_mozilla_api_key=e05d56db0a694edc8b5aaebda3f2db6a

prepare() {

  mkdir mozbuild

  cd firefox-${pkgver}

#  patch -Np1 -i ../build-pgo.patch

#  patch -Np1 -i ../nopgo.patch

#  patch -Np1 -i ../lto.patch

  echo -n "$_google_api_key" >google-api-key
  echo -n "$_mozilla_api_key" >mozilla-api-key

  gpg --import /usr/src/packages/SOURCES/14F26682D0916CDD81E37B6D61B7B526D98F0353.asc

  cat >../mozconfig <<END
#ac_add_options MOZ_PGO=1
ac_add_options --enable-application=browser
mk_add_options MOZ_OBJDIR=${PWD@Q}/obj
#mk_add_options MOZ_MAKE_FLAGS=-j8
#mk_add_options MOZ_MAKE_FLAGS=-j1
#mk_add_options MOZ_PARALLEL_BUILD=2

ac_add_options --prefix=/usr
ac_add_options --enable-release
#ac_add_options --enable-linker=gold
#ac_add_options --enable-linker=lld
ac_add_options --enable-linker=mold
ac_add_options --disable-install-strip
ac_add_options --disable-elf-hack
ac_add_options --disable-bootstrap
ac_add_options --with-wasi-sysroot=/usr/share/wasi-sysroot
ac_add_options --enable-hardening
ac_add_options --enable-lto
ac_add_options --enable-optimize
ac_add_options --enable-rust-simd
#export CC='clang --target=x86_64-unknown-linux-gnu'
#export CXX='clang++ --target=x86_64-unknown-linux-gnu'
#export AR=llvm-ar
#export NM=llvm-nm
#export RANLIB=llvm-ranlib

# Branding
ac_add_options --enable-official-branding
ac_add_options --enable-update-channel=release
export MOZ_ADDON_SIGNING=1
export MOZ_REQUIRE_SIGNING=1

# Keys
ac_add_options --with-google-location-service-api-keyfile=${PWD@Q}/google-api-key
ac_add_options --with-google-safebrowsing-api-keyfile=${PWD@Q}/google-api-key
ac_add_options --with-mozilla-api-keyfile=${PWD@Q}/mozilla-api-key

# System libraries
ac_add_options --with-system-nspr
ac_add_options --with-system-nss
#ac_add_options --with-system-icu
#ac_add_options --with-system-jpeg
#ac_add_options --with-system-png
#ac_add_options --with-system-zlib
#ac_add_options --with-system-bz2
#ac_add_options --with-system-libevent
#ac_add_options --with-system-libvpx
#ac_add_options --enable-system-sqlite
#ac_add_options --enable-system-ffi
#ac_add_options --enable-system-pixman

# Features
#ac_add_options --enable-startup-notification
ac_add_options --enable-alsa
ac_add_options --disable-crashreporter
ac_add_options --disable-updater
ac_add_options --disable-tests
ac_add_options --disable-debug-symbols
ac_add_options --disable-debug
ac_add_options --disable-rust-debug
ac_add_options --disable-eme
ac_add_options --disable-webrtc

#STRIP_FLAGS="--strip-debug"
END
}

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

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

  # Breaks compilation since https://bugzilla.mozilla.org/show_bug.cgi?id=1896066
  CFLAGS="${CFLAGS/-fexceptions/}"
  CXXFLAGS="${CXXFLAGS/-fexceptions/}"

  ulimit -n 4096
  
  # Do 3-tier PGO
  echo "Building instrumented browser..."
  cat >.mozconfig ../mozconfig - <<END
ac_add_options --enable-profile-generate=cross
END

  # Hardening
  LDFLAGS+=" -Wl,-z,noexecstack"
  
  CPPFLAGS+=" -D_GLIBCXX_ASSERTIONS"

  #CFLAGS+=" -fgraphite-identity -floop-nest-optimize"
  #CXXFLAGS+=" -fgraphite-identity -floop-nest-optimize"

  CFLAGS+=" -fstack-check"
  CXXFLAGS+=" -fstack-check"

  CFLAGS+=" -fno-strict-overflow"
  CXXFLAGS+=" -fno-strict-overflow"

  export RUSTFLAGS="-Cdebuginfo=0"
  
  ./mach build --priority normal
  
  echo "Profiling instrumented browser..."
  ./mach package
  LLVM_PROFDATA=llvm-profdata \
    JARLOG_FILE="$PWD/jarlog" \
    xvfb-run -s "-screen 0 1920x1080x24 -nolisten local" \
    ./mach python build/pgo/profileserver.py

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

  stat -c "Jar log found (%s bytes)" jarlog
  test -s jarlog

  echo "Removing instrumented browser..."
  ./mach clobber objdir

  echo "Building optimized browser..."
  cat >.mozconfig ../mozconfig - <<END
#ac_add_options --enable-lto=cross,full
ac_add_options --enable-lto=cross
ac_add_options --enable-profile-use=cross
ac_add_options --with-pgo-profile-path=${PWD@Q}/merged.profdata
ac_add_options --with-pgo-jarlog=${PWD@Q}/jarlog
END
  ./mach build --priority normal
}

package() {
  cd firefox-${pkgver}
  DESTDIR="$pkgdir" ./mach install

  local vendorjs="$pkgdir/usr/lib/$pkgname/browser/defaults/preferences/vendor.js"
  install -Dvm644 /dev/stdin "$vendorjs" <<END
// Use LANG environment variable to choose locale
pref("intl.locale.requested", "");

// Use system-provided dictionaries
pref("spellchecker.dictionary_path", "/usr/share/hunspell");

// Disable default browser checking.
pref("browser.shell.checkDefaultBrowser", false);

// Don't disable our bundled extensions in the application directory
pref("extensions.autoDisableScopes", 11);
END

  local i theme=official
  for i in 16 22 24 32 48 64 128 256; do
    install -Dm644 browser/branding/official/default$i.png \
      "$pkgdir/usr/share/icons/hicolor/${i}x${i}/apps/firefox.png"
  done
  install -Dm644 browser/branding/official/content/about-logo.png \
    "$pkgdir/usr/share/icons/hicolor/192x192/apps/firefox.png"
  install -Dm644 browser/branding/official/content/about-logo@2x.png \
    "$pkgdir/usr/share/icons/hicolor/384x384/apps/firefox.png"
  install -Dm644 browser/branding/$theme/content/about-logo.svg \
    "$pkgdir/usr/share/icons/hicolor/scalable/apps/firefox.svg"
  install -Dm644 ../identity-icons-brand.svg \
    "$pkgdir/usr/share/icons/hicolor/symbolic/apps/$pkgname-symbolic.svg"

  install -Dm644 ../firefox.desktop \
    "$pkgdir/usr/share/applications/firefox.desktop"

  # Install a wrapper to avoid confusion about binary path
  install -Dm755 /dev/stdin "$pkgdir/usr/bin/firefox" <<END
#!/bin/sh
exec /usr/lib/firefox/firefox "\$@"
END

  # Replace duplicate binary with wrapper
  # https://bugzilla.mozilla.org/show_bug.cgi?id=658850
  ln -srf "$pkgdir/usr/bin/firefox" \
    "$pkgdir/usr/lib/firefox/firefox-bin"
    
  # Use system certificates
  local nssckbi="$pkgdir/usr/lib/$pkgname/libnssckbi.so"
  if [[ -e $nssckbi ]]; then
    ln -srfv "$pkgdir/usr/lib/libnssckbi.so" "$nssckbi"
  fi

}
openSUSE Build Service is sponsored by