# Maintainer: Patrycja Rosa <alpine@ptrcnull.me>
pkgname=firefox-developer-edition
pkgver=134.0_beta2
_pkgver=${pkgver/_beta/b}
_releasedate=2024-11-27
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=19
makedepends="
	alsa-lib-dev
	automake
	bsd-compat-headers
	cargo
	cbindgen
	clang$_llvmver
	clang$_llvmver-libclang
	dbus-glib-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
	wireless-tools-dev
	zip
	"
source="https://ftp.mozilla.org/pub/firefox/releases/$_pkgver/source/firefox-$_pkgver.source.tar.xz
	bmo-1928504-enable-relr.patch
	fix-fortify-system-wrappers.patch
	fix-rust-target.patch
	fix-webrtc-glibcisms.patch
	install-dir.patch
	lfs64.patch
	no-ccache-stats.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"
	export CXX="clang++"

	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 CHOST CTARGET 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="
96f0425e44b6f31b68358ef3669b38675c24eea7176267b02cf16b804e36063c6b1cee6023bd115123d31a0c277b7fc3fc6745a09060350ea3ad6500934efb6c  firefox-134.0b2.source.tar.xz
106f168dc37a693e279f821695637e0ac0d3534b07f093ac835c5b11f604ad2f11817bbe9604009e27a2b1410c6584a84fe388621170f511914e3e8aa154639e  bmo-1928504-enable-relr.patch
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
1c6918dd6655d3a1251bfd4af2e1c561cbb00d540a883b4c1ebf7f5de530d754d9ac07b4b5f56cdab6c511d25c8910ec94043f5733e97501a67abffe1bafaeb1  rust-lto-thin.patch
f8c3555ef6207933cbffbf4fc101a9b4c0d2990c0063162f0f0bde70ef0b46f86bfac42e7110695183424a87948de593f3927b2d8509ede3e4fc7bd8a1fad1ce  sandbox-sched_setscheduler.patch
0b3f1e4b9fdc868e4738b5c81fd6c6128ce8885b260affcb9a65ff9d164d7232626ce1291aaea70132b3e3124f5e13fef4d39326b8e7173e362a823722a85127  stab.h
3b7c7bcea6e9fe0a8f77d1069515906b496ff7a7f50d5051bf35133c9f44af02d6d75eb1b69920702cf87416009f47c47b2e1d6da5836d845a156387887b10c0  mozconfig
2ff0cb8e2eb94cee306b488adf6d7f4debbaff6155be3ed3eeee814cdb356e2e60fe38cc29d6c8d10079937fd2c930cfddf283977cf645395d31eaf76c7f0ac2  firefox-developer-edition.desktop
4fd66382983d406ce0cd3586239fb1110224aeb2f9eee6fc931d980a31d18739d001d1f152208a7f1ccaae80867c40bd3d2ac674484617532a8f38049dc745bd  distribution.ini
37ba6a08a80f09195cbc290b5e47c9893fcf5907823f77f539cd35622d578f790940d4cbb2e0e3582ebce8af137213772f3b560d5a7d9bccc4e37d02d1ed030c  vendor-prefs.js
"
