blob: 5d2f17c3be2ef4d6fd300fcb92f61a8fde7e8cf7 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001inherit linux-kernel-base kernel-module-split
2
Andrew Geisslerd1e89492021-02-12 15:35:20 -06003COMPATIBLE_HOST = ".*-linux"
4
Brad Bishop316dfdd2018-06-25 12:45:53 -04005KERNEL_PACKAGE_NAME ??= "kernel"
6KERNEL_DEPLOYSUBDIR ??= "${@ "" if (d.getVar("KERNEL_PACKAGE_NAME") == "kernel") else d.getVar("KERNEL_PACKAGE_NAME") }"
7
Andrew Geissler595f6302022-01-24 19:11:47 +00008PROVIDES += "virtual/kernel"
Andrew Geissler475cb722020-07-10 16:00:51 -05009DEPENDS += "virtual/${TARGET_PREFIX}binutils virtual/${TARGET_PREFIX}gcc kmod-native bc-native bison-native"
10DEPENDS += "${@bb.utils.contains("INITRAMFS_FSTYPES", "cpio.lzo", "lzop-native", "", d)}"
11DEPENDS += "${@bb.utils.contains("INITRAMFS_FSTYPES", "cpio.lz4", "lz4-native", "", d)}"
Andrew Geisslerd159c7f2021-09-02 21:05:58 -050012DEPENDS += "${@bb.utils.contains("INITRAMFS_FSTYPES", "cpio.zst", "zstd-native", "", d)}"
Brad Bishopd7bf8c12018-02-25 22:55:05 -050013PACKAGE_WRITE_DEPS += "depmodwrapper-cross"
Brad Bishop6e60e8b2018-02-01 10:27:11 -050014
Andrew Geissler475cb722020-07-10 16:00:51 -050015do_deploy[depends] += "depmodwrapper-cross:do_populate_sysroot gzip-native:do_populate_sysroot"
Brad Bishop15ae2502019-06-18 21:44:24 -040016do_clean[depends] += "make-mod-scripts:do_clean"
Brad Bishop6e60e8b2018-02-01 10:27:11 -050017
18CVE_PRODUCT ?= "linux_kernel"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050019
20S = "${STAGING_KERNEL_DIR}"
21B = "${WORKDIR}/build"
22KBUILD_OUTPUT = "${B}"
23OE_TERMINAL_EXPORTS += "KBUILD_OUTPUT"
24
25# we include gcc above, we dont need virtual/libc
26INHIBIT_DEFAULT_DEPS = "1"
27
28KERNEL_IMAGETYPE ?= "zImage"
29INITRAMFS_IMAGE ?= ""
Brad Bishop6e60e8b2018-02-01 10:27:11 -050030INITRAMFS_IMAGE_NAME ?= "${@['${INITRAMFS_IMAGE}-${MACHINE}', ''][d.getVar('INITRAMFS_IMAGE') == '']}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050031INITRAMFS_TASK ?= ""
32INITRAMFS_IMAGE_BUNDLE ?= ""
Andrew Geissler7e0e3c02022-02-25 20:34:39 +000033INITRAMFS_DEPLOY_DIR_IMAGE ?= "${DEPLOY_DIR_IMAGE}"
34INITRAMFS_MULTICONFIG ?= ""
Patrick Williamsc124f4f2015-09-15 14:41:29 -050035
He Zhefe76b1e2016-05-25 04:47:16 -040036# KERNEL_VERSION is extracted from source code. It is evaluated as
37# None for the first parsing, since the code has not been fetched.
38# After the code is fetched, it will be evaluated as real version
39# number and cause kernel to be rebuilt. To avoid this, make
40# KERNEL_VERSION_NAME and KERNEL_VERSION_PKG_NAME depend on
41# LINUX_VERSION which is a constant.
Brad Bishop6e60e8b2018-02-01 10:27:11 -050042KERNEL_VERSION_NAME = "${@d.getVar('KERNEL_VERSION') or ""}"
He Zhefe76b1e2016-05-25 04:47:16 -040043KERNEL_VERSION_NAME[vardepvalue] = "${LINUX_VERSION}"
Brad Bishop6e60e8b2018-02-01 10:27:11 -050044KERNEL_VERSION_PKG_NAME = "${@legitimize_package_name(d.getVar('KERNEL_VERSION'))}"
He Zhefe76b1e2016-05-25 04:47:16 -040045KERNEL_VERSION_PKG_NAME[vardepvalue] = "${LINUX_VERSION}"
46
Patrick Williamsc124f4f2015-09-15 14:41:29 -050047python __anonymous () {
Brad Bishop316dfdd2018-06-25 12:45:53 -040048 pn = d.getVar("PN")
49 kpn = d.getVar("KERNEL_PACKAGE_NAME")
50
51 # XXX Remove this after bug 11905 is resolved
Patrick Williams213cb262021-08-07 19:21:33 -050052 # FILES:${KERNEL_PACKAGE_NAME}-dev doesn't expand correctly
Brad Bishop316dfdd2018-06-25 12:45:53 -040053 if kpn == pn:
54 bb.warn("Some packages (E.g. *-dev) might be missing due to "
55 "bug 11905 (variable KERNEL_PACKAGE_NAME == PN)")
56
57 # The default kernel recipe builds in a shared location defined by
58 # bitbake/distro confs: STAGING_KERNEL_DIR and STAGING_KERNEL_BUILDDIR.
59 # Set these variables to directories under ${WORKDIR} in alternate
60 # kernel recipes (I.e. where KERNEL_PACKAGE_NAME != kernel) so that they
61 # may build in parallel with the default kernel without clobbering.
62 if kpn != "kernel":
63 workdir = d.getVar("WORKDIR")
64 sourceDir = os.path.join(workdir, 'kernel-source')
65 artifactsDir = os.path.join(workdir, 'kernel-build-artifacts')
66 d.setVar("STAGING_KERNEL_DIR", sourceDir)
67 d.setVar("STAGING_KERNEL_BUILDDIR", artifactsDir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050068
He Zhefe76b1e2016-05-25 04:47:16 -040069 # Merge KERNEL_IMAGETYPE and KERNEL_ALT_IMAGETYPE into KERNEL_IMAGETYPES
Brad Bishop6e60e8b2018-02-01 10:27:11 -050070 type = d.getVar('KERNEL_IMAGETYPE') or ""
71 alttype = d.getVar('KERNEL_ALT_IMAGETYPE') or ""
72 types = d.getVar('KERNEL_IMAGETYPES') or ""
He Zhefe76b1e2016-05-25 04:47:16 -040073 if type not in types.split():
74 types = (type + ' ' + types).strip()
75 if alttype not in types.split():
76 types = (alttype + ' ' + types).strip()
77 d.setVar('KERNEL_IMAGETYPES', types)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050078
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080079 # KERNEL_IMAGETYPES may contain a mixture of image types supported directly
80 # by the kernel build system and types which are created by post-processing
81 # the output of the kernel build system (e.g. compressing vmlinux ->
Andrew Geissler595f6302022-01-24 19:11:47 +000082 # vmlinux.gz in kernel_do_transform_kernel()).
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080083 # KERNEL_IMAGETYPE_FOR_MAKE should contain only image types supported
84 # directly by the kernel build system.
85 if not d.getVar('KERNEL_IMAGETYPE_FOR_MAKE'):
86 typeformake = set()
87 for type in types.split():
88 if type == 'vmlinux.gz':
89 type = 'vmlinux'
90 typeformake.add(type)
91
92 d.setVar('KERNEL_IMAGETYPE_FOR_MAKE', ' '.join(sorted(typeformake)))
93
94 kname = d.getVar('KERNEL_PACKAGE_NAME') or "kernel"
95 imagedest = d.getVar('KERNEL_IMAGEDEST')
He Zhefe76b1e2016-05-25 04:47:16 -040096
Brad Bishop37a0e4d2017-12-04 01:01:44 -050097 for type in types.split():
Andrew Geissler5f350902021-07-23 13:09:54 -040098 if bb.data.inherits_class('nopackages', d):
99 continue
He Zhefe76b1e2016-05-25 04:47:16 -0400100 typelower = type.lower()
Brad Bishop316dfdd2018-06-25 12:45:53 -0400101 d.appendVar('PACKAGES', ' %s-image-%s' % (kname, typelower))
Patrick Williams213cb262021-08-07 19:21:33 -0500102 d.setVar('FILES:' + kname + '-image-' + typelower, '/' + imagedest + '/' + type + '-${KERNEL_VERSION_NAME}' + ' /' + imagedest + '/' + type)
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500103 d.appendVar('RDEPENDS:%s-image' % kname, ' %s-image-%s (= ${EXTENDPKGV})' % (kname, typelower))
104 splitmods = d.getVar("KERNEL_SPLIT_MODULES")
105 if splitmods != '1':
106 d.appendVar('RDEPENDS:%s-image' % kname, ' %s-modules (= ${EXTENDPKGV})' % kname)
107 d.appendVar('RDEPENDS:%s-image-%s' % (kname, typelower), ' %s-modules-${KERNEL_VERSION_PKG_NAME} (= ${EXTENDPKGV})' % kname)
108 d.setVar('PKG:%s-modules' % kname, '%s-modules-${KERNEL_VERSION_PKG_NAME}' % kname)
109 d.appendVar('RPROVIDES:%s-modules' % kname, '%s-modules-${KERNEL_VERSION_PKG_NAME}' % kname)
110
Patrick Williams213cb262021-08-07 19:21:33 -0500111 d.setVar('PKG:%s-image-%s' % (kname,typelower), '%s-image-%s-${KERNEL_VERSION_PKG_NAME}' % (kname, typelower))
112 d.setVar('ALLOW_EMPTY:%s-image-%s' % (kname, typelower), '1')
Andrew Geisslerd5838332022-05-27 11:33:10 -0500113 d.prependVar('pkg_postinst:%s-image-%s' % (kname,typelower), """set +e
Andrew Geissler475cb722020-07-10 16:00:51 -0500114if [ -n "$D" ]; then
115 ln -sf %s-${KERNEL_VERSION} $D/${KERNEL_IMAGEDEST}/%s > /dev/null 2>&1
116else
117 ln -sf %s-${KERNEL_VERSION} ${KERNEL_IMAGEDEST}/%s > /dev/null 2>&1
118 if [ $? -ne 0 ]; then
119 echo "Filesystem on ${KERNEL_IMAGEDEST}/ doesn't support symlinks, falling back to copied image (%s)."
120 install -m 0644 ${KERNEL_IMAGEDEST}/%s-${KERNEL_VERSION} ${KERNEL_IMAGEDEST}/%s
121 fi
122fi
123set -e
124""" % (type, type, type, type, type, type, type))
Patrick Williams213cb262021-08-07 19:21:33 -0500125 d.setVar('pkg_postrm:%s-image-%s' % (kname,typelower), """set +e
Andrew Geissler475cb722020-07-10 16:00:51 -0500126if [ -f "${KERNEL_IMAGEDEST}/%s" -o -L "${KERNEL_IMAGEDEST}/%s" ]; then
127 rm -f ${KERNEL_IMAGEDEST}/%s > /dev/null 2>&1
128fi
129set -e
130""" % (type, type, type))
131
He Zhefe76b1e2016-05-25 04:47:16 -0400132
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500133 image = d.getVar('INITRAMFS_IMAGE')
Brad Bishop79641f22019-09-10 07:20:22 -0400134 # If the INTIRAMFS_IMAGE is set but the INITRAMFS_IMAGE_BUNDLE is set to 0,
135 # the do_bundle_initramfs does nothing, but the INITRAMFS_IMAGE is built
136 # standalone for use by wic and other tools.
137 if image:
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000138 if d.getVar('INITRAMFS_MULTICONFIG'):
139 d.appendVarFlag('do_bundle_initramfs', 'mcdepends', ' mc::${INITRAMFS_MULTICONFIG}:${INITRAMFS_IMAGE}:do_image_complete')
140 else:
141 d.appendVarFlag('do_bundle_initramfs', 'depends', ' ${INITRAMFS_IMAGE}:do_image_complete')
Andrew Geissler595f6302022-01-24 19:11:47 +0000142 if image and bb.utils.to_boolean(d.getVar('INITRAMFS_IMAGE_BUNDLE')):
143 bb.build.addtask('do_transform_bundled_initramfs', 'do_deploy', 'do_bundle_initramfs', d)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500144
145 # NOTE: setting INITRAMFS_TASK is for backward compatibility
146 # The preferred method is to set INITRAMFS_IMAGE, because
147 # this INITRAMFS_TASK has circular dependency problems
148 # if the initramfs requires kernel modules
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500149 image_task = d.getVar('INITRAMFS_TASK')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500150 if image_task:
151 d.appendVarFlag('do_configure', 'depends', ' ${INITRAMFS_TASK}')
152}
153
154# Here we pull in all various kernel image types which we support.
155#
156# In case you're wondering why kernel.bbclass inherits the other image
157# types instead of the other way around, the reason for that is to
158# maintain compatibility with various currently existing meta-layers.
159# By pulling in the various kernel image types here, we retain the
160# original behavior of kernel.bbclass, so no meta-layers should get
161# broken.
162#
163# KERNEL_CLASSES by default pulls in kernel-uimage.bbclass, since this
164# used to be the default behavior when only uImage was supported. This
165# variable can be appended by users who implement support for new kernel
166# image types.
167
168KERNEL_CLASSES ?= " kernel-uimage "
169inherit ${KERNEL_CLASSES}
170
171# Old style kernels may set ${S} = ${WORKDIR}/git for example
172# We need to move these over to STAGING_KERNEL_DIR. We can't just
173# create the symlink in advance as the git fetcher can't cope with
174# the symlink.
175do_unpack[cleandirs] += " ${S} ${STAGING_KERNEL_DIR} ${B} ${STAGING_KERNEL_BUILDDIR}"
176do_clean[cleandirs] += " ${S} ${STAGING_KERNEL_DIR} ${B} ${STAGING_KERNEL_BUILDDIR}"
Brad Bishopc342db32019-05-15 21:57:59 -0400177python do_symlink_kernsrc () {
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500178 s = d.getVar("S")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500179 if s[-1] == '/':
180 # drop trailing slash, so that os.symlink(kernsrc, s) doesn't use s as directory name and fail
181 s=s[:-1]
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500182 kernsrc = d.getVar("STAGING_KERNEL_DIR")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500183 if s != kernsrc:
184 bb.utils.mkdirhier(kernsrc)
185 bb.utils.remove(kernsrc, recurse=True)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500186 if d.getVar("EXTERNALSRC"):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500187 # With EXTERNALSRC S will not be wiped so we can symlink to it
188 os.symlink(s, kernsrc)
189 else:
190 import shutil
191 shutil.move(s, kernsrc)
192 os.symlink(kernsrc, s)
193}
Andrew Geisslerc3d88e42020-10-02 09:45:00 -0500194# do_patch is normally ordered before do_configure, but
195# externalsrc.bbclass deletes do_patch, breaking the dependency of
196# do_configure on do_symlink_kernsrc.
197addtask symlink_kernsrc before do_patch do_configure after do_unpack
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500198
199inherit kernel-arch deploy
200
Brad Bishop316dfdd2018-06-25 12:45:53 -0400201PACKAGES_DYNAMIC += "^${KERNEL_PACKAGE_NAME}-module-.*"
202PACKAGES_DYNAMIC += "^${KERNEL_PACKAGE_NAME}-image-.*"
203PACKAGES_DYNAMIC += "^${KERNEL_PACKAGE_NAME}-firmware-.*"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500204
205export OS = "${TARGET_OS}"
206export CROSS_COMPILE = "${TARGET_PREFIX}"
Brad Bishop316dfdd2018-06-25 12:45:53 -0400207export KBUILD_BUILD_VERSION = "1"
Brad Bishop977dc1a2019-02-06 16:01:43 -0500208export KBUILD_BUILD_USER ?= "oe-user"
209export KBUILD_BUILD_HOST ?= "oe-host"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500210
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500211KERNEL_RELEASE ?= "${KERNEL_VERSION}"
212
He Zhefe76b1e2016-05-25 04:47:16 -0400213# The directory where built kernel lies in the kernel tree
214KERNEL_OUTPUT_DIR ?= "arch/${ARCH}/boot"
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800215KERNEL_IMAGEDEST ?= "boot"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500216
217#
218# configuration
219#
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500220export CMDLINE_CONSOLE = "console=${@d.getVar("KERNEL_CONSOLE") or "ttyS0"}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500221
222KERNEL_VERSION = "${@get_kernelversion_headers('${B}')}"
223
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500224# kernels are generally machine specific
225PACKAGE_ARCH = "${MACHINE_ARCH}"
226
227# U-Boot support
228UBOOT_ENTRYPOINT ?= "20008000"
229UBOOT_LOADADDRESS ?= "${UBOOT_ENTRYPOINT}"
230
231# Some Linux kernel configurations need additional parameters on the command line
232KERNEL_EXTRA_ARGS ?= ""
233
Andrew Geisslereff27472021-10-29 15:35:00 -0500234EXTRA_OEMAKE = " HOSTCC="${BUILD_CC}" HOSTCFLAGS="${BUILD_CFLAGS}" HOSTLDFLAGS="${BUILD_LDFLAGS}" HOSTCPP="${BUILD_CPP}""
Andrew Geissler9aee5002022-03-30 16:27:02 +0000235EXTRA_OEMAKE += " HOSTCXX="${BUILD_CXX}" HOSTCXXFLAGS="${BUILD_CXXFLAGS}" PAHOLE=false"
Andrew Geissler635e0e42020-08-21 15:58:33 -0500236
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500237KERNEL_ALT_IMAGETYPE ??= ""
238
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500239copy_initramfs() {
240 echo "Copying initramfs into ./usr ..."
241 # In case the directory is not created yet from the first pass compile:
242 mkdir -p ${B}/usr
243 # Find and use the first initramfs image archive type we find
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500244 rm -f ${B}/usr/${INITRAMFS_IMAGE_NAME}.cpio
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500245 for img in cpio cpio.gz cpio.lz4 cpio.lzo cpio.lzma cpio.xz cpio.zst; do
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000246 if [ -e "${INITRAMFS_DEPLOY_DIR_IMAGE}/${INITRAMFS_IMAGE_NAME}.$img" ]; then
247 cp ${INITRAMFS_DEPLOY_DIR_IMAGE}/${INITRAMFS_IMAGE_NAME}.$img ${B}/usr/.
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500248 case $img in
249 *gz)
250 echo "gzip decompressing image"
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500251 gunzip -f ${B}/usr/${INITRAMFS_IMAGE_NAME}.$img
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500252 break
253 ;;
254 *lz4)
255 echo "lz4 decompressing image"
Andrew Geissler475cb722020-07-10 16:00:51 -0500256 lz4 -df ${B}/usr/${INITRAMFS_IMAGE_NAME}.$img ${B}/usr/${INITRAMFS_IMAGE_NAME}.cpio
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500257 break
258 ;;
259 *lzo)
260 echo "lzo decompressing image"
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500261 lzop -df ${B}/usr/${INITRAMFS_IMAGE_NAME}.$img
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500262 break
263 ;;
264 *lzma)
265 echo "lzma decompressing image"
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500266 lzma -df ${B}/usr/${INITRAMFS_IMAGE_NAME}.$img
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500267 break
268 ;;
269 *xz)
270 echo "xz decompressing image"
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500271 xz -df ${B}/usr/${INITRAMFS_IMAGE_NAME}.$img
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500272 break
273 ;;
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500274 *zst)
275 echo "zst decompressing image"
276 zstd -df ${B}/usr/${INITRAMFS_IMAGE_NAME}.$img
277 break
278 ;;
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500279 esac
Brad Bishopf8caae32019-03-25 13:13:56 -0400280 break
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500281 fi
282 done
Brad Bishopf8caae32019-03-25 13:13:56 -0400283 # Verify that the above loop found a initramfs, fail otherwise
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000284 [ -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."
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500285}
286
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500287do_bundle_initramfs () {
288 if [ ! -z "${INITRAMFS_IMAGE}" -a x"${INITRAMFS_IMAGE_BUNDLE}" = x1 ]; then
289 echo "Creating a kernel image with a bundled initramfs..."
290 copy_initramfs
He Zhefe76b1e2016-05-25 04:47:16 -0400291 # Backing up kernel image relies on its type(regular file or symbolic link)
292 tmp_path=""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800293 for imageType in ${KERNEL_IMAGETYPE_FOR_MAKE} ; do
294 if [ -h ${KERNEL_OUTPUT_DIR}/$imageType ] ; then
295 linkpath=`readlink -n ${KERNEL_OUTPUT_DIR}/$imageType`
296 realpath=`readlink -fn ${KERNEL_OUTPUT_DIR}/$imageType`
He Zhefe76b1e2016-05-25 04:47:16 -0400297 mv -f $realpath $realpath.bak
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800298 tmp_path=$tmp_path" "$imageType"#"$linkpath"#"$realpath
299 elif [ -f ${KERNEL_OUTPUT_DIR}/$imageType ]; then
300 mv -f ${KERNEL_OUTPUT_DIR}/$imageType ${KERNEL_OUTPUT_DIR}/$imageType.bak
301 tmp_path=$tmp_path" "$imageType"##"
He Zhefe76b1e2016-05-25 04:47:16 -0400302 fi
303 done
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500304 use_alternate_initrd=CONFIG_INITRAMFS_SOURCE=${B}/usr/${INITRAMFS_IMAGE_NAME}.cpio
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500305 kernel_do_compile
He Zhefe76b1e2016-05-25 04:47:16 -0400306 # Restoring kernel image
307 for tp in $tmp_path ; do
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800308 imageType=`echo $tp|cut -d "#" -f 1`
He Zhefe76b1e2016-05-25 04:47:16 -0400309 linkpath=`echo $tp|cut -d "#" -f 2`
310 realpath=`echo $tp|cut -d "#" -f 3`
311 if [ -n "$realpath" ]; then
312 mv -f $realpath $realpath.initramfs
313 mv -f $realpath.bak $realpath
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800314 ln -sf $linkpath.initramfs ${B}/${KERNEL_OUTPUT_DIR}/$imageType.initramfs
He Zhefe76b1e2016-05-25 04:47:16 -0400315 else
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800316 mv -f ${KERNEL_OUTPUT_DIR}/$imageType ${KERNEL_OUTPUT_DIR}/$imageType.initramfs
317 mv -f ${KERNEL_OUTPUT_DIR}/$imageType.bak ${KERNEL_OUTPUT_DIR}/$imageType
He Zhefe76b1e2016-05-25 04:47:16 -0400318 fi
319 done
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500320 fi
321}
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600322do_bundle_initramfs[dirs] = "${B}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500323
Andrew Geissler595f6302022-01-24 19:11:47 +0000324kernel_do_transform_bundled_initramfs() {
325 # vmlinux.gz is not built by kernel
326 if (echo "${KERNEL_IMAGETYPES}" | grep -wq "vmlinux\.gz"); then
327 gzip -9cn < ${KERNEL_OUTPUT_DIR}/vmlinux.initramfs > ${KERNEL_OUTPUT_DIR}/vmlinux.gz.initramfs
328 fi
329}
330do_transform_bundled_initramfs[dirs] = "${B}"
331
Patrick Williams213cb262021-08-07 19:21:33 -0500332python do_devshell:prepend () {
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500333 os.environ["LDFLAGS"] = ''
334}
335
336addtask bundle_initramfs after do_install before do_deploy
337
Andrew Geisslereff27472021-10-29 15:35:00 -0500338KERNEL_DEBUG_TIMESTAMPS ??= "0"
339
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500340kernel_do_compile() {
341 unset CFLAGS CPPFLAGS CXXFLAGS LDFLAGS MACHINE
Andrew Geissler595f6302022-01-24 19:11:47 +0000342
343 # setup native pkg-config variables (kconfig scripts call pkg-config directly, cannot generically be overriden to pkg-config-native)
344 export PKG_CONFIG_DIR="${STAGING_DIR_NATIVE}${libdir_native}/pkgconfig"
345 export PKG_CONFIG_PATH="$PKG_CONFIG_DIR:${STAGING_DATADIR_NATIVE}/pkgconfig"
346 export PKG_CONFIG_LIBDIR="$PKG_CONFIG_DIR"
347 export PKG_CONFIG_SYSROOT_DIR=""
348
Andrew Geisslereff27472021-10-29 15:35:00 -0500349 if [ "${KERNEL_DEBUG_TIMESTAMPS}" != "1" ]; then
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500350 # kernel sources do not use do_unpack, so SOURCE_DATE_EPOCH may not
351 # be set....
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800352 if [ "${SOURCE_DATE_EPOCH}" = "" -o "${SOURCE_DATE_EPOCH}" = "0" ]; then
Andrew Geissler82c905d2020-04-13 13:39:40 -0500353 # The source directory is not necessarily a git repository, so we
354 # specify the git-dir to ensure that git does not query a
355 # repository in any parent directory.
356 SOURCE_DATE_EPOCH=`git --git-dir="${S}/.git" log -1 --pretty=%ct 2>/dev/null || echo "${REPRODUCIBLE_TIMESTAMP_ROOTFS}"`
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500357 fi
358
359 ts=`LC_ALL=C date -d @$SOURCE_DATE_EPOCH`
360 export KBUILD_BUILD_TIMESTAMP="$ts"
361 export KCONFIG_NOTIMESTAMP=1
362 bbnote "KBUILD_BUILD_TIMESTAMP: $ts"
363 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500364 # The $use_alternate_initrd is only set from
365 # do_bundle_initramfs() This variable is specifically for the
366 # case where we are making a second pass at the kernel
367 # compilation and we want to force the kernel build to use a
368 # different initramfs image. The way to do that in the kernel
369 # is to specify:
370 # make ...args... CONFIG_INITRAMFS_SOURCE=some_other_initramfs.cpio
371 if [ "$use_alternate_initrd" = "" ] && [ "${INITRAMFS_TASK}" != "" ] ; then
372 # The old style way of copying an prebuilt image and building it
373 # is turned on via INTIRAMFS_TASK != ""
374 copy_initramfs
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500375 use_alternate_initrd=CONFIG_INITRAMFS_SOURCE=${B}/usr/${INITRAMFS_IMAGE_NAME}.cpio
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500376 fi
He Zhefe76b1e2016-05-25 04:47:16 -0400377 for typeformake in ${KERNEL_IMAGETYPE_FOR_MAKE} ; do
Patrick Williams93c203f2021-10-06 16:15:23 -0500378 oe_runmake ${typeformake} CC="${KERNEL_CC}" LD="${KERNEL_LD}" ${KERNEL_EXTRA_ARGS} $use_alternate_initrd
He Zhefe76b1e2016-05-25 04:47:16 -0400379 done
Andrew Geissler595f6302022-01-24 19:11:47 +0000380}
381
382kernel_do_transform_kernel() {
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500383 # vmlinux.gz is not built by kernel
384 if (echo "${KERNEL_IMAGETYPES}" | grep -wq "vmlinux\.gz"); then
385 mkdir -p "${KERNEL_OUTPUT_DIR}"
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500386 gzip -9cn < ${B}/vmlinux > "${KERNEL_OUTPUT_DIR}/vmlinux.gz"
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500387 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500388}
Andrew Geissler595f6302022-01-24 19:11:47 +0000389do_transform_kernel[dirs] = "${B}"
390addtask transform_kernel after do_compile before do_install
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500391
392do_compile_kernelmodules() {
393 unset CFLAGS CPPFLAGS CXXFLAGS LDFLAGS MACHINE
Andrew Geisslereff27472021-10-29 15:35:00 -0500394 if [ "${KERNEL_DEBUG_TIMESTAMPS}" != "1" ]; then
Andrew Geissler1e34c2d2020-05-29 16:02:59 -0500395 # kernel sources do not use do_unpack, so SOURCE_DATE_EPOCH may not
396 # be set....
397 if [ "${SOURCE_DATE_EPOCH}" = "" -o "${SOURCE_DATE_EPOCH}" = "0" ]; then
398 # The source directory is not necessarily a git repository, so we
399 # specify the git-dir to ensure that git does not query a
400 # repository in any parent directory.
401 SOURCE_DATE_EPOCH=`git --git-dir="${S}/.git" log -1 --pretty=%ct 2>/dev/null || echo "${REPRODUCIBLE_TIMESTAMP_ROOTFS}"`
402 fi
403
404 ts=`LC_ALL=C date -d @$SOURCE_DATE_EPOCH`
405 export KBUILD_BUILD_TIMESTAMP="$ts"
406 export KCONFIG_NOTIMESTAMP=1
407 bbnote "KBUILD_BUILD_TIMESTAMP: $ts"
408 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500409 if (grep -q -i -e '^CONFIG_MODULES=y$' ${B}/.config); then
Patrick Williams93c203f2021-10-06 16:15:23 -0500410 oe_runmake -C ${B} ${PARALLEL_MAKE} modules CC="${KERNEL_CC}" LD="${KERNEL_LD}" ${KERNEL_EXTRA_ARGS}
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500411
412 # Module.symvers gets updated during the
413 # building of the kernel modules. We need to
414 # update this in the shared workdir since some
415 # external kernel modules has a dependency on
416 # other kernel modules and will look at this
417 # file to do symbol lookups
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600418 cp ${B}/Module.symvers ${STAGING_KERNEL_BUILDDIR}/
Andrew Geissler6ce62a22020-11-30 19:58:47 -0600419 # 5.10+ kernels have module.lds that we need to copy for external module builds
420 if [ -e "${B}/scripts/module.lds" ]; then
421 install -Dm 0644 ${B}/scripts/module.lds ${STAGING_KERNEL_BUILDDIR}/scripts/module.lds
422 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500423 else
424 bbnote "no modules to compile"
425 fi
426}
427addtask compile_kernelmodules after do_compile before do_strip
428
429kernel_do_install() {
430 #
431 # First install the modules
432 #
433 unset CFLAGS CPPFLAGS CXXFLAGS LDFLAGS MACHINE
434 if (grep -q -i -e '^CONFIG_MODULES=y$' .config); then
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500435 oe_runmake DEPMOD=echo MODLIB=${D}${nonarch_base_libdir}/modules/${KERNEL_VERSION} INSTALL_FW_PATH=${D}${nonarch_base_libdir}/firmware modules_install
436 rm "${D}${nonarch_base_libdir}/modules/${KERNEL_VERSION}/build"
437 rm "${D}${nonarch_base_libdir}/modules/${KERNEL_VERSION}/source"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500438 # If the kernel/ directory is empty remove it to prevent QA issues
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500439 rmdir --ignore-fail-on-non-empty "${D}${nonarch_base_libdir}/modules/${KERNEL_VERSION}/kernel"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500440 else
441 bbnote "no modules to install"
442 fi
443
444 #
445 # Install various kernel output (zImage, map file, config, module support files)
446 #
447 install -d ${D}/${KERNEL_IMAGEDEST}
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600448
449 #
450 # When including an initramfs bundle inside a FIT image, the fitImage is created after the install task
451 # by do_assemble_fitimage_initramfs.
452 # This happens after the generation of the initramfs bundle (done by do_bundle_initramfs).
453 # So, at the level of the install task we should not try to install the fitImage. fitImage is still not
454 # generated yet.
455 # After the generation of the fitImage, the deploy task copies the fitImage from the build directory to
456 # the deploy folder.
457 #
458
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800459 for imageType in ${KERNEL_IMAGETYPES} ; do
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600460 if [ $imageType != "fitImage" ] || [ "${INITRAMFS_IMAGE_BUNDLE}" != "1" ] ; then
461 install -m 0644 ${KERNEL_OUTPUT_DIR}/$imageType ${D}/${KERNEL_IMAGEDEST}/$imageType-${KERNEL_VERSION}
462 fi
He Zhefe76b1e2016-05-25 04:47:16 -0400463 done
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600464
Patrick Williamsde0582f2022-04-08 10:23:27 -0500465 install -m 0644 System.map ${D}/${KERNEL_IMAGEDEST}/System.map-${KERNEL_VERSION}
466 install -m 0644 .config ${D}/${KERNEL_IMAGEDEST}/config-${KERNEL_VERSION}
467 install -m 0644 vmlinux ${D}/${KERNEL_IMAGEDEST}/vmlinux-${KERNEL_VERSION}
468 [ -e Module.symvers ] && install -m 0644 Module.symvers ${D}/${KERNEL_IMAGEDEST}/Module.symvers-${KERNEL_VERSION}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500469 install -d ${D}${sysconfdir}/modules-load.d
470 install -d ${D}${sysconfdir}/modprobe.d
471}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500472
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600473# Must be ran no earlier than after do_kernel_checkout or else Makefile won't be in ${S}/Makefile
474do_kernel_version_sanity_check() {
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500475 if [ "x${KERNEL_VERSION_SANITY_SKIP}" = "x1" ]; then
476 exit 0
477 fi
478
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600479 # The Makefile determines the kernel version shown at runtime
480 # Don't use KERNEL_VERSION because the headers it grabs the version from aren't generated until do_compile
481 VERSION=$(grep "^VERSION =" ${S}/Makefile | sed s/.*=\ *//)
482 PATCHLEVEL=$(grep "^PATCHLEVEL =" ${S}/Makefile | sed s/.*=\ *//)
483 SUBLEVEL=$(grep "^SUBLEVEL =" ${S}/Makefile | sed s/.*=\ *//)
484 EXTRAVERSION=$(grep "^EXTRAVERSION =" ${S}/Makefile | sed s/.*=\ *//)
485
486 # Build a string for regex and a plain version string
487 reg="^${VERSION}\.${PATCHLEVEL}"
488 vers="${VERSION}.${PATCHLEVEL}"
489 if [ -n "${SUBLEVEL}" ]; then
490 # Ignoring a SUBLEVEL of zero is fine
491 if [ "${SUBLEVEL}" = "0" ]; then
492 reg="${reg}(\.${SUBLEVEL})?"
493 else
494 reg="${reg}\.${SUBLEVEL}"
495 vers="${vers}.${SUBLEVEL}"
496 fi
497 fi
498 vers="${vers}${EXTRAVERSION}"
499 reg="${reg}${EXTRAVERSION}"
500
501 if [ -z `echo ${PV} | grep -E "${reg}"` ]; then
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500502 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."
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600503 fi
504 exit 0
505}
506
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500507addtask shared_workdir after do_compile before do_compile_kernelmodules
508addtask shared_workdir_setscene
509
510do_shared_workdir_setscene () {
511 exit 1
512}
513
514emit_depmod_pkgdata() {
515 # Stash data for depmod
Brad Bishop316dfdd2018-06-25 12:45:53 -0400516 install -d ${PKGDESTWORK}/${KERNEL_PACKAGE_NAME}-depmod/
517 echo "${KERNEL_VERSION}" > ${PKGDESTWORK}/${KERNEL_PACKAGE_NAME}-depmod/${KERNEL_PACKAGE_NAME}-abiversion
518 cp ${B}/System.map ${PKGDESTWORK}/${KERNEL_PACKAGE_NAME}-depmod/System.map-${KERNEL_VERSION}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500519}
520
521PACKAGEFUNCS += "emit_depmod_pkgdata"
522
Brad Bishop316dfdd2018-06-25 12:45:53 -0400523do_shared_workdir[cleandirs] += " ${STAGING_KERNEL_BUILDDIR}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500524do_shared_workdir () {
525 cd ${B}
526
527 kerneldir=${STAGING_KERNEL_BUILDDIR}
528 install -d $kerneldir
529
530 #
531 # Store the kernel version in sysroots for module-base.bbclass
532 #
533
Brad Bishop316dfdd2018-06-25 12:45:53 -0400534 echo "${KERNEL_VERSION}" > $kerneldir/${KERNEL_PACKAGE_NAME}-abiversion
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500535
536 # Copy files required for module builds
537 cp System.map $kerneldir/System.map-${KERNEL_VERSION}
Andrew Geissler475cb722020-07-10 16:00:51 -0500538 [ -e Module.symvers ] && cp Module.symvers $kerneldir/
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500539 cp .config $kerneldir/
540 mkdir -p $kerneldir/include/config
541 cp include/config/kernel.release $kerneldir/include/config/kernel.release
Brad Bishop08902b02019-08-20 09:16:51 -0400542 if [ -e certs/signing_key.x509 ]; then
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600543 # The signing_key.* files are stored in the certs/ dir in
544 # newer Linux kernels
545 mkdir -p $kerneldir/certs
546 cp certs/signing_key.* $kerneldir/certs/
547 elif [ -e signing_key.priv ]; then
548 cp signing_key.* $kerneldir/
549 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500550
551 # We can also copy over all the generated files and avoid special cases
552 # like version.h, but we've opted to keep this small until file creep starts
553 # to happen
554 if [ -e include/linux/version.h ]; then
555 mkdir -p $kerneldir/include/linux
556 cp include/linux/version.h $kerneldir/include/linux/version.h
557 fi
558
559 # As of Linux kernel version 3.0.1, the clean target removes
560 # arch/powerpc/lib/crtsavres.o which is present in
561 # KBUILD_LDFLAGS_MODULE, making it required to build external modules.
562 if [ ${ARCH} = "powerpc" ]; then
Brad Bishop316dfdd2018-06-25 12:45:53 -0400563 if [ -e arch/powerpc/lib/crtsavres.o ]; then
564 mkdir -p $kerneldir/arch/powerpc/lib/
565 cp arch/powerpc/lib/crtsavres.o $kerneldir/arch/powerpc/lib/crtsavres.o
566 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500567 fi
568
569 if [ -d include/generated ]; then
570 mkdir -p $kerneldir/include/generated/
571 cp -fR include/generated/* $kerneldir/include/generated/
572 fi
573
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500574 if [ -d arch/${ARCH}/include/generated ]; then
575 mkdir -p $kerneldir/arch/${ARCH}/include/generated/
576 cp -fR arch/${ARCH}/include/generated/* $kerneldir/arch/${ARCH}/include/generated/
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500577 fi
Brad Bishop19323692019-04-05 15:28:33 -0400578
579 if (grep -q -i -e '^CONFIG_UNWINDER_ORC=y$' $kerneldir/.config); then
580 # With CONFIG_UNWINDER_ORC (the default in 4.14), objtool is required for
581 # out-of-tree modules to be able to generate object files.
582 if [ -x tools/objtool/objtool ]; then
583 mkdir -p ${kerneldir}/tools/objtool
584 cp tools/objtool/objtool ${kerneldir}/tools/objtool/
585 fi
586 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500587}
588
589# We don't need to stage anything, not the modules/firmware since those would clash with linux-firmware
590sysroot_stage_all () {
591 :
592}
593
Andrew Geissler82c905d2020-04-13 13:39:40 -0500594KERNEL_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"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500595
He Zhefe76b1e2016-05-25 04:47:16 -0400596python check_oldest_kernel() {
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500597 oldest_kernel = d.getVar('OLDEST_KERNEL')
598 kernel_version = d.getVar('KERNEL_VERSION')
599 tclibc = d.getVar('TCLIBC')
He Zhefe76b1e2016-05-25 04:47:16 -0400600 if tclibc == 'glibc':
601 kernel_version = kernel_version.split('-', 1)[0]
602 if oldest_kernel and kernel_version:
603 if bb.utils.vercmp_string(kernel_version, oldest_kernel) < 0:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500604 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))
He Zhefe76b1e2016-05-25 04:47:16 -0400605}
606
607check_oldest_kernel[vardepsexclude] += "OLDEST_KERNEL KERNEL_VERSION"
608do_configure[prefuncs] += "check_oldest_kernel"
609
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500610kernel_do_configure() {
611 # fixes extra + in /lib/modules/2.6.37+
612 # $ scripts/setlocalversion . => +
613 # $ make kernelversion => 2.6.37
614 # $ make kernelrelease => 2.6.37+
615 touch ${B}/.scmversion ${S}/.scmversion
616
617 if [ "${S}" != "${B}" ] && [ -f "${S}/.config" ] && [ ! -f "${B}/.config" ]; then
618 mv "${S}/.config" "${B}/.config"
619 fi
620
621 # Copy defconfig to .config if .config does not exist. This allows
Patrick Williams213cb262021-08-07 19:21:33 -0500622 # recipes to manage the .config themselves in do_configure:prepend().
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500623 if [ -f "${WORKDIR}/defconfig" ] && [ ! -f "${B}/.config" ]; then
624 cp "${WORKDIR}/defconfig" "${B}/.config"
625 fi
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500626
627 ${KERNEL_CONFIG_COMMAND}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500628}
629
630do_savedefconfig() {
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600631 bbplain "Saving defconfig to:\n${B}/defconfig"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500632 oe_runmake -C ${B} savedefconfig
633}
634do_savedefconfig[nostamp] = "1"
635addtask savedefconfig after do_configure
636
637inherit cml1
638
Andrew Geissler9aee5002022-03-30 16:27:02 +0000639KCONFIG_CONFIG_COMMAND:append = " PAHOLE=false LD='${KERNEL_LD}' HOSTLDFLAGS='${BUILD_LDFLAGS}'"
Brad Bishop316dfdd2018-06-25 12:45:53 -0400640
Andrew Geissler595f6302022-01-24 19:11:47 +0000641EXPORT_FUNCTIONS do_compile do_transform_kernel do_transform_bundled_initramfs do_install do_configure
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500642
643# kernel-base becomes kernel-${KERNEL_VERSION}
He Zhefe76b1e2016-05-25 04:47:16 -0400644# kernel-image becomes kernel-image-${KERNEL_VERSION}
Andrew Geissler595f6302022-01-24 19:11:47 +0000645PACKAGES = "${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"
Patrick Williams213cb262021-08-07 19:21:33 -0500646FILES:${PN} = ""
647FILES:${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"
648FILES:${KERNEL_PACKAGE_NAME}-image = ""
Patrick Williamsde0582f2022-04-08 10:23:27 -0500649FILES:${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"
650FILES:${KERNEL_PACKAGE_NAME}-vmlinux = "/${KERNEL_IMAGEDEST}/vmlinux-${KERNEL_VERSION_NAME}"
Patrick Williams213cb262021-08-07 19:21:33 -0500651FILES:${KERNEL_PACKAGE_NAME}-modules = ""
Andrew Geisslerd5838332022-05-27 11:33:10 -0500652FILES:${KERNEL_PACKAGE_NAME}-dbg = "/usr/lib/debug /usr/src/debug"
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500653RDEPENDS:${KERNEL_PACKAGE_NAME} = "${KERNEL_PACKAGE_NAME}-base (= ${EXTENDPKGV})"
He Zhefe76b1e2016-05-25 04:47:16 -0400654# Allow machines to override this dependency if kernel image files are
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500655# not wanted in images as standard
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000656RRECOMMENDS:${KERNEL_PACKAGE_NAME}-base ?= "${KERNEL_PACKAGE_NAME}-image (= ${EXTENDPKGV})"
Patrick Williams213cb262021-08-07 19:21:33 -0500657PKG:${KERNEL_PACKAGE_NAME}-image = "${KERNEL_PACKAGE_NAME}-image-${@legitimize_package_name(d.getVar('KERNEL_VERSION'))}"
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500658RDEPENDS:${KERNEL_PACKAGE_NAME}-image += "${@oe.utils.conditional('KERNEL_IMAGETYPE', 'vmlinux', '${KERNEL_PACKAGE_NAME}-vmlinux (= ${EXTENDPKGV})', '', d)}"
Patrick Williams213cb262021-08-07 19:21:33 -0500659PKG:${KERNEL_PACKAGE_NAME}-base = "${KERNEL_PACKAGE_NAME}-${@legitimize_package_name(d.getVar('KERNEL_VERSION'))}"
660RPROVIDES:${KERNEL_PACKAGE_NAME}-base += "${KERNEL_PACKAGE_NAME}-${KERNEL_VERSION}"
661ALLOW_EMPTY:${KERNEL_PACKAGE_NAME} = "1"
662ALLOW_EMPTY:${KERNEL_PACKAGE_NAME}-base = "1"
663ALLOW_EMPTY:${KERNEL_PACKAGE_NAME}-image = "1"
664ALLOW_EMPTY:${KERNEL_PACKAGE_NAME}-modules = "1"
665DESCRIPTION:${KERNEL_PACKAGE_NAME}-modules = "Kernel modules meta package"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500666
Patrick Williams213cb262021-08-07 19:21:33 -0500667pkg_postinst:${KERNEL_PACKAGE_NAME}-base () {
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500668 if [ ! -e "$D/lib/modules/${KERNEL_VERSION}" ]; then
669 mkdir -p $D/lib/modules/${KERNEL_VERSION}
670 fi
671 if [ -n "$D" ]; then
672 depmodwrapper -a -b $D ${KERNEL_VERSION}
673 else
674 depmod -a ${KERNEL_VERSION}
675 fi
676}
677
Patrick Williams213cb262021-08-07 19:21:33 -0500678PACKAGESPLITFUNCS:prepend = "split_kernel_packages "
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500679
680python split_kernel_packages () {
Brad Bishop19323692019-04-05 15:28:33 -0400681 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='')
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500682}
683
684# Many scripts want to look in arch/$arch/boot for the bootable
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600685# image. This poses a problem for vmlinux and vmlinuz based
686# booting. This task arranges to have vmlinux and vmlinuz appear
687# in the normalized directory location.
688do_kernel_link_images() {
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500689 if [ ! -d "${B}/arch/${ARCH}/boot" ]; then
690 mkdir ${B}/arch/${ARCH}/boot
691 fi
692 cd ${B}/arch/${ARCH}/boot
693 ln -sf ../../../vmlinux
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600694 if [ -f ../../../vmlinuz ]; then
695 ln -sf ../../../vmlinuz
696 fi
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500697 if [ -f ../../../vmlinuz.bin ]; then
698 ln -sf ../../../vmlinuz.bin
699 fi
Andrew Geissler82c905d2020-04-13 13:39:40 -0500700 if [ -f ../../../vmlinux.64 ]; then
701 ln -sf ../../../vmlinux.64
702 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500703}
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500704addtask kernel_link_images after do_compile before do_strip
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500705
Andrew Geissler595f6302022-01-24 19:11:47 +0000706python do_strip() {
707 import shutil
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500708
Andrew Geissler595f6302022-01-24 19:11:47 +0000709 strip = d.getVar('STRIP')
710 extra_sections = d.getVar('KERNEL_IMAGE_STRIP_EXTRA_SECTIONS')
711 kernel_image = d.getVar('B') + "/" + d.getVar('KERNEL_OUTPUT_DIR') + "/vmlinux"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500712
Patrick Williams58776372022-04-13 09:07:35 -0500713 if (extra_sections and kernel_image.find(d.getVar('KERNEL_IMAGEDEST') + '/vmlinux') != -1):
Andrew Geissler595f6302022-01-24 19:11:47 +0000714 kernel_image_stripped = kernel_image + ".stripped"
715 shutil.copy2(kernel_image, kernel_image_stripped)
716 oe.package.runstrip((kernel_image_stripped, 8, strip, extra_sections))
717 bb.debug(1, "KERNEL_IMAGE_STRIP_EXTRA_SECTIONS is set, stripping sections: " + \
718 extra_sections)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500719}
720do_strip[dirs] = "${B}"
721
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500722addtask strip before do_sizecheck after do_kernel_link_images
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500723
724# Support checking the kernel size since some kernels need to reside in partitions
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500725# with a fixed length or there is a limit in transferring the kernel to memory.
726# If more than one image type is enabled, warn on any that don't fit but only fail
727# if none fit.
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500728do_sizecheck() {
729 if [ ! -z "${KERNEL_IMAGE_MAXSIZE}" ]; then
730 invalid=`echo ${KERNEL_IMAGE_MAXSIZE} | sed 's/[0-9]//g'`
731 if [ -n "$invalid" ]; then
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500732 die "Invalid KERNEL_IMAGE_MAXSIZE: ${KERNEL_IMAGE_MAXSIZE}, should be an integer (The unit is Kbytes)"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500733 fi
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500734 at_least_one_fits=
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800735 for imageType in ${KERNEL_IMAGETYPES} ; do
736 size=`du -ks ${B}/${KERNEL_OUTPUT_DIR}/$imageType | awk '{print $1}'`
Andrew Geissler09036742021-06-25 14:25:14 -0500737 if [ $size -gt ${KERNEL_IMAGE_MAXSIZE} ]; then
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800738 bbwarn "This kernel $imageType (size=$size(K) > ${KERNEL_IMAGE_MAXSIZE}(K)) is too big for your device."
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500739 else
740 at_least_one_fits=y
He Zhefe76b1e2016-05-25 04:47:16 -0400741 fi
742 done
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500743 if [ -z "$at_least_one_fits" ]; then
744 die "All kernel images are too big for your device. Please reduce the size of the kernel by making more of it modular."
745 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500746 fi
747}
748do_sizecheck[dirs] = "${B}"
749
750addtask sizecheck before do_install after do_strip
751
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800752inherit kernel-artifact-names
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500753
754kernel_do_deploy() {
Brad Bishop316dfdd2018-06-25 12:45:53 -0400755 deployDir="${DEPLOYDIR}"
756 if [ -n "${KERNEL_DEPLOYSUBDIR}" ]; then
757 deployDir="${DEPLOYDIR}/${KERNEL_DEPLOYSUBDIR}"
758 mkdir "$deployDir"
759 fi
760
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800761 for imageType in ${KERNEL_IMAGETYPES} ; do
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500762 baseName=$imageType-${KERNEL_IMAGE_NAME}
Andrew Geissler595f6302022-01-24 19:11:47 +0000763
764 if [ -s ${KERNEL_OUTPUT_DIR}/$imageType.stripped ] ; then
765 install -m 0644 ${KERNEL_OUTPUT_DIR}/$imageType.stripped $deployDir/$baseName${KERNEL_IMAGE_BIN_EXT}
766 else
767 install -m 0644 ${KERNEL_OUTPUT_DIR}/$imageType $deployDir/$baseName${KERNEL_IMAGE_BIN_EXT}
768 fi
769 if [ -n "${KERNEL_IMAGE_LINK_NAME}" ] ; then
770 ln -sf $baseName${KERNEL_IMAGE_BIN_EXT} $deployDir/$imageType-${KERNEL_IMAGE_LINK_NAME}${KERNEL_IMAGE_BIN_EXT}
771 fi
772 if [ "${KERNEL_IMAGETYPE_SYMLINK}" = "1" ] ; then
773 ln -sf $baseName${KERNEL_IMAGE_BIN_EXT} $deployDir/$imageType
774 fi
He Zhefe76b1e2016-05-25 04:47:16 -0400775 done
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800776
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500777 if [ ${MODULE_TARBALL_DEPLOY} = "1" ] && (grep -q -i -e '^CONFIG_MODULES=y$' .config); then
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800778 mkdir -p ${D}${root_prefix}/lib
Andrew Geisslerc182c622020-05-15 14:13:32 -0500779 if [ -n "${SOURCE_DATE_EPOCH}" ]; then
780 TAR_ARGS="--sort=name --clamp-mtime --mtime=@${SOURCE_DATE_EPOCH}"
781 else
782 TAR_ARGS=""
783 fi
Andrew Geissler1e34c2d2020-05-29 16:02:59 -0500784 TAR_ARGS="$TAR_ARGS --owner=0 --group=0"
Andrew Geisslerc182c622020-05-15 14:13:32 -0500785 tar $TAR_ARGS -cv -C ${D}${root_prefix} lib | gzip -9n > $deployDir/modules-${MODULE_TARBALL_NAME}.tgz
786
Andrew Geissler595f6302022-01-24 19:11:47 +0000787 if [ -n "${MODULE_TARBALL_LINK_NAME}" ] ; then
788 ln -sf modules-${MODULE_TARBALL_NAME}.tgz $deployDir/modules-${MODULE_TARBALL_LINK_NAME}.tgz
789 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500790 fi
791
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800792 if [ ! -z "${INITRAMFS_IMAGE}" -a x"${INITRAMFS_IMAGE_BUNDLE}" = x1 ]; then
Andrew Geissler595f6302022-01-24 19:11:47 +0000793 for imageType in ${KERNEL_IMAGETYPES} ; do
Brad Bishop977dc1a2019-02-06 16:01:43 -0500794 if [ "$imageType" = "fitImage" ] ; then
795 continue
796 fi
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500797 initramfsBaseName=$imageType-${INITRAMFS_NAME}
Andrew Geissler595f6302022-01-24 19:11:47 +0000798 install -m 0644 ${KERNEL_OUTPUT_DIR}/$imageType.initramfs $deployDir/$initramfsBaseName${KERNEL_IMAGE_BIN_EXT}
799 if [ -n "${INITRAMFS_LINK_NAME}" ] ; then
800 ln -sf $initramfsBaseName${KERNEL_IMAGE_BIN_EXT} $deployDir/$imageType-${INITRAMFS_LINK_NAME}${KERNEL_IMAGE_BIN_EXT}
801 fi
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800802 done
803 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500804}
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500805
806# We deploy to filenames that include PKGV and PKGR, read the saved data to
807# ensure we get the right values for both
808do_deploy[prefuncs] += "read_subpackage_metadata"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500809
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500810addtask deploy after do_populate_sysroot do_packagedata
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500811
812EXPORT_FUNCTIONS do_deploy
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500813
814# Add using Device Tree support
815inherit kernel-devicetree