# Template build file for 'firefox'.
pkgname=firefox
version=51.0.1
revision=1
short_desc="Mozilla Firefox web browser"
maintainer="Juan RP <xtraeme@voidlinux.eu>"
homepage="https://www.mozilla.org/firefox/"
license="MPL-2.0, GPL-2, LGPL-2.1"
distfiles="${MOZILLA_SITE}/${pkgname}/releases/${version}/source/${pkgname}-${version}.source.tar.xz"
checksum=30ba00ba716ea1eeda526e2ccc8642f8d18a836793fde50e87a4fcb9d9fccca9

only_for_archs="i686 i686-musl x86_64 x86_64-musl"
nopie=yes
lib32disabled=yes

build_options="gtk3 rust"
desc_option_gtk3="Build the GTK+3 toolkit UI"
desc_option_rust="Build rust components"

hostmakedepends="autoconf213 unzip zip pkg-config perl python yasm
 $(vopt_if rust 'rust cargo')"
makedepends="
 nss-devel libjpeg-turbo-devel libpng-devel gtk+-devel
 icu-devel pixman-devel sqlite-devel pulseaudio-devel
 libevent-devel libnotify-devel libvpx-devel libXrender-devel
 startup-notification-devel dbus-glib-devel alsa-lib-devel
 hunspell-devel libXcomposite-devel libSM-devel libXScrnSaver-devel
 libXt-devel libXdamage-devel $(vopt_if gtk3 gtk+3-devel)"
depends="nss>=3.27 desktop-file-utils hicolor-icon-theme"
conflicts="firefox-esr>=0"

build_options_default="gtk3"
case "$XBPS_TARGET_MACHINE" in
	x86_64*) build_options_default+=" rust" ;;
esac

if [ ${XBPS_GCC_VERSION_MAJOR} -gt 5 ]; then
	# Append CFLAGS and CXXFLAGS to set work around code which gcc6 would
	# otherwise regard as out-of-specification and allow it to produce a
	# working program.
	CFLAGS+=" -fno-delete-null-pointer-checks -fno-lifetime-dse -fno-schedule-insns2"
	CXXFLAGS+=" -fno-delete-null-pointer-checks -fno-lifetime-dse -fno-schedule-insns2"
fi

post_extract() {
	[ ! -d "${wrkdrc}/xbps-build" ] && mkdir -p "${wrksrc}/xbps-build"
}
pre_configure() {
	case "$XBPS_TARGET_MACHINE" in
	*-musl)
		$XBPS_FETCH_CMD http://git.alpinelinux.org/cgit/aports/plain/community/firefox-esr/stab.h
		mv stab.h toolkit/crashreporter/google-breakpad/src/
		sed -i "s/\(x86_64-unknown-linux\)-gnu/\1-musl/" build/moz.configure/rust.configure
		;;
	esac

	# configure script misdetects the preprocessor without an optimization level
	sed -i '/ac_cpp=/s/$CPPFLAGS/& -O2/' old-configure

	# Google API key (see http://www.chromium.org/developers/how-tos/api-keys)
	# Note: This is for Void Linux use ONLY.
	_google_api_key="AIzaSyCIFdBA7eQP43R6kXRwTq7j6Mvj1ITze90"
	echo -n "$_google_api_key" > google-api-key
}
do_configure() {
	local _args

	case "$XBPS_TARGET_MACHINE" in
	*-musl) # XXX gold linking with --hash-style=sysv results in unhidden symbols
		# XXX see https://sourceware.org/ml/binutils/2014-09/msg00230.html
		# XXX disable jemalloc.
		_args+=" --disable-jemalloc --enable-gold=no"
		;;
	esac

	if [ "$CROSS_BUILD" ]; then
		_args+=" --target=$XBPS_CROSS_TRIPLET --disable-jemalloc"

		# Make config/system_wrappers/alsa/alsalib.h and pulse/pulse.h find
		# the required includes. Set system nspr and nss include paths.
		export HOST_CFLAGS="${XBPS_CFLAGS}"
		export HOST_CXXFLAGS="${XBPS_CXXFLAGS}"
		export CFLAGS+=" -I${XBPS_CROSS_BASE}/usr/include/alsa \
			-I${XBPS_CROSS_BASE}/usr/include/pulse \
			-I${XBPS_CROSS_BASE}/usr/include/nspr \
			-I${XBPS_CROSS_BASE}/usr/include/nss"
		export CXXFLAGS+=" ${CFLAGS}"
		export LDFLAGS+=" -L${XBPS_CROSS_BASE}/usr/lib"

		export ac_cv_sqlite_secure_delete=yes \
			ac_cv_sqlite_threadsafe=yes \
			ac_cv_sqlite_enable_fts3=yes \
			ac_cv_sqlite_dbstat_vtab=yes \
			ac_cv_sqlite_enable_unlock_notify=yes \
			ac_cv_prog_hostcxx_works=1
	fi

	mkdir -p /usr/lib/firefox
	export LDFLAGS+=" -Wl,-rpath=/usr/lib/firefox"

	cd xbps-build
	# XXX build without --system-cairo. See:
	# - https://github.com/voidlinux/void-packages/issues/2308#issuecomment-135426813
	# - https://bugs.gentoo.org/show_bug.cgi?id=558150
	SHELL=/bin/bash ../configure --prefix=/usr \
		--with-system-nspr --with-system-nss --with-system-bz2  \
		--with-system-jpeg --with-system-zlib --without-system-png \
		--with-system-libevent --with-system-libvpx \
		--enable-system-pixman --enable-system-hunspell --enable-system-sqlite \
		--enable-system-ffi --enable-startup-notification --enable-gio \
		--with-pthreads --enable-official-branding --enable-safe-browsing \
		--disable-debug --disable-gconf --disable-crashreporter --disable-updater \
		--disable-tests --disable-elf-hack --with-system-icu --enable-pulseaudio \
		--with-nspr-prefix=${XBPS_CROSS_BASE}/usr \
		--with-nss-prefix=${XBPS_CROSS_BASE}/usr \
		--with-google-api-keyfile="${wrksrc}/google-api-key" \
		--enable-optimize="$CFLAGS" --disable-strip --disable-install-strip \
		--enable-pie --disable-profiling $(vopt_enable rust) \
		--enable-application=browser \
		--enable-default-toolkit=$(vopt_if gtk3 'cairo-gtk3' 'cairo-gtk2') ${_args}
}
do_build() {
	if [ "$SOURCE_DATE_EPOCH" ]; then
		mozdate="MOZ_BUILD_DATE=$(date --date "@$SOURCE_DATE_EPOCH" "+%Y%m%d%H%M%S")"
	fi
	cd xbps-build
	SHELL=/bin/bash make ${mozdate} ${makejobs}
}
do_install() {
	cd xbps-build
	make DESTDIR=${DESTDIR} 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 256x256; do
		vinstall ${wrksrc}/browser/branding/official/default${i%x*}.png 644 \
			usr/share/icons/hicolor/${i}/apps firefox.png
	done

	# Use system-provided dictionaries
	rm -rf ${DESTDIR}/usr/lib/firefox/{dictionaries,hyphenation}
	ln -s /usr/share/hunspell ${DESTDIR}/usr/lib/firefox/dictionaries
	ln -s /usr/share/hyphen ${DESTDIR}/usr/lib/firefox/hyphenation

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