# Copyright 1999-2026 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

EAPI=8

MODULES_OPTIONAL_IUSE=+modules
inherit eapi9-ver flag-o-matic linux-mod-r1
inherit toolchain-funcs unpacker
inherit git-r3

MODULES_KERNEL_MAX=6.19
NV_URI="https://download.nvidia.com/XFree86/"

DESCRIPTION="NVIDIA Accelerated Graphics Driver"
HOMEPAGE="https://github.com/NVIDIA"
EGIT_REPO_URI="${HOMEPAGE}/open-gpu-kernel-modules.git"
S=${WORKDIR}
EGIT_CHECKOUT_DIR="${WORKDIR}/kernel-module-source"

LICENSE="
	NVIDIA-2025 Apache-2.0 Boost-1.0 BSD BSD-2 GPL-2 MIT ZLIB
	curl openssl public-domain
"
SLOT="0/${PV%%.*}"
IUSE="
	X abi_x86_32 abi_x86_64 persistenced
	static-libs +tools wayland
"

COMMON_DEPEND="
	x11-drivers/nvidia-modprobe
	X? ( x11-drivers/nvidia-xconfig )
	persistenced? ( x11-drivers/nvidia-persistenced )
	tools? ( x11-drivers/nvidia-settings[static-libs?] )
"
# egl-wayland2: nvidia currently ships both versions so, to ensure
# everything works properly, depend on both at same time for now
# (may use one or the other depending on setup)
RDEPEND="
	${COMMON_DEPEND}
	dev-libs/openssl:0/3
	sys-libs/glibc
	wayland? (
		>=gui-libs/egl-gbm-1.1.1-r2[abi_x86_32(-)?]
		>=gui-libs/egl-wayland-1.1.13.1[abi_x86_32(-)?]
		gui-libs/egl-wayland2[abi_x86_32(-)?]
	)
"
DEPEND="
	${COMMON_DEPEND}
	static-libs? (
		x11-base/xorg-proto
		x11-libs/libX11
		x11-libs/libXext
	)
"
BDEPEND="
	app-alternatives/awk
	sys-devel/m4
	virtual/pkgconfig
"

# there is some non-prebuilt exceptions but rather not maintain a list
QA_PREBUILT="lib/firmware/* usr/bin/* usr/lib*"

pkg_setup() {
	use modules && [[ ${MERGE_TYPE} != binary ]] || return

	# do early before linux-mod-r1 so can use chkconfig to setup CONFIG_CHECK
	get_version
	require_configured_kernel

	local CONFIG_CHECK="
		PROC_FS
		~DRM_KMS_HELPER
		~DRM_FBDEV_EMULATION
		~SYSVIPC
		~!LOCKDEP
		~!PREEMPT_RT
		~!RANDSTRUCT_FULL
		~!RANDSTRUCT_PERFORMANCE
		~!SLUB_DEBUG_ON
		!DEBUG_MUTEXES
	"

	kernel_is -ge 6 11 && linux_chkconfig_present DRM_FBDEV_EMULATION &&
		CONFIG_CHECK+=" DRM_TTM_HELPER"

	use amd64 && kernel_is -ge 5 8 && CONFIG_CHECK+=" X86_PAT" #817764

	CONFIG_CHECK+=" MMU_NOTIFIER" #843827

	local drm_helper_msg="Cannot be directly selected in the kernel's config menus, and may need
	selection of a DRM device even if unused, e.g. CONFIG_DRM_QXL=m or
	DRM_AMDGPU=m (among others, consult the kernel config's help), can
	also use DRM_NOUVEAU=m as long as built as module *not* built-in."
	local ERROR_DRM_KMS_HELPER="CONFIG_DRM_KMS_HELPER: is not set but is needed for nvidia-drm.modeset=1
	support (see ${EPREFIX}/etc/modprobe.d/nvidia.conf) which is needed for wayland
	and for config-less Xorg auto-detection.
	${drm_helper_msg}"
	local ERROR_DRM_TTM_HELPER="CONFIG_DRM_TTM_HELPER: is not set but is needed to compile when using
	kernel version 6.11.x or newer while DRM_FBDEV_EMULATION is set.
	${drm_helper_msg}"
	local ERROR_DRM_FBDEV_EMULATION="CONFIG_DRM_FBDEV_EMULATION: is not set but is needed for
	nvidia-drm.fbdev=1 support (see ${EPREFIX}/etc/modprobe.d/nvidia.conf), may
	result in a blank console/tty."
	local ERROR_MMU_NOTIFIER="CONFIG_MMU_NOTIFIER: is not set but needed to build with USE=kernel-open.
	Cannot be directly selected in the kernel's menuconfig, and may need
	selection of another option that requires it such as CONFIG_AMD_IOMMU=y,
	or DRM_I915=m (among others, consult the kernel config's help)."
	local ERROR_PREEMPT_RT="CONFIG_PREEMPT_RT: is set but is unsupported by NVIDIA upstream and
	will fail to build unless the env var IGNORE_PREEMPT_RT_PRESENCE=1 is
	set. Please do not report issues if run into e.g. kernel panics while
	ignoring this."
	local randstruct_msg="is set but NVIDIA may be unstable with
	it such as causing a kernel panic on shutdown, it is recommended to
	disable with CONFIG_RANDSTRUCT_NONE=y (https://bugs.gentoo.org/969413
	-- please report if this appears fixed on NVIDIA's side so can remove
	this warning)."
	local ERROR_RANDSTRUCT_FULL="CONFIG_RANDSTRUCT_FULL: ${randstruct_msg}"
	local ERROR_RANDSTRUCT_PERFORMANCE="CONFIG_RANDSTRUCT_PERFORMANCE: ${randstruct_msg}"

	linux-mod-r1_pkg_setup
}

src_prepare() {
	default

	# use alternative vulkan icd option if USE=-X (bug #909181)
	use X || sed -i 's/"libGLX/"libEGL/' nvidia_{layers,icd}.json || die
}

src_compile() {
	tc-export AR CC CXX LD OBJCOPY OBJDUMP PKG_CONFIG

	# extra flags for the libXNVCtrl.a static library
	local xnvflags=-fPIC #840389
	tc-is-lto && xnvflags+=" $(test-flags-CC -ffat-lto-objects)"

	# Same as uname -m.
	local target_arch
	case ${ARCH} in
		amd64) target_arch=x86_64 ;;
		arm64) target_arch=aarch64 ;;
		*) die "Unrecognised architecture: ${ARCH}" ;;
	esac

	NV_ARGS=(
		PREFIX="${EPREFIX}"/usr
		HOST_CC="$(tc-getBUILD_CC)"
		HOST_LD="$(tc-getBUILD_LD)"
		BUILD_GTK2LIB=
		NV_USE_BUNDLED_LIBJANSSON=0
		NV_VERBOSE=1 DO_STRIP= MANPAGE_GZIP= OUTPUTDIR=out
		TARGET_ARCH="${target_arch}"
		WAYLAND_AVAILABLE=$(usex wayland 1 0)
		XNVCTRL_CFLAGS="${xnvflags}"
	)

	if use modules; then
		local o_cflags=${CFLAGS} o_cxxflags=${CXXFLAGS} o_ldflags=${LDFLAGS}

		local modlistargs=video:kernel
		modlistargs+=-module-source:kernel-module-source/kernel-open

		# environment flags are normally unused for modules, but nvidia
		# uses it for building the "blob" and it is a bit fragile
		filter-flags -fno-plt #912949
		filter-lto
		CC=${KERNEL_CC} CXX=${KERNEL_CXX} strip-unsupported-flags

		LDFLAGS=$(raw-ldflags)

		local modlist=( nvidia{,-drm,-modeset,-peermem,-uvm}=${modlistargs} )
		local modargs=(
			IGNORE_CC_MISMATCH=yes NV_VERBOSE=1
			SYSOUT="${KV_OUT_DIR}" SYSSRC="${KV_DIR}"
			TARGET_ARCH="${target_arch}"

			# kernel takes "x86" and "x86_64" as meaning the same, but nvidia
			# makes the distinction (since 550.135) and is not happy with "x86"
			# TODO?: it should be ok/better for tc-arch-kernel to do x86_64
			$(usev amd64 ARCH=x86_64)
		)

		# temporary workaround for bug #914468
		addpredict "${KV_OUT_DIR}"

		linux-mod-r1_src_compile
		CFLAGS=${o_cflags} CXXFLAGS=${o_cxxflags} LDFLAGS=${o_ldflags}
	fi
}

src_install() {
	local libdir=$(get_libdir) libdir32=$(ABI=x86 get_libdir)

	NV_ARGS+=( DESTDIR="${D}" LIBDIR="${ED}"/usr/${libdir} )

	local -A paths=(
		[APPLICATION_PROFILE]=/usr/share/nvidia
		[CUDA_ICD]=/etc/OpenCL/vendors
		[EGL_EXTERNAL_PLATFORM_JSON]=/usr/share/egl/egl_external_platform.d
		[FIRMWARE]=/lib/firmware/nvidia/${PV}
		[GBM_BACKEND_LIB_SYMLINK]=/usr/${libdir}/gbm
		[GLVND_EGL_ICD_JSON]=/usr/share/glvnd/egl_vendor.d
		[OPENGL_DATA]=/usr/share/nvidia
		[VULKANSC_ICD_JSON]=/usr/share/vulkansc
		[VULKAN_ICD_JSON]=/usr/share/vulkan
		[WINE_LIB]=/usr/${libdir}/nvidia/wine
		[XORG_OUTPUTCLASS_CONFIG]=/usr/share/X11/xorg.conf.d

		[GLX_MODULE_SHARED_LIB]=/usr/${libdir}/xorg/modules/extensions
		[GLX_MODULE_SYMLINK]=/usr/${libdir}/xorg/modules
		[XMODULE_SHARED_LIB]=/usr/${libdir}/xorg/modules
	)

	local skip_files=(
		$(usev !X "libGLX_nvidia libglxserver_nvidia")
		libGLX_indirect # non-glvnd unused fallback
		libnvidia-{gtk,wayland-client} nvidia-{settings,xconfig} # from source
		libnvidia-egl-gbm 15_nvidia_gbm # gui-libs/egl-gbm
		libnvidia-egl-wayland 10_nvidia_wayland # gui-libs/egl-wayland
		libnvidia-egl-wayland2 99_nvidia_wayland2 # gui-libs/egl-wayland2
		libnvidia-egl-xcb 20_nvidia_xcb.json # gui-libs/egl-x11
		libnvidia-egl-xlib 20_nvidia_xlib.json # gui-libs/egl-x11
		libnvidia-pkcs11.so # using the openssl3 version instead
	)
	local skip_modules=(
		$(usev !X "nvfbc vdpau xdriver")
		$(usev !modules gsp)
		installer nvpd # handled separately / built from source
	)
	local skip_types=(
		GLVND_LIB GLVND_SYMLINK EGL_CLIENT.\* GLX_CLIENT.\* # media-libs/libglvnd
		OPENCL_WRAPPER.\* # virtual/opencl
		DOCUMENTATION DOT_DESKTOP .\*_SRC DKMS_CONF SYSTEMD_UNIT # handled separately / unused
	)

	# local DOCS=( README.md SECURITY.md CONTRIBUTING.md )
	# einstalldocs

	local DISABLE_AUTOFORMATTING=yes
	local DOC_CONTENTS="\
Trusted users should be in the 'video' group to use NVIDIA devices.
You can add yourself by using: gpasswd -a my-user video\
$(usev modules "

Like all out-of-tree kernel modules, it is necessary to rebuild
${PN} after upgrading or rebuilding the Linux kernel
by for example running \`emerge @module-rebuild\`. Alternatively,
if using a distribution kernel (sys-kernel/gentoo-kernel{,-bin}),
this can be automated by setting USE=dist-kernel globally.

Loaded kernel modules also must not mismatch with the installed
${PN} version (excluding -r revision), meaning should
ensure \`eselect kernel list\` points to the kernel that will be
booted before building and preferably reboot after upgrading
${PN} (the ebuild will emit a warning if mismatching).

See '${EPREFIX}/etc/modprobe.d/nvidia.conf' for modules options.")\
$(use amd64 && usev !abi_x86_32 "

Note that without USE=abi_x86_32 on ${PN}, 32bit applications
(typically using wine / steam) will not be able to use GPU acceleration.")

For additional information or for troubleshooting issues, please see
https://wiki.gentoo.org/wiki/NVIDIA/nvidia-drivers and NVIDIA's own
documentation that is installed alongside this README."

	if use modules; then
		linux-mod-r1_src_install

		# used for gpu verification with binpkgs (not kept, see pkg_preinst)
		insinto /usr/share/nvidia
	fi

	insinto /usr/share/nvidia/files.d

	# don't attempt to strip firmware files (silences errors)
	dostrip -x ${paths[FIRMWARE]}

	# sandbox issues with /dev/nvidiactl and others (bug #904292,#921578)
	# are widespread and sometime affect revdeps of packages built with
	# USE=opencl/cuda making it hard to manage in ebuilds (minimal set,
	# ebuilds should handle manually if need others or addwrite)
	insinto /etc/sandbox.d
	newins - 20nvidia <<<'SANDBOX_PREDICT="/dev/nvidiactl:/dev/nvidia-caps:/dev/char"'
}

pkg_preinst() {
	use modules || return
}

pkg_postinst() {
	linux-mod-r1_pkg_postinst

	if [[ -r /proc/driver/nvidia/version &&
		$(</proc/driver/nvidia/version) != *"  ${PV}  "* ]]; then
		ewarn "\nCurrently loaded NVIDIA modules do not match the newly installed"
		ewarn "libraries and may prevent launching GPU-accelerated applications."
		if use modules; then
			ewarn "Easiest way to fix this is normally to reboot. If still run into issues"
			ewarn "(e.g. API mismatch messages in the \`dmesg\` output), please verify"
			ewarn "that the running kernel is ${KV_FULL} and that (if used) the"
			ewarn "initramfs does not include NVIDIA modules (or at least, not old ones)."
		fi
	fi

	if [[ $(</proc/cmdline) == *slub_debug=[!-]* ]]; then
		ewarn "\nDetected that the current kernel command line is using 'slub_debug=',"
		ewarn "this may lead to system instability/freezes with this version of"
		ewarn "${PN}. Bug: https://bugs.gentoo.org/796329"
	fi

	if [[ -v NV_LEGACY_MASK ]]; then
		ewarn "\n***WARNING***"
		ewarn "\nYou are installing a version of ${PN} known not to work"
		ewarn "with a GPU of the current system. If unwanted, add the mask:"
		if [[ -d ${EROOT}/etc/portage/package.mask ]]; then
			ewarn "  echo '${NV_LEGACY_MASK}' > ${EROOT}/etc/portage/package.mask/${PN}"
		else
			ewarn "  echo '${NV_LEGACY_MASK}' >> ${EROOT}/etc/portage/package.mask"
		fi
		ewarn "...then downgrade to a legacy[1] branch if possible (not all old versions"
		ewarn "are available or fully functional, may need to consider nouveau[2])."
		ewarn "[1] https://www.nvidia.com/object/IO_32667.html"
		ewarn "[2] https://wiki.gentoo.org/wiki/Nouveau"
	fi

	if ver_replacing -lt 590; then
		elog "\n>=${PN}-590 has changes that may or may not need attention:"
		elog "1. support for Pascal, Maxwell, and Volta cards has been dropped"
		elog "  (if affected, there should be a another message about this above)"
		elog "2. USE=kernel-open is now enabled by default"
		elog "  (generally safe and recommended, but some setups may hit regressions)"
		elog "3. nvidia-drm.modeset=1 is now default regardless of USE=wayland"
		elog "4. nvidia-drm.fbdev=1 is now also tentatively default to match upstream"
		elog "(3+4 were also later changed in >=580.126.09-r1, may already be in-use)"
		elog "See ${EROOT}/etc/modprobe.d/nvidia.conf to modify settings if needed,"
		elog "fbdev=1 *could* cause issues for the console display with some setups."
	fi
}