# Contributor: Carlo Landmeter <clandmeter@gmail.com>
# Maintainer: Carlo Landmeter <clandmeter@gmail.com>
setvar pkgname = 'chromium'
setvar pkgver = '61.0.3163.100'
setvar pkgrel = '0'
setvar pkgdesc = ""chromium web browser""
setvar url = ""http://www.chromium.org/""
setvar arch = ""x86_64""
setvar license = ""BSD""
setvar depends = ""xdg-utils""
setvar depends_dev = """"
setvar 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
	""
setvar install = """"
# explicit depends for --headless
setvar depends = ""eudev-libs ttf-opensans""
setvar options = 'suid'
setvar subpackages = ""$pkgname-doc $pkgname-chromedriver""
setvar 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-hacks.patch
	musl-libc++.patch
	musl-sandbox.patch
	no-execinfo.patch
	no-getcontext.patch
	no-mallinfo.patch
	resolver.patch
	unset-madv_free.patch
	swiftshader.patch
	secure_getenv.patch
	gcc-fixes.patch
	fix-atk.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
	chromium-61.0.3163.79-gcc5-r1.patch
	chromium-61.0.3163.79-gtk2fix.patch
	""

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

case{
	x86_64 { setvar target_arch = 'x64'}
	x86 { setvar target_arch = 'ia32'}
	arm { setvar target_arch = 'arm'}
}

if test -n $DEBUG {
	setvar _buildtype = 'Debug'
	setvar _is_debug = 'true'
} else {
	setvar _buildtype = 'Release'
	setvar _is_debug = 'false'
}

proc prepare {
	cd $builddir

	local i
	cd $builddir
	for i in $source {
		case (i) {
		*.patch { msg $i; patch -p0 -i "$srcdir"/$i}
		}
	}

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

proc _gn_flags {
	echo $[join(ARGV)]
}

proc build {
	cd $builddir
	# reusable system library settings
	local use_system="
		flac
		freetype
		harfbuzz-ng
		libdrm
		libjpeg
		libpng
		libwebp
		libxslt
		snappy
		yasm
		zlib
		"
	for _lib in ${use_system} libjpeg_turbo {
		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
	}

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

	setvar _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!"
	# build mksnapshot and paxmark it
	ninja -C out/$_buildtype mksnapshot
	paxmark -m out/$_buildtype/mksnapshot

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

proc 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 {
		install -Dm755 out/$_buildtype/$bin \
			"$pkgdir"/usr/lib/$pkgname/$bin
	}
	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 {
		install -Dm644 $pak \
			"$pkgdir"/usr/lib/$pkgname/${pak##*/}
	}

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

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

        install -Dm644 out/$_buildtype/chrome.1 \
                "$pkgdir"/usr/share/man/man1/chrome.1

	install -Dm644 out/$_buildtype/chrome.1 \
		"$pkgdir"/usr/share/man/man1/chromium.1

	# 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 {
		install -Dm644 "chrome/app/theme/chromium/product_logo_$size.png" \
			"$pkgdir/usr/share/icons/hicolor/${size}x${size}/apps/chromium.png"
	}

	for size in 16 32 {
		install -Dm644 "chrome/app/theme/default_100_percent/chromium/product_logo_$size.png" \
			"$pkgdir/usr/share/icons/hicolor/${size}x${size}/apps/chromium.png"
	}

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

proc chromedriver {
	setvar 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
}

setvar sha512sums = ""4dfd3a493ceb7c605eeab6f387541273d529e8b935a6104927e34836469aedcfcbb4a1154591585621d5c2e842e68701d32328acba842f840a20498822165f78  chromium-61.0.3163.100.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
902aeffaf0a755dd856a7d96c390adafd53ff23aed0a3693acc86a680d8c001f0f565f50caafd8bac4bba6f5e04011ebd9c27cda3264fd2ffc27df2803b738b1  gn_bootstrap.patch
8fbfd67a0b6bbdf08364e810bd85b4a80dda9af73fefe3aba8010d9b33022d458a785c628515bbda9c743b8a0293d57cfe18fcc5aa2313c845c6fb948c2335f9  last-commit-position.patch
c78e868ec5b902e5fd5febca8aaf9649bd598c747d8e260683552d29bb322e2e4cb1e71d92750e3209096e24416297396cca47a6a855b657cb7685b39d06b0f0  musl-fixes.patch
507a8db2317f1f6ec18dec6cb5894b716e9b2542b58887bab9319bc6d4c66fe4a4d09b200ca8e3f11b32e380b282442a27e7a1b358d3c25eef0fa7655e9dc134  musl-hacks.patch
c38e3e30512f6f5d494bcd905b314dea52e09214f8fc779b66193f5eb3af8f1afb5cdf2d096aa4590e6a1dd28c289d26812d7268abf4459209fb8bdc8806a0be  musl-libc++.patch
9b75d6ac720d1b8ddc597f0f472bc400ff866a733f12b3a4cd3e7e18e724549c5f8e056c7e0d0462ef083bff5e677f8cef6b89b22f4740a40ad6398978269373  musl-sandbox.patch
fe2cee01d07e5dc638dad0c5453397eb03e30867c1e54591d7f612034366c96ccaa35216d2bcedbe796863eeb5b90dcf71c75e5391c266df4cb27b44e59bb040  no-execinfo.patch
a57858e45cfbb7e97f77b3b308502e377543d61d3d0085798ed73bc53da456828f5e010308d0de05fee4fe52071362d074f5af205f98f41d67222d997857d626  no-getcontext.patch
7a3a4ba4479a22a13126a78ad3d9991188ef030389026c4293d45b4440711751d68e43d28687653d5670d7f00ed5f1b820a8df50a3a08ce06e454f0cad9dbd3c  no-mallinfo.patch
ac0c9d7780ac897ea8ad5d909616d3aa29eed720353c7a660c850ee1404c80f51f5db274b3c5d3c730561a7c3cd138624e1901ece329926b5dc5ca15195d9abf  resolver.patch
27dd7916be3324294a0a913c43c856e9c00be767a2aec4f3da2455fb84e74ebb0017d1aa586b40dd9d989ada6b60fc531522f503d57e04324726a2171bf3a85e  unset-madv_free.patch
6b0812725a0fc562527f3556dc4979fec72d1ba92f26a5e78ff2016c39bb2c155a0ff95fc22101f9c097d14b84182d6615276f4247f60ae7833ab45da8366e6d  swiftshader.patch
67eea8e13c85aca7db5c85f5b0d33e5798e8063ab7f5b2f0a5c7f6fee8266e58bee4e72f4542472ef6634634ca0f4359a2d1113fa310a991373404320c784730  secure_getenv.patch
0d6935117e3531a33aab5d09093e854c7e82e799340222cd75e3a1d7e054fbe02524f59c56d01a36649da5c89042a6ce7af2313917d3d5a3947ee7c890b99ba6  gcc-fixes.patch
aa44cbc1857f407d897fba8c64faa6401aa9c30f4de0a6c79e0fb63086b0ee518e411335a53c0bd75c3023f3f064aff57270a47ffe4f8f31c251aa318570e0c2  fix-atk.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
b39701c208d2786cc4b68ea033807a8663358173442ce0884dbfdebaa4b784d3eae8d7c1fe96c71ce8919fea35c50e1951c6a12193c23ae3e520604b3374ca38  chromium-61.0.3163.79-gcc5-r1.patch
3820f10d6c857d2727bcb862745d4f00b217c585e7a19db56c390c3331939fdf22bee758c3ccfb4b360d75a1c05b65e3406fd27a23d69492f86d907830223063  chromium-61.0.3163.79-gtk2fix.patch""