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