# Contributor: Carlo Landmeter <clandmeter@gmail.com>
# Maintainer: Carlo Landmeter <clandmeter@gmail.com>
pkgname=chromium
pkgver=63.0.3239.108
pkgrel=0
pkgdesc="chromium web browser"
url="http://www.chromium.org/"
arch="x86_64"
license="BSD"
depends="xdg-utils"
depends_dev=""
makedepends="$depends_dev
	alsa-lib-dev
	bash
	binutils-gold
	bison flex
	bsd-compat-headers
	bzip2-dev
	cairo-dev
	cups-dev
	dbus-glib-dev
	eudev-dev
	ffmpeg-dev
	findutils
	flac-dev
	freetype-dev
	gnutls-dev
	gperf
	gtk+-dev
	gzip
	harfbuzz-dev
	hunspell-dev
	hwdata-usb
	jpeg-dev
	jsoncpp-dev
	krb5-dev
	libbsd-dev
	libcap-dev
	libelf-dev
	libexif-dev
	libgcrypt-dev
	libgnome-keyring-dev
	libjpeg-turbo-dev
	libpng-dev
	libusb-dev
	libwebp-dev
	libxcomposite-dev
	libxcursor-dev
	libxinerama-dev
	libxml2-dev
	libxrandr-dev
	libxscrnsaver-dev
	libxslt-dev
	libxtst-dev
	linux-headers
	mesa-dev
	mesa-dev
	minizip-dev
	ninja
	nodejs
	nss-dev
	paxmark
	pciutils-dev
	perl
	python2
	snappy-dev
	speex-dev
	sqlite-dev
	xdg-utils
	yasm
	zlib-dev
	"
install=""
# explicit depends for --headless
depends="eudev-libs ttf-opensans"
options=suid
subpackages="$pkgname-chromedriver"
source="https://commondatastorage.googleapis.com/chromium-browser-official/$pkgname-$pkgver.tar.xz
	pstables-2.8.h::http://git.savannah.gnu.org/cgit/freetype/freetype2.git/plain/src/psnames/pstables.h?h=VER-2-8
	chromium-launcher.sh
	chromium.conf
	chromium.desktop
	google-api.keys

	chromium-widevine.patch
	default-pthread-stacksize.patch
	gcc6.patch
	gn_bootstrap.patch
	last-commit-position.patch
	musl-fixes.patch
	musl-fixes-breakpad.patch
	musl-hacks.patch
	musl-libc++.patch
	musl-sandbox.patch
	no-execinfo.patch
	no-mallinfo.patch
	resolver.patch
	unset-madv_free.patch
	swiftshader.patch
	secure_getenv.patch
	chromium-webrtc-r0.patch
	chromium-48.0.2564.116-libusb_interrupt_event_handler.patch
	chromium-60.0.3112.78-gn-system.patch
	chromium-60.0.3112.78-jpeg-nomangle.patch
	chromium-60.0.3112.78-no-libpng-prefix.patch
	chromium-60.0.3112.78-no-zlib-mangle.patch
	"

builddir="$srcdir"/$pkgname-$pkgver

case "${CARCH}" in
	x86_64) target_arch=x64;;
	x86) target_arch=ia32;;
	arm) target_arch=arm;;
esac

if [ -n "$DEBUG" ]; then
	_buildtype=Debug
	_is_debug=true
else
	_buildtype=Release
	_is_debug=false
fi

prepare() {
	cd "$builddir"

	local i
	cd "$builddir"
	for i in $source; do
		case $i in
		*.patch) msg $i; patch -p0 -i "$srcdir"/$i;;
		esac
	done

	# https://groups.google.com/a/chromium.org/d/topic/chromium-packagers/9JX1N2nf4PU/discussion
	touch chrome/test/data/webui/i18n_process_css_test.html
	# 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

	mkdir -p third_party/node/linux/node-linux-x64/bin
	ln -s /usr/bin/node third_party/node/linux/node-linux-x64/bin/
}

_gn_flags() {
	echo $*
}

build() {
	cd "$builddir"
	# reusable system library settings
	local use_system="
		flac
		libdrm
		libjpeg
		libpng
		libwebp
		libxslt
		snappy
		yasm
		zlib
		"
	for _lib in ${use_system} libjpeg_turbo; do
		msg "Removing buildscripts for system provided $_lib"
		find -type f -path "*third_party/$_lib/*" \
			\! -path "*third_party/$_lib/chromium/*" \
			\! -path "*third_party/$_lib/google/*" \
			\! -regex '.*\.\(gn\|gni\|isolate\|py\)' \
			-delete
	done

	# pdfium uses internal headers in freetype
	# we copy from freetype sources
	# https://bugs.chromium.org/p/pdfium/issues/detail?id=733
	# should be fixed in freetype:
	# https://savannah.nongnu.org/bugs/index.php?51156
	mkdir -p "third_party/freetype/src/src/psnames/"
	cp "$srcdir"/pstables-2.8.h third_party/freetype/src/src/psnames/pstables.h

	# Work around bug in v8 in which GCC 6 optimizes away null pointer checks
	# https://bugs.chromium.org/p/v8/issues/detail?id=3782
	# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69234
	CFLAGS="$CFLAGS -fno-delete-null-pointer-checks"

	msg "Replacing gyp files"
	python build/linux/unbundle/replace_gn_files.py --system-libraries \
		${use_system}
	third_party/libaddressinput/chromium/tools/update-strings.py

	##############################################################
	# Please dont use these keys outside of Alpine Linux project #
	# You can create your own at:                                #
	# http://www.chromium.org/developers/how-tos/api-keys        #
	##############################################################
	eval "$(base64 -d < $srcdir/google-api.keys)"

	msg "Bootstrapping GN"
	local _c=$(_gn_flags is_clang=false \
		use_sysroot=false \
		treat_warnings_as_errors=false \
		fatal_linker_warnings=false \
		binutils_path=\"/usr/bin\" \
		use_gconf=false \
		use_gold=true \
		use_allocator=\"none\" \
		use_allocator_shim=false \
	)

	python tools/gn/bootstrap/bootstrap.py --gn-gen-args "$_c"

	_c=$(_gn_flags \
		enable_hotwording=false \
		enable_nacl=false \
		enable_nacl_nonsfi=false \
		enable_precompiled_headers=false \
		ffmpeg_branding=\"Chrome\" \
		fieldtrial_testing_like_official_build=true \
		google_api_key=\"$_google_api_key\" \
		google_default_client_id=\"$_google_default_client_id\" \
		google_default_client_secret=\"$_google_default_client_secret\" \
		is_clang=false \
		is_debug=$_is_debug \
		linux_use_bundled_binutils=false \
		proprietary_codecs=true \
		symbol_level=0 \
		treat_warnings_as_errors=false \
		use_allocator=\"none\" \
		use_allocator_shim=false \
		use_gconf=false \
		use_gnome_keyring=false \
		use_pulseaudio=false \
		use_sysroot=false \
	)

	out/Release/gn gen out/$_buildtype --args="$_c"

	msg "Ninja turtles GO!"
	# workaround parallel build
	ninja -C out/Release gen/ui/accessibility/ax_enums.h \
		gen/ui/accessibility/ax_enums.cc

	# build mksnapshot and paxmark it
	ninja -C out/$_buildtype mksnapshot
	paxmark -m out/$_buildtype/mksnapshot
	ninja -C out/$_buildtype v8_context_snapshot_generator
	paxmark -m out/Release/v8_context_snapshot_generator \
		out/Release/obj/tools/v8_context_snapshot/v8_context_snapshot_generator

	# finish rest of the build
	ninja -C out/$_buildtype chrome chrome_sandbox chromedriver
}

package() {
	cd "$builddir"
	local bin pak
	# paxmark inside chroot too
	paxmark -m out/$_buildtype/mksnapshot

	for bin in chrome natives_blob.bin snapshot_blob.bin; do
		install -Dm755 out/$_buildtype/$bin \
			"$pkgdir"/usr/lib/$pkgname/$bin
	done
	paxmark -m "$pkgdir"/usr/lib/$pkgname/chrome

        install -Dm4755 "$builddir"/out/$_buildtype/chrome_sandbox \
		"$pkgdir"/usr/lib/$pkgname/chrome-sandbox

	install -m644 "$builddir"/out/$_buildtype/icudtl.dat \
		"$pkgdir"/usr/lib/$pkgname/icudtl.dat

	for pak in out/$_buildtype/*.pak; do
		install -Dm644 $pak \
			"$pkgdir"/usr/lib/$pkgname/${pak##*/}
	done

	install -Dm755 "$srcdir"/chromium-launcher.sh \
		"$pkgdir"/usr/lib/$pkgname/chromium-launcher.sh

	cp -r out/$_buildtype/locales "$pkgdir"/usr/lib/$pkgname/

	# It is important that we name the target "chromium-browser",
	# xdg-utils expect it; bug #355517.
	mkdir -p "$pkgdir"/usr/bin
	cd "$pkgdir"/usr/bin
	ln -sf /usr/lib/$pkgname/chromium-launcher.sh chromium-browser
	cd "$builddir"

	install -Dm644 "$srcdir"/chromium.conf \
		"$pkgdir"/etc/chromium/chromium.conf

	install -Dm644 "$srcdir"/chromium.desktop \
		"$pkgdir"/usr/share/applications/chromium.desktop

	for size in 22 24 48 64 128 256; do
		install -Dm644 "chrome/app/theme/chromium/product_logo_$size.png" \
			"$pkgdir/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" \
			"$pkgdir/usr/share/icons/hicolor/${size}x${size}/apps/chromium.png"
	done

	install -Dm755 "$builddir"/out/$_buildtype/chromedriver \
		"$pkgdir"/usr/lib/$pkgname/chromedriver
	ln -sf /usr/lib/$pkgname/chromedriver "$pkgdir"/usr/bin/
}

chromedriver() {
	pkgdesc="WebDriver driver for the Chromium Browser"
	mkdir -p "$subpkgdir"/usr/lib/$pkgname "$subpkgdir"/usr/bin
	mv "$pkgdir"/usr/lib/$pkgname/chromedriver \
		"$subpkgdir"/usr/lib/$pkgname/
	mv "$pkgdir"/usr/bin/chromedriver "$subpkgdir"/usr/bin
}

sha512sums="3cb7c8569d2d061f5abf4ad68d9ea1716a1b07bdf8dfa7b600d558d308374ff5dbda9f947e67e21186e3e716fe9524fc63e8eabd9eac1fa805f0eb7d994c32c8  chromium-63.0.3239.108.tar.xz
a3bb959c65944ae2fb765725cedcffd743a58bc0c2cd1f1999d15fe79801d00f3474b08b4ed7b48859ed921eb57093d0ad09d90f201d729ed9b8a419a591ed29  pstables-2.8.h
b9a810416dd7a8ffc3a5ced85ad9acebda1665bd08a57eec7b189698cc5f74d2c3fd69044e20fcb83297a43214b2772a1312b2c6122ea0eb716abacf39524d60  chromium-launcher.sh
f6d962b9e4c22dd42183df3db5d3202dab33eccecafb1bf63ca678147289581262db1e5e64cbe8f9c212beefb0a6717bb8d311e497f56b55fe95b8bab2db493f  chromium.conf
e182c998a43d22d1c76a86c561619afd1fca8c2be668265ad5e2f81a3806f7a154272cc027a2f8b370fb69446892c69e5967a4be76082325c14245ee7915234c  chromium.desktop
2d8237a940ea691bd10b08315429677a587f7ef9692a0cca53bfd066eae82998a6c71f402a8669e9de39f94d7f3280745d1628ea6eac5d76ca7116844d4e0dac  google-api.keys
55921cc33d623e0751e94d4fef2ddd69afb57381da6115995b1f318e2398978b9173e80f6ffbc6e6c0c0c55a04e934e0e34a46c32e03947cc31f0f5a226e7725  chromium-widevine.patch
05fb6d9434565a7a73f5c18d470ae600bf4afbe15d0e4a7c2770bf2596a0bd2788cdfeb37e0b566fc3d26ff2d0791b70488b2c184e3286cff5a1fa25e17582cd  default-pthread-stacksize.patch
88928afcc33843e17daa082435a4b88c2106b0252a202ad19f684021c027859366a0c12970700c0d1b010211eef95d295362dc6afdea1b00199dd8d2efbdcfe7  gcc6.patch
f420f6b4827da4dbd416b2de094452917313cb79e01d33a07029589066a0b79e06446882f1ade5ac418f180ae581937d620d1d48698df77b5c5ddb4a3d6cf680  gn_bootstrap.patch
8fbfd67a0b6bbdf08364e810bd85b4a80dda9af73fefe3aba8010d9b33022d458a785c628515bbda9c743b8a0293d57cfe18fcc5aa2313c845c6fb948c2335f9  last-commit-position.patch
ca73f4912c4e41a235821fba5f3a7a5266739ce86dc1b27bc07d6bc2f97dc97b15b2fa47dc95c7a82a7e0b8fa1e6a17071f6dc06ddf47f65a56f216c0aaaf054  musl-fixes.patch
90efbc89151c77f32434364dcbaabaf9d9a207f4a77f147cd51b3fe100832fbfb3a9fb665303a79a3d788e400f4f41890de202ccbb7bd1fc6252e33c6e74e429  musl-fixes-breakpad.patch
507a8db2317f1f6ec18dec6cb5894b716e9b2542b58887bab9319bc6d4c66fe4a4d09b200ca8e3f11b32e380b282442a27e7a1b358d3c25eef0fa7655e9dc134  musl-hacks.patch
c38e3e30512f6f5d494bcd905b314dea52e09214f8fc779b66193f5eb3af8f1afb5cdf2d096aa4590e6a1dd28c289d26812d7268abf4459209fb8bdc8806a0be  musl-libc++.patch
9b75d6ac720d1b8ddc597f0f472bc400ff866a733f12b3a4cd3e7e18e724549c5f8e056c7e0d0462ef083bff5e677f8cef6b89b22f4740a40ad6398978269373  musl-sandbox.patch
fe2cee01d07e5dc638dad0c5453397eb03e30867c1e54591d7f612034366c96ccaa35216d2bcedbe796863eeb5b90dcf71c75e5391c266df4cb27b44e59bb040  no-execinfo.patch
7a3a4ba4479a22a13126a78ad3d9991188ef030389026c4293d45b4440711751d68e43d28687653d5670d7f00ed5f1b820a8df50a3a08ce06e454f0cad9dbd3c  no-mallinfo.patch
ac0c9d7780ac897ea8ad5d909616d3aa29eed720353c7a660c850ee1404c80f51f5db274b3c5d3c730561a7c3cd138624e1901ece329926b5dc5ca15195d9abf  resolver.patch
27dd7916be3324294a0a913c43c856e9c00be767a2aec4f3da2455fb84e74ebb0017d1aa586b40dd9d989ada6b60fc531522f503d57e04324726a2171bf3a85e  unset-madv_free.patch
6b0812725a0fc562527f3556dc4979fec72d1ba92f26a5e78ff2016c39bb2c155a0ff95fc22101f9c097d14b84182d6615276f4247f60ae7833ab45da8366e6d  swiftshader.patch
67eea8e13c85aca7db5c85f5b0d33e5798e8063ab7f5b2f0a5c7f6fee8266e58bee4e72f4542472ef6634634ca0f4359a2d1113fa310a991373404320c784730  secure_getenv.patch
e4963e60d46eba748ab5b54d08fe0ca5469d2db7b985f11fd3f4fc334608b84aae3da46eeaff8c1734ff35792c06dc42cdda6610c86dd0eff28465cf7c146bca  chromium-webrtc-r0.patch
2e9496aa9ccd6fed2d0df4b659d55e87abb67b1be4e3cc7cb0483ccb458b9071bed9939132a2345bf15192b4dcfe5da9474d7f653ae2f4b4d46d2f3c032b3a3f  chromium-48.0.2564.116-libusb_interrupt_event_handler.patch
197ca3fd315e15c3e0b796615afbd01f65e2040027a2cde6304dea500cfaf18372ce2be88f4030bcd26651b913dd4af741a4df8803cdf9ff062a8eef9ab6c5db  chromium-60.0.3112.78-gn-system.patch
e9fcc976d2ac1b2e73d310790e76dae15111e78cd8e3efa5febd702fd9492d7d68b1b57d95bca88d5e16c0627594a00ebdeaf570df52be6e9b23a4212906c141  chromium-60.0.3112.78-jpeg-nomangle.patch
9b08e895826fdd14fd6334662b8d07807beabab7368f3fcff3fcebe76baba4c7714c55042b379da6dab1ea0b1c4e5d657bf972b644089f00b418b7c2f5b6ef1b  chromium-60.0.3112.78-no-libpng-prefix.patch
87a0c6ff4906f0c4b6f6b471ec9bf0b2e2d28431122e99a68f957474cf27ec6b073ebca14660c8858817219521d74a184b8444cf2885db21d28fa4599fcaa66f  chromium-60.0.3112.78-no-zlib-mangle.patch"
