x11-drivers: update latest NVIDIA GPU drivers to 535.104.12

NVIDIA Datacenter driver update release
notes:https://docs.nvidia.com/datacenter/tesla/tesla-release-notes-535-104-12/index.html

BUG=b/302075323
TEST=presubmit
RELEASE_NOTE=Update latest NVIDIA GPU drivers to 535.104.12.

cos-patch: bug
Change-Id: I44f680b9fa42c917f2f39696d0eddd351a210a49
Reviewed-on: https://cos-review.googlesource.com/c/cos/overlays/board-overlays/+/59219
Reviewed-by: Oleksandr Tymoshenko <ovt@google.com>
Tested-by: Cusky Presubmit Bot <presubmit@cos-infra-prod.iam.gserviceaccount.com>
Main-Branch-Verified: Cusky Presubmit Bot <presubmit@cos-infra-prod.iam.gserviceaccount.com>
diff --git a/project-lakitu/scripts/gpu_R535_version b/project-lakitu/scripts/gpu_R535_version
index 256fbcc..0949fb1 100644
--- a/project-lakitu/scripts/gpu_R535_version
+++ b/project-lakitu/scripts/gpu_R535_version
@@ -1 +1 @@
-535.104.05
\ No newline at end of file
+535.104.12
\ No newline at end of file
diff --git a/project-lakitu/scripts/gpu_latest_version b/project-lakitu/scripts/gpu_latest_version
index 256fbcc..0949fb1 100644
--- a/project-lakitu/scripts/gpu_latest_version
+++ b/project-lakitu/scripts/gpu_latest_version
@@ -1 +1 @@
-535.104.05
\ No newline at end of file
+535.104.12
\ No newline at end of file
diff --git a/project-lakitu/virtual/target-lakitu/target-lakitu-1.5.ebuild b/project-lakitu/virtual/target-lakitu/target-lakitu-1.5.ebuild
index 268b541..d8089b9 100644
--- a/project-lakitu/virtual/target-lakitu/target-lakitu-1.5.ebuild
+++ b/project-lakitu/virtual/target-lakitu/target-lakitu-1.5.ebuild
@@ -112,5 +112,6 @@
 		x11-drivers/nvidia-drivers:1/470
 		x11-drivers/nvidia-drivers:0/525
 		x11-drivers/nvidia-drivers:3/535
+		x11-drivers/nvidia-drivers:4/535
 	)
 "
diff --git a/project-lakitu/x11-drivers/nvidia-drivers/Manifest b/project-lakitu/x11-drivers/nvidia-drivers/Manifest
index db2a22a..ff7dab6 100644
--- a/project-lakitu/x11-drivers/nvidia-drivers/Manifest
+++ b/project-lakitu/x11-drivers/nvidia-drivers/Manifest
@@ -2,3 +2,4 @@
 DIST NVIDIA-Linux-x86_64-470.199.02.run 273256770 BLAKE2B 5ceca89da4b4c7de701602d3dbf26e71a1163be26e0e5eac65354ecd40bfc8d68c981a6ef75b13e95079835c95ef7f9e10e5f95a0ed09beffd5bc22cb81a5330 SHA512 e1265b6266473af652e9d7bf85dcd76e312af281c5f4f158ab322e34d378738acb87c30cfff7bf1f6e1b238883e7f665d9fc5151b1e0078dd9aece5a52655405
 DIST NVIDIA-Linux-x86_64-525.125.06.run 414318862 BLAKE2B 204c53bbf579edf6d88c9ab4efcf82983a2b58262df4748076de96f3d67b107949407ae0c5ffe9bf6747783b1bcb9a9efc4a3c60d7161427b1c85ecfc494171a SHA512 a5f13b633d111d9dc928e8522cd916a2b756fccbf2dc532649762a3f9bdc5503bd57c9c698da8205c49e82720b45789413a1afc26be77d741f823b49ae2f333d
 DIST NVIDIA-Linux-x86_64-535.104.05.run 340936244 BLAKE2B b549d84df020291180c2dd08f42abd806c01091ccc976932ed35d5ce0c3fe629adb892dfb48837b57f1eb2aefe13c6a06c7d3a05a490bf4de3c6d5315cf40114 SHA512 9aba3bada23b41acf7420da57cfced8bf883ef9210bbca5db9882e6e682e9755e0bc48fa12bb2ed114d4d68ba99443f26c22c94d3063d0daf8e9c1e21b369bb5
+DIST NVIDIA-Linux-x86_64-535.104.12.run 340891704 BLAKE2B 571e28b236eef516519d36cd96275bd6618c15c73d47744432b4eb812681f0af00ea8971613b56deb0e980d4e4e2fb9ae38c33288a707181841f32163c74203d SHA512 109cba805617aa309abb13256a2a70d9c36cb9bfbec8a423bc4301d8c2e8059e0b06b619fde4395c8cad17b9d09e07a318514ec59205e4a02ba620382029effd
diff --git a/project-lakitu/x11-drivers/nvidia-drivers/nvidia-drivers-535.104.12.ebuild b/project-lakitu/x11-drivers/nvidia-drivers/nvidia-drivers-535.104.12.ebuild
new file mode 100644
index 0000000..36d17b7
--- /dev/null
+++ b/project-lakitu/x11-drivers/nvidia-drivers/nvidia-drivers-535.104.12.ebuild
@@ -0,0 +1,138 @@
+#
+# Copyright 2023 Google LLC
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# version 2 as published by the Free Software Foundation.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+
+EAPI=7
+
+inherit cos-linux-mod linux-mod eutils toolchain-funcs unpacker
+
+DESCRIPTION="NVIDIA Accelerated Graphics Driver Precompilation"
+
+NV_KERNEL_MAX="6.2"
+NV_URI="https://us.download.nvidia.com/tesla/"
+
+INSTALLER_NAME="NVIDIA-Linux-x86_64-${PV}"
+RUNFILE="${INSTALLER_NAME}.run"
+PRECOMPILED="${INSTALLER_NAME}-custom"
+DESCRIPTION="NVIDIA Accelerated Graphics Driver"
+HOMEPAGE="https://www.nvidia.com/download/index.aspx"
+SRC_URI="${NV_URI}${PV}/${RUNFILE}"
+
+S="${WORKDIR}"
+
+LICENSE="NVIDIA-r2 BSD BSD-2 GPL-2 MIT ZLIB curl openssl"
+SLOT="4/${PV%%.*}"
+KEYWORDS="*"
+IUSE="+clang kernel-5_10"
+
+COMMON=""
+DEPEND="
+	${COMMON}
+	kernel-5_10? ( sys-kernel/lakitu-kernel-5_10[kernel_sources] )
+"
+RDEPEND="
+	${COMMON}"
+
+src_unpack() {
+	# we want the entire .run file
+	# using the default src_unpack extracts the runfile contents.
+	cp ${DISTDIR}/${RUNFILE} ${S}
+}
+
+src_prepare() {
+	use clang || cros_use_gcc
+	default
+}
+
+# create a wrapper that removes -Werror=strict-prototypes
+# from the compiler options
+create_cc_wrapper() {
+	cp "${FILESDIR}/nvidia-installer-cc.wrapper" "${S}/${CC}"
+	chmod a+x "${S}/${CC}"
+}
+
+# the $LD points to the ld.lld linker which has a bug
+# .bss relocations are not done properly with the nv-kernel.ld linker script.
+# ld.bfd doesn't seem to have this issue, but we don't use ld.bfd to compile our kernel.
+force_symlink_bfd_ld() {
+	ln -sf "/usr/bin/${CROSS_COMPILE}ld.bfd" "${S}/ld"
+}
+
+src_compile() {
+	local -r kernel_headers_tarball="${SYSROOT}/opt/google/src/kernel-headers.tgz"
+	tar xf "${kernel_headers_tarball}" -C "${S}"
+	local -r kernel_headers_path="${S}/usr/src/linux-headers-${KV_FULL}"
+	create_cc_wrapper
+	force_symlink_bfd_ld
+	local -r logfile="${S}/nvidia-installer.log"
+	sudo PATH="${S}:${PATH}" \
+		CC="${CC}" \
+		sh ${RUNFILE} \
+		--kernel-source-path="${kernel_headers_path}" \
+		--add-this-kernel \
+		--no-install-compat32-libs --silent --accept-license  \
+		--log-file-name="${logfile}" || die "unable to produce precompiled drivers runfile"
+}
+
+src_install() {
+	# extracting precompiled drivers
+	sh "${PRECOMPILED}.run" --extract-only
+	precompiled_archive="$(find ${PRECOMPILED}/kernel/precompiled -name nvidia-precompiled*)"
+	echo "precompiling......................."
+	sudo "./${PRECOMPILED}/mkprecompiled" \
+		"--unpack" \
+		"$precompiled_archive" \
+		"-o" \
+		"${PRECOMPILED}/kernel/precompiled"
+
+	# link
+	linkerscript="${S}/usr/src/linux-headers-${KV_FULL}/scripts/module.lds"
+	# link nvidia.ko
+	$(tc-getLD) -T ${linkerscript} \
+		-r -o "${PRECOMPILED}/kernel/precompiled/nvidia.ko" \
+		"${PRECOMPILED}/kernel/precompiled/nv-linux.o" \
+		"${PRECOMPILED}/kernel/nvidia/nv-kernel.o_binary"
+
+	# link nvidia-modeset.ko
+	$(tc-getLD) -T ${linkerscript} \
+		-r -o "${PRECOMPILED}/kernel/precompiled/nvidia-modeset.ko" \
+		"${PRECOMPILED}/kernel/precompiled/nv-modeset-linux.o" \
+		"${PRECOMPILED}/kernel/nvidia-modeset/nv-modeset-kernel.o_binary"
+
+	if use module_sign ; then
+		TMP_DIR=$(mktemp -d)
+		SIGNATURES_DIR="${TMP_DIR}/signatures"
+		mkdir -p "${SIGNATURES_DIR}"
+		MODULE_OBJECTS=(nvidia.ko nvidia-drm.ko nvidia-modeset.ko nvidia-peermem.ko nvidia-uvm.ko)
+		pushd "${PRECOMPILED}/kernel/precompiled/" || die
+		for MODULE_OBJECT in "${MODULE_OBJECTS[@]}"; do
+			# Sign the nvidia modules.
+			cos-linux-mod_sign_detached "${MODULE_OBJECT}" "${KBUILD_OUTPUT}/certs" || die "signing failed"
+			# copy the object file with extended attributes.
+			cp "${MODULE_OBJECT}.p7s" "${SIGNATURES_DIR}/${MODULE_OBJECT}.sig"
+		done
+		popd || die
+
+		# Nvidia drivers are stored as build artifacts and not installed in image.
+		local source_dir=opt/google/drivers
+		dodir ${source_dir}
+		# tar the files with extended attributes.
+		tar -czf "${D}/${source_dir}/nvidia-drivers-${PV}-signature.tar.gz" -C "${SIGNATURES_DIR}"/ .  || die
+		rm -rf "${TMP_DIR}"
+	fi
+
+	# precompiled driver stored as build artifacts
+	# exported out of the image
+	local source_dir=opt/google/drivers
+	dodir ${source_dir}
+	mv "${S}/${INSTALLER_NAME}-custom.run" "${D}/${source_dir}"
+}