summaryrefslogtreecommitdiffstats
path: root/core/modules/nvidia-common/module.build
blob: 619d11702820e4862ca0ca796b34d74a9d200619 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/bin/bash
fetch_source() {
	local version
	mkdir -p src
	cd src || perror "Could not change into src directory."
	for version in $NVIDIA_VERSIONS; do
		download "${REQUIRED_URL//%VERSION%/$version}" "${version}.run"
	done
}

build() {
	local bundle destdir builddir version
	pdebug "--- chroot ---------------------------------------------------------------------"
	pdebug "-                                                                              -"
	pdebug "-   Notice: This may take a while!                                             -"
	pdebug "-                                                                              -"
	pdebug "-      Please keep note the Nvidia installer _will_ complain about             -"
	pdebug "-      several warnings and errors. It will do this in any case.               -"
	pdebug "-                                                                              -"
	pdebug "-      This does _not_ mean the library module compilation was unsuccessful!   -"
	pdebug "-                                                                              -"
	pdebug "--------------------------------------------------------------------------------"

	declare -A pciids
	local file RESULT NVIDIA_MODULES drmfile filenum
	filenum=10
	for version in $NVIDIA_VERSIONS; do
		bundle="${MODULE_WORK_DIR}/src/${version}.run"
		builddir="${MODULE_BUILD_DIR}/${version}"
		destdir="${builddir}/NVIDIA"

		pdebug "Unpacking NVidia-Installer ..."
		[ -d "$destdir" ] && rm -rf "$destdir"
		sh "$bundle" --extract-only --target "$destdir" ||	perror "Could not extract $bundle to $destdir."
		download "${REQUIRED_HTML//%VERSION%/$version}" "$builddir/supportedchips.html"
		local KMOD_ERROR="(ERROR: The nvidia kernel module was not created.|.ko failed to build)"
		local KMOD_LOG="/var/log/nvidia-installer.kmod.log"
		pinfo "Ready to chroot - may take some time."
		chroot_run "${builddir}" <<-EOF
		/NVIDIA/nvidia-installer --no-nouveau-check --no-network --no-backup --no-rpms --no-runlevel-check \
				--no-distro-scripts --no-cc-version-check --no-x-check --no-precompiled-interface --silent \
				--log-file-name="$KMOD_LOG" --kernel-source-path "/$KERNEL_HEADERS_DIR"

		grep -qE "$KMOD_ERROR" "$KMOD_LOG" \
			|| /NVIDIA/nvidia-installer --no-nouveau-check --no-network --no-backup --no-rpms --no-runlevel-check \
				--no-distro-scripts --no-cc-version-check --no-x-check --no-precompiled-interface --silent \
				--no-kernel-module
		exit 0 # Fake it since it will try to load the module and probably fail
		EOF
		if grep -qE "$KMOD_ERROR" "${builddir}${KMOD_LOG}"; then
			pwarning "Could not compile all kernel modules: $( grep -E "$KMOD_ERROR" "${builddir}${KMOD_LOG}" )"
			perror "Bailing out..."
		fi
		pinfo "chroot terminated, cleaning up"

		# move whiteout list to its /opt/openslx/etc/nvidia.whiteout (inside $builddir, for lib target)
		if [ -e "${builddir}/overlay.whiteout.list" ]; then
			mkdir -p "${builddir}/opt/openslx/etc"
			mv "${builddir}/overlay.whiteout.list" "${builddir}/opt/openslx/etc/nvidia.whiteout"
		fi

		# Kernel related - outside of version subdir ($builddir)
		NVIDIA_MODULES="${MODULE_BUILD_DIR}/lib/modules/nvidia/${version}"
		mkdir -p "$NVIDIA_MODULES"
		pinfo "Copying kernel modules..."
		for file in nvidia.ko nvidia-uvm.ko nvidia-modeset.ko nvidia-drm.ko; do
			RESULT=$(find "$builddir/NVIDIA/kernel" -name "$file" | head -n 1)
			[ -z "$RESULT" ] && perror "Could not find built module $file in ./NVIDIA/kernel"
			pinfo "Fetching ${file}..."
			strip -g "$RESULT" || pwarning "Could not strip $file"
			cp "$RESULT" "$NVIDIA_MODULES" || perror "Could not copy $file"
		done

		mkdir -p "${MODULE_BUILD_DIR}/drm.cfg.d"
		drmfile="${MODULE_BUILD_DIR}/drm.cfg.d/$(( filenum++ ))-nvidia-$version"
		cat > "$drmfile" <<-BLOFF
			@nvidia-${version}            acpi_ipmi nvidia/$version/nvidia.ko nvidia/$version/nvidia-uvm.ko nvidia/$version/nvidia-modeset.ko nvidia/$version/nvidia-drm.ko
			@nvidia-${version}_fallback   nouveau
		BLOFF
		if ! [ -s "${builddir}/supportedchips.html" ]; then
			pwarning "No supportedchips.html -- THE GOGGLES, THEY DO NOTHING!"
		else
			local mode line name dev version
			pinfo "Creating drm.cfg"
			mode=0
			while read -r line; do
				if [[ "$line" =~ '<a name="legacy_' ]]; then
					break # For now, we stop here; older versions will be fetched from their own html file
				elif [[ "$mode" = 0 ]] && [[ "$line" =~ ^'<tr' ]]; then
					mode=1
				elif [[ "$mode" = 1 ]] && [[ "$line" =~ ^'<td' ]]; then
					mode=2
					name=$(echo "$line" | sed -r 's/^.*<td[^>]*>([^<]+)<.*$/\1/')
				elif [[ "$mode" = 2 ]] && [[ "$line" =~ ^'<td'[^'>']*'>'[0-9A-F]{4}'<' ]]; then
					mode=0
					dev=$(echo "$line" | sed -r 's/^.*<td[^>]*>([0-9A-Fa-f]{4})<.*$/\1/' | tr 'A-Z' 'a-z')
					if [ -n "${pciids[$dev]}" ]; then
						pdebug "$dev already handled"
					else
						echo "10de:$dev          @nvidia-$version"
						pciids[$dev]=1
					fi
				else
					mode=0
					name=
					dev=
				fi
			done < "${builddir}/supportedchips.html" >> "$drmfile"
		fi
		
		rm -rf -- "$destdir"
	done
}

post_copy() {
	:
}