summaryrefslogtreecommitdiff
blob: b8984fae097172736c9acd8b5bdebc04a95402a2 (plain)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# Copyright 1999-2004 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: /var/cvsroot/gentoo-x86/eclass/linux-mod.eclass,v 1.20 2005/01/15 21:46:00 johnm Exp $

# Description: This eclass is used to interface with linux-info in such a way
#              to provide the functionality required and initial functions
#			   required to install external modules against a kernel source
#			   tree.
#
# Maintainer: John Mylchreest <johnm@gentoo.org>
# Copyright 2004 Gentoo Linux
#
# Please direct your bugs to the current eclass maintainer :)

# A Couple of env vars are available to effect usage of this eclass
# These are as follows:
# 
# Env Var		Option		Default			Description
# KERNEL_DIR	<string>	/usr/src/linux	The directory containing kernel
#											the target kernel sources.
# BUILD_PARAMS	<string>					The parameters to pass to make.
# BUILD_TARGETS	<string>	clean modules	The build targets to pass to make.
# MODULE_NAMES	<string>					This is the modules which are
#											to be built automatically using the
#											default pkg_compile/install. They
#											are explained properly below.
#											It will only make BUILD_TARGETS once
#											in any directory.
# NO_MODULESD	<string>					Set this to something to prevent
#											modulesd file generation


# MODULE_NAMES - Detailed Overview
# 
# The structure of each MODULE_NAMES entry is as follows:
# modulename(libmodulesdir:modulesourcedir)
# for example:
# MODULE_NAMES="module_pci(pci:${S}/pci) module_usb(usb:${S}/usb)"
# 
# what this would do is
#  cd ${S}/pci
#  make ${BUILD_PARAMS} ${BUILD_TARGETS}
#  insinto /lib/modules/${KV_FULL}/pci
#  doins module_pci.${KV_OBJ}
#
#  cd ${S}/usb
#  make ${BUILD_PARAMS} ${BUILD_TARGETS}
#  insinto /lib/modules/${KV_FULL}/usb
#  doins module_usb.${KV_OBJ}
#
# if the modulessourcedir isnt specified, it assumes ${S}
# if the libmodulesdir isnt specified, it assumes misc.
# if no seperator is defined ":" then it assumes the argument is modulesourcedir

inherit linux-info
ECLASS=linux-mod
INHERITED="$INHERITED $ECLASS"
EXPORT_FUNCTIONS pkg_setup src_install pkg_postinst src_compile

DESCRIPTION="Based on the $ECLASS eclass"
SLOT=0
DEPEND="virtual/linux-sources
		sys-apps/sed
		virtual/modutils"

# eclass utilities
# ----------------------------------

use_m() {
	# if we haven't determined the version yet, we need too.
	get_version;
	
	# if the kernel version is greater than 2.6.6 then we should use
	# M= instead of SUBDIRS=
	[ ${KV_MAJOR} -eq 2 -a ${KV_MINOR} -gt 5 -a ${KV_PATCH} -gt 5 ] && \
		return 0 || return 1
}

convert_to_m() {
	[ ! -f "${1}" ] && die "convert_to_m() requires a filename as an argument"
	if use_m
	then
		ebegin "Converting ${1/${WORKDIR}\//} to use M= instead of SUBDIRS="
		sed -i 's:SUBDIRS=:M=:g' ${1}
		eend $?
	fi
}

update_depmod() {
	# if we haven't determined the version yet, we need too.
	get_version;
	
	ebegin "Updating module dependencies for ${KV_FULL}"
	if [ -r ${KV_OUT_DIR}/System.map ]
	then
		depmod -ae -F ${KV_OUT_DIR}/System.map -b ${ROOT} -r ${KV_FULL}
		eend $?
	else
		ewarn
		ewarn "${KV_OUT_DIR}/System.map not found."
		ewarn "You must manually update the kernel module dependencies using depmod."
		eend 1
		ewarn
	fi
}

update_modules() {
	if [ -x /sbin/modules-update ] ;
	then
		ebegin "Updating modules.conf"
		/sbin/modules-update
		eend $?
	fi
}

set_kvobj() {
	if kernel_is 2 6
	then
		KV_OBJ="ko"
	else
		KV_OBJ="o"
	fi
	einfo "Using KV_OBJ=${KV_OBJ}"
}

generate_modulesd() {
	# This function will generate the neccessary modules.d file from the
	# information contained in the modules exported parms
	
	local selectedmodule selectedmodule_full selectedmodulevars parameter modinfop arg xifs temp
	local module_docs module_opts module_aliases module_config
	
	for arg in ${@}
	do
		selectedmodule_full="${arg}"
		# strip the directory
		selectedmodule="${selectedmodule_full/*\//}"	
		# convert the modulename to uppercase
		selectedmodule="$(echo ${selectedmodule} | tr '[:lower:]' '[:upper:]')"

		module_docs="MODULESD_${selectedmodule}_DOCS"
		module_aliases="$(eval echo \$\{#MODULESD_${selectedmodule}_ALIASES[*]\})"
		[ ${module_aliases} == 0 ] && unset module_aliases
		module_docs="${!module_docs}"
		modinfop="$(modinfo -p ${selectedmodule_full}.${KV_OBJ})"
		
		# By now we know if there is anything we can use to generate a file with
		# so unset empty vars and bail out if we find nothing.
		for parameter in ${!module_*}
		do
			[ -z "${!parameter}" ] && unset ${parameter}
		done
		[ -z "${!module_*}" -a -z "${modinfop}" ] && return

		#so now we can set the configfilevar
		module_config="${T}/modulesd-${selectedmodule}"
	
		# and being working on things.	
		ebegin "Preparing file for modules.d"
		echo  "# modules.d config file for ${selectedmodule}" >> ${module_config}
		echo  "# this file was automatically generated from linux-mod.eclass" >> ${module_config}
		for temp in ${module_docs}
		do
			echo "#  Please read ${temp/*\//} for more info" >> ${module_config}
		done

		if [ ${module_aliases} > 0 ];
		then
			echo >> ${module_config}
			echo  "# Internal Aliases - Do not edit" >> ${module_config}
			echo  "# ------------------------------" >> ${module_config}

			(( module_aliases-- ))
			for temp in $(seq 0 ${module_aliases})
			do
				echo "alias $(eval echo \$\{MODULESD_${selectedmodule}_ALIASES[$temp]\})" >> ${module_config}
			done
		fi

		# and then stating any module parameters defined from the module
		if [ -n "${modinfop}" ];
		then
			echo >> ${module_config}
			echo  "# Configurable module parameters" >> ${module_config}
			echo  "# ------------------------------" >> ${module_config}
		
			xifs="${IFS}"
			IFS="$(echo -en "\n\b")"
			for parameter in ${modinfop}
			do
				temp="$(echo ${parameter#*:} | grep -e " [0-9][ =]" | sed "s:.*\([01][= ]\).*:\1:")"
				if [ -n "${temp}" ];
				then
					module_opts="${module_opts} ${parameter%%:*}:${temp}"
				fi
				echo -e "# ${parameter%%:*}:\t${parameter#*:}" >> ${module_config}
			done
			IFS="${xifs}"
		fi
		
		# and any examples we can gather from them
		if [ -n "${module_opts}" ];
		then
			echo >> ${module_config}
			echo  "# For Example..." >> ${module_config}
			echo  "# ------------------------------" >> ${module_config}
			for parameter in ${module_opts}
			do
				echo "# options ${selectedmodule_full/*\//} ${parameter//:*}=${parameter//*:}" >> ${module_config}
			done
		fi
		
		# then we install it
		insinto /etc/modules.d
		newins ${module_config} ${selectedmodule_full/*\//}
		
		# and install any documentation we might have.
		[ -n "${module_docs}" ] && dodoc ${module_docs}
	done
	eend 0
}

display_postinst() {
	# if we haven't determined the version yet, we need too.
	get_version;
	
	local modulename moduledir sourcedir moduletemp file i
	
	file=${ROOT}/etc/modules.autoload.d/kernel-${KV_MAJOR}.${KV_MINOR}
	file=${file/\/\///}

	for i in ${MODULE_IGNORE}
	do
		MODULE_NAMES=${MODULE_NAMES//${i}(*}
	done

	einfo "If you would like to load this module automatically upon boot"
	einfo "please type the following as root:"
	for i in ${MODULE_NAMES}
	do
		for n in $(find_module_params ${i})
		do
			eval ${n/:*}=${n/*:/}
		done
		libdir=${libdir:-misc}
		srcdir=${srcdir:-${S}}
		objdir=${objdir:-${srcdir}}
		
		einfo "    # echo \"${modulename}\" >> ${file}"
	done
	echo
}

find_module_params() {
	local matched_offset=0 matched_opts=0 test="${@}" temp_var result
	local i=0 y=0 z=0
	
	for((i=0; i<=${#test}; i++))
	do
		case ${test:${i}:1} in
			\()		matched_offset[0]=${i};;
			\:)		matched_opts=$((${matched_opts} + 1));
					matched_offset[${matched_opts}]="${i}";;
			\))		matched_opts=$((${matched_opts} + 1));
					matched_offset[${matched_opts}]="${i}";;
		esac
	done
	
	for((i=0; i<=${matched_opts}; i++))
	do
		# i			= offset were working on
		# y			= last offset
		# z			= current offset - last offset
		# temp_var	= temporary name
		case ${i} in
			0)	tempvar=${test:0:${matched_offset[0]}};;
			*)	y=$((${matched_offset[$((${i} - 1))]} + 1))
				z=$((${matched_offset[${i}]} - ${matched_offset[$((${i} - 1))]}));
				z=$((${z} - 1))
				tempvar=${test:${y}:${z}};;
		esac
		
		case ${i} in
			0)	result="${result} modulename:${tempvar}";;
			1)	result="${result} libdir:${tempvar}";;
			2)	result="${result} srcdir:${tempvar}";;
			3)	result="${result} objdir:${tempvar}";;
		esac
	done
	
	echo ${result}
}

# default ebuild functions
# --------------------------------

linux-mod_pkg_setup() {
	linux-info_pkg_setup;
	check_kernel_built;
	check_modules_supported;
	set_kvobj;
}

linux-mod_src_compile() {
	local modulename libdir srcdir objdir i n
	
	BUILD_TARGETS=${BUILD_TARGETS:-clean module}
	
	for i in ${MODULE_IGNORE}
	do
		MODULE_NAMES=${MODULE_NAMES//${i}(*}
	done

	for i in ${MODULE_NAMES}
	do
		for n in $(find_module_params ${i})
		do
			eval ${n/:*}=${n/*:/}
		done
		libdir=${libdir:-misc}
		srcdir=${srcdir:-${S}}
		objdir=${objdir:-${srcdir}}
		
		if [ ! -f "${srcdir}/.built" ];
		then
			cd ${srcdir}
			einfo "Preparing ${modulename} module"
			env -u ARCH emake ${BUILD_FIXES} ${BUILD_PARAMS} ${BUILD_TARGETS} \
				|| die "Unable to make \
				   ${BUILD_FIXES} ${BUILD_PARAMS} ${BUILD_TARGETS}."
			touch ${srcdir}/.built
			cd ${OLDPWD}
		fi
	done
}

linux-mod_src_install() {
	local modulename libdir srcdir objdir i n
	
	for i in ${MODULE_IGNORE}
	do
		MODULE_NAMES=${MODULE_NAMES//${i}(*}
	done

	for i in ${MODULE_NAMES}
	do
		for n in $(find_module_params ${i})
		do
			eval ${n/:*}=${n/*:/}
		done
		libdir=${libdir:-misc}
		srcdir=${srcdir:-${S}}
		objdir=${objdir:-${srcdir}}

		einfo "Installing ${modulename} module"
		cd ${objdir}

		insinto /lib/modules/${KV_FULL}/${libdir}
		doins ${modulename}.${KV_OBJ}
		cd ${OLDPWD}
		
		[ -z "${NO_MODULESD}" ] && generate_modulesd ${objdir}/${modulename}
	done
}

linux-mod_pkg_postinst() {
	update_depmod;
	update_modules;
	display_postinst;
}