# Maintainer: Patrycja Rosa <alpine@ptrcnull.me>
pkgname=firefox-developer-edition
pkgver=140.0_beta2
_pkgver=${pkgver/_beta/b}
_releasedate=2025-05-28
pkgrel=0
pkgdesc="Firefox web browser"
url="https://www.firefox.com/"
arch="x86_64 aarch64" # only common desktop architectures
license="GPL-3.0-only AND LGPL-2.1-only AND LGPL-3.0-only AND MPL-2.0"
depends="
	ffmpeg-libavcodec
	"
_llvmver=20
makedepends="
	alsa-lib-dev
	automake
	bsd-compat-headers
	cargo
	cbindgen
	clang$_llvmver
	clang$_llvmver-libclang
	dbus-dev
	gettext
	gtk+3.0-dev
	hunspell-dev
	icu-dev
	libevent-dev
	libffi-dev
	libjpeg-turbo-dev
	libnotify-dev
	libogg-dev
	libtheora-dev
	libtool
	libvorbis-dev
	libvpx-dev
	libwebp-dev
	libxcomposite-dev
	libxt-dev
	lld
	llvm$_llvmver-dev
	m4
	mesa-dev
	nasm
	nodejs
	nspr-dev
	nss-dev
	pipewire-dev
	pulseaudio-dev
	python3
	sed
	wasi-sdk
	wasi-compiler-rt~$_llvmver
	wireless-tools-dev
	zip
	"
source="https://ftp.mozilla.org/pub/firefox/releases/$_pkgver/source/firefox-$_pkgver.source.tar.xz
	fix-fortify-system-wrappers.patch
	fix-rust-target.patch
	fix-webrtc-glibcisms.patch
	install-dir.patch
	lfs64.patch
	no-ccache-stats.patch
	no-pip-check.patch
	rust-lto-thin.patch
	sandbox-sched_setscheduler.patch

	stab.h

	mozconfig
	firefox-developer-edition.desktop
	distribution.ini
	vendor-prefs.js
	"
options="!check" # no tests
builddir="$srcdir/firefox-${pkgver/_*/}"

_mozappdir=/usr/lib/firefox-developer-edition

# help our shared-object scanner to find the libs
ldpath="$_mozappdir"
sonameprefix="$pkgname:"

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

export CFLAGS="${CFLAGS/-fstack-clash-protection/} -g0 -O2"
export CXXFLAGS="${CXXFLAGS/-fstack-clash-protection/} -g0 -O2 -Wno-deprecated-builtins -Wno-deprecated-declarations"

prepare() {
	default_prepare

	cp "$srcdir"/stab.h toolkit/crashreporter/google-breakpad/src/

	_clear_vendor_checksums audio_thread_priority
}

build() {
	# for lto
	ulimit -n 4096

	# can't be set here and fail
	unset RUSTFLAGS

	local thinlto_jobs=${JOBS:-1}

	case "$CARCH" in
	# on this platform, lld seems to not utilise >1 threads for thinlto for some reason.
	# at the same time, having more than 8 also crashes lld for firefox buildsystems (why?).
	aarch64)
		if [ $thinlto_jobs -gt 8 ]; then
			thinlto_jobs=8
		fi
	esac

	export LDFLAGS="$LDFLAGS -Wl,--thinlto-jobs=$thinlto_jobs"

	export CC="clang-$_llvmver"
	export CXX="clang++-$_llvmver"

	export SHELL=/bin/sh
	export BUILD_OFFICIAL=1
	export MOZILLA_OFFICIAL=1
	export USE_SHORT_LIBNAME=1
	export MACH_BUILD_PYTHON_NATIVE_PACKAGE_SOURCE=system
	export MOZ_APP_REMOTINGNAME=firefoxdeveloperedition
	export MOZBUILD_STATE_PATH="$srcdir"/mozbuild
	# allow unsigned addons for dev-edition
	export MOZ_REQUIRE_SIGNING=
	# disable desktop notifications
	export MOZ_NOSPAM=1
	# Find our triplet JSON
	export RUST_TARGET="$CTARGET"

	# set rpath so linker finds the libs
	export LDFLAGS="$LDFLAGS -Wl,-rpath,$_mozappdir"

	# let firefox do this itself.
	unset CARGO_PROFILE_RELEASE_OPT_LEVEL
	unset CARGO_PROFILE_RELEASE_CODEGEN_UNITS
	unset CARGO_PROFILE_RELEASE_PANIC
	unset CARGO_PROFILE_RELEASE_LTO

	export MOZ_BUILD_DATE="$(date ${SOURCE_DATE_EPOCH:+ -d@${SOURCE_DATE_EPOCH}} "+%Y%m%d%H%M%S")"

	export CBUILD CHOST builddir
	envsubst < "$srcdir"/mozconfig > base-mozconfig
	export MOZCONFIG=base-mozconfig
	./mach build
}

package() {
	DESTDIR="$pkgdir" ./mach install

	local _png
	for _png in ./browser/branding/aurora/default*.png; do
		local i=${_png%.png}
		i=${i##*/default}
		install -Dm644 "$_png" \
			"$pkgdir"/usr/share/icons/hicolor/"$i"x"$i"/apps/$pkgname.png
	done

	install -Dm644 browser/branding/aurora/content/about-logo.png \
		"$pkgdir"/usr/share/icons/hicolor/192x192/apps/$pkgname.png
	install -Dm644 browser/branding/aurora/content/about-logo@2x.png \
		"$pkgdir"/usr/share/icons/hicolor/384x384/apps/$pkgname.png
	install -Dm644 browser/branding/aurora/content/about-logo.svg \
		"$pkgdir"/usr/share/icons/hicolor/scalable/apps/$pkgname.svg

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

	install -Dm644 "$srcdir"/distribution.ini \
		"$pkgdir"/$_mozappdir/distribution/distribution.ini

	# install our vendor prefs
	install -Dm644 "$srcdir"/vendor-prefs.js \
		"$pkgdir"/$_mozappdir/browser/defaults/preferences/vendor.js

	# Replace duplicate binary with wrapper
	# https://bugzilla.mozilla.org/show_bug.cgi?id=658850
	install -Dm755 /dev/stdin "$pkgdir"/usr/bin/firefox-developer-edition <<- EOF
	#!/bin/sh
	exec $_mozappdir/firefox "\$@"
	EOF
	rm "$pkgdir"/$_mozappdir/firefox-bin
	ln -sfv /usr/bin/firefox-developer-edition "$pkgdir"/$_mozappdir/firefox-bin
}

sha512sums="
c8010b6cdf90f52d5105971d726139460a0194ca7e84ed57184897f4f258957ff64117ef14a38a6dba4cab3074f3a92aee0eaf221ecf22218f6408636ab9c960  firefox-140.0b2.source.tar.xz
3c28e243c1c74a9bd1ea13729926b1cae3ee35843b14be92ff3287a8cd745e95954e1bc842f2add165923d159bb8d2a7bcd92ba8fb2f4017ba1aef892d77aecf  fix-fortify-system-wrappers.patch
cd68b89e29e5f6379fbd5679db27b9a5ef70ea65e51c0d0a8137e1f1fd210e35a8cfb047798e9549bc7275606d7ec5c8d8af1335d29da4699db7acd8bc7ff556  fix-rust-target.patch
305c874fdea3096e9c4c6aa6520ac64bb1c347c4b59db8360096646593fe684c3b5377874d91cecd33d56d1410b4714fbdea2b514923723ecbeff79d51265d9b  fix-webrtc-glibcisms.patch
ff471e5160e313a9657bcbb767a6a581bab9da053cced02bde38d1ef03349472ad3602dba026c10d676d55c1712af772dd2b7bdbfa72b5db498c9ce8a2133423  install-dir.patch
e75daab5573ec6e28d3940a9bb98304d572dfb26ce7c1709e99fdd75dfa58abce170c96de683f8dc2224ea2e118aa7d78affbd54f99c4e328a4641685d64bd7d  lfs64.patch
64b6458099baa225b346b40e3080cea3a5c73a834e39fa4a438af77701e6ded84e2fb0d6fffff8c2533cc43b898fe9f411be023e875acebec452aa2379861f04  no-ccache-stats.patch
aabec21c290bded5f1bde0b23db7a5c10ca1663673a4c9ad29ac0a495522a754bdfbd95def3bada29acb9b263b5578ee213bf46b963342e74cd60ecc8beae97b  no-pip-check.patch
1c6918dd6655d3a1251bfd4af2e1c561cbb00d540a883b4c1ebf7f5de530d754d9ac07b4b5f56cdab6c511d25c8910ec94043f5733e97501a67abffe1bafaeb1  rust-lto-thin.patch
f8c3555ef6207933cbffbf4fc101a9b4c0d2990c0063162f0f0bde70ef0b46f86bfac42e7110695183424a87948de593f3927b2d8509ede3e4fc7bd8a1fad1ce  sandbox-sched_setscheduler.patch
0b3f1e4b9fdc868e4738b5c81fd6c6128ce8885b260affcb9a65ff9d164d7232626ce1291aaea70132b3e3124f5e13fef4d39326b8e7173e362a823722a85127  stab.h
aa4dbec39297542f2f36323ef4e6b79719864acd7306b53d4b7970526891b877cc8df7fc7b621ea6d1f7209b980143d456b9de707ee7ab520aff4df1907a9c74  mozconfig
2ff0cb8e2eb94cee306b488adf6d7f4debbaff6155be3ed3eeee814cdb356e2e60fe38cc29d6c8d10079937fd2c930cfddf283977cf645395d31eaf76c7f0ac2  firefox-developer-edition.desktop
4fd66382983d406ce0cd3586239fb1110224aeb2f9eee6fc931d980a31d18739d001d1f152208a7f1ccaae80867c40bd3d2ac674484617532a8f38049dc745bd  distribution.ini
62e135345856f29f9dfde6e703527a3aecdc0da606b6dd72607c25b6646221d4bab86609f78ffaca051fb8b4c1c0eb5040957caf79a09852ad62cf124c1bb36f  vendor-prefs.js
"
