# Template file for 'nvidia'

_desc="NVIDIA drivers for linux"

pkgname=nvidia
version=510.60.02
revision=2
maintainer="Andrew Benson <abenson+void@gmail.com>"
license="custom:NVIDIA Proprietary"
homepage="https://www.nvidia.com/en-us/drivers/unix/"

archs="x86_64"
nopie=yes
repository="nonfree"
create_wrksrc=yes
short_desc="${_desc} - Libraries and Utilities"
hostmakedepends="tar"
conflicts="xserver-abi-video>25_1"

_pkg="NVIDIA-Linux-x86_64-${version}"
distfiles="http://uk.download.nvidia.com/XFree86/Linux-x86_64/${version}/${_pkg}.run"
checksum=a800dfc0549078fd8c6e8e6780efb8eee87872e6055c7f5f386a4768ce07e003
# subpackages need to be processed in this specific order
subpackages="nvidia-gtklibs nvidia-dkms nvidia-opencl nvidia-libs nvidia-libs-32bit"
depends="nvidia-libs-${version}_${revision}
 nvidia-gtklibs-${version}_${revision}
 nvidia-dkms-${version}_${revision}"

_install_libs() {
	local libdir=$1

	# GLX client libs
	# Required for GLVND option
	vinstall libGLX_nvidia.so.${version} 755 ${libdir}
	ln -sf libGLX_nvidia.so.${version} ${DESTDIR}/${libdir}/libGLX_nvidia.so.0
	ln -sf libGLX_nvidia.so.${version} ${DESTDIR}/${libdir}/libGLX_indirect.so.0

	# OpenGL core library
	vinstall libnvidia-glcore.so.${version} 755 ${libdir}
	vinstall libnvidia-eglcore.so.${version} 755 ${libdir}
	vinstall libnvidia-glsi.so.${version} 755 ${libdir}

	# Vulkan core library
	vinstall libnvidia-glvkspirv.so.${version} 755 ${libdir}

	vinstall libEGL_nvidia.so.${version} 755 ${libdir}
	ln -sf libEGL_nvidia.so.${version} ${DESTDIR}/${libdir}/libEGL_nvidia.so.0

	vinstall libGLESv1_CM_nvidia.so.${version} 755 ${libdir}
	ln -sf libGLESv1_CM_nvidia.so.${version} \
		${DESTDIR}/${libdir}/libGLESv1_CM_nvidia.so.1

	vinstall libGLESv2_nvidia.so.${version} 755 ${libdir}
	ln -sf libGLESv2_nvidia.so.${version} ${DESTDIR}/${libdir}/libGLESv2_nvidia.so.2

	# Thread Local Storage
	vinstall libnvidia-tls.so.${version} 755 ${libdir}

	# VDPAU
	vinstall libvdpau_nvidia.so.${version} 755 ${libdir}/vdpau
	ln -sf libvdpau_nvidia.so.${version} \
		${DESTDIR}/${libdir}/vdpau/libvdpau_nvidia.so.1

	vinstall libnvidia-ml.so.${version} 755 ${libdir}
	ln -sf libnvidia-ml.so.${version} ${DESTDIR}/${libdir}/libnvidia-ml.so
	ln -sf libnvidia-ml.so.${version} ${DESTDIR}/${libdir}/libnvidia-ml.so.1

	vinstall libnvidia-encode.so.${version} 755 ${libdir}
	ln -sf libnvidia-encode.so.${version} ${DESTDIR}/${libdir}/libnvidia-encode.so
	ln -sf libnvidia-encode.so.${version} ${DESTDIR}/${libdir}/libnvidia-encode.so.1

	vinstall libnvidia-fbc.so.${version} 755 ${libdir}
	ln -sf libnvidia-fbc.so.${version} ${DESTDIR}/${libdir}/libnvidia-fbc.so
	ln -sf libnvidia-fbc.so.${version} ${DESTDIR}/${libdir}/libnvidia-fbc.so.1

	# CUDA
	vinstall libcuda.so.${version} 755 ${libdir}
	ln -sf libcuda.so.${version} ${DESTDIR}/${libdir}/libcuda.so
	ln -sf libcuda.so.${version} ${DESTDIR}/${libdir}/libcuda.so.1

	vinstall libnvcuvid.so.${version} 755 ${libdir}
	ln -sf libnvcuvid.so.${version} ${DESTDIR}/${libdir}/libnvcuvid.so
	ln -sf libnvcuvid.so.${version} ${DESTDIR}/${libdir}/libnvcuvid.so.1

	vinstall libnvidia-opticalflow.so.${version} 755 ${libdir}
	ln -sf libnvidia-opticalflow.so.${version} \
		${DESTDIR}/${libdir}/libnvidia-opticalflow.so
	ln -sf libnvidia-opticalflow.so.${version} \
		${DESTDIR}/${libdir}/libnvidia-opticalflow.so.1

	vinstall libnvidia-ptxjitcompiler.so.${version} 755 ${libdir}
	ln -sf libnvidia-ptxjitcompiler.so.${version} \
		${DESTDIR}/${libdir}/libnvidia-ptxjitcompiler.so.1
	ln -sf libnvidia-ptxjitcompiler.so.1 \
		${DESTDIR}/${libdir}/libnvidia-ptxjitcompiler.so

	vinstall libnvidia-allocator.so.${version} 755 ${libdir}
	ln -sf libnvidia-allocator.so.${version} \
		${DESTDIR}/${libdir}/libnvidia-allocator.so.1
	ln -sf libnvidia-allocator.so.1 \
		${DESTDIR}/${libdir}/libnvidia-allocator.so
}

do_extract() {
	install -m755 ${XBPS_SRCDISTDIR}/${pkgname}-${version}/${_pkg}.run ${wrksrc}
	cd ${wrksrc}
	./${_pkg}.run --extract-only
	rm -f ${_pkg}.run
}

do_install() {
	cd ${_pkg}

	# X driver
	vinstall nvidia_drv.so 755 usr/lib/xorg/modules/drivers

	# firmware
	vinstall firmware/gsp.bin 644 usr/lib/firmware/nvidia/${version}

	# GLX extension module for X
	vinstall libglxserver_nvidia.so.${version} 755 usr/lib/nvidia/xorg
	ln -sf libglxserver_nvidia.so.${version} \
		${DESTDIR}/usr/lib/nvidia/xorg/libglxserver_nvidia.so
	ln -sf libglxserver_nvidia.so.${version} \
		${DESTDIR}/usr/lib/nvidia/xorg/libglxserver_nvidia.so.1

	# Wayland/GBM
	vinstall libnvidia-egl-wayland.so.1.1.9 755 usr/lib
	ln -sf libnvidia-egl-wayland.so.1.1.9 \
		${DESTDIR}/usr/lib/libnvidia-egl-wayland.so.1
	vinstall libnvidia-egl-gbm.so.1.1.0 755 usr/lib
	ln -sf libnvidia-egl-gbm.so.1.1.0 \
		${DESTDIR}/usr/lib/libnvidia-egl-gbm.so.1

	vmkdir usr/share/glvnd/egl_vendor.d
	vinstall 15_nvidia_gbm.json 755 usr/share/glvnd/egl_vendor.d

	vmkdir usr/lib/gbm
	ln -sf /usr/lib/libnvidia-allocator.so.${version} \
		${DESTDIR}/usr/lib/gbm/nvidia-drm_gbm.so

	vinstall 10_nvidia.json 755 usr/share/glvnd/egl_vendor.d
	vinstall 10_nvidia_wayland.json \
		755 usr/share/egl/egl_external_platform.d

	vinstall libnvidia-cfg.so.${version} 755 usr/lib
	ln -sf libnvidia-cfg.so.${version} ${DESTDIR}/usr/lib/libnvidia-cfg.so
	ln -sf libnvidia-cfg.so.${version} ${DESTDIR}/usr/lib/libnvidia-cfg.so.1

	vbin nvidia-cuda-mps-control
	vbin nvidia-cuda-mps-server
	gzip -d nvidia-cuda-mps-control.1.gz
	vman nvidia-cuda-mps-control.1

	# nvidia-xconfig
	vbin nvidia-xconfig
	gzip -d nvidia-xconfig.1.gz
	vman nvidia-xconfig.1

	# nvidia-settings
	vbin nvidia-settings
	gzip -d nvidia-settings.1.gz
	vman nvidia-settings.1
	vinstall nvidia-settings.desktop 644 usr/share/applications
	vinstall nvidia-settings.png 644 usr/share/pixmaps
	sed -e 's:__UTILS_PATH__:/usr/bin:' \
		-e 's:__PIXMAP_PATH__:/usr/share/pixmaps:' \
		-i ${DESTDIR}/usr/share/applications/nvidia-settings.desktop

	vinstall libnvidia-gtk3.so.${version} 755 usr/lib
	vinstall libnvidia-gtk2.so.${version} 755 usr/lib

	# application profiles (needed by nvidia-settings)
	vinstall nvidia-application-profiles-${version}-key-documentation \
		644 usr/share/nvidia
	vinstall nvidia-application-profiles-${version}-rc 644 usr/share/nvidia

	# nvidia-bug-report
	vbin nvidia-bug-report.sh nvidia-bug-report
	vbin nvidia-debugdump

	# nvidia-smi
	vbin nvidia-smi
	gzip -d nvidia-smi.1.gz
	vman nvidia-smi.1

	# nvidia-modprobe
	vinstall nvidia-modprobe 4755 usr/bin
	gzip -d nvidia-modprobe.1.gz
	vman nvidia-modprobe.1

	# nvidia-persistenced
	vbin nvidia-persistenced
	gzip -d nvidia-persistenced.1.gz
	vman nvidia-persistenced.1

	# nvidia-powerd
	vbin nvidia-powerd
	vsv nvidia-powerd

	# opencl pkg
	vinstall nvidia.icd 644 etc/OpenCL/vendors
	vinstall nvidia_layers.json 644 usr/share/vulkan/implicit_layer.d

	vinstall libnvidia-compiler.so.${version} 755 usr/lib

	vinstall libnvidia-opencl.so.${version} 755 usr/lib
	ln -sf libnvidia-opencl.so.${version} \
		${DESTDIR}/usr/lib/libnvidia-opencl.so
	ln -sf libnvidia-opencl.so.${version} \
		${DESTDIR}/usr/lib/libnvidia-opencl.so.1

	# vulkan icd
	vinstall nvidia_icd.json 644 usr/share/vulkan/icd.d

	# vulkan real-time ray tracing extensions
	vinstall libnvidia-rtcore.so.${version} 755 usr/lib

	vinstall libnvidia-vulkan-producer.so.${version} 755 usr/lib
	ln -sf libnvidia-vulkan-producer.so.${version} \
		${DESTDIR}/usr/lib/libnvidia-vulkan-producer.so
	ln -sf libnvidia-vulkan-producer.so.${version} \
		${DESTDIR}/usr/lib/libnvidia-vulkan-producer.so.1

	# ngx
	vbin nvidia-ngx-updater
	vinstall libnvidia-ngx.so.${version} 755 usr/lib
	ln -sf libnvidia-ngx.so.${version} \
		${DESTDIR}/usr/lib/libnvidia-ngx.so.1
	ln -sf libnvidia-ngx.so.1 \
		${DESTDIR}/usr/lib/libnvidia-ngx.so

	vinstall _nvngx.dll 755 usr/lib/nvidia/wine
	vinstall nvngx.dll 755 usr/lib/nvidia/wine

	# optix ray racing engine
	vinstall libnvoptix.so.${version} 755 usr/lib
	ln -sf libnvoptix.so.${version} ${DESTDIR}/usr/lib/libnvoptix.so
	ln -sf libnvoptix.so.${version} ${DESTDIR}/usr/lib/libnvoptix.so.1

	# dkms pkg
	vmkdir usr/src/nvidia-${version}
	vcopy "kernel/*" usr/src/nvidia-${version}
	vcopy ${FILESDIR}/dkms.conf usr/src/nvidia-${version}
	sed -e "s/__PKGVER/${version}/g" \
		-e 's/__MAKEJOBS/-j$(nproc)/g' \
		-i ${DESTDIR}/usr/src/nvidia-${version}/dkms.conf

	# Xorg conf for PRIME
	vmkdir /usr/share/X11/xorg.conf.d/
	vinstall ${FILESDIR}/nvidia-drm-outputclass.conf 644 \
		/usr/share/X11/xorg.conf.d 10-nvidia-drm-outputclass.conf

	vbin ${FILESDIR}/prime-run

	# Blacklist nouveau
	vmkdir usr/lib/modprobe.d
	echo "blacklist nouveau" > ${DESTDIR}/usr/lib/modprobe.d/nvidia.conf
	chmod 644 ${DESTDIR}/usr/lib/modprobe.d/nvidia.conf

	# License and documentation
	vlicense LICENSE
	vdoc README.txt README
	vdoc NVIDIA_Changelog

	# nvvm compiler
	vinstall libnvidia-nvvm.so.4.0.0 755 usr/lib
	ln -sf libnvidia-nvvm.so.4.0.0 \
		${DESTDIR}/usr/lib/libnvidia-nvvm.so.4
	ln -sf libnvidia-nvvm.so.4 \
		${DESTDIR}/usr/lib/libnvidia-nvvm.so

	# Libraries (64bit)
	_install_libs usr/lib
	cd 32
	# Libraries (32bit)
	_install_libs usr/lib32
}

nvidia-gtklibs_package() {
	short_desc="${_desc} - GTK+ libraries"
	pkg_install() {
		vmove "usr/lib/lib*gtk*.so*"
	}
}

nvidia-libs-32bit_package() {
	short_desc="${_desc} - common libraries (32bit)"
	# manually set 32bit depends for libglvnd
	depends="glibc-32bit>=0 libX11-32bit>=0 libXext-32bit>=0 libglvnd-32bit>=0"
	repository="multilib/nonfree"
	pkg_install() {
		vmove usr/lib32
	}
}

nvidia-libs_package() {
	short_desc="${_desc} - common libraries"
	depends="libglvnd"
	nostrip_files="gsp.bin"
	pkg_install() {
		vmove usr/lib
	}
}

nvidia-dkms_package() {
	short_desc="${_desc} - DKMS kernel module"
	depends="dkms xbps-triggers>=0.121_1"
	dkms_modules="nvidia ${version}"
	# dkms must be before initramfs-regenerate to build modules before images
	triggers="dkms initramfs-regenerate"

	pkg_install() {
		vmove usr/src
		vmove usr/lib/modprobe.d
	}
}

nvidia-opencl_package() {
	short_desc="${_desc} - OpenCL implementation"
	depends="ocl-icd"
	pkg_install() {
		vmove "usr/lib/libnvidia-compiler*"
		vmove "usr/lib/libnvidia-opencl*"
		vmove etc/OpenCL
	}
}
