# Template file for 'firefox'
#
# THIS PKG MUST BE SYNCHRONIZED WITH "srcpkgs/firefox-i18n".
#
pkgname=firefox
version=136.0
revision=1
build_helper="rust"
short_desc="Mozilla Firefox web browser"
maintainer="Duncaen <duncaen@voidlinux.org>"
license="MPL-2.0, GPL-2.0-or-later, LGPL-2.1-or-later"
homepage="https://www.mozilla.org/firefox/"
changelog="https://www.mozilla.org/en-US/firefox/${version}/releasenotes/"
distfiles="${MOZILLA_SITE}/firefox/releases/${version/beta/b}/source/firefox-${version/beta/b}.source.tar.xz"
checksum=3bee314eb7934451be4e2c7ecac38b382f8422fed8287e05be26fe94dd286f57

lib32disabled=yes
_llvmver=19 # needs to match rust
hostmakedepends="autoconf213 unzip zip pkg-config perl python3 yasm rust
 cargo llvm${_llvmver} clang${_llvmver} lld${_llvmver} nodejs cbindgen nasm which
 tar xz $(vopt_if pgo 'xvfb-run dbus mesa-dri vulkan-loader pciutils')
 $(vopt_if wasi wasi-sdk)"
makedepends="nss-devel libjpeg-turbo-devel gtk+3-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 wasi wasi-sdk)
 $(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 pciutils"
conflicts="firefox-esr>=0"

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

if [ "$XBPS_WORDSIZE" != "32" ]; then
	build_options_default+=" debug"
fi

desc_option_lto="Enable Link Time Optimization"
desc_option_pgo="Enable Profile-guided Optimization"
desc_option_clang="Build with clang"
desc_option_wasi="Build wasm sandboxed libraries"

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

if [ "$CROSS_BUILD" -a "$build_option_pgo" ]; then
	broken="pgo can't be enabled for cross-compilation"
fi

# 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() {
	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}"

	if [ "$build_option_clang" ]; then
		export CC=clang
		export CXX=clang++
		export AR=llvm-ar
		export NM=llvm-nm
		export HOST_CC=clang
		export HOST_CXX=clang++
	fi

	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
	}

	disable_wasi() {
		if [ "$build_option_wasi" ]; then
			echo "ac_add_options --with-wasi-sysroot=/usr/share/wasi-sysroot"
		else
			echo "ac_add_options --without-wasm-sandboxed-libraries"
		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)

	$(disable_wasi)

	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-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')

	mk_add_options MOZ_OBJDIR="${wrksrc}/obj"

	MOZ_APP_REMOTINGNAME=Firefox
	!

	# XXX: setting --enable-debug/--enable-debug-symbols explicitly
	# seems to result in a laggy/crashy firefox.
	if [ -z "$build_option_debug" ]; then
		echo "ac_add_options --disable-debug-symbols" >>.mozconfig
		echo "ac_add_options --disable-debug" >>.mozconfig
	fi

	# work around large debug symbols on 32-bit hosts
	if [ "$XBPS_WORDSIZE" = "32" ]; then
		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

	if [ "$build_option_pgo" ]; then
		cp .mozconfig{,.orig}
		echo "ac_add_options --enable-profile-generate=cross" >> .mozconfig

		./mach build
		./mach package
		LLVM_PROFDATA=llvm-profdata \
		JARLOG_FILE="$PWD/jarlog" \
		GDK_BACKEND=x11 \
		LD_LIBRARY_PATH="$PWD/obj/dist/firefox" \
		XDG_RUNTIME_DIR="$(mktemp -d "${wrksrc}/pgo-runtime-XXXXXX")" \
		MOZ_ENABLE_WAYLAND=0 \
			xvfb-run -s "-screen 0 1920x1080x24 -nolisten local" \
			dbus-run-session -- ./mach python build/pgo/profileserver.py

		test -s merged.profdata
		test -s jarlog

		./mach clobber objdir

		cp .mozconfig{.orig,}
		cat <<-! >>.mozconfig
		ac_add_options --enable-lto=cross,full
		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
		!
	fi

	./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 "${FILESDIR}/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
}
