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