perl-functions.eclass: should 'just work' in EAPI=6
[gentoo.git] / eclass / linux-info.eclass
1 # Copyright 1999-2015 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3 # $Id$
4
5 # @ECLASS: linux-info.eclass
6 # @MAINTAINER:
7 # kernel-misc@gentoo.org
8 # @AUTHOR:
9 # Original author: John Mylchreest <johnm@gentoo.org>
10 # @BLURB: eclass used for accessing kernel related information
11 # @DESCRIPTION:
12 # This eclass is used as a central eclass for accessing kernel
13 # related information for source or binary already installed.
14 # It is vital for linux-mod.eclass to function correctly, and is split
15 # out so that any ebuild behaviour "templates" are abstracted out
16 # using additional eclasses.
17 #
18 # "kernel config" in this file means:
19 # The .config of the currently installed sources is used as the first
20 # preference, with a fall-back to bundled config (/proc/config.gz) if available.
21 #
22 # Before using any of the config-handling functions in this eclass, you must
23 # ensure that one of the following functions has been called (in order of
24 # preference), otherwise you will get bugs like #364041):
25 # linux-info_pkg_setup
26 # linux-info_get_any_version
27 # get_version
28 # get_running_version
29
30 # A Couple of env vars are available to effect usage of this eclass
31 # These are as follows:
32
33 # @ECLASS-VARIABLE: KERNEL_DIR
34 # @DESCRIPTION:
35 # A string containing the directory of the target kernel sources. The default value is
36 # "/usr/src/linux"
37
38 # @ECLASS-VARIABLE: CONFIG_CHECK
39 # @DESCRIPTION:
40 # A string containing a list of .config options to check for before
41 # proceeding with the install.
42 #
43 #   e.g.: CONFIG_CHECK="MTRR"
44 #
45 # You can also check that an option doesn't exist by
46 # prepending it with an exclamation mark (!).
47 #
48 #   e.g.: CONFIG_CHECK="!MTRR"
49 #
50 # To simply warn about a missing option, prepend a '~'.
51 # It may be combined with '!'.
52 #
53 # In general, most checks should be non-fatal. The only time fatal checks should
54 # be used is for building kernel modules or cases that a compile will fail
55 # without the option.
56 #
57 # This is to allow usage of binary kernels, and minimal systems without kernel
58 # sources.
59
60 # @ECLASS-VARIABLE: ERROR_<CFG>
61 # @DESCRIPTION:
62 # A string containing the error message to display when the check against CONFIG_CHECK
63 # fails. <CFG> should reference the appropriate option used in CONFIG_CHECK.
64 #
65 #   e.g.: ERROR_MTRR="MTRR exists in the .config but shouldn't!!"
66
67 # @ECLASS-VARIABLE: KBUILD_OUTPUT
68 # @DESCRIPTION:
69 # A string passed on commandline, or set from the kernel makefile. It contains the directory
70 # which is to be used as the kernel object directory.
71
72 # There are also a couple of variables which are set by this, and shouldn't be
73 # set by hand. These are as follows:
74
75 # @ECLASS-VARIABLE: KV_FULL
76 # @DESCRIPTION:
77 # A read-only variable. It's a string containing the full kernel version. ie: 2.6.9-gentoo-johnm-r1
78
79 # @ECLASS-VARIABLE: KV_MAJOR
80 # @DESCRIPTION:
81 # A read-only variable. It's an integer containing the kernel major version. ie: 2
82
83 # @ECLASS-VARIABLE: KV_MINOR
84 # @DESCRIPTION:
85 # A read-only variable. It's an integer containing the kernel minor version. ie: 6
86
87 # @ECLASS-VARIABLE: KV_PATCH
88 # @DESCRIPTION:
89 # A read-only variable. It's an integer containing the kernel patch version. ie: 9
90
91 # @ECLASS-VARIABLE: KV_EXTRA
92 # @DESCRIPTION:
93 # A read-only variable. It's a string containing the kernel EXTRAVERSION. ie: -gentoo
94
95 # @ECLASS-VARIABLE: KV_LOCAL
96 # @DESCRIPTION:
97 # A read-only variable. It's a string containing the kernel LOCALVERSION concatenation. ie: -johnm
98
99 # @ECLASS-VARIABLE: KV_DIR
100 # @DESCRIPTION:
101 # A read-only variable. It's a string containing the kernel source directory, will be null if
102 # KERNEL_DIR is invalid.
103
104 # @ECLASS-VARIABLE: KV_OUT_DIR
105 # @DESCRIPTION:
106 # A read-only variable. It's a string containing the kernel object directory, will be KV_DIR unless
107 # KBUILD_OUTPUT is used. This should be used for referencing .config.
108
109 # And to ensure all the weirdness with crosscompile
110 inherit toolchain-funcs versionator
111
112 EXPORT_FUNCTIONS pkg_setup
113
114 # Overwritable environment Var's
115 # ---------------------------------------
116 KERNEL_DIR="${KERNEL_DIR:-${ROOT}usr/src/linux}"
117
118
119 # Bug fixes
120 # fix to bug #75034
121 case ${ARCH} in
122         ppc)    BUILD_FIXES="${BUILD_FIXES} TOUT=${T}/.tmp_gas_check";;
123         ppc64)  BUILD_FIXES="${BUILD_FIXES} TOUT=${T}/.tmp_gas_check";;
124 esac
125
126 # @FUNCTION: set_arch_to_kernel
127 # @DESCRIPTION:
128 # Set the env ARCH to match what the kernel expects.
129 set_arch_to_kernel() { export ARCH=$(tc-arch-kernel); }
130 # @FUNCTION: set_arch_to_portage
131 # @DESCRIPTION:
132 # Set the env ARCH to match what portage expects.
133 set_arch_to_portage() { export ARCH=$(tc-arch); }
134
135 # qeinfo "Message"
136 # -------------------
137 # qeinfo is a quiet einfo call when EBUILD_PHASE
138 # should not have visible output.
139 qout() {
140         local outputmsg type
141         type=${1}
142         shift
143         outputmsg="${@}"
144         case "${EBUILD_PHASE}" in
145                 depend)  unset outputmsg;;
146                 clean)   unset outputmsg;;
147                 preinst) unset outputmsg;;
148         esac
149         [ -n "${outputmsg}" ] && ${type} "${outputmsg}"
150 }
151
152 qeinfo() { qout einfo "${@}" ; }
153 qewarn() { qout ewarn "${@}" ; }
154 qeerror() { qout eerror "${@}" ; }
155
156 # File Functions
157 # ---------------------------------------
158
159 # @FUNCTION: getfilevar
160 # @USAGE: variable configfile
161 # @RETURN: the value of the variable
162 # @DESCRIPTION:
163 # It detects the value of the variable defined in the file configfile. This is
164 # done by including the configfile, and printing the variable with Make.
165 # It WILL break if your makefile has missing dependencies!
166 getfilevar() {
167         local ERROR basefname basedname myARCH="${ARCH}" M="${S}"
168         ERROR=0
169
170         [ -z "${1}" ] && ERROR=1
171         [ ! -f "${2}" ] && ERROR=1
172
173         if [ "${ERROR}" = 1 ]
174         then
175                 echo -e "\n"
176                 eerror "getfilevar requires 2 variables, with the second a valid file."
177                 eerror "   getfilevar <VARIABLE> <CONFIGFILE>"
178         else
179                 basefname="$(basename ${2})"
180                 basedname="$(dirname ${2})"
181                 unset ARCH
182
183                 # We use nonfatal because we want the caller to take care of things #373151
184                 [[ ${EAPI:-0} == [0123] ]] && nonfatal() { "$@"; }
185                 case ${EBUILD_PHASE_FUNC} in
186                         pkg_info|pkg_nofetch|pkg_pretend) M="${T}" ;;
187                 esac
188                 echo -e "e:\\n\\t@echo \$(${1})\\ninclude ${basefname}" | \
189                         nonfatal emake -C "${basedname}" M="${M}" ${BUILD_FIXES} -s -f - 2>/dev/null
190
191                 ARCH=${myARCH}
192         fi
193 }
194
195 # @FUNCTION: getfilevar_noexec
196 # @USAGE: variable configfile
197 # @RETURN: the value of the variable
198 # @DESCRIPTION:
199 # It detects the value of the variable defined in the file configfile.
200 # This is done with sed matching an expression only. If the variable is defined,
201 # you will run into problems. See getfilevar for those cases.
202 getfilevar_noexec() {
203         local ERROR basefname basedname mycat myARCH="${ARCH}"
204         ERROR=0
205         mycat='cat'
206
207         [ -z "${1}" ] && ERROR=1
208         [ ! -f "${2}" ] && ERROR=1
209         [ "${2%.gz}" != "${2}" ] && mycat='zcat'
210
211         if [ "${ERROR}" = 1 ]
212         then
213                 echo -e "\n"
214                 eerror "getfilevar_noexec requires 2 variables, with the second a valid file."
215                 eerror "   getfilevar_noexec <VARIABLE> <CONFIGFILE>"
216         else
217                 ${mycat} "${2}" | \
218                 sed -n \
219                 -e "/^[[:space:]]*${1}[[:space:]]*:\\?=[[:space:]]*\(.*\)\$/{
220                         s,^[^=]*[[:space:]]*=[[:space:]]*,,g ;
221                         s,[[:space:]]*\$,,g ;
222                         p
223                 }"
224         fi
225 }
226
227 # @ECLASS-VARIABLE: _LINUX_CONFIG_EXISTS_DONE
228 # @INTERNAL
229 # @DESCRIPTION:
230 # This is only set if one of the linux_config_*exists functions has been called.
231 # We use it for a QA warning that the check for a config has not been performed,
232 # as linux_chkconfig* in non-legacy mode WILL return an undefined value if no
233 # config is available at all.
234 _LINUX_CONFIG_EXISTS_DONE=
235
236 linux_config_qa_check() {
237         local f="$1"
238         if [ -z "${_LINUX_CONFIG_EXISTS_DONE}" ]; then
239                 ewarn "QA: You called $f before any linux_config_exists!"
240                 ewarn "QA: The return value of $f will NOT guaranteed later!"
241         fi
242 }
243
244 # @FUNCTION: linux_config_src_exists
245 # @RETURN: true or false
246 # @DESCRIPTION:
247 # It returns true if .config exists in a build directory otherwise false
248 linux_config_src_exists() {
249         export _LINUX_CONFIG_EXISTS_DONE=1
250         [[ -n ${KV_OUT_DIR} && -s ${KV_OUT_DIR}/.config ]]
251 }
252
253 # @FUNCTION: linux_config_bin_exists
254 # @RETURN: true or false
255 # @DESCRIPTION:
256 # It returns true if .config exists in /proc, otherwise false
257 linux_config_bin_exists() {
258         export _LINUX_CONFIG_EXISTS_DONE=1
259         [[ -s /proc/config.gz ]]
260 }
261
262 # @FUNCTION: linux_config_exists
263 # @RETURN: true or false
264 # @DESCRIPTION:
265 # It returns true if .config exists otherwise false
266 #
267 # This function MUST be checked before using any of the linux_chkconfig_*
268 # functions.
269 linux_config_exists() {
270         linux_config_src_exists || linux_config_bin_exists
271 }
272
273 # @FUNCTION: linux_config_path
274 # @DESCRIPTION:
275 # Echo the name of the config file to use.  If none are found,
276 # then return false.
277 linux_config_path() {
278         if linux_config_src_exists; then
279                 echo "${KV_OUT_DIR}/.config"
280         elif linux_config_bin_exists; then
281                 echo "/proc/config.gz"
282         else
283                 return 1
284         fi
285 }
286
287 # @FUNCTION: require_configured_kernel
288 # @DESCRIPTION:
289 # This function verifies that the current kernel is configured (it checks against the existence of .config)
290 # otherwise it dies.
291 require_configured_kernel() {
292         if ! linux_config_src_exists; then
293                 qeerror "Could not find a usable .config in the kernel source directory."
294                 qeerror "Please ensure that ${KERNEL_DIR} points to a configured set of Linux sources."
295                 qeerror "If you are using KBUILD_OUTPUT, please set the environment var so that"
296                 qeerror "it points to the necessary object directory so that it might find .config."
297                 die "Kernel not configured; no .config found in ${KV_OUT_DIR}"
298         fi
299 }
300
301 # @FUNCTION: linux_chkconfig_present
302 # @USAGE: option
303 # @RETURN: true or false
304 # @DESCRIPTION:
305 # It checks that CONFIG_<option>=y or CONFIG_<option>=m is present in the current kernel .config
306 # If linux_config_exists returns false, the results of this are UNDEFINED. You
307 # MUST call linux_config_exists first.
308 linux_chkconfig_present() {
309         linux_config_qa_check linux_chkconfig_present
310         [[ $(getfilevar_noexec "CONFIG_$1" "$(linux_config_path)") == [my] ]]
311 }
312
313 # @FUNCTION: linux_chkconfig_module
314 # @USAGE: option
315 # @RETURN: true or false
316 # @DESCRIPTION:
317 # It checks that CONFIG_<option>=m is present in the current kernel .config
318 # If linux_config_exists returns false, the results of this are UNDEFINED. You
319 # MUST call linux_config_exists first.
320 linux_chkconfig_module() {
321         linux_config_qa_check linux_chkconfig_module
322         [[ $(getfilevar_noexec "CONFIG_$1" "$(linux_config_path)") == m ]]
323 }
324
325 # @FUNCTION: linux_chkconfig_builtin
326 # @USAGE: option
327 # @RETURN: true or false
328 # @DESCRIPTION:
329 # It checks that CONFIG_<option>=y is present in the current kernel .config
330 # If linux_config_exists returns false, the results of this are UNDEFINED. You
331 # MUST call linux_config_exists first.
332 linux_chkconfig_builtin() {
333         linux_config_qa_check linux_chkconfig_builtin
334         [[ $(getfilevar_noexec "CONFIG_$1" "$(linux_config_path)") == y ]]
335 }
336
337 # @FUNCTION: linux_chkconfig_string
338 # @USAGE: option
339 # @RETURN: CONFIG_<option>
340 # @DESCRIPTION:
341 # It prints the CONFIG_<option> value of the current kernel .config (it requires a configured kernel).
342 # If linux_config_exists returns false, the results of this are UNDEFINED. You
343 # MUST call linux_config_exists first.
344 linux_chkconfig_string() {
345         linux_config_qa_check linux_chkconfig_string
346         getfilevar_noexec "CONFIG_$1" "$(linux_config_path)"
347 }
348
349 # Versioning Functions
350 # ---------------------------------------
351
352 # @FUNCTION: kernel_is
353 # @USAGE: [-lt -gt -le -ge -eq] major_number [minor_number patch_number]
354 # @RETURN: true or false
355 # @DESCRIPTION:
356 # It returns true when the current kernel version satisfies the comparison against the passed version.
357 # -eq is the default comparison.
358 #
359 # @CODE
360 # For Example where KV = 2.6.9
361 # kernel_is 2 4   returns false
362 # kernel_is 2     returns true
363 # kernel_is 2 6   returns true
364 # kernel_is 2 6 8 returns false
365 # kernel_is 2 6 9 returns true
366 # @CODE
367
368 # Note: duplicated in kernel-2.eclass
369 kernel_is() {
370         # if we haven't determined the version yet, we need to.
371         linux-info_get_any_version
372
373         # Now we can continue
374         local operator test value
375
376         case ${1#-} in
377           lt) operator="-lt"; shift;;
378           gt) operator="-gt"; shift;;
379           le) operator="-le"; shift;;
380           ge) operator="-ge"; shift;;
381           eq) operator="-eq"; shift;;
382            *) operator="-eq";;
383         esac
384         [[ $# -gt 3 ]] && die "Error in kernel-2_kernel_is(): too many parameters"
385
386         : $(( test = (KV_MAJOR << 16) + (KV_MINOR << 8) + KV_PATCH ))
387         : $(( value = (${1:-${KV_MAJOR}} << 16) + (${2:-${KV_MINOR}} << 8) + ${3:-${KV_PATCH}} ))
388         [ ${test} ${operator} ${value} ]
389 }
390
391 get_localversion() {
392         local lv_list i x
393
394         # ignore files with ~ in it.
395         for i in $(ls ${1}/localversion* 2>/dev/null); do
396                 [[ -n ${i//*~*} ]] && lv_list="${lv_list} ${i}"
397         done
398
399         for i in ${lv_list}; do
400                 x="${x}$(<${i})"
401         done
402         x=${x/ /}
403         echo ${x}
404 }
405
406 # Check if the Makefile is valid for direct parsing.
407 # Check status results:
408 # - PASS, use 'getfilevar' to extract values
409 # - FAIL, use 'getfilevar_noexec' to extract values
410 # The check may fail if:
411 # - make is not present
412 # - corruption exists in the kernel makefile
413 get_makefile_extract_function() {
414         local a='' b='' mkfunc='getfilevar'
415         a="$(getfilevar VERSION ${KERNEL_MAKEFILE})"
416         b="$(getfilevar_noexec VERSION ${KERNEL_MAKEFILE})"
417         [[ "${a}" != "${b}" ]] && mkfunc='getfilevar_noexec'
418         echo "${mkfunc}"
419 }
420
421 # internal variable, so we know to only print the warning once
422 get_version_warning_done=
423
424 # @FUNCTION: get_version
425 # @DESCRIPTION:
426 # It gets the version of the kernel inside KERNEL_DIR and populates the KV_FULL variable
427 # (if KV_FULL is already set it does nothing).
428 #
429 # The kernel version variables (KV_MAJOR, KV_MINOR, KV_PATCH, KV_EXTRA and KV_LOCAL) are also set.
430 #
431 # The KV_DIR is set using the KERNEL_DIR env var, the KV_DIR_OUT is set using a valid
432 # KBUILD_OUTPUT (in a decreasing priority list, we look for the env var, makefile var or the
433 # symlink /lib/modules/${KV_MAJOR}.${KV_MINOR}.${KV_PATCH}${KV_EXTRA}/build).
434 get_version() {
435         local tmplocal
436
437         # no need to execute this twice assuming KV_FULL is populated.
438         # we can force by unsetting KV_FULL
439         [ -n "${KV_FULL}" ] && return 0
440
441         # if we dont know KV_FULL, then we need too.
442         # make sure KV_DIR isnt set since we need to work it out via KERNEL_DIR
443         unset KV_DIR
444
445         # KV_DIR will contain the full path to the sources directory we should use
446         [ -z "${get_version_warning_done}" ] && \
447         qeinfo "Determining the location of the kernel source code"
448         [ -h "${KERNEL_DIR}" ] && KV_DIR="$(readlink -f ${KERNEL_DIR})"
449         [ -d "${KERNEL_DIR}" ] && KV_DIR="${KERNEL_DIR}"
450
451         if [ -z "${KV_DIR}" ]
452         then
453                 if [ -z "${get_version_warning_done}" ]; then
454                         get_version_warning_done=1
455                         qewarn "Unable to find kernel sources at ${KERNEL_DIR}"
456                         #qeinfo "This package requires Linux sources."
457                         if [ "${KERNEL_DIR}" == "/usr/src/linux" ] ; then
458                                 qeinfo "Please make sure that ${KERNEL_DIR} points at your running kernel, "
459                                 qeinfo "(or the kernel you wish to build against)."
460                                 qeinfo "Alternatively, set the KERNEL_DIR environment variable to the kernel sources location"
461                         else
462                                 qeinfo "Please ensure that the KERNEL_DIR environment variable points at full Linux sources of the kernel you wish to compile against."
463                         fi
464                 fi
465                 return 1
466         fi
467
468         # See if the kernel dir is actually an output dir. #454294
469         if [ -z "${KBUILD_OUTPUT}" -a -L "${KERNEL_DIR}/source" ]; then
470                 KBUILD_OUTPUT=${KERNEL_DIR}
471                 KERNEL_DIR=$(readlink -f "${KERNEL_DIR}/source")
472                 KV_DIR=${KERNEL_DIR}
473         fi
474
475         if [ -z "${get_version_warning_done}" ]; then
476                 qeinfo "Found kernel source directory:"
477                 qeinfo "    ${KV_DIR}"
478         fi
479
480         if [ ! -s "${KV_DIR}/Makefile" ]
481         then
482                 if [ -z "${get_version_warning_done}" ]; then
483                         get_version_warning_done=1
484                         qeerror "Could not find a Makefile in the kernel source directory."
485                         qeerror "Please ensure that ${KERNEL_DIR} points to a complete set of Linux sources"
486                 fi
487                 return 1
488         fi
489
490         # OK so now we know our sources directory, but they might be using
491         # KBUILD_OUTPUT, and we need this for .config and localversions-*
492         # so we better find it eh?
493         # do we pass KBUILD_OUTPUT on the CLI?
494         local OUTPUT_DIR=${KBUILD_OUTPUT}
495
496         # keep track of it
497         KERNEL_MAKEFILE="${KV_DIR}/Makefile"
498
499         if [[ -z ${OUTPUT_DIR} ]]; then
500                 # Decide the function used to extract makefile variables.
501                 local mkfunc=$(get_makefile_extract_function "${KERNEL_MAKEFILE}")
502
503                 # And if we didn't pass it, we can take a nosey in the Makefile.
504                 OUTPUT_DIR=$(${mkfunc} KBUILD_OUTPUT "${KERNEL_MAKEFILE}")
505         fi
506
507         # And contrary to existing functions I feel we shouldn't trust the
508         # directory name to find version information as this seems insane.
509         # So we parse ${KERNEL_MAKEFILE}.  We should be able to trust that
510         # the Makefile is simple enough to use the noexec extract function.
511         # This has been true for every release thus far, and it's faster
512         # than using make to evaluate the Makefile every time.
513         KV_MAJOR=$(getfilevar_noexec VERSION "${KERNEL_MAKEFILE}")
514         KV_MINOR=$(getfilevar_noexec PATCHLEVEL "${KERNEL_MAKEFILE}")
515         KV_PATCH=$(getfilevar_noexec SUBLEVEL "${KERNEL_MAKEFILE}")
516         KV_EXTRA=$(getfilevar_noexec EXTRAVERSION "${KERNEL_MAKEFILE}")
517
518         if [ -z "${KV_MAJOR}" -o -z "${KV_MINOR}" -o -z "${KV_PATCH}" ]
519         then
520                 if [ -z "${get_version_warning_done}" ]; then
521                         get_version_warning_done=1
522                         qeerror "Could not detect kernel version."
523                         qeerror "Please ensure that ${KERNEL_DIR} points to a complete set of Linux sources."
524                 fi
525                 return 1
526         fi
527
528         # and in newer versions we can also pull LOCALVERSION if it is set.
529         # but before we do this, we need to find if we use a different object directory.
530         # This *WILL* break if the user is using localversions, but we assume it was
531         # caught before this if they are.
532         if [[ -z ${OUTPUT_DIR} ]] ; then
533                 # Try to locate a kernel that is most relevant for us.
534                 for OUTPUT_DIR in "${SYSROOT}" "${ROOT}" "" ; do
535                         OUTPUT_DIR+="/lib/modules/${KV_MAJOR}.${KV_MINOR}.${KV_PATCH}${KV_EXTRA}/build"
536                         if [[ -e ${OUTPUT_DIR} ]] ; then
537                                 break
538                         fi
539                 done
540         fi
541
542         [ -h "${OUTPUT_DIR}" ] && KV_OUT_DIR="$(readlink -f ${OUTPUT_DIR})"
543         [ -d "${OUTPUT_DIR}" ] && KV_OUT_DIR="${OUTPUT_DIR}"
544         if [ -n "${KV_OUT_DIR}" ];
545         then
546                 qeinfo "Found kernel object directory:"
547                 qeinfo "    ${KV_OUT_DIR}"
548         fi
549         # and if we STILL have not got it, then we better just set it to KV_DIR
550         KV_OUT_DIR="${KV_OUT_DIR:-${KV_DIR}}"
551
552         # Grab the kernel release from the output directory.
553         # TODO: we MUST detect kernel.release being out of date, and 'return 1' from
554         # this function.
555         if [ -s "${KV_OUT_DIR}"/include/config/kernel.release ]; then
556                 KV_LOCAL=$(<"${KV_OUT_DIR}"/include/config/kernel.release)
557         elif [ -s "${KV_OUT_DIR}"/.kernelrelease ]; then
558                 KV_LOCAL=$(<"${KV_OUT_DIR}"/.kernelrelease)
559         else
560                 KV_LOCAL=
561         fi
562
563         # KV_LOCAL currently contains the full release; discard the first bits.
564         tmplocal=${KV_LOCAL#${KV_MAJOR}.${KV_MINOR}.${KV_PATCH}${KV_EXTRA}}
565
566         # If the updated local version was not changed, the tree is not prepared.
567         # Clear out KV_LOCAL in that case.
568         # TODO: this does not detect a change in the localversion part between
569         # kernel.release and the value that would be generated.
570         if [ "$KV_LOCAL" = "$tmplocal" ]; then
571                 KV_LOCAL=
572         else
573                 KV_LOCAL=$tmplocal
574         fi
575
576         # And we should set KV_FULL to the full expanded version
577         KV_FULL="${KV_MAJOR}.${KV_MINOR}.${KV_PATCH}${KV_EXTRA}${KV_LOCAL}"
578
579         qeinfo "Found sources for kernel version:"
580         qeinfo "    ${KV_FULL}"
581
582         return 0
583 }
584
585 # @FUNCTION: get_running_version
586 # @DESCRIPTION:
587 # It gets the version of the current running kernel and the result is the same as get_version() if the
588 # function can find the sources.
589 get_running_version() {
590         KV_FULL=$(uname -r)
591
592         if [[ -f ${ROOT}/lib/modules/${KV_FULL}/source/Makefile && -f ${ROOT}/lib/modules/${KV_FULL}/build/Makefile ]]; then
593                 KERNEL_DIR=$(readlink -f ${ROOT}/lib/modules/${KV_FULL}/source)
594                 KBUILD_OUTPUT=$(readlink -f ${ROOT}/lib/modules/${KV_FULL}/build)
595                 unset KV_FULL
596                 get_version
597                 return $?
598         elif [[ -f ${ROOT}/lib/modules/${KV_FULL}/source/Makefile ]]; then
599                 KERNEL_DIR=$(readlink -f ${ROOT}/lib/modules/${KV_FULL}/source)
600                 unset KV_FULL
601                 get_version
602                 return $?
603         elif [[ -f ${ROOT}/lib/modules/${KV_FULL}/build/Makefile ]]; then
604                 KERNEL_DIR=$(readlink -f ${ROOT}/lib/modules/${KV_FULL}/build)
605                 unset KV_FULL
606                 get_version
607                 return $?
608         else
609                 # This handles a variety of weird kernel versions.  Make sure to update
610                 # tests/linux-info_get_running_version.sh if you want to change this.
611                 local kv_full=${KV_FULL//[-+_]*}
612                 KV_MAJOR=$(get_version_component_range 1 ${kv_full})
613                 KV_MINOR=$(get_version_component_range 2 ${kv_full})
614                 KV_PATCH=$(get_version_component_range 3 ${kv_full})
615                 KV_EXTRA="${KV_FULL#${KV_MAJOR}.${KV_MINOR}${KV_PATCH:+.${KV_PATCH}}}"
616                 : ${KV_PATCH:=0}
617         fi
618         return 0
619 }
620
621 # This next function is named with the eclass prefix to avoid conflicts with
622 # some old versionator-like eclass functions.
623
624 # @FUNCTION: linux-info_get_any_version
625 # @DESCRIPTION:
626 # This attempts to find the version of the sources, and otherwise falls back to
627 # the version of the running kernel.
628 linux-info_get_any_version() {
629         get_version
630         if [[ $? -ne 0 ]]; then
631                 ewarn "Unable to calculate Linux Kernel version for build, attempting to use running version"
632                 get_running_version
633         fi
634 }
635
636
637 # ebuild check functions
638 # ---------------------------------------
639
640 # @FUNCTION: check_kernel_built
641 # @DESCRIPTION:
642 # This function verifies that the current kernel sources have been already prepared otherwise it dies.
643 check_kernel_built() {
644         # if we haven't determined the version yet, we need to
645         require_configured_kernel
646         get_version
647
648         local versionh_path
649         if kernel_is -ge 3 7; then
650                 versionh_path="include/generated/uapi/linux/version.h"
651         else
652                 versionh_path="include/linux/version.h"
653         fi
654
655         if [ ! -f "${KV_OUT_DIR}/${versionh_path}" ]
656         then
657                 eerror "These sources have not yet been prepared."
658                 eerror "We cannot build against an unprepared tree."
659                 eerror "To resolve this, please type the following:"
660                 eerror
661                 eerror "# cd ${KV_DIR}"
662                 eerror "# make oldconfig"
663                 eerror "# make modules_prepare"
664                 eerror
665                 eerror "Then please try merging this module again."
666                 die "Kernel sources need compiling first"
667         fi
668 }
669
670 # @FUNCTION: check_modules_supported
671 # @DESCRIPTION:
672 # This function verifies that the current kernel support modules (it checks CONFIG_MODULES=y) otherwise it dies.
673 check_modules_supported() {
674         # if we haven't determined the version yet, we need too.
675         require_configured_kernel
676         get_version
677
678         if ! linux_chkconfig_builtin "MODULES"; then
679                 eerror "These sources do not support loading external modules."
680                 eerror "to be able to use this module please enable \"Loadable modules support\""
681                 eerror "in your kernel, recompile and then try merging this module again."
682                 die "No support for external modules in ${KV_FULL} config"
683         fi
684 }
685
686 # @FUNCTION: check_extra_config
687 # @DESCRIPTION:
688 # It checks the kernel config options specified by CONFIG_CHECK. It dies only when a required config option (i.e.
689 # the prefix ~ is not used) doesn't satisfy the directive.
690 check_extra_config() {
691         local config negate die error reworkmodulenames
692         local soft_errors_count=0 hard_errors_count=0 config_required=0
693         # store the value of the QA check, because otherwise we won't catch usages
694         # after if check_extra_config is called AND other direct calls are done
695         # later.
696         local old_LINUX_CONFIG_EXISTS_DONE="${_LINUX_CONFIG_EXISTS_DONE}"
697
698         # if we haven't determined the version yet, we need to
699         linux-info_get_any_version
700
701         # Determine if we really need a .config. The only time when we don't need
702         # one is when all of the CONFIG_CHECK options are prefixed with "~".
703         for config in ${CONFIG_CHECK}; do
704                 if [[ "${config:0:1}" != "~" ]]; then
705                         config_required=1
706                         break
707                 fi
708         done
709
710         if [[ ${config_required} == 0 ]]; then
711                 # In the case where we don't require a .config, we can now bail out
712                 # if the user has no .config as there is nothing to do. Otherwise
713                 # code later will cause a failure due to missing .config.
714                 if ! linux_config_exists; then
715                         ewarn "Unable to check for the following kernel config options due"
716                         ewarn "to absence of any configured kernel sources or compiled"
717                         ewarn "config:"
718                         for config in ${CONFIG_CHECK}; do
719                                 local_error="ERROR_${config#\~}"
720                                 msg="${!local_error}"
721                                 if [[ "x${msg}" == "x" ]]; then
722                                         local_error="WARNING_${config#\~}"
723                                         msg="${!local_error}"
724                                 fi
725                                 ewarn " - ${config#\~}${msg:+ - }${msg}"
726                         done
727                         ewarn "You're on your own to make sure they are set if needed."
728                         export LINUX_CONFIG_EXISTS_DONE="${old_LINUX_CONFIG_EXISTS_DONE}"
729                         return 0
730                 fi
731         else
732                 require_configured_kernel
733         fi
734
735         einfo "Checking for suitable kernel configuration options..."
736
737         for config in ${CONFIG_CHECK}
738         do
739                 # if we specify any fatal, ensure we honor them
740                 die=1
741                 error=0
742                 negate=0
743                 reworkmodulenames=0
744
745                 if [[ ${config:0:1} == "~" ]]; then
746                         die=0
747                         config=${config:1}
748                 elif [[ ${config:0:1} == "@" ]]; then
749                         die=0
750                         reworkmodulenames=1
751                         config=${config:1}
752                 fi
753                 if [[ ${config:0:1} == "!" ]]; then
754                         negate=1
755                         config=${config:1}
756                 fi
757
758                 if [[ ${negate} == 1 ]]; then
759                         linux_chkconfig_present ${config} && error=2
760                 elif [[ ${reworkmodulenames} == 1 ]]; then
761                         local temp_config="${config//*:}" i n
762                         config="${config//:*}"
763                         if linux_chkconfig_present ${config}; then
764                                 for i in ${MODULE_NAMES}; do
765                                         n="${i//${temp_config}}"
766                                         [[ -z ${n//\(*} ]] && \
767                                                 MODULE_IGNORE="${MODULE_IGNORE} ${temp_config}"
768                                 done
769                                 error=2
770                         fi
771                 else
772                         linux_chkconfig_present ${config} || error=1
773                 fi
774
775                 if [[ ${error} > 0 ]]; then
776                         local report_func="eerror" local_error
777                         local_error="ERROR_${config}"
778                         local_error="${!local_error}"
779
780                         if [[ -z "${local_error}" ]]; then
781                                 # using old, deprecated format.
782                                 local_error="${config}_ERROR"
783                                 local_error="${!local_error}"
784                         fi
785                         if [[ ${die} == 0 && -z "${local_error}" ]]; then
786                                 #soft errors can be warnings
787                                 local_error="WARNING_${config}"
788                                 local_error="${!local_error}"
789                                 if [[ -n "${local_error}" ]] ; then
790                                         report_func="ewarn"
791                                 fi
792                         fi
793
794                         if [[ -z "${local_error}" ]]; then
795                                 [[ ${error} == 1 ]] \
796                                         && local_error="is not set when it should be." \
797                                         || local_error="should not be set. But it is."
798                                 local_error="CONFIG_${config}:\t ${local_error}"
799                         fi
800                         if [[ ${die} == 0 ]]; then
801                                 ${report_func} "  ${local_error}"
802                                 soft_errors_count=$[soft_errors_count + 1]
803                         else
804                                 ${report_func} "  ${local_error}"
805                                 hard_errors_count=$[hard_errors_count + 1]
806                         fi
807                 fi
808         done
809
810         if [[ ${hard_errors_count} > 0 ]]; then
811                 eerror "Please check to make sure these options are set correctly."
812                 eerror "Failure to do so may cause unexpected problems."
813                 eerror "Once you have satisfied these options, please try merging"
814                 eerror "this package again."
815                 export LINUX_CONFIG_EXISTS_DONE="${old_LINUX_CONFIG_EXISTS_DONE}"
816                 die "Incorrect kernel configuration options"
817         elif [[ ${soft_errors_count} > 0 ]]; then
818                 ewarn "Please check to make sure these options are set correctly."
819                 ewarn "Failure to do so may cause unexpected problems."
820         else
821                 eend 0
822         fi
823         export LINUX_CONFIG_EXISTS_DONE="${old_LINUX_CONFIG_EXISTS_DONE}"
824 }
825
826 check_zlibinflate() {
827         # if we haven't determined the version yet, we need to
828         require_configured_kernel
829         get_version
830
831         # although I restructured this code - I really really really dont support it!
832
833         # bug #27882 - zlib routines are only linked into the kernel
834         # if something compiled into the kernel calls them
835         #
836         # plus, for the cloop module, it appears that there's no way
837         # to get cloop.o to include a static zlib if CONFIG_MODVERSIONS
838         # is on
839
840         local INFLATE
841         local DEFLATE
842
843         einfo "Determining the usability of ZLIB_INFLATE support in your kernel"
844
845         ebegin "checking ZLIB_INFLATE"
846         linux_chkconfig_builtin CONFIG_ZLIB_INFLATE
847         eend $?
848         [ "$?" != 0 ] && die
849
850         ebegin "checking ZLIB_DEFLATE"
851         linux_chkconfig_builtin CONFIG_ZLIB_DEFLATE
852         eend $?
853         [ "$?" != 0 ] && die
854
855         local LINENO_START
856         local LINENO_END
857         local SYMBOLS
858         local x
859
860         LINENO_END="$(grep -n 'CONFIG_ZLIB_INFLATE y' ${KV_DIR}/lib/Config.in | cut -d : -f 1)"
861         LINENO_START="$(head -n $LINENO_END ${KV_DIR}/lib/Config.in | grep -n 'if \[' | tail -n 1 | cut -d : -f 1)"
862         (( LINENO_AMOUNT = $LINENO_END - $LINENO_START ))
863         (( LINENO_END = $LINENO_END - 1 ))
864         SYMBOLS="$(head -n $LINENO_END ${KV_DIR}/lib/Config.in | tail -n $LINENO_AMOUNT | sed -e 's/^.*\(CONFIG_[^\" ]*\).*/\1/g;')"
865
866         # okay, now we have a list of symbols
867         # we need to check each one in turn, to see whether it is set or not
868         for x in $SYMBOLS ; do
869                 if [ "${!x}" = "y" ]; then
870                         # we have a winner!
871                         einfo "${x} ensures zlib is linked into your kernel - excellent"
872                         return 0
873                 fi
874         done
875
876         eerror
877         eerror "This kernel module requires ZLIB library support."
878         eerror "You have enabled zlib support in your kernel, but haven't enabled"
879         eerror "enabled any option that will ensure that zlib is linked into your"
880         eerror "kernel."
881         eerror
882         eerror "Please ensure that you enable at least one of these options:"
883         eerror
884
885         for x in $SYMBOLS ; do
886                 eerror "  * $x"
887         done
888
889         eerror
890         eerror "Please remember to recompile and install your kernel, and reboot"
891         eerror "into your new kernel before attempting to load this kernel module."
892
893         die "Kernel doesn't include zlib support"
894 }
895
896 ################################
897 # Default pkg_setup
898 # Also used when inheriting linux-mod to force a get_version call
899 # @FUNCTION: linux-info_pkg_setup
900 # @DESCRIPTION:
901 # Force a get_version() call when inherited from linux-mod.eclass and then check if the kernel is configured
902 # to support the options specified in CONFIG_CHECK (if not null)
903 linux-info_pkg_setup() {
904         linux-info_get_any_version
905
906         if kernel_is 2 4; then
907                 if [ "$( gcc-major-version )" -eq "4" ] ; then
908                         echo
909                         ewarn "Be warned !! >=sys-devel/gcc-4.0.0 isn't supported with"
910                         ewarn "linux-2.4 (or modules building against a linux-2.4 kernel)!"
911                         echo
912                         ewarn "Either switch to another gcc-version (via gcc-config) or use a"
913                         ewarn "newer kernel that supports gcc-4."
914                         echo
915                         ewarn "Also be aware that bugreports about gcc-4 not working"
916                         ewarn "with linux-2.4 based ebuilds will be closed as INVALID!"
917                         echo
918                         epause 10
919                 fi
920         fi
921
922         [ -n "${CONFIG_CHECK}" ] && check_extra_config;
923 }