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