# Template file for 'firefox-esr'
#
# THIS PKG MUST BE SYNCHRONIZED WITH "srcpkgs/firefox-esr-i18n".
#
pkgname=firefox-esr
version=128.3.1
revision=1
build_helper="rust"
short_desc="Mozilla Firefox web browser - Extended Support Release"
maintainer="Orphaned <orphan@voidlinux.org>"
license="MPL-2.0, GPL-2.0-or-later, LGPL-2.1-or-later"
homepage="https://www.mozilla.org/firefox/"
distfiles="${MOZILLA_SITE}/firefox/releases/${version}esr/source/firefox-${version}esr.source.tar.xz"
checksum=c1f4052f3a88d96a122551d5025053304007f7649886d5e2fdfd1a11ce3d70a8

lib32disabled=yes

hostmakedepends="autoconf213 unzip zip pkg-config perl python3 yasm rust
 cargo llvm18 clang18 lld18 nodejs cbindgen nasm which tar"
makedepends="nss-devel libjpeg-turbo-devel gtk+3-devel icu-devel
 pixman-devel libevent-devel libnotify-devel libvpx-devel libwebp-devel
 libXrender-devel libXcomposite-devel libSM-devel libXt-devel rust-std
 libXdamage-devel freetype-devel
 $(vopt_if alsa alsa-lib-devel) $(vopt_if dbus dbus-glib-devel)
 $(vopt_if pulseaudio pulseaudio-devel) $(vopt_if xscreensaver libXScrnSaver-devel)
 $(vopt_if sndio sndio-devel) $(vopt_if jack jack-devel)"
depends="nss>=3.72 nspr>=4.32 desktop-file-utils hicolor-icon-theme"
conflicts="firefox>=0"
replaces="firefox>=0"
provides="firefox-${version}_${revision}"

build_options="alsa jack dbus pulseaudio xscreensaver sndio wayland lto clang"
build_options_default="alsa jack dbus pulseaudio xscreensaver sndio wayland clang"

desc_option_lto="Enable Link Time Optimization"
desc_option_clang="Build with clang"

case $XBPS_TARGET_MACHINE in
	armv[56]*) broken="required NEON extensions are not supported on armv6" ;;
	ppc64*) ;;
	ppc*) broken="xptcall bitrot" ;;
esac

# we need this because cargo verifies checksums of all files in vendor
# crates when it builds and gives us no way to override or update the
# file sanely... so just clear out the file list
_clear_vendor_checksums() {
	sed -i 's/\("files":{\)[^}]*/\1/' third_party/rust/$1/.cargo-checksum.json
}

post_extract() {
	case "$XBPS_TARGET_MACHINE" in
	*-musl)
		cp "${FILESDIR}/stab.h" toolkit/crashreporter/google-breakpad/src/
		;;
	esac

	# Mozilla API keys (see https://location.services.mozilla.com/api)
	# Note: This is for Void Linux use ONLY.
	echo -n "cd894504-7a2a-4263-abff-ff73ee89ffca" > mozilla-api-key
}

post_patch() {
	: # _clear_vendor_checksums num-traits
}

do_build() {
	if [ "$build_option_clang" ]; then
		export CC=clang
		export CXX=clang++

		if [ "$CROSS_BUILD" ]; then
			mkdir -p wrapper

			local gcc_version=$(gcc -dumpversion)
			local clang_version=$(clang -dumpversion)

			cat <<-! >"wrapper/${XBPS_TARGET_MACHINE}-clang"
			#!/bin/sh
			exec clang \
				--target="${XBPS_CROSS_TRIPLET}" \
				--sysroot="${XBPS_CROSS_BASE}" \
				"\$@"
			!

			cat <<-! >"wrapper/${XBPS_TARGET_MACHINE}-clang++"
			#!/bin/sh
			exec clang++ \
				--target="${XBPS_CROSS_TRIPLET}" \
				--sysroot="${XBPS_CROSS_BASE}" \
				"\$@"
			!

			chmod +x wrapper/*

			export PATH="${wrksrc}/wrapper:$PATH"
			export CC=${XBPS_TARGET_MACHINE}-clang
			export CXX=${XBPS_TARGET_MACHINE}-clang++
		fi

		export AR=llvm-ar
		export NM=llvm-nm
		export HOST_CC=clang
		export HOST_CXX=clang++
	fi

	export AS="${CC}"
	export CFLAGS="-O2"
	export CXXFLAGS="-O2"
	export HOST_CFLAGS=""
	export HOST_CXXFLAGS=""
	export LDFLAGS="-Wl,-rpath=/usr/lib/firefox"
	# export LDFLAGS+="-Wl,--threads=${XBPS_MAKEJOBS}"

	disable_jemalloc() {
		if [ "$XBPS_TARGET_LIBC" = "musl" ]; then
			echo "ac_add_options --disable-jemalloc"
		fi
	}

	disable_elfhack() {
		case "$XBPS_TARGET_MACHINE" in
		x86_64*|i686*|arm*|aarch64*) echo "ac_add_options --disable-elf-hack" ;;
		esac
	}

	disable_webrtc() {
		# it seems mozilla has started catching up with google's webrtc
		# and this newly involves introducing several megabytes of generated
		# json junk that we just cannot maintain in-tree, additionally they
		# have indicated that they will be re-generating these frequently
		#
		# it is unacceptable to keep a 7MB patch downstream, so disable it
		#
		# https://phabricator.services.mozilla.com/D134738
		#
		case "$XBPS_TARGET_MACHINE" in
		armv7l*) echo "ac_add_options --disable-webrtc" ;;
		esac

		# third_party/libwebrtc/common_audio/wav_file.cc:93:2: error:
		# #error "Need to convert samples to big-endian when reading from WAV file"
		if [ "$XBPS_TARGET_ENDIAN" = "be" ]; then
			echo "ac_add_options --disable-webrtc"
		fi
	}

	cat <<-! >.mozconfig
	ac_add_options --prefix=/usr
	ac_add_options --libdir=/usr/lib
	ac_add_options --host=${XBPS_TRIPLET}
	ac_add_options --target=${XBPS_CROSS_TRIPLET:-${XBPS_TRIPLET}}
	ac_add_options --enable-linker=$(vopt_if clang lld bfd)
	$(vopt_if lto 'ac_add_options --enable-lto=cross')
	$(vopt_if clang 'ac_add_options --with-libclang-path=/usr/lib')

	ac_add_options --enable-official-branding
	ac_add_options --enable-application=browser
	ac_add_options --enable-release
	ac_add_options --enable-hardening
	ac_add_options --enable-optimize="\${CFLAGS}"
	ac_add_options --enable-path-remapping=c,rust
	ac_add_options --disable-tests
	ac_add_options --disable-crashreporter
	ac_add_options --disable-updater
	ac_add_options --disable-install-strip
	ac_add_options --disable-strip
	ac_add_options --disable-profiling
	$(disable_jemalloc)
	$(disable_elfhack)
	$(disable_webrtc)

	# XXX: wasi currently not ready
	# ac_add_options --with-wasi-sysroot=/usr/share/wasi-sysroot
	ac_add_options --without-wasm-sandboxed-libraries

	ac_add_options --with-mozilla-api-keyfile="${wrksrc}/mozilla-api-key"

	ac_add_options --enable-system-pixman
	ac_add_options --with-system-ffi
	ac_add_options --with-system-icu
	ac_add_options --with-system-jpeg
	ac_add_options --with-system-libevent
	ac_add_options --with-system-libvpx
	ac_add_options --with-system-nspr
	ac_add_options --with-system-nss
	ac_add_options --with-system-webp
	ac_add_options --with-system-zlib
	# XXX: the system's libpng doesn't have APNG support
	ac_add_options --without-system-png

	ac_add_options --with-unsigned-addon-scopes=app,system
	ac_add_options --allow-addon-sideload

	ac_add_options $(vopt_enable dbus)
	ac_add_options $(vopt_enable dbus necko-wifi)
	ac_add_options --disable-audio-backends
	ac_add_options $(vopt_enable alsa)
	ac_add_options $(vopt_enable jack)
	ac_add_options $(vopt_enable pulseaudio)
	ac_add_options $(vopt_enable sndio)
	ac_add_options --enable-default-toolkit=$(vopt_if wayland 'cairo-gtk3-wayland' 'cairo-gtk3')

	MOZ_APP_REMOTINGNAME=Firefox
	!

	# work around large debug symbols on 32-bit hosts
	if [ "$XBPS_WORDSIZE" = "32" ]; then
		echo "ac_add_options --disable-debug-symbols" >>.mozconfig
		echo "ac_add_options --disable-debug" >>.mozconfig
		export LDFLAGS+=" -Wl,--no-keep-memory"
	fi

	if [ "$SOURCE_DATE_EPOCH" ]; then
		export MOZ_BUILD_DATE=$(date --date "@$SOURCE_DATE_EPOCH" "+%Y%m%d%H%M%S")
	fi

	export MOZ_MAKE_FLAGS="${makejobs}"
	export MOZ_NOSPAM=1
	export MOZBUILD_STATE_PATH="${wrksrc}/mozbuild"
	export MACH_BUILD_PYTHON_NATIVE_PACKAGE_SOURCE=system

	rm -f old-configure
	./mach build
}

do_install() {
	export MACH_BUILD_PYTHON_NATIVE_PACKAGE_SOURCE=system
	export MOZBUILD_STATE_PATH="${wrksrc}/mozbuild"
	DESTDIR="$DESTDIR" ./mach install

	vinstall ${FILESDIR}/vendor.js 644 usr/lib/firefox/browser/defaults/preferences
	vinstall taskcluster/docker/firefox-snap/firefox.desktop 644 usr/share/applications

	for i in 16x16 22x22 24x24 32x32 48x48 128x128 256x256; do
		vinstall ${wrksrc}/browser/branding/official/default${i%x*}.png 644 \
			usr/share/icons/hicolor/${i}/apps firefox.png
	done

	# We don't want the development stuff
	rm -rf ${DESTDIR}/usr/{include,lib/firefox-devel,share/idl}

	# https://bugzilla.mozilla.org/show_bug.cgi?id=658850
	ln -sf firefox ${DESTDIR}/usr/lib/firefox/firefox-bin

	vbin ${FILESDIR}/firefox-wayland
}
