blob: ce2cab65ae400b47742f8b596c91521646239af0 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001inherit linux-kernel-base kernel-module-split
2
3PROVIDES += "virtual/kernel"
Brad Bishop6e60e8b2018-02-01 10:27:11 -05004DEPENDS += "virtual/${TARGET_PREFIX}binutils virtual/${TARGET_PREFIX}gcc kmod-native bc-native lzop-native"
5PACKAGE_WRITE_DEPS += "depmodwrapper-cross virtual/update-alternatives-native"
6
7do_deploy[depends] += "depmodwrapper-cross:do_populate_sysroot"
8
9CVE_PRODUCT ?= "linux_kernel"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050010
11S = "${STAGING_KERNEL_DIR}"
12B = "${WORKDIR}/build"
13KBUILD_OUTPUT = "${B}"
14OE_TERMINAL_EXPORTS += "KBUILD_OUTPUT"
15
16# we include gcc above, we dont need virtual/libc
17INHIBIT_DEFAULT_DEPS = "1"
18
19KERNEL_IMAGETYPE ?= "zImage"
20INITRAMFS_IMAGE ?= ""
Brad Bishop6e60e8b2018-02-01 10:27:11 -050021INITRAMFS_IMAGE_NAME ?= "${@['${INITRAMFS_IMAGE}-${MACHINE}', ''][d.getVar('INITRAMFS_IMAGE') == '']}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050022INITRAMFS_TASK ?= ""
23INITRAMFS_IMAGE_BUNDLE ?= ""
24
He Zhefe76b1e2016-05-25 04:47:16 -040025# KERNEL_VERSION is extracted from source code. It is evaluated as
26# None for the first parsing, since the code has not been fetched.
27# After the code is fetched, it will be evaluated as real version
28# number and cause kernel to be rebuilt. To avoid this, make
29# KERNEL_VERSION_NAME and KERNEL_VERSION_PKG_NAME depend on
30# LINUX_VERSION which is a constant.
Brad Bishop6e60e8b2018-02-01 10:27:11 -050031KERNEL_VERSION_NAME = "${@d.getVar('KERNEL_VERSION') or ""}"
He Zhefe76b1e2016-05-25 04:47:16 -040032KERNEL_VERSION_NAME[vardepvalue] = "${LINUX_VERSION}"
Brad Bishop6e60e8b2018-02-01 10:27:11 -050033KERNEL_VERSION_PKG_NAME = "${@legitimize_package_name(d.getVar('KERNEL_VERSION'))}"
He Zhefe76b1e2016-05-25 04:47:16 -040034KERNEL_VERSION_PKG_NAME[vardepvalue] = "${LINUX_VERSION}"
35
Patrick Williamsc124f4f2015-09-15 14:41:29 -050036python __anonymous () {
Patrick Williamsc124f4f2015-09-15 14:41:29 -050037
He Zhefe76b1e2016-05-25 04:47:16 -040038 # Merge KERNEL_IMAGETYPE and KERNEL_ALT_IMAGETYPE into KERNEL_IMAGETYPES
Brad Bishop6e60e8b2018-02-01 10:27:11 -050039 type = d.getVar('KERNEL_IMAGETYPE') or ""
40 alttype = d.getVar('KERNEL_ALT_IMAGETYPE') or ""
41 types = d.getVar('KERNEL_IMAGETYPES') or ""
He Zhefe76b1e2016-05-25 04:47:16 -040042 if type not in types.split():
43 types = (type + ' ' + types).strip()
44 if alttype not in types.split():
45 types = (alttype + ' ' + types).strip()
46 d.setVar('KERNEL_IMAGETYPES', types)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050047
Brad Bishop6e60e8b2018-02-01 10:27:11 -050048 # some commonly used kernel images aren't generated by the kernel build system, such as vmlinux.gz
49 # typeformake lists only valid kernel make targets, and post processing can be done after the kernel
50 # is built (such as using gzip to compress vmlinux)
51 typeformake = types.replace('vmlinux.gz', 'vmlinux')
He Zhefe76b1e2016-05-25 04:47:16 -040052 d.setVar('KERNEL_IMAGETYPE_FOR_MAKE', typeformake)
53
Brad Bishop37a0e4d2017-12-04 01:01:44 -050054 for type in types.split():
He Zhefe76b1e2016-05-25 04:47:16 -040055 typelower = type.lower()
Brad Bishop6e60e8b2018-02-01 10:27:11 -050056 imagedest = d.getVar('KERNEL_IMAGEDEST')
He Zhefe76b1e2016-05-25 04:47:16 -040057
58 d.appendVar('PACKAGES', ' ' + 'kernel-image-' + typelower)
59
Brad Bishop6e60e8b2018-02-01 10:27:11 -050060 d.setVar('FILES_kernel-image-' + typelower, '/' + imagedest + '/' + type + '-${KERNEL_VERSION_NAME}')
He Zhefe76b1e2016-05-25 04:47:16 -040061
62 d.appendVar('RDEPENDS_kernel-image', ' ' + 'kernel-image-' + typelower)
63
64 d.setVar('PKG_kernel-image-' + typelower, 'kernel-image-' + typelower + '-${KERNEL_VERSION_PKG_NAME}')
65
66 d.setVar('ALLOW_EMPTY_kernel-image-' + typelower, '1')
67
Brad Bishop6e60e8b2018-02-01 10:27:11 -050068 priority = d.getVar('KERNEL_PRIORITY')
69 postinst = '#!/bin/sh\n' + 'update-alternatives --install /' + imagedest + '/' + type + ' ' + type + ' ' + type + '-${KERNEL_VERSION_NAME} ' + priority + ' || true' + '\n'
He Zhefe76b1e2016-05-25 04:47:16 -040070 d.setVar('pkg_postinst_kernel-image-' + typelower, postinst)
71
72 postrm = '#!/bin/sh\n' + 'update-alternatives --remove' + ' ' + type + ' ' + type + '-${KERNEL_VERSION_NAME} || true' + '\n'
73 d.setVar('pkg_postrm_kernel-image-' + typelower, postrm)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050074
Brad Bishop6e60e8b2018-02-01 10:27:11 -050075 image = d.getVar('INITRAMFS_IMAGE')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050076 if image:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050077 d.appendVarFlag('do_bundle_initramfs', 'depends', ' ${INITRAMFS_IMAGE}:do_image_complete')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050078
79 # NOTE: setting INITRAMFS_TASK is for backward compatibility
80 # The preferred method is to set INITRAMFS_IMAGE, because
81 # this INITRAMFS_TASK has circular dependency problems
82 # if the initramfs requires kernel modules
Brad Bishop6e60e8b2018-02-01 10:27:11 -050083 image_task = d.getVar('INITRAMFS_TASK')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050084 if image_task:
85 d.appendVarFlag('do_configure', 'depends', ' ${INITRAMFS_TASK}')
86}
87
88# Here we pull in all various kernel image types which we support.
89#
90# In case you're wondering why kernel.bbclass inherits the other image
91# types instead of the other way around, the reason for that is to
92# maintain compatibility with various currently existing meta-layers.
93# By pulling in the various kernel image types here, we retain the
94# original behavior of kernel.bbclass, so no meta-layers should get
95# broken.
96#
97# KERNEL_CLASSES by default pulls in kernel-uimage.bbclass, since this
98# used to be the default behavior when only uImage was supported. This
99# variable can be appended by users who implement support for new kernel
100# image types.
101
102KERNEL_CLASSES ?= " kernel-uimage "
103inherit ${KERNEL_CLASSES}
104
105# Old style kernels may set ${S} = ${WORKDIR}/git for example
106# We need to move these over to STAGING_KERNEL_DIR. We can't just
107# create the symlink in advance as the git fetcher can't cope with
108# the symlink.
109do_unpack[cleandirs] += " ${S} ${STAGING_KERNEL_DIR} ${B} ${STAGING_KERNEL_BUILDDIR}"
110do_clean[cleandirs] += " ${S} ${STAGING_KERNEL_DIR} ${B} ${STAGING_KERNEL_BUILDDIR}"
111base_do_unpack_append () {
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500112 s = d.getVar("S")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500113 if s[-1] == '/':
114 # drop trailing slash, so that os.symlink(kernsrc, s) doesn't use s as directory name and fail
115 s=s[:-1]
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500116 kernsrc = d.getVar("STAGING_KERNEL_DIR")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500117 if s != kernsrc:
118 bb.utils.mkdirhier(kernsrc)
119 bb.utils.remove(kernsrc, recurse=True)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500120 if d.getVar("EXTERNALSRC"):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500121 # With EXTERNALSRC S will not be wiped so we can symlink to it
122 os.symlink(s, kernsrc)
123 else:
124 import shutil
125 shutil.move(s, kernsrc)
126 os.symlink(kernsrc, s)
127}
128
129inherit kernel-arch deploy
130
131PACKAGES_DYNAMIC += "^kernel-module-.*"
132PACKAGES_DYNAMIC += "^kernel-image-.*"
133PACKAGES_DYNAMIC += "^kernel-firmware-.*"
134
135export OS = "${TARGET_OS}"
136export CROSS_COMPILE = "${TARGET_PREFIX}"
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500137export KBUILD_BUILD_USER = "oe-user"
138export KBUILD_BUILD_HOST = "oe-host"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500139
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500140KERNEL_PRIORITY ?= "${@int(d.getVar('PV').split('-')[0].split('+')[0].split('.')[0]) * 10000 + \
141 int(d.getVar('PV').split('-')[0].split('+')[0].split('.')[1]) * 100 + \
142 int(d.getVar('PV').split('-')[0].split('+')[0].split('.')[-1])}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500143
144KERNEL_RELEASE ?= "${KERNEL_VERSION}"
145
He Zhefe76b1e2016-05-25 04:47:16 -0400146# The directory where built kernel lies in the kernel tree
147KERNEL_OUTPUT_DIR ?= "arch/${ARCH}/boot"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500148KERNEL_IMAGEDEST = "boot"
149
150#
151# configuration
152#
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500153export CMDLINE_CONSOLE = "console=${@d.getVar("KERNEL_CONSOLE") or "ttyS0"}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500154
155KERNEL_VERSION = "${@get_kernelversion_headers('${B}')}"
156
157KERNEL_LOCALVERSION ?= ""
158
159# kernels are generally machine specific
160PACKAGE_ARCH = "${MACHINE_ARCH}"
161
162# U-Boot support
163UBOOT_ENTRYPOINT ?= "20008000"
164UBOOT_LOADADDRESS ?= "${UBOOT_ENTRYPOINT}"
165
166# Some Linux kernel configurations need additional parameters on the command line
167KERNEL_EXTRA_ARGS ?= ""
168
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600169EXTRA_OEMAKE = " HOSTCC="${BUILD_CC}" HOSTCPP="${BUILD_CPP}""
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500170KERNEL_ALT_IMAGETYPE ??= ""
171
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500172copy_initramfs() {
173 echo "Copying initramfs into ./usr ..."
174 # In case the directory is not created yet from the first pass compile:
175 mkdir -p ${B}/usr
176 # Find and use the first initramfs image archive type we find
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500177 rm -f ${B}/usr/${INITRAMFS_IMAGE_NAME}.cpio
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500178 for img in cpio cpio.gz cpio.lz4 cpio.lzo cpio.lzma cpio.xz; do
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500179 if [ -e "${DEPLOY_DIR_IMAGE}/${INITRAMFS_IMAGE_NAME}.$img" ]; then
180 cp ${DEPLOY_DIR_IMAGE}/${INITRAMFS_IMAGE_NAME}.$img ${B}/usr/.
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500181 case $img in
182 *gz)
183 echo "gzip decompressing image"
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500184 gunzip -f ${B}/usr/${INITRAMFS_IMAGE_NAME}.$img
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500185 break
186 ;;
187 *lz4)
188 echo "lz4 decompressing image"
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500189 lz4 -df ${B}/usr/${INITRAMFS_IMAGE_NAME}.$img
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500190 break
191 ;;
192 *lzo)
193 echo "lzo decompressing image"
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500194 lzop -df ${B}/usr/${INITRAMFS_IMAGE_NAME}.$img
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500195 break
196 ;;
197 *lzma)
198 echo "lzma decompressing image"
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500199 lzma -df ${B}/usr/${INITRAMFS_IMAGE_NAME}.$img
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500200 break
201 ;;
202 *xz)
203 echo "xz decompressing image"
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500204 xz -df ${B}/usr/${INITRAMFS_IMAGE_NAME}.$img
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500205 break
206 ;;
207 esac
208 fi
209 done
210 echo "Finished copy of initramfs into ./usr"
211}
212
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600213INITRAMFS_BASE_NAME ?= "initramfs-${PV}-${PR}-${MACHINE}-${DATETIME}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500214INITRAMFS_BASE_NAME[vardepsexclude] = "DATETIME"
215do_bundle_initramfs () {
216 if [ ! -z "${INITRAMFS_IMAGE}" -a x"${INITRAMFS_IMAGE_BUNDLE}" = x1 ]; then
217 echo "Creating a kernel image with a bundled initramfs..."
218 copy_initramfs
He Zhefe76b1e2016-05-25 04:47:16 -0400219 # Backing up kernel image relies on its type(regular file or symbolic link)
220 tmp_path=""
221 for type in ${KERNEL_IMAGETYPES} ; do
222 if [ -h ${KERNEL_OUTPUT_DIR}/$type ] ; then
223 linkpath=`readlink -n ${KERNEL_OUTPUT_DIR}/$type`
224 realpath=`readlink -fn ${KERNEL_OUTPUT_DIR}/$type`
225 mv -f $realpath $realpath.bak
226 tmp_path=$tmp_path" "$type"#"$linkpath"#"$realpath
227 elif [ -f ${KERNEL_OUTPUT_DIR}/$type ]; then
228 mv -f ${KERNEL_OUTPUT_DIR}/$type ${KERNEL_OUTPUT_DIR}/$type.bak
229 tmp_path=$tmp_path" "$type"##"
230 fi
231 done
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500232 use_alternate_initrd=CONFIG_INITRAMFS_SOURCE=${B}/usr/${INITRAMFS_IMAGE_NAME}.cpio
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500233 kernel_do_compile
He Zhefe76b1e2016-05-25 04:47:16 -0400234 # Restoring kernel image
235 for tp in $tmp_path ; do
236 type=`echo $tp|cut -d "#" -f 1`
237 linkpath=`echo $tp|cut -d "#" -f 2`
238 realpath=`echo $tp|cut -d "#" -f 3`
239 if [ -n "$realpath" ]; then
240 mv -f $realpath $realpath.initramfs
241 mv -f $realpath.bak $realpath
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600242 ln -sf $linkpath.initramfs ${B}/${KERNEL_OUTPUT_DIR}/$type.initramfs
He Zhefe76b1e2016-05-25 04:47:16 -0400243 else
244 mv -f ${KERNEL_OUTPUT_DIR}/$type ${KERNEL_OUTPUT_DIR}/$type.initramfs
245 mv -f ${KERNEL_OUTPUT_DIR}/$type.bak ${KERNEL_OUTPUT_DIR}/$type
246 fi
247 done
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500248 fi
249}
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600250do_bundle_initramfs[dirs] = "${B}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500251
252python do_devshell_prepend () {
253 os.environ["LDFLAGS"] = ''
254}
255
256addtask bundle_initramfs after do_install before do_deploy
257
258kernel_do_compile() {
259 unset CFLAGS CPPFLAGS CXXFLAGS LDFLAGS MACHINE
260 # The $use_alternate_initrd is only set from
261 # do_bundle_initramfs() This variable is specifically for the
262 # case where we are making a second pass at the kernel
263 # compilation and we want to force the kernel build to use a
264 # different initramfs image. The way to do that in the kernel
265 # is to specify:
266 # make ...args... CONFIG_INITRAMFS_SOURCE=some_other_initramfs.cpio
267 if [ "$use_alternate_initrd" = "" ] && [ "${INITRAMFS_TASK}" != "" ] ; then
268 # The old style way of copying an prebuilt image and building it
269 # is turned on via INTIRAMFS_TASK != ""
270 copy_initramfs
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500271 use_alternate_initrd=CONFIG_INITRAMFS_SOURCE=${B}/usr/${INITRAMFS_IMAGE_NAME}.cpio
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500272 fi
He Zhefe76b1e2016-05-25 04:47:16 -0400273 for typeformake in ${KERNEL_IMAGETYPE_FOR_MAKE} ; do
274 oe_runmake ${typeformake} CC="${KERNEL_CC}" LD="${KERNEL_LD}" ${KERNEL_EXTRA_ARGS} $use_alternate_initrd
He Zhefe76b1e2016-05-25 04:47:16 -0400275 done
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500276 # vmlinux.gz is not built by kernel
277 if (echo "${KERNEL_IMAGETYPES}" | grep -wq "vmlinux\.gz"); then
278 mkdir -p "${KERNEL_OUTPUT_DIR}"
279 gzip -9c < ${B}/vmlinux > "${KERNEL_OUTPUT_DIR}/vmlinux.gz"
280 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500281}
282
283do_compile_kernelmodules() {
284 unset CFLAGS CPPFLAGS CXXFLAGS LDFLAGS MACHINE
285 if (grep -q -i -e '^CONFIG_MODULES=y$' ${B}/.config); then
286 oe_runmake -C ${B} ${PARALLEL_MAKE} modules CC="${KERNEL_CC}" LD="${KERNEL_LD}" ${KERNEL_EXTRA_ARGS}
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500287
288 # Module.symvers gets updated during the
289 # building of the kernel modules. We need to
290 # update this in the shared workdir since some
291 # external kernel modules has a dependency on
292 # other kernel modules and will look at this
293 # file to do symbol lookups
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600294 cp ${B}/Module.symvers ${STAGING_KERNEL_BUILDDIR}/
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500295 else
296 bbnote "no modules to compile"
297 fi
298}
299addtask compile_kernelmodules after do_compile before do_strip
300
301kernel_do_install() {
302 #
303 # First install the modules
304 #
305 unset CFLAGS CPPFLAGS CXXFLAGS LDFLAGS MACHINE
306 if (grep -q -i -e '^CONFIG_MODULES=y$' .config); then
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500307 oe_runmake DEPMOD=echo MODLIB=${D}${nonarch_base_libdir}/modules/${KERNEL_VERSION} INSTALL_FW_PATH=${D}${nonarch_base_libdir}/firmware modules_install
308 rm "${D}${nonarch_base_libdir}/modules/${KERNEL_VERSION}/build"
309 rm "${D}${nonarch_base_libdir}/modules/${KERNEL_VERSION}/source"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500310 # If the kernel/ directory is empty remove it to prevent QA issues
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500311 rmdir --ignore-fail-on-non-empty "${D}${nonarch_base_libdir}/modules/${KERNEL_VERSION}/kernel"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500312 else
313 bbnote "no modules to install"
314 fi
315
316 #
317 # Install various kernel output (zImage, map file, config, module support files)
318 #
319 install -d ${D}/${KERNEL_IMAGEDEST}
320 install -d ${D}/boot
He Zhefe76b1e2016-05-25 04:47:16 -0400321 for type in ${KERNEL_IMAGETYPES} ; do
322 install -m 0644 ${KERNEL_OUTPUT_DIR}/${type} ${D}/${KERNEL_IMAGEDEST}/${type}-${KERNEL_VERSION}
323 done
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500324 install -m 0644 System.map ${D}/boot/System.map-${KERNEL_VERSION}
325 install -m 0644 .config ${D}/boot/config-${KERNEL_VERSION}
326 install -m 0644 vmlinux ${D}/boot/vmlinux-${KERNEL_VERSION}
327 [ -e Module.symvers ] && install -m 0644 Module.symvers ${D}/boot/Module.symvers-${KERNEL_VERSION}
328 install -d ${D}${sysconfdir}/modules-load.d
329 install -d ${D}${sysconfdir}/modprobe.d
330}
331do_install[prefuncs] += "package_get_auto_pr"
332
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600333# Must be ran no earlier than after do_kernel_checkout or else Makefile won't be in ${S}/Makefile
334do_kernel_version_sanity_check() {
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500335 if [ "x${KERNEL_VERSION_SANITY_SKIP}" = "x1" ]; then
336 exit 0
337 fi
338
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600339 # The Makefile determines the kernel version shown at runtime
340 # Don't use KERNEL_VERSION because the headers it grabs the version from aren't generated until do_compile
341 VERSION=$(grep "^VERSION =" ${S}/Makefile | sed s/.*=\ *//)
342 PATCHLEVEL=$(grep "^PATCHLEVEL =" ${S}/Makefile | sed s/.*=\ *//)
343 SUBLEVEL=$(grep "^SUBLEVEL =" ${S}/Makefile | sed s/.*=\ *//)
344 EXTRAVERSION=$(grep "^EXTRAVERSION =" ${S}/Makefile | sed s/.*=\ *//)
345
346 # Build a string for regex and a plain version string
347 reg="^${VERSION}\.${PATCHLEVEL}"
348 vers="${VERSION}.${PATCHLEVEL}"
349 if [ -n "${SUBLEVEL}" ]; then
350 # Ignoring a SUBLEVEL of zero is fine
351 if [ "${SUBLEVEL}" = "0" ]; then
352 reg="${reg}(\.${SUBLEVEL})?"
353 else
354 reg="${reg}\.${SUBLEVEL}"
355 vers="${vers}.${SUBLEVEL}"
356 fi
357 fi
358 vers="${vers}${EXTRAVERSION}"
359 reg="${reg}${EXTRAVERSION}"
360
361 if [ -z `echo ${PV} | grep -E "${reg}"` ]; then
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500362 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 -0600363 fi
364 exit 0
365}
366
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500367addtask shared_workdir after do_compile before do_compile_kernelmodules
368addtask shared_workdir_setscene
369
370do_shared_workdir_setscene () {
371 exit 1
372}
373
374emit_depmod_pkgdata() {
375 # Stash data for depmod
376 install -d ${PKGDESTWORK}/kernel-depmod/
377 echo "${KERNEL_VERSION}" > ${PKGDESTWORK}/kernel-depmod/kernel-abiversion
378 cp ${B}/System.map ${PKGDESTWORK}/kernel-depmod/System.map-${KERNEL_VERSION}
379}
380
381PACKAGEFUNCS += "emit_depmod_pkgdata"
382
383do_shared_workdir () {
384 cd ${B}
385
386 kerneldir=${STAGING_KERNEL_BUILDDIR}
387 install -d $kerneldir
388
389 #
390 # Store the kernel version in sysroots for module-base.bbclass
391 #
392
393 echo "${KERNEL_VERSION}" > $kerneldir/kernel-abiversion
394
395 # Copy files required for module builds
396 cp System.map $kerneldir/System.map-${KERNEL_VERSION}
397 cp Module.symvers $kerneldir/
398 cp .config $kerneldir/
399 mkdir -p $kerneldir/include/config
400 cp include/config/kernel.release $kerneldir/include/config/kernel.release
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600401 if [ -e certs/signing_key.pem ]; then
402 # The signing_key.* files are stored in the certs/ dir in
403 # newer Linux kernels
404 mkdir -p $kerneldir/certs
405 cp certs/signing_key.* $kerneldir/certs/
406 elif [ -e signing_key.priv ]; then
407 cp signing_key.* $kerneldir/
408 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500409
410 # We can also copy over all the generated files and avoid special cases
411 # like version.h, but we've opted to keep this small until file creep starts
412 # to happen
413 if [ -e include/linux/version.h ]; then
414 mkdir -p $kerneldir/include/linux
415 cp include/linux/version.h $kerneldir/include/linux/version.h
416 fi
417
418 # As of Linux kernel version 3.0.1, the clean target removes
419 # arch/powerpc/lib/crtsavres.o which is present in
420 # KBUILD_LDFLAGS_MODULE, making it required to build external modules.
421 if [ ${ARCH} = "powerpc" ]; then
422 mkdir -p $kerneldir/arch/powerpc/lib/
423 cp arch/powerpc/lib/crtsavres.o $kerneldir/arch/powerpc/lib/crtsavres.o
424 fi
425
426 if [ -d include/generated ]; then
427 mkdir -p $kerneldir/include/generated/
428 cp -fR include/generated/* $kerneldir/include/generated/
429 fi
430
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500431 if [ -d arch/${ARCH}/include/generated ]; then
432 mkdir -p $kerneldir/arch/${ARCH}/include/generated/
433 cp -fR arch/${ARCH}/include/generated/* $kerneldir/arch/${ARCH}/include/generated/
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500434 fi
435}
436
437# We don't need to stage anything, not the modules/firmware since those would clash with linux-firmware
438sysroot_stage_all () {
439 :
440}
441
442KERNEL_CONFIG_COMMAND ?= "oe_runmake_call -C ${S} O=${B} oldnoconfig || yes '' | oe_runmake -C ${S} O=${B} oldconfig"
443
He Zhefe76b1e2016-05-25 04:47:16 -0400444python check_oldest_kernel() {
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500445 oldest_kernel = d.getVar('OLDEST_KERNEL')
446 kernel_version = d.getVar('KERNEL_VERSION')
447 tclibc = d.getVar('TCLIBC')
He Zhefe76b1e2016-05-25 04:47:16 -0400448 if tclibc == 'glibc':
449 kernel_version = kernel_version.split('-', 1)[0]
450 if oldest_kernel and kernel_version:
451 if bb.utils.vercmp_string(kernel_version, oldest_kernel) < 0:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500452 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 -0400453}
454
455check_oldest_kernel[vardepsexclude] += "OLDEST_KERNEL KERNEL_VERSION"
456do_configure[prefuncs] += "check_oldest_kernel"
457
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500458kernel_do_configure() {
459 # fixes extra + in /lib/modules/2.6.37+
460 # $ scripts/setlocalversion . => +
461 # $ make kernelversion => 2.6.37
462 # $ make kernelrelease => 2.6.37+
463 touch ${B}/.scmversion ${S}/.scmversion
464
465 if [ "${S}" != "${B}" ] && [ -f "${S}/.config" ] && [ ! -f "${B}/.config" ]; then
466 mv "${S}/.config" "${B}/.config"
467 fi
468
469 # Copy defconfig to .config if .config does not exist. This allows
470 # recipes to manage the .config themselves in do_configure_prepend().
471 if [ -f "${WORKDIR}/defconfig" ] && [ ! -f "${B}/.config" ]; then
472 cp "${WORKDIR}/defconfig" "${B}/.config"
473 fi
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500474
475 ${KERNEL_CONFIG_COMMAND}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500476}
477
478do_savedefconfig() {
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600479 bbplain "Saving defconfig to:\n${B}/defconfig"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500480 oe_runmake -C ${B} savedefconfig
481}
482do_savedefconfig[nostamp] = "1"
483addtask savedefconfig after do_configure
484
485inherit cml1
486
487EXPORT_FUNCTIONS do_compile do_install do_configure
488
489# kernel-base becomes kernel-${KERNEL_VERSION}
He Zhefe76b1e2016-05-25 04:47:16 -0400490# kernel-image becomes kernel-image-${KERNEL_VERSION}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500491PACKAGES = "kernel kernel-base kernel-vmlinux kernel-image kernel-dev kernel-modules"
492FILES_${PN} = ""
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500493FILES_kernel-base = "${nonarch_base_libdir}/modules/${KERNEL_VERSION}/modules.order ${nonarch_base_libdir}/modules/${KERNEL_VERSION}/modules.builtin"
He Zhefe76b1e2016-05-25 04:47:16 -0400494FILES_kernel-image = ""
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500495FILES_kernel-dev = "/boot/System.map* /boot/Module.symvers* /boot/config* ${KERNEL_SRC_PATH} ${nonarch_base_libdir}/modules/${KERNEL_VERSION}/build"
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500496FILES_kernel-vmlinux = "/boot/vmlinux-${KERNEL_VERSION_NAME}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500497FILES_kernel-modules = ""
498RDEPENDS_kernel = "kernel-base"
He Zhefe76b1e2016-05-25 04:47:16 -0400499# Allow machines to override this dependency if kernel image files are
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500500# not wanted in images as standard
501RDEPENDS_kernel-base ?= "kernel-image"
502PKG_kernel-image = "kernel-image-${@legitimize_package_name('${KERNEL_VERSION}')}"
503RDEPENDS_kernel-image += "${@base_conditional('KERNEL_IMAGETYPE', 'vmlinux', 'kernel-vmlinux', '', d)}"
504PKG_kernel-base = "kernel-${@legitimize_package_name('${KERNEL_VERSION}')}"
505RPROVIDES_kernel-base += "kernel-${KERNEL_VERSION}"
506ALLOW_EMPTY_kernel = "1"
507ALLOW_EMPTY_kernel-base = "1"
508ALLOW_EMPTY_kernel-image = "1"
509ALLOW_EMPTY_kernel-modules = "1"
510DESCRIPTION_kernel-modules = "Kernel modules meta package"
511
512pkg_postinst_kernel-base () {
513 if [ ! -e "$D/lib/modules/${KERNEL_VERSION}" ]; then
514 mkdir -p $D/lib/modules/${KERNEL_VERSION}
515 fi
516 if [ -n "$D" ]; then
517 depmodwrapper -a -b $D ${KERNEL_VERSION}
518 else
519 depmod -a ${KERNEL_VERSION}
520 fi
521}
522
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500523PACKAGESPLITFUNCS_prepend = "split_kernel_packages "
524
525python split_kernel_packages () {
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500526 do_split_packages(d, root='${nonarch_base_libdir}/firmware', file_regex='^(.*)\.(bin|fw|cis|csp|dsp)$', output_pattern='kernel-firmware-%s', description='Firmware for %s', recursive=True, extra_depends='')
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500527}
528
529# Many scripts want to look in arch/$arch/boot for the bootable
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600530# image. This poses a problem for vmlinux and vmlinuz based
531# booting. This task arranges to have vmlinux and vmlinuz appear
532# in the normalized directory location.
533do_kernel_link_images() {
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500534 if [ ! -d "${B}/arch/${ARCH}/boot" ]; then
535 mkdir ${B}/arch/${ARCH}/boot
536 fi
537 cd ${B}/arch/${ARCH}/boot
538 ln -sf ../../../vmlinux
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600539 if [ -f ../../../vmlinuz ]; then
540 ln -sf ../../../vmlinuz
541 fi
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500542 if [ -f ../../../vmlinuz.bin ]; then
543 ln -sf ../../../vmlinuz.bin
544 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500545}
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500546addtask kernel_link_images after do_compile before do_strip
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500547
548do_strip() {
549 if [ -n "${KERNEL_IMAGE_STRIP_EXTRA_SECTIONS}" ]; then
He Zhefe76b1e2016-05-25 04:47:16 -0400550 if ! (echo "${KERNEL_IMAGETYPES}" | grep -wq "vmlinux"); then
551 bbwarn "image type(s) will not be stripped (not supported): ${KERNEL_IMAGETYPES}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500552 return
553 fi
554
555 cd ${B}
He Zhefe76b1e2016-05-25 04:47:16 -0400556 headers=`"$CROSS_COMPILE"readelf -S ${KERNEL_OUTPUT_DIR}/vmlinux | \
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500557 grep "^ \{1,\}\[[0-9 ]\{1,\}\] [^ ]" | \
558 sed "s/^ \{1,\}\[[0-9 ]\{1,\}\] //" | \
559 gawk '{print $1}'`
560
561 for str in ${KERNEL_IMAGE_STRIP_EXTRA_SECTIONS}; do {
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500562 if ! (echo "$headers" | grep -q "^$str$"); then
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500563 bbwarn "Section not found: $str";
564 fi
565
He Zhefe76b1e2016-05-25 04:47:16 -0400566 "$CROSS_COMPILE"strip -s -R $str ${KERNEL_OUTPUT_DIR}/vmlinux
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500567 }; done
568
569 bbnote "KERNEL_IMAGE_STRIP_EXTRA_SECTIONS is set, stripping sections:" \
570 "${KERNEL_IMAGE_STRIP_EXTRA_SECTIONS}"
571 fi;
572}
573do_strip[dirs] = "${B}"
574
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500575addtask strip before do_sizecheck after do_kernel_link_images
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500576
577# Support checking the kernel size since some kernels need to reside in partitions
578# with a fixed length or there is a limit in transferring the kernel to memory
579do_sizecheck() {
580 if [ ! -z "${KERNEL_IMAGE_MAXSIZE}" ]; then
581 invalid=`echo ${KERNEL_IMAGE_MAXSIZE} | sed 's/[0-9]//g'`
582 if [ -n "$invalid" ]; then
583 die "Invalid KERNEL_IMAGE_MAXSIZE: ${KERNEL_IMAGE_MAXSIZE}, should be an integerx (The unit is Kbytes)"
584 fi
He Zhefe76b1e2016-05-25 04:47:16 -0400585 for type in ${KERNEL_IMAGETYPES} ; do
586 size=`du -ks ${B}/${KERNEL_OUTPUT_DIR}/$type | awk '{print $1}'`
587 if [ $size -ge ${KERNEL_IMAGE_MAXSIZE} ]; then
588 warn "This kernel $type (size=$size(K) > ${KERNEL_IMAGE_MAXSIZE}(K)) is too big for your device. Please reduce the size of the kernel by making more of it modular."
589 fi
590 done
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500591 fi
592}
593do_sizecheck[dirs] = "${B}"
594
595addtask sizecheck before do_install after do_strip
596
He Zhefe76b1e2016-05-25 04:47:16 -0400597KERNEL_IMAGE_BASE_NAME ?= "${PKGE}-${PKGV}-${PKGR}-${MACHINE}-${DATETIME}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500598# Don't include the DATETIME variable in the sstate package signatures
599KERNEL_IMAGE_BASE_NAME[vardepsexclude] = "DATETIME"
He Zhefe76b1e2016-05-25 04:47:16 -0400600KERNEL_IMAGE_SYMLINK_NAME ?= "${MACHINE}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500601MODULE_IMAGE_BASE_NAME ?= "modules-${PKGE}-${PKGV}-${PKGR}-${MACHINE}-${DATETIME}"
602MODULE_IMAGE_BASE_NAME[vardepsexclude] = "DATETIME"
603MODULE_TARBALL_BASE_NAME ?= "${MODULE_IMAGE_BASE_NAME}.tgz"
604# Don't include the DATETIME variable in the sstate package signatures
605MODULE_TARBALL_SYMLINK_NAME ?= "modules-${MACHINE}.tgz"
606MODULE_TARBALL_DEPLOY ?= "1"
607
608kernel_do_deploy() {
He Zhefe76b1e2016-05-25 04:47:16 -0400609 for type in ${KERNEL_IMAGETYPES} ; do
610 base_name=${type}-${KERNEL_IMAGE_BASE_NAME}
611 install -m 0644 ${KERNEL_OUTPUT_DIR}/${type} ${DEPLOYDIR}/${base_name}.bin
612 done
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500613 if [ ${MODULE_TARBALL_DEPLOY} = "1" ] && (grep -q -i -e '^CONFIG_MODULES=y$' .config); then
614 mkdir -p ${D}/lib
615 tar -cvzf ${DEPLOYDIR}/${MODULE_TARBALL_BASE_NAME} -C ${D} lib
616 ln -sf ${MODULE_TARBALL_BASE_NAME} ${DEPLOYDIR}/${MODULE_TARBALL_SYMLINK_NAME}
617 fi
618
He Zhefe76b1e2016-05-25 04:47:16 -0400619 for type in ${KERNEL_IMAGETYPES} ; do
620 base_name=${type}-${KERNEL_IMAGE_BASE_NAME}
621 symlink_name=${type}-${KERNEL_IMAGE_SYMLINK_NAME}
622 ln -sf ${base_name}.bin ${DEPLOYDIR}/${symlink_name}.bin
623 ln -sf ${base_name}.bin ${DEPLOYDIR}/${type}
624 done
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500625
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500626 cd ${B}
627 # Update deploy directory
He Zhefe76b1e2016-05-25 04:47:16 -0400628 for type in ${KERNEL_IMAGETYPES} ; do
629 if [ -e "${KERNEL_OUTPUT_DIR}/${type}.initramfs" ]; then
630 echo "Copying deploy ${type} kernel-initramfs image and setting up links..."
631 initramfs_base_name=${type}-${INITRAMFS_BASE_NAME}
632 initramfs_symlink_name=${type}-initramfs-${MACHINE}
633 install -m 0644 ${KERNEL_OUTPUT_DIR}/${type}.initramfs ${DEPLOYDIR}/${initramfs_base_name}.bin
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600634 ln -sf ${initramfs_base_name}.bin ${DEPLOYDIR}/${initramfs_symlink_name}.bin
He Zhefe76b1e2016-05-25 04:47:16 -0400635 fi
636 done
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500637}
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500638do_deploy[cleandirs] = "${DEPLOYDIR}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500639do_deploy[dirs] = "${DEPLOYDIR} ${B}"
640do_deploy[prefuncs] += "package_get_auto_pr"
641
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500642addtask deploy after do_populate_sysroot do_packagedata
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500643
644EXPORT_FUNCTIONS do_deploy