linux-info.eclass 32 KB

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