kernel.bbclass 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. inherit linux-kernel-base kernel-module-split
  2. COMPATIBLE_HOST = ".*-linux"
  3. KERNEL_PACKAGE_NAME ??= "kernel"
  4. KERNEL_DEPLOYSUBDIR ??= "${@ "" if (d.getVar("KERNEL_PACKAGE_NAME") == "kernel") else d.getVar("KERNEL_PACKAGE_NAME") }"
  5. PROVIDES += "${@ "virtual/kernel" if (d.getVar("KERNEL_PACKAGE_NAME") == "kernel") else "" }"
  6. DEPENDS += "virtual/${TARGET_PREFIX}binutils virtual/${TARGET_PREFIX}gcc kmod-native bc-native bison-native"
  7. DEPENDS += "${@bb.utils.contains("INITRAMFS_FSTYPES", "cpio.lzo", "lzop-native", "", d)}"
  8. DEPENDS += "${@bb.utils.contains("INITRAMFS_FSTYPES", "cpio.lz4", "lz4-native", "", d)}"
  9. PACKAGE_WRITE_DEPS += "depmodwrapper-cross"
  10. do_deploy[depends] += "depmodwrapper-cross:do_populate_sysroot gzip-native:do_populate_sysroot"
  11. do_clean[depends] += "make-mod-scripts:do_clean"
  12. CVE_PRODUCT ?= "linux_kernel"
  13. S = "${STAGING_KERNEL_DIR}"
  14. B = "${WORKDIR}/build"
  15. KBUILD_OUTPUT = "${B}"
  16. OE_TERMINAL_EXPORTS += "KBUILD_OUTPUT"
  17. # we include gcc above, we dont need virtual/libc
  18. INHIBIT_DEFAULT_DEPS = "1"
  19. KERNEL_IMAGETYPE ?= "zImage"
  20. INITRAMFS_IMAGE ?= ""
  21. INITRAMFS_IMAGE_NAME ?= "${@['${INITRAMFS_IMAGE}-${MACHINE}', ''][d.getVar('INITRAMFS_IMAGE') == '']}"
  22. INITRAMFS_TASK ?= ""
  23. INITRAMFS_IMAGE_BUNDLE ?= ""
  24. # KERNEL_VERSION is extracted from source code. It is evaluated as
  25. # None for the first parsing, since the code has not been fetched.
  26. # After the code is fetched, it will be evaluated as real version
  27. # number and cause kernel to be rebuilt. To avoid this, make
  28. # KERNEL_VERSION_NAME and KERNEL_VERSION_PKG_NAME depend on
  29. # LINUX_VERSION which is a constant.
  30. KERNEL_VERSION_NAME = "${@d.getVar('KERNEL_VERSION') or ""}"
  31. KERNEL_VERSION_NAME[vardepvalue] = "${LINUX_VERSION}"
  32. KERNEL_VERSION_PKG_NAME = "${@legitimize_package_name(d.getVar('KERNEL_VERSION'))}"
  33. KERNEL_VERSION_PKG_NAME[vardepvalue] = "${LINUX_VERSION}"
  34. python __anonymous () {
  35. pn = d.getVar("PN")
  36. kpn = d.getVar("KERNEL_PACKAGE_NAME")
  37. # XXX Remove this after bug 11905 is resolved
  38. # FILES_${KERNEL_PACKAGE_NAME}-dev doesn't expand correctly
  39. if kpn == pn:
  40. bb.warn("Some packages (E.g. *-dev) might be missing due to "
  41. "bug 11905 (variable KERNEL_PACKAGE_NAME == PN)")
  42. # The default kernel recipe builds in a shared location defined by
  43. # bitbake/distro confs: STAGING_KERNEL_DIR and STAGING_KERNEL_BUILDDIR.
  44. # Set these variables to directories under ${WORKDIR} in alternate
  45. # kernel recipes (I.e. where KERNEL_PACKAGE_NAME != kernel) so that they
  46. # may build in parallel with the default kernel without clobbering.
  47. if kpn != "kernel":
  48. workdir = d.getVar("WORKDIR")
  49. sourceDir = os.path.join(workdir, 'kernel-source')
  50. artifactsDir = os.path.join(workdir, 'kernel-build-artifacts')
  51. d.setVar("STAGING_KERNEL_DIR", sourceDir)
  52. d.setVar("STAGING_KERNEL_BUILDDIR", artifactsDir)
  53. # Merge KERNEL_IMAGETYPE and KERNEL_ALT_IMAGETYPE into KERNEL_IMAGETYPES
  54. type = d.getVar('KERNEL_IMAGETYPE') or ""
  55. alttype = d.getVar('KERNEL_ALT_IMAGETYPE') or ""
  56. types = d.getVar('KERNEL_IMAGETYPES') or ""
  57. if type not in types.split():
  58. types = (type + ' ' + types).strip()
  59. if alttype not in types.split():
  60. types = (alttype + ' ' + types).strip()
  61. d.setVar('KERNEL_IMAGETYPES', types)
  62. # KERNEL_IMAGETYPES may contain a mixture of image types supported directly
  63. # by the kernel build system and types which are created by post-processing
  64. # the output of the kernel build system (e.g. compressing vmlinux ->
  65. # vmlinux.gz in kernel_do_compile()).
  66. # KERNEL_IMAGETYPE_FOR_MAKE should contain only image types supported
  67. # directly by the kernel build system.
  68. if not d.getVar('KERNEL_IMAGETYPE_FOR_MAKE'):
  69. typeformake = set()
  70. for type in types.split():
  71. if type == 'vmlinux.gz':
  72. type = 'vmlinux'
  73. typeformake.add(type)
  74. d.setVar('KERNEL_IMAGETYPE_FOR_MAKE', ' '.join(sorted(typeformake)))
  75. kname = d.getVar('KERNEL_PACKAGE_NAME') or "kernel"
  76. imagedest = d.getVar('KERNEL_IMAGEDEST')
  77. for type in types.split():
  78. typelower = type.lower()
  79. d.appendVar('PACKAGES', ' %s-image-%s' % (kname, typelower))
  80. d.setVar('FILES_' + kname + '-image-' + typelower, '/' + imagedest + '/' + type + '-${KERNEL_VERSION_NAME}' + ' /' + imagedest + '/' + type)
  81. d.appendVar('RDEPENDS_%s-image' % kname, ' %s-image-%s' % (kname, typelower))
  82. d.setVar('PKG_%s-image-%s' % (kname,typelower), '%s-image-%s-${KERNEL_VERSION_PKG_NAME}' % (kname, typelower))
  83. d.setVar('ALLOW_EMPTY_%s-image-%s' % (kname, typelower), '1')
  84. d.setVar('pkg_postinst_%s-image-%s' % (kname,typelower), """set +e
  85. if [ -n "$D" ]; then
  86. ln -sf %s-${KERNEL_VERSION} $D/${KERNEL_IMAGEDEST}/%s > /dev/null 2>&1
  87. else
  88. ln -sf %s-${KERNEL_VERSION} ${KERNEL_IMAGEDEST}/%s > /dev/null 2>&1
  89. if [ $? -ne 0 ]; then
  90. echo "Filesystem on ${KERNEL_IMAGEDEST}/ doesn't support symlinks, falling back to copied image (%s)."
  91. install -m 0644 ${KERNEL_IMAGEDEST}/%s-${KERNEL_VERSION} ${KERNEL_IMAGEDEST}/%s
  92. fi
  93. fi
  94. set -e
  95. """ % (type, type, type, type, type, type, type))
  96. d.setVar('pkg_postrm_%s-image-%s' % (kname,typelower), """set +e
  97. if [ -f "${KERNEL_IMAGEDEST}/%s" -o -L "${KERNEL_IMAGEDEST}/%s" ]; then
  98. rm -f ${KERNEL_IMAGEDEST}/%s > /dev/null 2>&1
  99. fi
  100. set -e
  101. """ % (type, type, type))
  102. image = d.getVar('INITRAMFS_IMAGE')
  103. # If the INTIRAMFS_IMAGE is set but the INITRAMFS_IMAGE_BUNDLE is set to 0,
  104. # the do_bundle_initramfs does nothing, but the INITRAMFS_IMAGE is built
  105. # standalone for use by wic and other tools.
  106. if image:
  107. d.appendVarFlag('do_bundle_initramfs', 'depends', ' ${INITRAMFS_IMAGE}:do_image_complete')
  108. # NOTE: setting INITRAMFS_TASK is for backward compatibility
  109. # The preferred method is to set INITRAMFS_IMAGE, because
  110. # this INITRAMFS_TASK has circular dependency problems
  111. # if the initramfs requires kernel modules
  112. image_task = d.getVar('INITRAMFS_TASK')
  113. if image_task:
  114. d.appendVarFlag('do_configure', 'depends', ' ${INITRAMFS_TASK}')
  115. }
  116. # Here we pull in all various kernel image types which we support.
  117. #
  118. # In case you're wondering why kernel.bbclass inherits the other image
  119. # types instead of the other way around, the reason for that is to
  120. # maintain compatibility with various currently existing meta-layers.
  121. # By pulling in the various kernel image types here, we retain the
  122. # original behavior of kernel.bbclass, so no meta-layers should get
  123. # broken.
  124. #
  125. # KERNEL_CLASSES by default pulls in kernel-uimage.bbclass, since this
  126. # used to be the default behavior when only uImage was supported. This
  127. # variable can be appended by users who implement support for new kernel
  128. # image types.
  129. KERNEL_CLASSES ?= " kernel-uimage "
  130. inherit ${KERNEL_CLASSES}
  131. # Old style kernels may set ${S} = ${WORKDIR}/git for example
  132. # We need to move these over to STAGING_KERNEL_DIR. We can't just
  133. # create the symlink in advance as the git fetcher can't cope with
  134. # the symlink.
  135. do_unpack[cleandirs] += " ${S} ${STAGING_KERNEL_DIR} ${B} ${STAGING_KERNEL_BUILDDIR}"
  136. do_clean[cleandirs] += " ${S} ${STAGING_KERNEL_DIR} ${B} ${STAGING_KERNEL_BUILDDIR}"
  137. python do_symlink_kernsrc () {
  138. s = d.getVar("S")
  139. if s[-1] == '/':
  140. # drop trailing slash, so that os.symlink(kernsrc, s) doesn't use s as directory name and fail
  141. s=s[:-1]
  142. kernsrc = d.getVar("STAGING_KERNEL_DIR")
  143. if s != kernsrc:
  144. bb.utils.mkdirhier(kernsrc)
  145. bb.utils.remove(kernsrc, recurse=True)
  146. if d.getVar("EXTERNALSRC"):
  147. # With EXTERNALSRC S will not be wiped so we can symlink to it
  148. os.symlink(s, kernsrc)
  149. else:
  150. import shutil
  151. shutil.move(s, kernsrc)
  152. os.symlink(kernsrc, s)
  153. }
  154. # do_patch is normally ordered before do_configure, but
  155. # externalsrc.bbclass deletes do_patch, breaking the dependency of
  156. # do_configure on do_symlink_kernsrc.
  157. addtask symlink_kernsrc before do_patch do_configure after do_unpack
  158. inherit kernel-arch deploy
  159. PACKAGES_DYNAMIC += "^${KERNEL_PACKAGE_NAME}-module-.*"
  160. PACKAGES_DYNAMIC += "^${KERNEL_PACKAGE_NAME}-image-.*"
  161. PACKAGES_DYNAMIC += "^${KERNEL_PACKAGE_NAME}-firmware-.*"
  162. export OS = "${TARGET_OS}"
  163. export CROSS_COMPILE = "${TARGET_PREFIX}"
  164. export KBUILD_BUILD_VERSION = "1"
  165. export KBUILD_BUILD_USER ?= "oe-user"
  166. export KBUILD_BUILD_HOST ?= "oe-host"
  167. KERNEL_RELEASE ?= "${KERNEL_VERSION}"
  168. # The directory where built kernel lies in the kernel tree
  169. KERNEL_OUTPUT_DIR ?= "arch/${ARCH}/boot"
  170. KERNEL_IMAGEDEST ?= "boot"
  171. #
  172. # configuration
  173. #
  174. export CMDLINE_CONSOLE = "console=${@d.getVar("KERNEL_CONSOLE") or "ttyS0"}"
  175. KERNEL_VERSION = "${@get_kernelversion_headers('${B}')}"
  176. KERNEL_LOCALVERSION ?= ""
  177. # kernels are generally machine specific
  178. PACKAGE_ARCH = "${MACHINE_ARCH}"
  179. # U-Boot support
  180. UBOOT_ENTRYPOINT ?= "20008000"
  181. UBOOT_LOADADDRESS ?= "${UBOOT_ENTRYPOINT}"
  182. # Some Linux kernel configurations need additional parameters on the command line
  183. KERNEL_EXTRA_ARGS ?= ""
  184. EXTRA_OEMAKE = " HOSTCC="${BUILD_CC} ${BUILD_CFLAGS} ${BUILD_LDFLAGS}" HOSTCPP="${BUILD_CPP}""
  185. EXTRA_OEMAKE += " HOSTCXX="${BUILD_CXX} ${BUILD_CXXFLAGS} ${BUILD_LDFLAGS}""
  186. KERNEL_ALT_IMAGETYPE ??= ""
  187. copy_initramfs() {
  188. echo "Copying initramfs into ./usr ..."
  189. # In case the directory is not created yet from the first pass compile:
  190. mkdir -p ${B}/usr
  191. # Find and use the first initramfs image archive type we find
  192. rm -f ${B}/usr/${INITRAMFS_IMAGE_NAME}.cpio
  193. for img in cpio cpio.gz cpio.lz4 cpio.lzo cpio.lzma cpio.xz; do
  194. if [ -e "${DEPLOY_DIR_IMAGE}/${INITRAMFS_IMAGE_NAME}.$img" ]; then
  195. cp ${DEPLOY_DIR_IMAGE}/${INITRAMFS_IMAGE_NAME}.$img ${B}/usr/.
  196. case $img in
  197. *gz)
  198. echo "gzip decompressing image"
  199. gunzip -f ${B}/usr/${INITRAMFS_IMAGE_NAME}.$img
  200. break
  201. ;;
  202. *lz4)
  203. echo "lz4 decompressing image"
  204. lz4 -df ${B}/usr/${INITRAMFS_IMAGE_NAME}.$img ${B}/usr/${INITRAMFS_IMAGE_NAME}.cpio
  205. break
  206. ;;
  207. *lzo)
  208. echo "lzo decompressing image"
  209. lzop -df ${B}/usr/${INITRAMFS_IMAGE_NAME}.$img
  210. break
  211. ;;
  212. *lzma)
  213. echo "lzma decompressing image"
  214. lzma -df ${B}/usr/${INITRAMFS_IMAGE_NAME}.$img
  215. break
  216. ;;
  217. *xz)
  218. echo "xz decompressing image"
  219. xz -df ${B}/usr/${INITRAMFS_IMAGE_NAME}.$img
  220. break
  221. ;;
  222. esac
  223. break
  224. fi
  225. done
  226. # Verify that the above loop found a initramfs, fail otherwise
  227. [ -f ${B}/usr/${INITRAMFS_IMAGE_NAME}.cpio ] && echo "Finished copy of initramfs into ./usr" || die "Could not find any ${DEPLOY_DIR_IMAGE}/${INITRAMFS_IMAGE_NAME}.cpio{.gz|.lz4|.lzo|.lzma|.xz) for bundling; INITRAMFS_IMAGE_NAME might be wrong."
  228. }
  229. do_bundle_initramfs () {
  230. if [ ! -z "${INITRAMFS_IMAGE}" -a x"${INITRAMFS_IMAGE_BUNDLE}" = x1 ]; then
  231. echo "Creating a kernel image with a bundled initramfs..."
  232. copy_initramfs
  233. # Backing up kernel image relies on its type(regular file or symbolic link)
  234. tmp_path=""
  235. for imageType in ${KERNEL_IMAGETYPE_FOR_MAKE} ; do
  236. if [ -h ${KERNEL_OUTPUT_DIR}/$imageType ] ; then
  237. linkpath=`readlink -n ${KERNEL_OUTPUT_DIR}/$imageType`
  238. realpath=`readlink -fn ${KERNEL_OUTPUT_DIR}/$imageType`
  239. mv -f $realpath $realpath.bak
  240. tmp_path=$tmp_path" "$imageType"#"$linkpath"#"$realpath
  241. elif [ -f ${KERNEL_OUTPUT_DIR}/$imageType ]; then
  242. mv -f ${KERNEL_OUTPUT_DIR}/$imageType ${KERNEL_OUTPUT_DIR}/$imageType.bak
  243. tmp_path=$tmp_path" "$imageType"##"
  244. fi
  245. done
  246. use_alternate_initrd=CONFIG_INITRAMFS_SOURCE=${B}/usr/${INITRAMFS_IMAGE_NAME}.cpio
  247. kernel_do_compile
  248. # Restoring kernel image
  249. for tp in $tmp_path ; do
  250. imageType=`echo $tp|cut -d "#" -f 1`
  251. linkpath=`echo $tp|cut -d "#" -f 2`
  252. realpath=`echo $tp|cut -d "#" -f 3`
  253. if [ -n "$realpath" ]; then
  254. mv -f $realpath $realpath.initramfs
  255. mv -f $realpath.bak $realpath
  256. ln -sf $linkpath.initramfs ${B}/${KERNEL_OUTPUT_DIR}/$imageType.initramfs
  257. else
  258. mv -f ${KERNEL_OUTPUT_DIR}/$imageType ${KERNEL_OUTPUT_DIR}/$imageType.initramfs
  259. mv -f ${KERNEL_OUTPUT_DIR}/$imageType.bak ${KERNEL_OUTPUT_DIR}/$imageType
  260. fi
  261. done
  262. fi
  263. }
  264. do_bundle_initramfs[dirs] = "${B}"
  265. python do_devshell_prepend () {
  266. os.environ["LDFLAGS"] = ''
  267. }
  268. addtask bundle_initramfs after do_install before do_deploy
  269. get_cc_option () {
  270. # Check if KERNEL_CC supports the option "file-prefix-map".
  271. # This option allows us to build images with __FILE__ values that do not
  272. # contain the host build path.
  273. if ${KERNEL_CC} -Q --help=joined | grep -q "\-ffile-prefix-map=<old=new>"; then
  274. echo "-ffile-prefix-map=${S}=/kernel-source/"
  275. fi
  276. }
  277. kernel_do_compile() {
  278. unset CFLAGS CPPFLAGS CXXFLAGS LDFLAGS MACHINE
  279. if [ "${BUILD_REPRODUCIBLE_BINARIES}" = "1" ]; then
  280. # kernel sources do not use do_unpack, so SOURCE_DATE_EPOCH may not
  281. # be set....
  282. if [ "${SOURCE_DATE_EPOCH}" = "" -o "${SOURCE_DATE_EPOCH}" = "0" ]; then
  283. # The source directory is not necessarily a git repository, so we
  284. # specify the git-dir to ensure that git does not query a
  285. # repository in any parent directory.
  286. SOURCE_DATE_EPOCH=`git --git-dir="${S}/.git" log -1 --pretty=%ct 2>/dev/null || echo "${REPRODUCIBLE_TIMESTAMP_ROOTFS}"`
  287. fi
  288. ts=`LC_ALL=C date -d @$SOURCE_DATE_EPOCH`
  289. export KBUILD_BUILD_TIMESTAMP="$ts"
  290. export KCONFIG_NOTIMESTAMP=1
  291. bbnote "KBUILD_BUILD_TIMESTAMP: $ts"
  292. fi
  293. # The $use_alternate_initrd is only set from
  294. # do_bundle_initramfs() This variable is specifically for the
  295. # case where we are making a second pass at the kernel
  296. # compilation and we want to force the kernel build to use a
  297. # different initramfs image. The way to do that in the kernel
  298. # is to specify:
  299. # make ...args... CONFIG_INITRAMFS_SOURCE=some_other_initramfs.cpio
  300. if [ "$use_alternate_initrd" = "" ] && [ "${INITRAMFS_TASK}" != "" ] ; then
  301. # The old style way of copying an prebuilt image and building it
  302. # is turned on via INTIRAMFS_TASK != ""
  303. copy_initramfs
  304. use_alternate_initrd=CONFIG_INITRAMFS_SOURCE=${B}/usr/${INITRAMFS_IMAGE_NAME}.cpio
  305. fi
  306. cc_extra=$(get_cc_option)
  307. for typeformake in ${KERNEL_IMAGETYPE_FOR_MAKE} ; do
  308. oe_runmake ${typeformake} CC="${KERNEL_CC} $cc_extra " LD="${KERNEL_LD}" ${KERNEL_EXTRA_ARGS} $use_alternate_initrd
  309. done
  310. # vmlinux.gz is not built by kernel
  311. if (echo "${KERNEL_IMAGETYPES}" | grep -wq "vmlinux\.gz"); then
  312. mkdir -p "${KERNEL_OUTPUT_DIR}"
  313. gzip -9cn < ${B}/vmlinux > "${KERNEL_OUTPUT_DIR}/vmlinux.gz"
  314. fi
  315. }
  316. do_compile_kernelmodules() {
  317. unset CFLAGS CPPFLAGS CXXFLAGS LDFLAGS MACHINE
  318. if [ "${BUILD_REPRODUCIBLE_BINARIES}" = "1" ]; then
  319. # kernel sources do not use do_unpack, so SOURCE_DATE_EPOCH may not
  320. # be set....
  321. if [ "${SOURCE_DATE_EPOCH}" = "" -o "${SOURCE_DATE_EPOCH}" = "0" ]; then
  322. # The source directory is not necessarily a git repository, so we
  323. # specify the git-dir to ensure that git does not query a
  324. # repository in any parent directory.
  325. SOURCE_DATE_EPOCH=`git --git-dir="${S}/.git" log -1 --pretty=%ct 2>/dev/null || echo "${REPRODUCIBLE_TIMESTAMP_ROOTFS}"`
  326. fi
  327. ts=`LC_ALL=C date -d @$SOURCE_DATE_EPOCH`
  328. export KBUILD_BUILD_TIMESTAMP="$ts"
  329. export KCONFIG_NOTIMESTAMP=1
  330. bbnote "KBUILD_BUILD_TIMESTAMP: $ts"
  331. fi
  332. if (grep -q -i -e '^CONFIG_MODULES=y$' ${B}/.config); then
  333. cc_extra=$(get_cc_option)
  334. oe_runmake -C ${B} ${PARALLEL_MAKE} modules CC="${KERNEL_CC} $cc_extra " LD="${KERNEL_LD}" ${KERNEL_EXTRA_ARGS}
  335. # Module.symvers gets updated during the
  336. # building of the kernel modules. We need to
  337. # update this in the shared workdir since some
  338. # external kernel modules has a dependency on
  339. # other kernel modules and will look at this
  340. # file to do symbol lookups
  341. cp ${B}/Module.symvers ${STAGING_KERNEL_BUILDDIR}/
  342. # 5.10+ kernels have module.lds that we need to copy for external module builds
  343. if [ -e "${B}/scripts/module.lds" ]; then
  344. install -Dm 0644 ${B}/scripts/module.lds ${STAGING_KERNEL_BUILDDIR}/scripts/module.lds
  345. fi
  346. else
  347. bbnote "no modules to compile"
  348. fi
  349. }
  350. addtask compile_kernelmodules after do_compile before do_strip
  351. kernel_do_install() {
  352. #
  353. # First install the modules
  354. #
  355. unset CFLAGS CPPFLAGS CXXFLAGS LDFLAGS MACHINE
  356. if (grep -q -i -e '^CONFIG_MODULES=y$' .config); then
  357. oe_runmake DEPMOD=echo MODLIB=${D}${nonarch_base_libdir}/modules/${KERNEL_VERSION} INSTALL_FW_PATH=${D}${nonarch_base_libdir}/firmware modules_install
  358. rm "${D}${nonarch_base_libdir}/modules/${KERNEL_VERSION}/build"
  359. rm "${D}${nonarch_base_libdir}/modules/${KERNEL_VERSION}/source"
  360. # If the kernel/ directory is empty remove it to prevent QA issues
  361. rmdir --ignore-fail-on-non-empty "${D}${nonarch_base_libdir}/modules/${KERNEL_VERSION}/kernel"
  362. else
  363. bbnote "no modules to install"
  364. fi
  365. #
  366. # Install various kernel output (zImage, map file, config, module support files)
  367. #
  368. install -d ${D}/${KERNEL_IMAGEDEST}
  369. install -d ${D}/boot
  370. #
  371. # When including an initramfs bundle inside a FIT image, the fitImage is created after the install task
  372. # by do_assemble_fitimage_initramfs.
  373. # This happens after the generation of the initramfs bundle (done by do_bundle_initramfs).
  374. # So, at the level of the install task we should not try to install the fitImage. fitImage is still not
  375. # generated yet.
  376. # After the generation of the fitImage, the deploy task copies the fitImage from the build directory to
  377. # the deploy folder.
  378. #
  379. for imageType in ${KERNEL_IMAGETYPES} ; do
  380. if [ $imageType != "fitImage" ] || [ "${INITRAMFS_IMAGE_BUNDLE}" != "1" ] ; then
  381. install -m 0644 ${KERNEL_OUTPUT_DIR}/$imageType ${D}/${KERNEL_IMAGEDEST}/$imageType-${KERNEL_VERSION}
  382. fi
  383. done
  384. install -m 0644 System.map ${D}/boot/System.map-${KERNEL_VERSION}
  385. install -m 0644 .config ${D}/boot/config-${KERNEL_VERSION}
  386. install -m 0644 vmlinux ${D}/boot/vmlinux-${KERNEL_VERSION}
  387. [ -e Module.symvers ] && install -m 0644 Module.symvers ${D}/boot/Module.symvers-${KERNEL_VERSION}
  388. install -d ${D}${sysconfdir}/modules-load.d
  389. install -d ${D}${sysconfdir}/modprobe.d
  390. }
  391. # Must be ran no earlier than after do_kernel_checkout or else Makefile won't be in ${S}/Makefile
  392. do_kernel_version_sanity_check() {
  393. if [ "x${KERNEL_VERSION_SANITY_SKIP}" = "x1" ]; then
  394. exit 0
  395. fi
  396. # The Makefile determines the kernel version shown at runtime
  397. # Don't use KERNEL_VERSION because the headers it grabs the version from aren't generated until do_compile
  398. VERSION=$(grep "^VERSION =" ${S}/Makefile | sed s/.*=\ *//)
  399. PATCHLEVEL=$(grep "^PATCHLEVEL =" ${S}/Makefile | sed s/.*=\ *//)
  400. SUBLEVEL=$(grep "^SUBLEVEL =" ${S}/Makefile | sed s/.*=\ *//)
  401. EXTRAVERSION=$(grep "^EXTRAVERSION =" ${S}/Makefile | sed s/.*=\ *//)
  402. # Build a string for regex and a plain version string
  403. reg="^${VERSION}\.${PATCHLEVEL}"
  404. vers="${VERSION}.${PATCHLEVEL}"
  405. if [ -n "${SUBLEVEL}" ]; then
  406. # Ignoring a SUBLEVEL of zero is fine
  407. if [ "${SUBLEVEL}" = "0" ]; then
  408. reg="${reg}(\.${SUBLEVEL})?"
  409. else
  410. reg="${reg}\.${SUBLEVEL}"
  411. vers="${vers}.${SUBLEVEL}"
  412. fi
  413. fi
  414. vers="${vers}${EXTRAVERSION}"
  415. reg="${reg}${EXTRAVERSION}"
  416. if [ -z `echo ${PV} | grep -E "${reg}"` ]; then
  417. bbfatal "Package Version (${PV}) does not match of kernel being built (${vers}). Please update the PV variable to match the kernel source or set KERNEL_VERSION_SANITY_SKIP=\"1\" in your recipe."
  418. fi
  419. exit 0
  420. }
  421. addtask shared_workdir after do_compile before do_compile_kernelmodules
  422. addtask shared_workdir_setscene
  423. do_shared_workdir_setscene () {
  424. exit 1
  425. }
  426. emit_depmod_pkgdata() {
  427. # Stash data for depmod
  428. install -d ${PKGDESTWORK}/${KERNEL_PACKAGE_NAME}-depmod/
  429. echo "${KERNEL_VERSION}" > ${PKGDESTWORK}/${KERNEL_PACKAGE_NAME}-depmod/${KERNEL_PACKAGE_NAME}-abiversion
  430. cp ${B}/System.map ${PKGDESTWORK}/${KERNEL_PACKAGE_NAME}-depmod/System.map-${KERNEL_VERSION}
  431. }
  432. PACKAGEFUNCS += "emit_depmod_pkgdata"
  433. do_shared_workdir[cleandirs] += " ${STAGING_KERNEL_BUILDDIR}"
  434. do_shared_workdir () {
  435. cd ${B}
  436. kerneldir=${STAGING_KERNEL_BUILDDIR}
  437. install -d $kerneldir
  438. #
  439. # Store the kernel version in sysroots for module-base.bbclass
  440. #
  441. echo "${KERNEL_VERSION}" > $kerneldir/${KERNEL_PACKAGE_NAME}-abiversion
  442. # Copy files required for module builds
  443. cp System.map $kerneldir/System.map-${KERNEL_VERSION}
  444. [ -e Module.symvers ] && cp Module.symvers $kerneldir/
  445. cp .config $kerneldir/
  446. mkdir -p $kerneldir/include/config
  447. cp include/config/kernel.release $kerneldir/include/config/kernel.release
  448. if [ -e certs/signing_key.x509 ]; then
  449. # The signing_key.* files are stored in the certs/ dir in
  450. # newer Linux kernels
  451. mkdir -p $kerneldir/certs
  452. cp certs/signing_key.* $kerneldir/certs/
  453. elif [ -e signing_key.priv ]; then
  454. cp signing_key.* $kerneldir/
  455. fi
  456. # We can also copy over all the generated files and avoid special cases
  457. # like version.h, but we've opted to keep this small until file creep starts
  458. # to happen
  459. if [ -e include/linux/version.h ]; then
  460. mkdir -p $kerneldir/include/linux
  461. cp include/linux/version.h $kerneldir/include/linux/version.h
  462. fi
  463. # As of Linux kernel version 3.0.1, the clean target removes
  464. # arch/powerpc/lib/crtsavres.o which is present in
  465. # KBUILD_LDFLAGS_MODULE, making it required to build external modules.
  466. if [ ${ARCH} = "powerpc" ]; then
  467. if [ -e arch/powerpc/lib/crtsavres.o ]; then
  468. mkdir -p $kerneldir/arch/powerpc/lib/
  469. cp arch/powerpc/lib/crtsavres.o $kerneldir/arch/powerpc/lib/crtsavres.o
  470. fi
  471. fi
  472. if [ -d include/generated ]; then
  473. mkdir -p $kerneldir/include/generated/
  474. cp -fR include/generated/* $kerneldir/include/generated/
  475. fi
  476. if [ -d arch/${ARCH}/include/generated ]; then
  477. mkdir -p $kerneldir/arch/${ARCH}/include/generated/
  478. cp -fR arch/${ARCH}/include/generated/* $kerneldir/arch/${ARCH}/include/generated/
  479. fi
  480. if (grep -q -i -e '^CONFIG_UNWINDER_ORC=y$' $kerneldir/.config); then
  481. # With CONFIG_UNWINDER_ORC (the default in 4.14), objtool is required for
  482. # out-of-tree modules to be able to generate object files.
  483. if [ -x tools/objtool/objtool ]; then
  484. mkdir -p ${kerneldir}/tools/objtool
  485. cp tools/objtool/objtool ${kerneldir}/tools/objtool/
  486. fi
  487. fi
  488. }
  489. # We don't need to stage anything, not the modules/firmware since those would clash with linux-firmware
  490. sysroot_stage_all () {
  491. :
  492. }
  493. KERNEL_CONFIG_COMMAND ?= "oe_runmake_call -C ${S} CC="${KERNEL_CC}" LD="${KERNEL_LD}" O=${B} olddefconfig || oe_runmake -C ${S} O=${B} CC="${KERNEL_CC}" LD="${KERNEL_LD}" oldnoconfig"
  494. python check_oldest_kernel() {
  495. oldest_kernel = d.getVar('OLDEST_KERNEL')
  496. kernel_version = d.getVar('KERNEL_VERSION')
  497. tclibc = d.getVar('TCLIBC')
  498. if tclibc == 'glibc':
  499. kernel_version = kernel_version.split('-', 1)[0]
  500. if oldest_kernel and kernel_version:
  501. if bb.utils.vercmp_string(kernel_version, oldest_kernel) < 0:
  502. bb.warn('%s: OLDEST_KERNEL is "%s" but the version of the kernel you are building is "%s" - therefore %s as built may not be compatible with this kernel. Either set OLDEST_KERNEL to an older version, or build a newer kernel.' % (d.getVar('PN'), oldest_kernel, kernel_version, tclibc))
  503. }
  504. check_oldest_kernel[vardepsexclude] += "OLDEST_KERNEL KERNEL_VERSION"
  505. do_configure[prefuncs] += "check_oldest_kernel"
  506. kernel_do_configure() {
  507. # fixes extra + in /lib/modules/2.6.37+
  508. # $ scripts/setlocalversion . => +
  509. # $ make kernelversion => 2.6.37
  510. # $ make kernelrelease => 2.6.37+
  511. touch ${B}/.scmversion ${S}/.scmversion
  512. if [ "${S}" != "${B}" ] && [ -f "${S}/.config" ] && [ ! -f "${B}/.config" ]; then
  513. mv "${S}/.config" "${B}/.config"
  514. fi
  515. # Copy defconfig to .config if .config does not exist. This allows
  516. # recipes to manage the .config themselves in do_configure_prepend().
  517. if [ -f "${WORKDIR}/defconfig" ] && [ ! -f "${B}/.config" ]; then
  518. cp "${WORKDIR}/defconfig" "${B}/.config"
  519. fi
  520. ${KERNEL_CONFIG_COMMAND}
  521. }
  522. do_savedefconfig() {
  523. bbplain "Saving defconfig to:\n${B}/defconfig"
  524. oe_runmake -C ${B} savedefconfig
  525. }
  526. do_savedefconfig[nostamp] = "1"
  527. addtask savedefconfig after do_configure
  528. inherit cml1
  529. KCONFIG_CONFIG_COMMAND_append = " LD='${KERNEL_LD}' HOSTLDFLAGS='${BUILD_LDFLAGS}'"
  530. EXPORT_FUNCTIONS do_compile do_install do_configure
  531. # kernel-base becomes kernel-${KERNEL_VERSION}
  532. # kernel-image becomes kernel-image-${KERNEL_VERSION}
  533. PACKAGES = "${KERNEL_PACKAGE_NAME} ${KERNEL_PACKAGE_NAME}-base ${KERNEL_PACKAGE_NAME}-vmlinux ${KERNEL_PACKAGE_NAME}-image ${KERNEL_PACKAGE_NAME}-dev ${KERNEL_PACKAGE_NAME}-modules"
  534. FILES_${PN} = ""
  535. FILES_${KERNEL_PACKAGE_NAME}-base = "${nonarch_base_libdir}/modules/${KERNEL_VERSION}/modules.order ${nonarch_base_libdir}/modules/${KERNEL_VERSION}/modules.builtin ${nonarch_base_libdir}/modules/${KERNEL_VERSION}/modules.builtin.modinfo"
  536. FILES_${KERNEL_PACKAGE_NAME}-image = ""
  537. FILES_${KERNEL_PACKAGE_NAME}-dev = "/boot/System.map* /boot/Module.symvers* /boot/config* ${KERNEL_SRC_PATH} ${nonarch_base_libdir}/modules/${KERNEL_VERSION}/build"
  538. FILES_${KERNEL_PACKAGE_NAME}-vmlinux = "/boot/vmlinux-${KERNEL_VERSION_NAME}"
  539. FILES_${KERNEL_PACKAGE_NAME}-modules = ""
  540. RDEPENDS_${KERNEL_PACKAGE_NAME} = "${KERNEL_PACKAGE_NAME}-base"
  541. # Allow machines to override this dependency if kernel image files are
  542. # not wanted in images as standard
  543. RDEPENDS_${KERNEL_PACKAGE_NAME}-base ?= "${KERNEL_PACKAGE_NAME}-image"
  544. PKG_${KERNEL_PACKAGE_NAME}-image = "${KERNEL_PACKAGE_NAME}-image-${@legitimize_package_name(d.getVar('KERNEL_VERSION'))}"
  545. RDEPENDS_${KERNEL_PACKAGE_NAME}-image += "${@oe.utils.conditional('KERNEL_IMAGETYPE', 'vmlinux', '${KERNEL_PACKAGE_NAME}-vmlinux', '', d)}"
  546. PKG_${KERNEL_PACKAGE_NAME}-base = "${KERNEL_PACKAGE_NAME}-${@legitimize_package_name(d.getVar('KERNEL_VERSION'))}"
  547. RPROVIDES_${KERNEL_PACKAGE_NAME}-base += "${KERNEL_PACKAGE_NAME}-${KERNEL_VERSION}"
  548. ALLOW_EMPTY_${KERNEL_PACKAGE_NAME} = "1"
  549. ALLOW_EMPTY_${KERNEL_PACKAGE_NAME}-base = "1"
  550. ALLOW_EMPTY_${KERNEL_PACKAGE_NAME}-image = "1"
  551. ALLOW_EMPTY_${KERNEL_PACKAGE_NAME}-modules = "1"
  552. DESCRIPTION_${KERNEL_PACKAGE_NAME}-modules = "Kernel modules meta package"
  553. pkg_postinst_${KERNEL_PACKAGE_NAME}-base () {
  554. if [ ! -e "$D/lib/modules/${KERNEL_VERSION}" ]; then
  555. mkdir -p $D/lib/modules/${KERNEL_VERSION}
  556. fi
  557. if [ -n "$D" ]; then
  558. depmodwrapper -a -b $D ${KERNEL_VERSION}
  559. else
  560. depmod -a ${KERNEL_VERSION}
  561. fi
  562. }
  563. PACKAGESPLITFUNCS_prepend = "split_kernel_packages "
  564. python split_kernel_packages () {
  565. do_split_packages(d, root='${nonarch_base_libdir}/firmware', file_regex=r'^(.*)\.(bin|fw|cis|csp|dsp)$', output_pattern='${KERNEL_PACKAGE_NAME}-firmware-%s', description='Firmware for %s', recursive=True, extra_depends='')
  566. }
  567. # Many scripts want to look in arch/$arch/boot for the bootable
  568. # image. This poses a problem for vmlinux and vmlinuz based
  569. # booting. This task arranges to have vmlinux and vmlinuz appear
  570. # in the normalized directory location.
  571. do_kernel_link_images() {
  572. if [ ! -d "${B}/arch/${ARCH}/boot" ]; then
  573. mkdir ${B}/arch/${ARCH}/boot
  574. fi
  575. cd ${B}/arch/${ARCH}/boot
  576. ln -sf ../../../vmlinux
  577. if [ -f ../../../vmlinuz ]; then
  578. ln -sf ../../../vmlinuz
  579. fi
  580. if [ -f ../../../vmlinuz.bin ]; then
  581. ln -sf ../../../vmlinuz.bin
  582. fi
  583. if [ -f ../../../vmlinux.64 ]; then
  584. ln -sf ../../../vmlinux.64
  585. fi
  586. }
  587. addtask kernel_link_images after do_compile before do_strip
  588. do_strip() {
  589. if [ -n "${KERNEL_IMAGE_STRIP_EXTRA_SECTIONS}" ]; then
  590. if ! (echo "${KERNEL_IMAGETYPES}" | grep -wq "vmlinux"); then
  591. bbwarn "image type(s) will not be stripped (not supported): ${KERNEL_IMAGETYPES}"
  592. return
  593. fi
  594. cd ${B}
  595. headers=`"$CROSS_COMPILE"readelf -S ${KERNEL_OUTPUT_DIR}/vmlinux | \
  596. grep "^ \{1,\}\[[0-9 ]\{1,\}\] [^ ]" | \
  597. sed "s/^ \{1,\}\[[0-9 ]\{1,\}\] //" | \
  598. gawk '{print $1}'`
  599. for str in ${KERNEL_IMAGE_STRIP_EXTRA_SECTIONS}; do {
  600. if ! (echo "$headers" | grep -q "^$str$"); then
  601. bbwarn "Section not found: $str";
  602. fi
  603. "$CROSS_COMPILE"strip -s -R $str ${KERNEL_OUTPUT_DIR}/vmlinux
  604. }; done
  605. bbnote "KERNEL_IMAGE_STRIP_EXTRA_SECTIONS is set, stripping sections:" \
  606. "${KERNEL_IMAGE_STRIP_EXTRA_SECTIONS}"
  607. fi;
  608. }
  609. do_strip[dirs] = "${B}"
  610. addtask strip before do_sizecheck after do_kernel_link_images
  611. # Support checking the kernel size since some kernels need to reside in partitions
  612. # with a fixed length or there is a limit in transferring the kernel to memory.
  613. # If more than one image type is enabled, warn on any that don't fit but only fail
  614. # if none fit.
  615. do_sizecheck() {
  616. if [ ! -z "${KERNEL_IMAGE_MAXSIZE}" ]; then
  617. invalid=`echo ${KERNEL_IMAGE_MAXSIZE} | sed 's/[0-9]//g'`
  618. if [ -n "$invalid" ]; then
  619. die "Invalid KERNEL_IMAGE_MAXSIZE: ${KERNEL_IMAGE_MAXSIZE}, should be an integer (The unit is Kbytes)"
  620. fi
  621. at_least_one_fits=
  622. for imageType in ${KERNEL_IMAGETYPES} ; do
  623. size=`du -ks ${B}/${KERNEL_OUTPUT_DIR}/$imageType | awk '{print $1}'`
  624. if [ $size -gt ${KERNEL_IMAGE_MAXSIZE} ]; then
  625. bbwarn "This kernel $imageType (size=$size(K) > ${KERNEL_IMAGE_MAXSIZE}(K)) is too big for your device."
  626. else
  627. at_least_one_fits=y
  628. fi
  629. done
  630. if [ -z "$at_least_one_fits" ]; then
  631. die "All kernel images are too big for your device. Please reduce the size of the kernel by making more of it modular."
  632. fi
  633. fi
  634. }
  635. do_sizecheck[dirs] = "${B}"
  636. addtask sizecheck before do_install after do_strip
  637. inherit kernel-artifact-names
  638. kernel_do_deploy() {
  639. deployDir="${DEPLOYDIR}"
  640. if [ -n "${KERNEL_DEPLOYSUBDIR}" ]; then
  641. deployDir="${DEPLOYDIR}/${KERNEL_DEPLOYSUBDIR}"
  642. mkdir "$deployDir"
  643. fi
  644. for imageType in ${KERNEL_IMAGETYPES} ; do
  645. baseName=$imageType-${KERNEL_IMAGE_NAME}
  646. install -m 0644 ${KERNEL_OUTPUT_DIR}/$imageType $deployDir/$baseName.bin
  647. ln -sf $baseName.bin $deployDir/$imageType-${KERNEL_IMAGE_LINK_NAME}.bin
  648. ln -sf $baseName.bin $deployDir/$imageType
  649. done
  650. if [ ${MODULE_TARBALL_DEPLOY} = "1" ] && (grep -q -i -e '^CONFIG_MODULES=y$' .config); then
  651. mkdir -p ${D}${root_prefix}/lib
  652. if [ -n "${SOURCE_DATE_EPOCH}" ]; then
  653. TAR_ARGS="--sort=name --clamp-mtime --mtime=@${SOURCE_DATE_EPOCH}"
  654. else
  655. TAR_ARGS=""
  656. fi
  657. TAR_ARGS="$TAR_ARGS --owner=0 --group=0"
  658. tar $TAR_ARGS -cv -C ${D}${root_prefix} lib | gzip -9n > $deployDir/modules-${MODULE_TARBALL_NAME}.tgz
  659. ln -sf modules-${MODULE_TARBALL_NAME}.tgz $deployDir/modules-${MODULE_TARBALL_LINK_NAME}.tgz
  660. fi
  661. if [ ! -z "${INITRAMFS_IMAGE}" -a x"${INITRAMFS_IMAGE_BUNDLE}" = x1 ]; then
  662. for imageType in ${KERNEL_IMAGETYPE_FOR_MAKE} ; do
  663. if [ "$imageType" = "fitImage" ] ; then
  664. continue
  665. fi
  666. initramfsBaseName=$imageType-${INITRAMFS_NAME}
  667. install -m 0644 ${KERNEL_OUTPUT_DIR}/$imageType.initramfs $deployDir/$initramfsBaseName.bin
  668. ln -sf $initramfsBaseName.bin $deployDir/$imageType-${INITRAMFS_LINK_NAME}.bin
  669. done
  670. fi
  671. }
  672. # We deploy to filenames that include PKGV and PKGR, read the saved data to
  673. # ensure we get the right values for both
  674. do_deploy[prefuncs] += "read_subpackage_metadata"
  675. addtask deploy after do_populate_sysroot do_packagedata
  676. EXPORT_FUNCTIONS do_deploy
  677. # Add using Device Tree support
  678. inherit kernel-devicetree