# Template file for 'chromium'
pkgname=chromium
# See http://www.chromium.org/developers/calendar for the latest version
version=55.0.2883.87
revision=1
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=e81bd3140d9c84dfee04d9a94686dfe6a20ae79475d84f17154c5536dcb81a58

only_for_archs="i686 x86_64 x86_64-musl"
lib32disabled=yes
nodebug=yes
nopie=yes  # contains tools that are not PIE, enables PIE itself

build_options="nacl"
desc_option_nacl="Enable NaCL support"

hostmakedepends="yasm python pkg-config perl gperf bison ninja hwids
 libatomic-devel libevent-devel libglib-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 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 harfbuzz-devel
 minizip-devel jsoncpp-devel zlib-devel libcap-devel libXdamage-devel"
depends="libexif hwids desktop-file-utils hicolor-icon-theme"
nostrip_files="$(vopt_if nacl 'nacl_irt_x86_64.nexe nacl_irt_x86_32.nexe')"

# nacl currently makes problems with GN
[ "$build_option_nacl" ] && broken=yes

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

	# Use the file at run time instead of effectively compiling it in
	sed 's|//third_party/usb_ids/usb.ids|/usr/share/hwdata/usb.ids|g' \
		-i device/usb/BUILD.gn

	# Work around bug in blink in which GCC 6 optimizes away null pointer checks
	# https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=833524
	# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68853#c2
	sed -i '/config("compiler")/ a cflags_cc = [ "-fno-delete-null-pointer-checks" ]' \
		build/config/linux/BUILD.gn
}
do_configure() {
	local system="" bootstrap_conf="" conf=""
	export -n CFLAGS CXXFLAGS LDFLAGS

	# 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
	# use_system_opus=1 (needs opus_custom.h)
	# use_system_protobuf
	# use_system_v8=1
	# use_system_zlib=1
	# use_system_libvpx=1
	# use_system_re2=1 // breaks with re2-devel-2016.06.01
	# bzip2 jsoncpp minizip xdg_utils speex
	system="
		flac
		harfbuzz-ng
		libevent
		libjpeg
		libpng
		libxml
		libxslt
		snappy
		yasm"

	# TODO: is this still broken?
	[ -z "$CROSS_BUILD" ] && system+=" libwebp"

	# remove build scripts for system provided dependencies
	for LIB in ${system} libjpeg_turbo; do
		find -type f -path "*third_party/$LIB/*" \
			\! -path "*third_party/$LIB/chromium/*" \
			\! -path "*third_party/$LIB/google/*" \
			\! -regex '.*\.\(gn\|gni\|isolate\|py\)' \
			-delete
	done

	# switch to system provided dependencies
	python2 build/linux/unbundle/replace_gn_files.py --system-libraries ${system}

	python2 third_party/libaddressinput/chromium/tools/update-strings.py

	# bootstrap NaCL toolchain
	if [ -n "$build_option_nacl" ] && [ "$XBPS_TARGET_MACHINE" = "x86_64" ]; then
		# XXX xtraeme: fix gcc{,-c++}-multilib instead.
		ldconfig &>/dev/null
		# Download NaCL toolchains
		python2 nbuild/download_acl_toolchains.py \
			--packages nacl_x86_newlib,pnacl_newlib,pnacl_translator sync --extract
		# donwload clang
		python2 tools/clang/scripts/update.py
		ln -s /usr/lib/libncursesw.so.6 third_party/llvm-build/Release+Asserts/lib/libtinfo.so.5
	fi

	# bootstrap GN
	bootstrap_conf='
		is_clang=false
		treat_warnings_as_errors=false
		binutils_path="/usr/bin"
		gold_path="/usr/bin/ld.gold"
		use_gconf=false
		use_sysroot=false
		use_gold=true
		use_allocator="none"
		use_experimental_allocator_shim=false'
	AR="ar" CC=$CC_FOR_BUILD CXX=$CXX_FOR_BUILD LD=$CXX_FOR_BUILD \
		python2 tools/gn/bootstrap/bootstrap.py --gn-gen-args "${bootstrap_conf//	/ }"

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


	if [ "$build_option_nacl" ] && [ "$XBPS_TARGET_MACHINE" = "x86_64" ]; then
		# Disable glibc Native Client toolchain.
		conf+=' is_nacl_glibc=false'
		conf+=' enable_nacl=true enable_nacl_nonsfi=true'
	else
		conf+=' enable_nacl=false enable_nacl_nonsfi=false'
	fi

	conf+='
		is_clang=false
		is_debug=false
		remove_webcore_debug_symbols=true
		symbol_level=0
		icu_use_data_file=true
		use_cups=true
		use_pulseaudio=true
		use_gconf=false
		use_sysroot=false
		use_allocator="none"
		use_experimental_allocator_shim=false
		enable_widevine=true
		enable_hangout_services_extension=true'

	# TODO: is this still broken?
	[ "$CROSS_BUILD" ] && conf+=' enable_webrtc=false'

	# Use explicit library dependencies instead of dlopen.
	# GN only has "link_pulseaudio", the other options used before are not available atm
	# linux_link_cups=true
	# linux_link_gsettings=true
	# linux_link_libpci=true
	# linux_link_libspeechd=true
	# libspeechd_h_prefix=\"speech-dispatcher/\""
	conf+=' link_pulseaudio=true'

	# Never use bundled binutils/gold binary.
	conf+="
		binutils_path=\"${XBPS_CROSS_BASE}/usr/bin\"
		gold_path=\"${XBPS_CROSS_BASE}/usr/bin/ld.gold\"
		use_gold=true"

	# Always support proprietary codecs.
	# Enable H.264 support in bundled ffmpeg.
	conf+='
		proprietary_codecs=true
		ffmpeg_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+='
		treat_warnings_as_errors=false
		fatal_linker_warnings=false'

	# Save space by removing DLOG and DCHECK messages (about 6% reduction).
	# conf+=" logging_like_official_build=true"
	conf+=' fieldtrial_testing_like_official_build=true'

	case "${XBPS_TARGET_MACHINE}" in
		x86_64*) conf+=' target_cpu="x64"' ;;
		i686*) conf+=' target_cpu="x86"' ;;
		arm*) conf+=' target_cpu="arm"' ;;
		aarch64*) conf+=' target_cpu="arm64"' ;;
	esac

	out/Release/gn gen out/Release --args="${conf//	/ }"
}
do_build() {
	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 [ "$build_option_nacl" ] && [ "$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

	vinstall out/Release/icudtl.dat 0644 usr/lib/chromium

	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
}