# Maintainer: lauren n. liberda <lauren@selfisekai.rocks>
pkgname=electron
pkgver=27.1.2
_semver="${pkgver/_beta/-beta.}"
pkgrel=0
_chromium=118.0.5993.162
_depot_tools=b5509953468edd0906f2dc297886939abbd2bed5
_extra_patches=118.0.5993.11
pkgdesc="Electron cross-platform desktop toolkit"
url="https://github.com/electron/electron"
arch="aarch64 x86_64" # same as chromium
license="MIT"
depends="gtk+3.0 so:libudev.so.1 xdg-utils"
makedepends="
	alsa-lib-dev
	aom-dev
	bash
	brotli-dev
	bsd-compat-headers
	bzip2-dev
	c-ares-dev
	cairo-dev
	clang16-dev
	clang-extra-tools
	compiler-rt
	cups-dev
	curl-dev
	dav1d-dev
	dbus-glib-dev
	eudev-dev
	ffmpeg-dev
	findutils
	flac-dev
	flex
	freetype-dev
	gperf
	gtk+3.0-dev
	gn
	gzip
	harfbuzz-dev
	hunspell-dev
	http-parser-dev
	hwdata-dev
	java-jdk
	jpeg-dev
	jsoncpp-dev
	krb5-dev
	lcms2-dev
	libarchive-tools
	libavif-dev
	libbsd-dev
	libcap-dev
	libevent-dev
	libexif-dev
	libgcrypt-dev
	libjpeg-turbo-dev
	libnotify-dev
	libusb-dev
	libva-dev
	libwebp-dev
	libxcomposite-dev
	libxcursor-dev
	libxinerama-dev
	libxml2-dev
	libxrandr-dev
	libxscrnsaver-dev
	libxslt-dev
	linux-headers
	lld
	llvm16
	mesa-dev
	minizip-dev
	nghttp2-dev
	nodejs
	npm
	nss-dev
	opus-dev
	pciutils-dev
	perl
	pipewire-dev
	pulseaudio-dev
	py3-httplib2
	py3-parsing
	py3-six
	python3
	qt5-qtbase-dev
	re2-dev
	rsync
	rust
	samurai
	snappy-dev
	speex-dev
	sqlite-dev
	woff2-dev
	xcb-proto
	yarn
	zlib-dev
	zstd-dev
	"
subpackages="$pkgname-lang $pkgname-dev"
# the lower patches are specific to electron, the top ones are from the equivalent chromium version
source="https://s3.sakamoto.pl/lnl-aports-snapshots/electron-$_semver-$_chromium.tar.zst
	https://gitlab.com/Matt.Jolly/chromium-patches/-/archive/$_extra_patches/chromium-patches-$_extra_patches.tar.gz
	chromium-revert-drop-of-system-java.patch
	chromium-use-alpine-target.patch
	fix-missing-cstdint-include-musl.patch
	gdbinit.patch
	import-version.patch
	libstdc++13.patch
	mman.patch
	musl-sandbox.patch
	musl-tid-caching.patch
	musl-v8-monotonic-pthread-cont_timedwait.patch
	no-execinfo.patch
	no-mallinfo.patch
	no-mte.patch
	no-res-ninit-nclose.patch
	no-sandbox-settls.patch
	temp-failure-retry.patch

	icon.patch
	python-jinja-3.10.patch
	vector-const.patch
	webpack-hash.patch
	chromium-icu-74.patch

	default.conf
	electron.desktop
	electron-launcher.sh
	"
# tests are todo for some base checks
options="!check net suid"
builddir="$srcdir/electron-$_semver-$_chromium"

export PATH="$PATH:/usr/lib/qt5/bin"

# clang uses much less memory (and this doesn't support gcc)
export CC=clang-16
export CXX=clang++-16

# required to find the tools
export AR=llvm16-ar
export NM=llvm16-nm
export LD=clang++-16

# less log spam, reproducible
export CFLAGS="${CFLAGS/-g/} -O2 -Wno-unknown-warning-option -Wno-builtin-macro-redefined -Wno-deprecated-declarations"
export CXXFLAGS="${CXXFLAGS/-g/} -O2 -Wno-unknown-warning-option -Wno-builtin-macro-redefined -Wno-deprecated-declarations"
export CPPFLAGS="${CPPFLAGS/-g/} -D__DATE__=  -D__TIME__=  -D__TIMESTAMP__="

# breaks chromium-based stuff
export CXXFLAGS="${CXXFLAGS/-D_GLIBCXX_ASSERTIONS=1}"

# creates a dist tarball that does not need to git clone everything at build time.
snapshot() {
	deps
	# vpython3 execs system python3 with this set
	export VPYTHON_BYPASS="manually managed python not supported by chrome operations"
	export CHROMIUM_BUILDTOOLS_PATH="$srcdir/src/buildtools"
	mkdir -p "$srcdir"
	cd "$srcdir"
	if ! [ -d src ]; then
		git clone --branch=$_chromium --depth=1 \
			https://chromium.googlesource.com/chromium/src.git
	fi

	if ! [ -d electron ]; then
		git clone https://github.com/electron/electron.git
	fi

	if ! [ -d depot_tools ]; then
		(
			git clone --depth 1 -b main https://chromium.googlesource.com/chromium/tools/depot_tools.git
			cd depot_tools
			git fetch --depth 1 origin $_depot_tools
			git checkout $_depot_tools
		)
	fi

	export PATH="$PATH:$srcdir/depot_tools"

	echo "solutions = [
	{
		\"name\": \"src/electron\",
		\"url\": \"file://$srcdir/electron@v$_semver\",
		\"deps_file\": \"DEPS\",
		\"managed\": False,
		\"custom_deps\": {
			\"src\": None,
		},
		\"custom_vars\": {},
	},
	]" > .gclient

	python3 depot_tools/gclient.py sync \
		--with_branch_heads \
		--with_tags \
		--nohooks

	python3 src/build/landmines.py
	python3 src/build/util/lastchange.py -o src/build/util/LASTCHANGE
	python3 src/build/util/lastchange.py -s src/third_party/dawn \
		--revision src/gpu/webgpu/DAWN_VERSION
	python3 src/build/util/lastchange.py -m GPU_LISTS_VERSION \
		--revision-id-only --header src/gpu/config/gpu_lists_version.h
	python3 src/build/util/lastchange.py -m SKIA_COMMIT_HASH \
		-s src/third_party/skia --header src/skia/ext/skia_commit_hash.h

	# why?
	cp -r electron/patches/ffmpeg src/electron/patches/

	python3 electron/script/apply_all_patches.py \
		electron/patches/config.json

	python3 src/tools/update_pgo_profiles.py \
		--target=linux \
		update \
		--gs-url-base=chromium-optimization-profiles/pgo_profiles

	python3 src/tools/download_optimization_profile.py \
		--newest_state=src/chrome/android/profiles/newest.txt \
		--local_state=src/chrome/android/profiles/local.txt \
		--output_name=src/chrome/android/profiles/afdo.prof \
		--gs_url_base=chromeos-prebuilt/afdo-job/llvm

	mv src $pkgname-$_semver-$_chromium

	# extra binaries are most likely things we don't want, so nuke them all
	for elf in $(scanelf -RA -F "%F" $pkgname-$_semver-$_chromium); do
		rm -f "$elf"
	done

	msg "generating tarball.. (this takes a while)"
	tar -cf $pkgname-$_semver-$_chromium.tar \
		--exclude="ChangeLog*" \
		--exclude="testdata/*" \
		--exclude="test_data/*" \
		--exclude="android_rust_toolchain/*" \
		--exclude-backups \
		--exclude-caches-all \
		--exclude-vcs \
		$pkgname-$_semver-$_chromium

	zstd --auto-threads=logical --ultra --long -22 -T"${ZSTD_LIMIT:-0}" -vv $pkgname-$_semver-$_chromium.tar
}

_extra_patch() {
	msg chromium-"$1".patch
	patch -Np1 < "$srcdir"/chromium-patches-"$_extra_patches"/chromium-"$1".patch
}

prepare() {
	dos2unix third_party/vulkan_memory_allocator/include/vk_mem_alloc.h

	default_prepare

	_extra_patch 118-SensorReadingField-include
	_extra_patch 117-material-color-include
	_extra_patch 118-system-freetype
	_extra_patch 117-system-zstd
	_extra_patch 118-compiler

	git init -q .

	# link to system tools
	ln -sfv /usr/bin/clang-format buildtools/linux64/clang-format
	mkdir -p third_party/node/linux/node-linux-x64/bin
	ln -sfv /usr/bin/node third_party/node/linux/node-linux-x64/bin/node
	ln -sfv /usr/bin/java third_party/jdk/current/bin/java

	(
		cd electron
		git init -q .
		git config user.email "example@example.com"
		git config user.name "example"
		git config commit.gpgsign false
		git add LICENSE
		git commit -m "init"
		git tag "v$_semver"
		git pack-refs
		yarn install --frozen-lockfile --no-scripts
	)

	(
		cd third_party/node
		./update_npm_deps
	)

	# libaom - https://aomedia.googlesource.com/aom/+/706ee36dcc82%5E%21/
	local use_system="
		dav1d
		ffmpeg
		flac
		fontconfig
		freetype
		harfbuzz-ng
		icu
		jsoncpp
		libavif
		libdrm
		libevent
		libjpeg
		libwebp
		libxml
		libxslt
		opus
		re2
		snappy
		woff2
		zlib
		zstd
		"

	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/*" \
			\! -path './base/third_party/icu/*' \
			\! -path './third_party/libxml/*' \
			\! -path './third_party/pdfium/third_party/freetype/include/pstables.h' \
			\! -path './third_party/harfbuzz-ng/utils/hb_scoped.h' \
			\! -path './third_party/crashpad/crashpad/third_party/zlib/zlib_crashpad.h' \
			\! -regex '.*\.\(gn\|gni\|isolate\|py\)' \
			-delete
	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 services/device/public/cpp/usb/BUILD.gn

	msg "Running debundle script"
	python3 build/linux/unbundle/replace_gn_files.py --system-libraries \
		$use_system
	python3 third_party/libaddressinput/chromium/tools/update-strings.py

	# prevent annoying errors when regenerating gni
	sed -i 's,^update_readme$,#update_readme,' \
		third_party/libvpx/generate_gni.sh

	# allow system dependencies in "official builds"
	sed -i 's/OFFICIAL_BUILD/GOOGLE_CHROME_BUILD/' \
		tools/generate_shim_headers/generate_shim_headers.py

	# https://crbug.com/893950
	sed -i -e 's/\<xmlMalloc\>/malloc/' -e 's/\<xmlFree\>/free/' \
		third_party/blink/renderer/core/xml/*.cc \
		third_party/blink/renderer/core/xml/parser/xml_document_parser.cc \
		third_party/libxml/chromium/*.cc \
		third_party/maldoca/src/maldoca/ole/oss_utils.h

	_configure
}

_configure() {
	cd "$builddir"
	msg "Configuring build"

	# shellcheck disable=2089
	local gn_config="
		clang_base_path=\"/usr\"
		custom_toolchain=\"//build/toolchain/linux/unbundle:default\"
		host_toolchain=\"//build/toolchain/linux/unbundle:default\"
		import(\"//electron/build/args/release.gn\")

		blink_enable_generated_code_formatting=false
		chrome_pgo_phase=0
		clang_use_chrome_plugins=false
		fatal_linker_warnings=false
		ffmpeg_branding=\"Chrome\"
		icu_use_data_file=true
		is_cfi=false
		is_clang=true
		is_component_ffmpeg=true
		is_debug=false
		is_official_build=true
		symbol_level=0
		treat_warnings_as_errors=false

		angle_enable_gl_null=false
		build_tflite_with_xnnpack=false
		build_with_tflite_lib=true
		disable_fieldtrial_testing_config=true
		enable_hangout_services_extension=true
		enable_library_cdms=false
		enable_media_remoting=false
		enable_nacl=false
		enable_paint_preview=false
		enable_reading_list=false
		enable_remoting=false
		enable_reporting=false
		enable_rust=false
		enable_screen_ai_service=false
		enable_service_discovery=false
		enable_stripping=false
		enable_vr=false
		ozone_platform_headless=false

		link_pulseaudio=true
		proprietary_codecs=true
		rtc_link_pipewire=true
		rtc_use_pipewire=true
		use_custom_libcxx=false
		use_dawn=false
		use_pulseaudio=true
		use_sysroot=false
		use_system_cares=true
		use_system_freetype=true
		use_system_harfbuzz=true
		use_system_lcms2=true
		use_system_libdrm=true
		use_system_libffi=true
		use_system_libjpeg=true
		use_system_nghttp2=true
		use_vaapi=true
		"

	# shellcheck disable=2086,2090,2116
	gn gen out/Release --args="$(echo $gn_config)" \
		--export-compile-commands
}

build() {
	export PATH="$PATH:/usr/lib/qt5/bin"

	ninja -C out/Release \
		electron_dist_zip \
		node_gypi_headers \
		node_version_header \
		tar_headers
}

package() {
	mkdir -p "$pkgdir"/usr/lib/electron "$pkgdir"/usr/bin

	bsdtar -xf out/Release/dist.zip -C "$pkgdir"/usr/lib/electron

	chmod u+s "$pkgdir"/usr/lib/electron/chrome-sandbox

	install -Dm755 "$srcdir"/electron-launcher.sh "$pkgdir"/usr/bin/electron
	install -Dm755 "$srcdir"/default.conf "$pkgdir"/etc/electron/default.conf

	mkdir -p "$pkgdir"/usr/include/electron

	mv -v "$builddir"/out/Release/gen/node_headers "$pkgdir"/usr/include/electron
	ln -sv /usr/include/electron/node_headers/include/node "$pkgdir"/usr/include/electron/node

	mkdir -p "$pkgdir"/usr/include/electron/node_headers/include/nan
	cp -v "$builddir"/third_party/nan/*.h "$pkgdir"/usr/include/electron/node_headers/include/nan
	ln -sv /usr/include/electron/node_headers/include/nan "$pkgdir"/usr/include/electron/nan

	install -Dm644 electron/default_app/icon.png \
		"$pkgdir"/usr/share/icons/hicolor/1024x1024/apps/electron.png
	install -Dm644 "$srcdir"/electron.desktop \
		-t "$pkgdir"/usr/share/applications/
}

lang() {
	pkgdesc="$pkgdesc (translations)"
	install_if="$pkgname=$pkgver-r$pkgrel lang"

	mkdir -p "$subpkgdir"/usr/lib/electron/locales

	mv "$pkgdir"/usr/lib/electron/locales/*.pak \
		"$subpkgdir"/usr/lib/electron/locales
	mv "$subpkgdir"/usr/lib/electron/locales/en-US.pak \
		"$pkgdir"/usr/lib/electron/locales
}

sha512sums="
07f653f24e7fe1ef96a0ff676fa3c987fd0826980b5e9611705cb7fc44f00182fa62e67e67c6df5a3b1b4c063d99a3266054a83aa67fb44b11ffffcd8e23c3eb  electron-27.1.2-118.0.5993.162.tar.zst
194c3a7a0fa03a85df6fe52ece3d53d4d15b9d0cb440b56a2ccb1b5c0d3f6481b6f7287aa705c596ceea92d475677ddaf58926f3b31c03a3c20e80ad7e481ce7  chromium-patches-118.0.5993.11.tar.gz
29bb685e03356a77df5fd347cdf55194cc8b3265c421cc76e54d64edefc329dbcb052deb26b22e8f587ce68456876c071de1b7d258dd0fcc6ee66c875ec4a020  chromium-revert-drop-of-system-java.patch
fa291e941076146d0edd5b96c088240a44a6e0aca3dfc744929655607182d2dc47e6c35ecb419f7c623fcf7f26dc3c4dd924dbf5ed10c3b986283f5ef2f72573  chromium-use-alpine-target.patch
9200f78bad70e95c648a5e8392d50642190600f655c6baa366ff6467ebad52d3b3f305dad58f3610da67136f4b723557653b174ec5c25be8d8737ee04d9ee09f  fix-missing-cstdint-include-musl.patch
33ee60863cc438ef57ffef92ba4cf67a856a5ffc16138bce241bcf87e47b15154aa86918e793c26f7ec4dc62a445257ad5673ed7001daf22c4043cf6cc57da7f  gdbinit.patch
8de65109ece27ea63bd469f2220c56b8c752ba0a50fdf390082a2d5ae74b8e010199126175569f6d5084270dd4e0571e68aec32c0bca8211a6699925b3a09124  import-version.patch
49851d42ce8ccd533d01d1bb2477930802b0bcebab8dd52f2da292088378c6ed9b74146e7dad55edfe096281fc84b2c55abaf832744fd4553a97c38ed891df3a  libstdc++13.patch
0e991842e23a4b9133898125eeb39e45e3f86f886eef5d2f0d9a72ee143a3e124b3b4f60be94edd57ce4185bcd69704edb51f76d08fdb6207f5559a08dd41ab0  mman.patch
50c274a420bb8a7f14fcb56e40920dac8f708792a4520789b4987facea459bef88113d5a2b60fa8c57bee6e92bff3617d6b73fa305c8c44614c638971cffd440  musl-sandbox.patch
e7163ac5810ac85366cef2447412287c856e3d67c6b77f219a6e5a418b1965b98e449c409424ad0704a5bded9355dd0aec3dc4585918ce5a2ab36c079707afe2  musl-tid-caching.patch
92eb002718026611f5542362ad69b67f0a398ff71b3fca5c05d55cb5c6f9f29334e5e127bb4860cfaa3fba0f0d4c901e2b98808217e7dc02e254a64a5c9521aa  musl-v8-monotonic-pthread-cont_timedwait.patch
8cc774e8d84e434960222c0497ad8193ae35c0732f98d3282d5fd4b4930f914809eec97832c199517ca89ca6b9d1d011db5ce533c40c68ce5fa464609d131a23  no-execinfo.patch
b5479874d125ee95a311295f227f8881a83023ec34fded7a6160b3ae32ea3ba0f2b833a9fb264c57f3d22746b6d8b00bdc8eb2ff86c43c412d6d3b55ae15b16b  no-mallinfo.patch
8a52ff52201a5e20344f5497ee2ffef0520f7b2d934be92227e49c3f2c12a94c33650eefc88a0e451a6b81d44ce197db421aaec7388e6bb1cb525a43628779d3  no-mte.patch
e4c4e5bc6f828f9c883dd418c0ba01887949c29c311f76206a1ec29f620b0c0ba0452949dc2778a9c46ea066405857536964a36436a68eecf7da7952736333cf  no-res-ninit-nclose.patch
6dc4d8dc92e685dace62265a1ddb3aebc558aed54d20ff6d36b030be0c48d7e84662326c31363612492574d9a03c62653cdc21a60995b97dee1d75cae86a9f9b  no-sandbox-settls.patch
e48693e6b7aeebf69a5acbf80d9a35defe4c23835121dfeb58b051ac7c527e758a41004f4d193274fe1b01c0bfb1dbc77b09cb6a404a3fdee507a2918afb0edb  temp-failure-retry.patch
905565c10f5e5600e7d4db965c892cc45009a258e9995da958974d838ace469e1db1019195307e8807860d5b55ba6bfeea478b1f39a9b99e82c619b2816a1a22  icon.patch
e05180199ee1d559e4e577cedd3e589844ecf40d98a86321bf1bea5607b02eeb5feb486deddae40e1005b644550331f6b8500177aa7e79bcb3750d3c1ceb76c3  python-jinja-3.10.patch
71571b15cf8bd6259b7fd22bea0e46b64890f3db776365de33fe539f26ce9ef99459e05c3dde9434c3657225bc67160abc915acd93033cb487c770c6a2a5975f  vector-const.patch
2aa340854316f1284217c0ca17cbf44953684ad6c7da90815117df30928612eb9fb9ffb734b948dfc309cd25d1a67cd57f77aac2d052a3dd9aca07a3a58cbb30  webpack-hash.patch
4c540972fa12acd9f0aafb8dc7e9987c3d6e4f28ff679dde522ebcec2dc5ae1a62d9d255bed0a30b9c79ae3b90ab0f5b9ae1ef5b7bf338612e28d9ef70250ca3  chromium-icu-74.patch
07e9203b05402f81c0ded5871a845e37bdc4c09b7bb2839312396f298a9ce8196e2c24508675e3d6f695f1e2b0ff1c2c64f4e9dfff3ff5359a87cb7b9b972393  default.conf
191559fc7aa1ea0353c6fb0cc321ee1d5803a0e44848c8be941cfab96277b0de6a59962d373e2a2a1686c8f9be2bcf2d2f33706759a339a959e297d3f7fda463  electron.desktop
ff1844036c8ae0a0a57a16211a816bc0ad550ccf6ea1cf718e228b8c95b9c4f5c9772d1a1a23638c0e140703a7b52874371e27a0d9d54a7b9468e5c384759be5  electron-launcher.sh
"
