# Template file for 'chromium'
pkgname=chromium
# See http://www.chromium.org/developers/calendar for the latest version
version=48.0.2564.116
revision=2
short_desc="Google's attempt at creating a safer, faster, and more stable browser"
maintainer="Juan RP <xtraeme@voidlinux.eu>"
homepage="http://www.chromium.org/"
license="BSD"
distfiles="https://commondatastorage.googleapis.com/chromium-browser-official/${pkgname}-${version}.tar.xz"
checksum=6a1eb9b4c853f15eeec0a55af7ac3b41835f0fc592ba6c0a500873cb12a84d0f

only_for_archs="i686 x86_64"
lib32disabled=yes
nodebug=yes

hostmakedepends="yasm python pkg-config perl gperf bison ninja hwids icu-devel"
makedepends="libpng-devel gtk+-devel nss-devel pciutils-devel
 libXi-devel libgcrypt-devel libgnome-keyring-devel cups-devel elfutils-devel
 libXcomposite-devel speech-dispatcher-devel libXrandr-devel mit-krb5-devel
 libXScrnSaver-devel alsa-lib-devel icu-devel snappy-devel
 libxml2-devel libxslt-devel pulseaudio-devel nss-devel libexif-devel
 libXcursor-devel libflac-devel speex-devel libmtp-devel libwebp-devel
 libjpeg-turbo-devel libevent-devel json-c-devel re2-devel harfbuzz-devel
 minizip-devel jsoncpp-devel zlib-devel libcap-devel libXdamage-devel"
depends="libexif hwids desktop-file-utils hicolor-icon-theme"
nostrip_files="nacl_irt_x86_64.nexe nacl_irt_x86_32.nexe"

if [ -z "$CROSS_BUILD" -a "$XBPS_MACHINE" = "x86_64" ]; then
	hostmakedepends+=" gcc-c++-multilib"
fi

post_extract() {
	case "${XBPS_TARGET_MACHINE}" in
	*-musl)
		for f in ${FILESDIR}/musl-patches/*.patch; do
			patch -Np0 -i $f
		done
		;;
	esac

}
pre_configure() {
	if [ "$XBPS_TARGET_MACHINE" = "x86_64" ]; then
		# XXX xtraeme: fix gcc{,-c++}-multilib instead.
		ldconfig &>/dev/null
		# Download NaCL toolchains
		python2 build/download_nacl_toolchains.py \
			--packages nacl_x86_newlib,pnacl_newlib,pnacl_translator \
			sync --extract
	fi

	# Remove bundled ICU; its header files appear to get picked up instead of
	# the system ones, leading to errors during the final link stage
	# https://groups.google.com/a/chromium.org/d/topic/chromium-packagers/BNGvJc08B6Q
	find third_party/icu -type f \! -regex '.*\.\(gyp\|gypi\|isolate\)' -delete

	# https://groups.google.com/a/chromium.org/d/topic/chromium-packagers/9JX1N2nf4PU/discussion
	touch chrome/test/data/webui/i18n_process_css_test.html

	# https://code.google.com/p/chromium/issues/detail?id=541273
	sed -i "/'target_name': 'libvpx'/s/libvpx/&_new/" build/linux/unbundle/libvpx.gyp
}
do_configure() {
	local conf=""

	export -n CFLAGS CXXFLAGS LDFLAGS
	if [ "$CROSS_BUILD" ]; then
		export GYP_CROSSCOMPILE=1
	fi

	# Google API keys (see http://www.chromium.org/developers/how-tos/api-keys)
	# Note: These are for Void Linux use ONLY.
	conf+="
		-Dgoogle_api_key=AIzaSyCIFdBA7eQP43R6kXRwTq7j6Mvj1ITze90
		-Dgoogle_default_client_id=858256686058.apps.googleusercontent.com
		-Dgoogle_default_client_secret=sMVx271L_MAz23TW2ek3AASn"

	# Disable glibc Native Client toolchain.
	conf+=" -Ddisable_glibc=1"

	# Disable clang for native and host builds.
	conf+=" -Dclang=0 -Dhost_clang=0"

	# Use system-provided libraries.
	# TODO: use_system_hunspell (upstream changes needed).
	# TODO: use_system_libsrtp.
	# TODO: use_system_libusb (http://crbug.com/266149).
	# TODO: use_system_ssl (http://crbug.com/58087).
	# TODO: use_system_sqlite (http://crbug.com/22208).
	#
	# XXX xtraeme: broken currently
	# -Duse_system_opus=1 (needs opus_custom.h)
	# -Duse_system_protobuf
	# -Duse_system_v8=1
	# -Duse_system_zlib=1
	# -Duse_system_livbpx=1
	conf+="
		-Dclang_use_chrome_plugins=0
		-Dpython_ver=2.7
		-Dicu_use_data_file_flag=0
		-Duse_system_bzip2=1
		-Duse_system_flac=1
		-Duse_system_harfbuzz=1
		-Duse_system_jsoncpp=1
		-Duse_system_libevent=1
		-Duse_system_libjpeg=1
		-Duse_system_libpng=1
		-Duse_system_libxml=1
		-Duse_system_libxslt=1
		-Duse_system_minizip=1
		-Duse_system_nspr=1
		-Duse_system_re2=1
		-Duse_system_snappy=1
		-Duse_system_speex=1
		-Duse_system_xdg_utils=1
		-Duse_system_yasm=1
		-Duse_system_icu=1
		-Duse_system_libvpx=0
		-Duse_cups=1
		-Duse_pulseaudio=1
		-Duse_mojo=0
		-Duse_gconf=0
		-Denable_widevine=1
		-Denable_hangout_services_extension=1"

	if [ "$CROSS_BUILD" ]; then
		conf+=" -Denable_webrtc=0"
		conf+=" -Duse_system_libwebp=0"
	else
		conf+=" -Duse_system_libwebp=1"
	fi

	# Use explicit library dependencies instead of dlopen.
	conf+="
		-Dlinux_link_cups=1
		-Dlinux_link_pulseaudio=1
		-Dlinux_link_gsettings=1
		-Dlinux_link_libpci=1
		-Dlinux_link_libspeechd=1
		-Dlibspeechd_h_prefix=speech-dispatcher/"

	# TODO: use the file at run time instead of effectively compiling it in.
	conf+=" -Dusb_ids_path=/usr/share/hwdata/usb.ids"

	# Never use bundled binutils/gold binary. Disable gold linker flags for now.
	conf+=" -Dlinux_use_bundled_binutils=0 -Dlinux_use_gold_binary=0 -Dlinux_use_gold_flags=0"

	# Always support proprietary codecs.
	conf+=" -Dproprietary_codecs=1"

	# Enable H.264 support in bundled ffmpeg.
	conf+=" -Dffmpeg_branding=Chrome"

	# Make sure that -Werror doesn't get added to CFLAGS by the build system.
	# Depending on GCC version the warnings are different and we don't want
	# the build to fail because of that.
	conf+=" -Dwerror="

	# Save space by removing DLOG and DCHECK messages (about 6% reduction).
	conf+=" -Dlogging_like_official_build=1"
	conf+=" -Dtracing_like_official_build=1"
	conf+=" -Dfieldtrial_testing_like_official_build=1"

	case "${XBPS_TARGET_MACHINE}" in
		x86_64-musl*)
			conf+=" -Ddisable_nacl=1 -Ddisable_pnacl=1"
			conf+=" -Duse_allocator=none -Dtarget_arch=x64"
			;;
		i686-musl*)
			conf+=" -Ddisable_nacl=1 -Ddisable_pnacl=1"
			conf+=" -Duse_allocator=none -Dtarget_arch=ia32"
			;;
		i686)
			conf+=" -Ddisable_nacl=1 -Ddisable_pnacl=1"
			;;
		arm*)
			conf+=" -Ddisable_nacl=1 -Ddisable_pnacl=1"
			conf+=" -Dtarget_arch=arm -Dv8_use_arm_eabi_hardfloat=true"
			conf+=" -Darm_float_abi=hard"
			if [ "$CROSS_BUILD" ]; then
				sed -i "s/arm-linux-gnueabihf-/${XBPS_CROSS_TRIPLET}-/g" build/common.gypi
				conf+=" -Dsysroot=${XBPS_CROSS_BASE}"
			else
				conf+=" -Dsysroot="
			fi
			;;
	esac
	case "${XBPS_TARGET_MACHINE}" in
		armv7*) conf+="
			-Darmv7=1
			-Darm_neon=1
			-Darm_thumb=1
			-Darm_fpu=neon"
			CFLAGS=${CFLAGS/vfpv3/neon}
			;;
		armv6*)
			conf+="
			-Darmv7=0
			-Darmv6=1
			-Darm_neon=0
			-Darm_fpu=vfp
			-Darm_thumb=0
			-Dv8_can_use_vfp_instructions=false
			-Dv8_can_use_vfp2_instructions=true";;
	esac
	
	build/linux/unbundle/replace_gyp_files.py ${conf}
	build/gyp_chromium --depth=. ${conf} -Drelease_extra_cflags="${CFLAGS}"
}
do_build() {
	export -n CFLAGS CXXFLAGS LDFLAGS
	ninja -C out/Release ${makejobs} chrome chrome_sandbox chromedriver
}
do_install() {
	vinstall out/Release/chrome 755 usr/lib/${pkgname} ${pkgname}
	vinstall out/Release/chromedriver 755 usr/lib/${pkgname} chromedriver
	vinstall out/Release/chrome_sandbox 4755 usr/lib/${pkgname} chrome-sandbox

	if [ "$XBPS_TARGET_MACHINE" = "x86_64" ]; then
		cp out/Release/nacl_helper{,_bootstrap} ${DESTDIR}/usr/lib/chromium
		cp out/Release/nacl_irt_x86_64.nexe ${DESTDIR}/usr/lib/chromium
	fi

	cp out/Release/{*.pak,*.bin} ${DESTDIR}/usr/lib/chromium
	cp -a out/Release/locales ${DESTDIR}/usr/lib/chromium

	vman out/Release/chrome.1 chromium.1
	vinstall ${FILESDIR}/chromium.desktop 644 usr/share/applications

	for size in 22 24 48 64 128 256; do
		install -Dm644 "chrome/app/theme/chromium/product_logo_${size}.png" \
		${DESTDIR}/usr/share/icons/hicolor/${size}x${size}/apps/chromium.png
	done
	for size in 16 32; do
		install -Dm644 "chrome/app/theme/default_100_percent/chromium/product_logo_${size}.png" \
		${DESTDIR}/usr/share/icons/hicolor/${size}x${size}/apps/chromium.png
	done

	vbin ${FILESDIR}/chromium.sh chromium
	vlicense LICENSE
	ln -s /usr/lib/chromium/chromedriver ${DESTDIR}/usr/bin/chromedriver
}
