linux-info.eclass 29 KB

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