x11-drivers/nvidia-drivers: Stable
[gentoo.git] / x11-drivers / nvidia-drivers / nvidia-drivers-340.108.ebuild
1 # Copyright 1999-2020 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
3
4 EAPI=7
5 inherit desktop flag-o-matic linux-info linux-mod multilib-minimal \
6         nvidia-driver portability toolchain-funcs unpacker user udev
7
8 NV_URI="https://us.download.nvidia.com/XFree86/"
9 X86_NV_PACKAGE="NVIDIA-Linux-x86-${PV}"
10 AMD64_NV_PACKAGE="NVIDIA-Linux-x86_64-${PV}"
11 X86_FBSD_NV_PACKAGE="NVIDIA-FreeBSD-x86-${PV}"
12 AMD64_FBSD_NV_PACKAGE="NVIDIA-FreeBSD-x86_64-${PV}"
13
14 DESCRIPTION="NVIDIA Accelerated Graphics Driver"
15 HOMEPAGE="https://www.nvidia.com/"
16 SRC_URI="
17         amd64-fbsd? ( ${NV_URI}FreeBSD-x86_64/${PV}/${AMD64_FBSD_NV_PACKAGE}.tar.gz )
18         amd64? ( ${NV_URI}Linux-x86_64/${PV}/${AMD64_NV_PACKAGE}.run )
19         x86-fbsd? ( ${NV_URI}FreeBSD-x86/${PV}/${X86_FBSD_NV_PACKAGE}.tar.gz )
20         x86? ( ${NV_URI}Linux-x86/${PV}/${X86_NV_PACKAGE}.run )
21         tools? (
22                 https://download.nvidia.com/XFree86/nvidia-settings/nvidia-settings-${PV}.tar.bz2
23         )
24 "
25
26 EMULTILIB_PKG="true"
27 IUSE="acpi multilib kernel_FreeBSD kernel_linux static-libs +tools +X"
28 KEYWORDS="-* amd64 x86"
29 LICENSE="GPL-2 NVIDIA-r2"
30 SLOT="0/${PV%.*}"
31
32 COMMON="
33         app-eselect/eselect-opencl
34         kernel_linux? ( >=sys-libs/glibc-2.6.1 )
35         tools? (
36                 >=x11-libs/gtk+-2.4:2
37                 dev-libs/atk
38                 dev-libs/glib:2
39                 dev-libs/jansson
40                 x11-libs/gdk-pixbuf[X]
41                 x11-libs/libX11
42                 x11-libs/libXext
43                 x11-libs/libXv
44                 x11-libs/pango[X]
45         )
46         X? (
47                 >=app-eselect/eselect-opengl-1.0.9
48         )
49 "
50 DEPEND="
51         ${COMMON}
52         app-arch/xz-utils
53         kernel_linux? ( virtual/linux-sources )
54 "
55 RDEPEND="
56         ${COMMON}
57         acpi? ( sys-power/acpid )
58         tools? ( !media-video/nvidia-settings )
59         X? (
60                 <x11-base/xorg-server-1.20.99:=
61                 >=x11-libs/libvdpau-0.3-r1
62                 sys-libs/zlib[${MULTILIB_USEDEP}]
63                 multilib? (
64                         >=x11-libs/libX11-1.6.2[${MULTILIB_USEDEP}]
65                         >=x11-libs/libXext-1.3.2[${MULTILIB_USEDEP}]
66                 )
67         )
68 "
69 REQUIRED_USE="tools? ( X )"
70 QA_PREBUILT="opt/* usr/lib*"
71 S=${WORKDIR}/
72 NV_KV_MAX_PLUS="5.5"
73 CONFIG_CHECK="!DEBUG_MUTEXES ~!LOCKDEP ~MTRR ~SYSVIPC ~ZONE_DMA"
74
75 pkg_pretend() {
76         use x86 && CONFIG_CHECK+=" ~HIGHMEM"
77         nvidia-driver_check
78 }
79
80 pkg_setup() {
81         use x86 && CONFIG_CHECK+=" ~HIGHMEM"
82         nvidia-driver_check
83
84         # try to turn off distcc and ccache for people that have a problem with it
85         export DISTCC_DISABLE=1
86         export CCACHE_DISABLE=1
87
88         if use kernel_linux; then
89                 MODULE_NAMES="nvidia(video:${S}/kernel)"
90
91                 # This needs to run after MODULE_NAMES (so that the eclass checks
92                 # whether the kernel supports loadable modules) but before BUILD_PARAMS
93                 # is set (so that KV_DIR is populated).
94                 linux-mod_pkg_setup
95
96                 BUILD_PARAMS="IGNORE_CC_MISMATCH=yes V=1 SYSSRC=${KV_DIR} \
97                 SYSOUT=${KV_OUT_DIR} CC=$(tc-getBUILD_CC)"
98
99                 # linux-mod_src_compile calls set_arch_to_kernel, which
100                 # sets the ARCH to x86 but NVIDIA's wrapping Makefile
101                 # expects x86_64 or i386 and then converts it to x86
102                 # later on in the build process
103                 BUILD_FIXES="ARCH=$(uname -m | sed -e 's/i.86/i386/')"
104         fi
105
106         # set variables to where files are in the package structure
107         if use kernel_FreeBSD; then
108                 use x86-fbsd   && S="${WORKDIR}/${X86_FBSD_NV_PACKAGE}"
109                 use amd64-fbsd && S="${WORKDIR}/${AMD64_FBSD_NV_PACKAGE}"
110                 NV_DOC="${S}/doc"
111                 NV_OBJ="${S}/obj"
112                 NV_SRC="${S}/src"
113                 NV_MAN="${S}/x11/man"
114                 NV_X11="${S}/obj"
115                 NV_SOVER=1
116         elif use kernel_linux; then
117                 NV_DOC="${S}"
118                 NV_OBJ="${S}"
119                 NV_SRC="${S}/kernel"
120                 NV_MAN="${S}"
121                 NV_X11="${S}"
122                 NV_SOVER=${PV}
123         else
124                 die "Could not determine proper NVIDIA package"
125         fi
126 }
127
128 src_prepare() {
129         # Please add a brief description for every added patch
130
131         if use kernel_linux; then
132                 if kernel_is lt 2 6 9 ; then
133                         eerror "You must build this against 2.6.9 or higher kernels."
134                 fi
135
136                 # If greater than 2.6.5 use M= instead of SUBDIR=
137 #               convert_to_m "${NV_SRC}"/Makefile.kbuild
138         fi
139
140         local man_file
141         for man_file in "${NV_MAN}"/*1.gz; do
142                 gunzip $man_file || die
143         done
144
145         # Allow user patches so they can support RC kernels and whatever else
146         eapply_user
147 }
148
149 src_compile() {
150         # This is already the default on Linux, as there's no toplevel Makefile, but
151         # on FreeBSD there's one and triggers the kernel module build, as we install
152         # it by itself, pass this.
153
154         cd "${NV_SRC}"
155         if use kernel_FreeBSD; then
156                 MAKE="$(get_bmake)" CFLAGS="-Wno-sign-compare" emake CC="$(tc-getCC)" \
157                         LD="$(tc-getLD)" LDFLAGS="$(raw-ldflags)" || die
158         elif use kernel_linux; then
159                 BUILD_TARGETS=module linux-mod_src_compile
160         fi
161
162         if use tools; then
163                 emake -C "${S}"/nvidia-settings-${PV}/src/libXNVCtrl clean
164                 emake -C "${S}"/nvidia-settings-${PV}/src/libXNVCtrl \
165                         AR="$(tc-getAR)" \
166                         CC="$(tc-getCC)" \
167                         RANLIB="$(tc-getRANLIB)" \
168                         libXNVCtrl.a
169                 emake -C "${S}"/nvidia-settings-${PV}/src \
170                         AR="$(tc-getAR)" \
171                         CC="$(tc-getCC)" \
172                         LD="$(tc-getCC)" \
173                         LIBDIR="$(get_libdir)" \
174                         NVML_ENABLED=0 \
175                         NV_USE_BUNDLED_LIBJANSSON=0 \
176                         NV_VERBOSE=1 \
177                         RANLIB="$(tc-getRANLIB)" \
178                         STRIP_CMD=true
179         fi
180 }
181
182 # Install nvidia library:
183 # the first parameter is the library to install
184 # the second parameter is the provided soversion
185 # the third parameter is the target directory if its not /usr/lib
186 donvidia() {
187         # Full path to library minus SOVER
188         MY_LIB="$1"
189
190         # SOVER to use
191         MY_SOVER="$2"
192
193         # Where to install
194         MY_DEST="$3"
195
196         if [[ -z "${MY_DEST}" ]]; then
197                 MY_DEST="/usr/$(get_libdir)"
198                 action="dolib.so"
199         else
200                 exeinto ${MY_DEST}
201                 action="doexe"
202         fi
203
204         # Get just the library name
205         libname=$(basename $1)
206
207         # Install the library with the correct SOVER
208         ${action} ${MY_LIB}.${MY_SOVER} || \
209                 die "failed to install ${libname}"
210
211         # If SOVER wasn't 1, then we need to create a .1 symlink
212         if [[ "${MY_SOVER}" != "1" ]]; then
213                 dosym ${libname}.${MY_SOVER} \
214                         ${MY_DEST}/${libname}.1 || \
215                         die "failed to create ${libname} symlink"
216         fi
217
218         # Always create the symlink from the raw lib to the .1
219         dosym ${libname}.1 \
220                 ${MY_DEST}/${libname} || \
221                 die "failed to create ${libname} symlink"
222 }
223
224 src_install() {
225         if use kernel_linux; then
226                 linux-mod_src_install
227
228                 # Add the aliases
229                 # This file is tweaked with the appropriate video group in
230                 # pkg_preinst, see bug #491414
231                 insinto /etc/modprobe.d
232                 newins "${FILESDIR}"/nvidia-169.07 nvidia.conf
233
234                 # Ensures that our device nodes are created when not using X
235                 exeinto "$(get_udevdir)"
236                 newexe "${FILESDIR}"/nvidia-udev.sh-r1 nvidia-udev.sh
237                 udev_newrules "${FILESDIR}"/nvidia.udev-rule 99-nvidia.rules
238         elif use kernel_FreeBSD; then
239                 if use x86-fbsd; then
240                         insinto /boot/modules
241                         doins "${S}/src/nvidia.kld"
242                 fi
243
244                 exeinto /boot/modules
245                 doexe "${S}/src/nvidia.ko"
246         fi
247
248         # NVIDIA kernel <-> userspace driver config lib
249         donvidia "${NV_OBJ}"/libnvidia-cfg.so ${NV_SOVER}
250
251         # NVIDIA framebuffer capture library
252         donvidia "${NV_OBJ}"/libnvidia-fbc.so ${NV_SOVER}
253
254         # NVIDIA video encode/decode <-> CUDA
255         if use kernel_linux; then
256                 donvidia "${NV_OBJ}"/libnvcuvid.so ${NV_SOVER}
257                 donvidia "${NV_OBJ}"/libnvidia-encode.so ${NV_SOVER}
258         fi
259
260         if use X; then
261                 # Xorg DDX driver
262                 insinto /usr/$(get_libdir)/xorg/modules/drivers
263                 doins "${NV_X11}"/nvidia_drv.so
264
265                 # Xorg GLX driver
266                 donvidia "${NV_X11}"/libglx.so ${NV_SOVER} \
267                         /usr/$(get_libdir)/opengl/nvidia/extensions
268         fi
269
270         # OpenCL ICD for NVIDIA
271         if use kernel_linux; then
272                 insinto /etc/OpenCL/vendors
273                 doins "${NV_OBJ}"/nvidia.icd
274         fi
275
276         # Helper Apps
277         exeinto /opt/bin/
278
279         if use X; then
280                 doexe "${NV_OBJ}"/nvidia-xconfig
281         fi
282
283         if use kernel_linux ; then
284                 doexe "${NV_OBJ}"/nvidia-cuda-mps-control
285                 doexe "${NV_OBJ}"/nvidia-cuda-mps-server
286                 doexe "${NV_OBJ}"/nvidia-debugdump
287                 doexe "${NV_OBJ}"/nvidia-persistenced
288                 doexe "${NV_OBJ}"/nvidia-smi
289
290                 # install nvidia-modprobe setuid and symlink in /usr/bin (bug #505092)
291                 doexe "${NV_OBJ}"/nvidia-modprobe
292                 fowners root:video /opt/bin/nvidia-modprobe
293                 fperms 4710 /opt/bin/nvidia-modprobe
294                 dosym /{opt,usr}/bin/nvidia-modprobe
295
296                 doman nvidia-cuda-mps-control.1
297                 doman nvidia-modprobe.1
298                 doman nvidia-persistenced.1
299                 newinitd "${FILESDIR}/nvidia-smi.init" nvidia-smi
300                 newconfd "${FILESDIR}/nvidia-persistenced.conf" nvidia-persistenced
301                 newinitd "${FILESDIR}/nvidia-persistenced.init" nvidia-persistenced
302         fi
303
304         if use tools; then
305                 emake -C "${S}"/nvidia-settings-${PV}/src/ \
306                         DESTDIR="${D}" \
307                         LIBDIR="${D}/usr/$(get_libdir)" \
308                         PREFIX=/usr \
309                         NV_USE_BUNDLED_LIBJANSSON=0 \
310                         install
311
312                 if use static-libs; then
313                         dolib.a "${S}"/nvidia-settings-${PV}/src/libXNVCtrl/libXNVCtrl.a
314
315                         insinto /usr/include/NVCtrl
316                         doins "${S}"/nvidia-settings-${PV}/src/libXNVCtrl/*.h
317                 fi
318
319                 insinto /usr/share/nvidia/
320                 doins nvidia-application-profiles-${PV}-key-documentation
321
322                 insinto /etc/nvidia
323                 newins \
324                         nvidia-application-profiles-${PV}-rc nvidia-application-profiles-rc
325
326                 # There is no icon in the FreeBSD tarball.
327                 use kernel_FreeBSD || \
328                         doicon "${NV_OBJ}"/nvidia-settings.png
329
330                 domenu "${FILESDIR}"/nvidia-settings.desktop
331
332                 exeinto /etc/X11/xinit/xinitrc.d
333                 newexe "${FILESDIR}"/95-nvidia-settings-r1 95-nvidia-settings
334
335         fi
336
337         dobin "${NV_OBJ}"/nvidia-bug-report.sh
338
339         #doenvd "${FILESDIR}"/50nvidia-prelink-blacklist
340
341         if has_multilib_profile && use multilib ; then
342                 local OABI=${ABI}
343                 for ABI in $(get_install_abis) ; do
344                         src_install-libs
345                 done
346                 ABI=${OABI}
347                 unset OABI
348         else
349                 src_install-libs
350         fi
351
352         is_final_abi || die "failed to iterate through all ABIs"
353
354         # Documentation
355         if use kernel_FreeBSD; then
356                 dodoc "${NV_DOC}"/README
357                 use X && doman "${NV_MAN}"/nvidia-xconfig.1
358                 use tools && doman "${NV_MAN}"/nvidia-settings.1
359         else
360                 # Docs
361                 newdoc "${NV_DOC}"/README.txt README
362                 dodoc "${NV_DOC}"/NVIDIA_Changelog
363                 doman "${NV_MAN}"/nvidia-smi.1
364                 use X && doman "${NV_MAN}"/nvidia-xconfig.1
365                 use tools && doman "${NV_MAN}"/nvidia-settings.1
366                 doman "${NV_MAN}"/nvidia-cuda-mps-control.1
367         fi
368
369         readme.gentoo_create_doc
370
371         docinto html
372         dodoc -r "${NV_DOC}"/html/*
373 }
374
375 src_install-libs() {
376         local inslibdir=$(get_libdir)
377         local GL_ROOT="/usr/$(get_libdir)/opengl/nvidia/lib"
378         local CL_ROOT="/usr/$(get_libdir)/OpenCL/vendors/nvidia"
379         local nv_libdir="${NV_OBJ}"
380
381         if use kernel_linux && has_multilib_profile && \
382                         [[ ${ABI} == "x86" ]] ; then
383                 nv_libdir="${NV_OBJ}"/32
384         fi
385
386         if use X; then
387                 # The GLX libraries
388                 donvidia "${nv_libdir}"/libEGL.so ${NV_SOVER} ${GL_ROOT}
389                 donvidia "${nv_libdir}"/libGL.so ${NV_SOVER} ${GL_ROOT}
390                 donvidia "${nv_libdir}"/libGLESv1_CM.so ${NV_SOVER} ${GL_ROOT}
391                 donvidia "${nv_libdir}"/libnvidia-eglcore.so ${NV_SOVER}
392                 donvidia "${nv_libdir}"/libnvidia-glcore.so ${NV_SOVER}
393                 donvidia "${nv_libdir}"/libnvidia-glsi.so ${NV_SOVER}
394                 donvidia "${nv_libdir}"/libnvidia-ifr.so ${NV_SOVER}
395                 if use kernel_FreeBSD; then
396                         donvidia "${nv_libdir}"/libnvidia-tls.so ${NV_SOVER}
397                 else
398                         donvidia "${nv_libdir}"/tls/libnvidia-tls.so ${NV_SOVER}
399                 fi
400
401                 # VDPAU
402                 donvidia "${nv_libdir}"/libvdpau_nvidia.so ${NV_SOVER}
403
404                 # GLES v2 libraries
405                 insinto ${GL_ROOT}
406                 doexe "${nv_libdir}"/libGLESv2.so.${PV}
407                 dosym libGLESv2.so.${PV} ${GL_ROOT}/libGLESv2.so.2
408                 dosym libGLESv2.so.2 ${GL_ROOT}/libGLESv2.so
409         fi
410
411         # NVIDIA monitoring library
412         if use kernel_linux ; then
413                 donvidia "${nv_libdir}"/libnvidia-ml.so ${NV_SOVER}
414         fi
415
416         # CUDA & OpenCL
417         if use kernel_linux; then
418                 donvidia "${nv_libdir}"/libcuda.so ${NV_SOVER}
419                 donvidia "${nv_libdir}"/libnvidia-compiler.so ${NV_SOVER}
420                 donvidia "${nv_libdir}"/libOpenCL.so 1.0.0 ${CL_ROOT}
421                 donvidia "${nv_libdir}"/libnvidia-opencl.so ${NV_SOVER}
422         fi
423 }
424
425 pkg_preinst() {
426         if use kernel_linux; then
427                 linux-mod_pkg_preinst
428
429                 local videogroup="$(egetent group video | cut -d ':' -f 3)"
430                 if [ -z "${videogroup}" ]; then
431                         eerror "Failed to determine the video group gid"
432                         die "Failed to determine the video group gid"
433                 else
434                         sed -i \
435                                 -e "s:PACKAGE:${PF}:g" \
436                                 -e "s:VIDEOGID:${videogroup}:" \
437                                 "${D}"/etc/modprobe.d/nvidia.conf || die
438                 fi
439         fi
440
441         # Clean the dynamic libGL stuff's home to ensure
442         # we dont have stale libs floating around
443         if [ -d "${ROOT}"/usr/lib/opengl/nvidia ] ; then
444                 rm -rf "${ROOT}"/usr/lib/opengl/nvidia/*
445         fi
446         # Make sure we nuke the old nvidia-glx's env.d file
447         if [ -e "${ROOT}"/etc/env.d/09nvidia ] ; then
448                 rm -f "${ROOT}"/etc/env.d/09nvidia
449         fi
450 }
451
452 pkg_postinst() {
453         use kernel_linux && linux-mod_pkg_postinst
454
455         # Switch to the nvidia implementation
456         use X && "${ROOT}"/usr/bin/eselect opengl set --use-old nvidia
457         "${ROOT}"/usr/bin/eselect opencl set --use-old nvidia
458
459         readme.gentoo_print_elog
460
461         if ! use X; then
462                 elog "You have elected to not install the X.org driver. Along with"
463                 elog "this the OpenGL libraries and VDPAU libraries were not"
464                 elog "installed. Additionally, once the driver is loaded your card"
465                 elog "and fan will run at max speed which may not be desirable."
466                 elog "Use the 'nvidia-smi' init script to have your card and fan"
467                 elog "speed scale appropriately."
468                 elog
469         fi
470         if ! use tools; then
471                 elog "USE=tools controls whether the nvidia-settings application"
472                 elog "is installed. If you would like to use it, enable that"
473                 elog "flag and re-emerge this ebuild. Optionally you can install"
474                 elog "media-video/nvidia-settings"
475                 elog
476         fi
477 }
478
479 pkg_prerm() {
480         use X && "${ROOT}"/usr/bin/eselect opengl set --use-old xorg-x11
481 }
482
483 pkg_postrm() {
484         use kernel_linux && linux-mod_pkg_postrm
485         use X && "${ROOT}"/usr/bin/eselect opengl set --use-old xorg-x11
486 }