blob: 14f41e9b177e608beffb92fdc433d1aec1f1e2d0 [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"
Brad Bishopd7bf8c12018-02-25 22:55:05 -05005PACKAGE_WRITE_DEPS += "depmodwrapper-cross"
Brad Bishop6e60e8b2018-02-01 10:27:11 -05006
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 Bishopd7bf8c12018-02-25 22:55:05 -050060 d.setVar('FILES_kernel-image-' + typelower, '/' + imagedest + '/' + type + '-${KERNEL_VERSION_NAME}' + ' /' + imagedest + '/' + type)
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 image = d.getVar('INITRAMFS_IMAGE')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050069 if image:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050070 d.appendVarFlag('do_bundle_initramfs', 'depends', ' ${INITRAMFS_IMAGE}:do_image_complete')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050071
72 # NOTE: setting INITRAMFS_TASK is for backward compatibility
73 # The preferred method is to set INITRAMFS_IMAGE, because
74 # this INITRAMFS_TASK has circular dependency problems
75 # if the initramfs requires kernel modules
Brad Bishop6e60e8b2018-02-01 10:27:11 -050076 image_task = d.getVar('INITRAMFS_TASK')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050077 if image_task:
78 d.appendVarFlag('do_configure', 'depends', ' ${INITRAMFS_TASK}')
79}
80
81# Here we pull in all various kernel image types which we support.
82#
83# In case you're wondering why kernel.bbclass inherits the other image
84# types instead of the other way around, the reason for that is to
85# maintain compatibility with various currently existing meta-layers.
86# By pulling in the various kernel image types here, we retain the
87# original behavior of kernel.bbclass, so no meta-layers should get
88# broken.
89#
90# KERNEL_CLASSES by default pulls in kernel-uimage.bbclass, since this
91# used to be the default behavior when only uImage was supported. This
92# variable can be appended by users who implement support for new kernel
93# image types.
94
95KERNEL_CLASSES ?= " kernel-uimage "
96inherit ${KERNEL_CLASSES}
97
98# Old style kernels may set ${S} = ${WORKDIR}/git for example
99# We need to move these over to STAGING_KERNEL_DIR. We can't just
100# create the symlink in advance as the git fetcher can't cope with
101# the symlink.
102do_unpack[cleandirs] += " ${S} ${STAGING_KERNEL_DIR} ${B} ${STAGING_KERNEL_BUILDDIR}"
103do_clean[cleandirs] += " ${S} ${STAGING_KERNEL_DIR} ${B} ${STAGING_KERNEL_BUILDDIR}"
104base_do_unpack_append () {
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500105 s = d.getVar("S")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500106 if s[-1] == '/':
107 # drop trailing slash, so that os.symlink(kernsrc, s) doesn't use s as directory name and fail
108 s=s[:-1]
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500109 kernsrc = d.getVar("STAGING_KERNEL_DIR")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500110 if s != kernsrc:
111 bb.utils.mkdirhier(kernsrc)
112 bb.utils.remove(kernsrc, recurse=True)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500113 if d.getVar("EXTERNALSRC"):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500114 # With EXTERNALSRC S will not be wiped so we can symlink to it
115 os.symlink(s, kernsrc)
116 else:
117 import shutil
118 shutil.move(s, kernsrc)
119 os.symlink(kernsrc, s)
120}
121
122inherit kernel-arch deploy
123
124PACKAGES_DYNAMIC += "^kernel-module-.*"
125PACKAGES_DYNAMIC += "^kernel-image-.*"
126PACKAGES_DYNAMIC += "^kernel-firmware-.*"
127
128export OS = "${TARGET_OS}"
129export CROSS_COMPILE = "${TARGET_PREFIX}"
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500130export KBUILD_BUILD_USER = "oe-user"
131export KBUILD_BUILD_HOST = "oe-host"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500132
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500133KERNEL_RELEASE ?= "${KERNEL_VERSION}"
134
He Zhefe76b1e2016-05-25 04:47:16 -0400135# The directory where built kernel lies in the kernel tree
136KERNEL_OUTPUT_DIR ?= "arch/${ARCH}/boot"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500137KERNEL_IMAGEDEST = "boot"
138
139#
140# configuration
141#
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500142export CMDLINE_CONSOLE = "console=${@d.getVar("KERNEL_CONSOLE") or "ttyS0"}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500143
144KERNEL_VERSION = "${@get_kernelversion_headers('${B}')}"
145
146KERNEL_LOCALVERSION ?= ""
147
148# kernels are generally machine specific
149PACKAGE_ARCH = "${MACHINE_ARCH}"
150
151# U-Boot support
152UBOOT_ENTRYPOINT ?= "20008000"
153UBOOT_LOADADDRESS ?= "${UBOOT_ENTRYPOINT}"
154
155# Some Linux kernel configurations need additional parameters on the command line
156KERNEL_EXTRA_ARGS ?= ""
157
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500158EXTRA_OEMAKE = " HOSTCC="${BUILD_CC} ${BUILD_CFLAGS} ${BUILD_LDFLAGS}" HOSTCPP="${BUILD_CPP}""
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500159KERNEL_ALT_IMAGETYPE ??= ""
160
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500161copy_initramfs() {
162 echo "Copying initramfs into ./usr ..."
163 # In case the directory is not created yet from the first pass compile:
164 mkdir -p ${B}/usr
165 # Find and use the first initramfs image archive type we find
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500166 rm -f ${B}/usr/${INITRAMFS_IMAGE_NAME}.cpio
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500167 for img in cpio cpio.gz cpio.lz4 cpio.lzo cpio.lzma cpio.xz; do
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500168 if [ -e "${DEPLOY_DIR_IMAGE}/${INITRAMFS_IMAGE_NAME}.$img" ]; then
169 cp ${DEPLOY_DIR_IMAGE}/${INITRAMFS_IMAGE_NAME}.$img ${B}/usr/.
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500170 case $img in
171 *gz)
172 echo "gzip decompressing image"
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500173 gunzip -f ${B}/usr/${INITRAMFS_IMAGE_NAME}.$img
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500174 break
175 ;;
176 *lz4)
177 echo "lz4 decompressing image"
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500178 lz4 -df ${B}/usr/${INITRAMFS_IMAGE_NAME}.$img
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500179 break
180 ;;
181 *lzo)
182 echo "lzo decompressing image"
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500183 lzop -df ${B}/usr/${INITRAMFS_IMAGE_NAME}.$img
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500184 break
185 ;;
186 *lzma)
187 echo "lzma decompressing image"
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500188 lzma -df ${B}/usr/${INITRAMFS_IMAGE_NAME}.$img
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500189 break
190 ;;
191 *xz)
192 echo "xz decompressing image"
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500193 xz -df ${B}/usr/${INITRAMFS_IMAGE_NAME}.$img
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500194 break
195 ;;
196 esac
197 fi
198 done
199 echo "Finished copy of initramfs into ./usr"
200}
201
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600202INITRAMFS_BASE_NAME ?= "initramfs-${PV}-${PR}-${MACHINE}-${DATETIME}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500203INITRAMFS_BASE_NAME[vardepsexclude] = "DATETIME"
204do_bundle_initramfs () {
205 if [ ! -z "${INITRAMFS_IMAGE}" -a x"${INITRAMFS_IMAGE_BUNDLE}" = x1 ]; then
206 echo "Creating a kernel image with a bundled initramfs..."
207 copy_initramfs
He Zhefe76b1e2016-05-25 04:47:16 -0400208 # Backing up kernel image relies on its type(regular file or symbolic link)
209 tmp_path=""
210 for type in ${KERNEL_IMAGETYPES} ; do
211 if [ -h ${KERNEL_OUTPUT_DIR}/$type ] ; then
212 linkpath=`readlink -n ${KERNEL_OUTPUT_DIR}/$type`
213 realpath=`readlink -fn ${KERNEL_OUTPUT_DIR}/$type`
214 mv -f $realpath $realpath.bak
215 tmp_path=$tmp_path" "$type"#"$linkpath"#"$realpath
216 elif [ -f ${KERNEL_OUTPUT_DIR}/$type ]; then
217 mv -f ${KERNEL_OUTPUT_DIR}/$type ${KERNEL_OUTPUT_DIR}/$type.bak
218 tmp_path=$tmp_path" "$type"##"
219 fi
220 done
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500221 use_alternate_initrd=CONFIG_INITRAMFS_SOURCE=${B}/usr/${INITRAMFS_IMAGE_NAME}.cpio
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500222 kernel_do_compile
He Zhefe76b1e2016-05-25 04:47:16 -0400223 # Restoring kernel image
224 for tp in $tmp_path ; do
225 type=`echo $tp|cut -d "#" -f 1`
226 linkpath=`echo $tp|cut -d "#" -f 2`
227 realpath=`echo $tp|cut -d "#" -f 3`
228 if [ -n "$realpath" ]; then
229 mv -f $realpath $realpath.initramfs
230 mv -f $realpath.bak $realpath
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600231 ln -sf $linkpath.initramfs ${B}/${KERNEL_OUTPUT_DIR}/$type.initramfs
He Zhefe76b1e2016-05-25 04:47:16 -0400232 else
233 mv -f ${KERNEL_OUTPUT_DIR}/$type ${KERNEL_OUTPUT_DIR}/$type.initramfs
234 mv -f ${KERNEL_OUTPUT_DIR}/$type.bak ${KERNEL_OUTPUT_DIR}/$type
235 fi
236 done
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500237 fi
238}
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600239do_bundle_initramfs[dirs] = "${B}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500240
241python do_devshell_prepend () {
242 os.environ["LDFLAGS"] = ''
243}
244
245addtask bundle_initramfs after do_install before do_deploy
246
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500247get_cc_option () {
248 # Check if KERNEL_CC supports the option "file-prefix-map".
249 # This option allows us to build images with __FILE__ values that do not
250 # contain the host build path.
251 if ${KERNEL_CC} -Q --help=joined | grep -q "\-ffile-prefix-map=<old=new>"; then
252 echo "-ffile-prefix-map=${S}=/kernel-source/"
253 fi
254}
255
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500256kernel_do_compile() {
257 unset CFLAGS CPPFLAGS CXXFLAGS LDFLAGS MACHINE
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500258 if [ "$BUILD_REPRODUCIBLE_BINARIES" = "1" ]; then
259 # kernel sources do not use do_unpack, so SOURCE_DATE_EPOCH may not
260 # be set....
261 if [ "$SOURCE_DATE_EPOCH" = "0" ]; then
262 olddir=`pwd`
263 cd ${S}
264 SOURCE_DATE_EPOCH=`git log -1 --pretty=%ct`
265 # git repo not guaranteed, so fall back to REPRODUCIBLE_TIMESTAMP_ROOTFS
266 if [ $? -ne 0 ]; then
267 SOURCE_DATE_EPOCH=${REPRODUCIBLE_TIMESTAMP_ROOTFS}
268 fi
269 cd $olddir
270 fi
271
272 ts=`LC_ALL=C date -d @$SOURCE_DATE_EPOCH`
273 export KBUILD_BUILD_TIMESTAMP="$ts"
274 export KCONFIG_NOTIMESTAMP=1
275 bbnote "KBUILD_BUILD_TIMESTAMP: $ts"
276 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500277 # The $use_alternate_initrd is only set from
278 # do_bundle_initramfs() This variable is specifically for the
279 # case where we are making a second pass at the kernel
280 # compilation and we want to force the kernel build to use a
281 # different initramfs image. The way to do that in the kernel
282 # is to specify:
283 # make ...args... CONFIG_INITRAMFS_SOURCE=some_other_initramfs.cpio
284 if [ "$use_alternate_initrd" = "" ] && [ "${INITRAMFS_TASK}" != "" ] ; then
285 # The old style way of copying an prebuilt image and building it
286 # is turned on via INTIRAMFS_TASK != ""
287 copy_initramfs
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500288 use_alternate_initrd=CONFIG_INITRAMFS_SOURCE=${B}/usr/${INITRAMFS_IMAGE_NAME}.cpio
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500289 fi
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500290 cc_extra=$(get_cc_option)
He Zhefe76b1e2016-05-25 04:47:16 -0400291 for typeformake in ${KERNEL_IMAGETYPE_FOR_MAKE} ; do
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500292 oe_runmake ${typeformake} CC="${KERNEL_CC} $cc_extra " LD="${KERNEL_LD}" ${KERNEL_EXTRA_ARGS} $use_alternate_initrd
He Zhefe76b1e2016-05-25 04:47:16 -0400293 done
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500294 # vmlinux.gz is not built by kernel
295 if (echo "${KERNEL_IMAGETYPES}" | grep -wq "vmlinux\.gz"); then
296 mkdir -p "${KERNEL_OUTPUT_DIR}"
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500297 gzip -9cn < ${B}/vmlinux > "${KERNEL_OUTPUT_DIR}/vmlinux.gz"
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500298 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500299}
300
301do_compile_kernelmodules() {
302 unset CFLAGS CPPFLAGS CXXFLAGS LDFLAGS MACHINE
303 if (grep -q -i -e '^CONFIG_MODULES=y$' ${B}/.config); then
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500304 cc_extra=$(get_cc_option)
305 oe_runmake -C ${B} ${PARALLEL_MAKE} modules CC="${KERNEL_CC} $cc_extra " LD="${KERNEL_LD}" ${KERNEL_EXTRA_ARGS}
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500306
307 # Module.symvers gets updated during the
308 # building of the kernel modules. We need to
309 # update this in the shared workdir since some
310 # external kernel modules has a dependency on
311 # other kernel modules and will look at this
312 # file to do symbol lookups
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600313 cp ${B}/Module.symvers ${STAGING_KERNEL_BUILDDIR}/
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500314 else
315 bbnote "no modules to compile"
316 fi
317}
318addtask compile_kernelmodules after do_compile before do_strip
319
320kernel_do_install() {
321 #
322 # First install the modules
323 #
324 unset CFLAGS CPPFLAGS CXXFLAGS LDFLAGS MACHINE
325 if (grep -q -i -e '^CONFIG_MODULES=y$' .config); then
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500326 oe_runmake DEPMOD=echo MODLIB=${D}${nonarch_base_libdir}/modules/${KERNEL_VERSION} INSTALL_FW_PATH=${D}${nonarch_base_libdir}/firmware modules_install
327 rm "${D}${nonarch_base_libdir}/modules/${KERNEL_VERSION}/build"
328 rm "${D}${nonarch_base_libdir}/modules/${KERNEL_VERSION}/source"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500329 # If the kernel/ directory is empty remove it to prevent QA issues
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500330 rmdir --ignore-fail-on-non-empty "${D}${nonarch_base_libdir}/modules/${KERNEL_VERSION}/kernel"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500331 else
332 bbnote "no modules to install"
333 fi
334
335 #
336 # Install various kernel output (zImage, map file, config, module support files)
337 #
338 install -d ${D}/${KERNEL_IMAGEDEST}
339 install -d ${D}/boot
He Zhefe76b1e2016-05-25 04:47:16 -0400340 for type in ${KERNEL_IMAGETYPES} ; do
341 install -m 0644 ${KERNEL_OUTPUT_DIR}/${type} ${D}/${KERNEL_IMAGEDEST}/${type}-${KERNEL_VERSION}
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500342 ln -sf ${type}-${KERNEL_VERSION} ${D}/${KERNEL_IMAGEDEST}/${type}
He Zhefe76b1e2016-05-25 04:47:16 -0400343 done
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500344 install -m 0644 System.map ${D}/boot/System.map-${KERNEL_VERSION}
345 install -m 0644 .config ${D}/boot/config-${KERNEL_VERSION}
346 install -m 0644 vmlinux ${D}/boot/vmlinux-${KERNEL_VERSION}
347 [ -e Module.symvers ] && install -m 0644 Module.symvers ${D}/boot/Module.symvers-${KERNEL_VERSION}
348 install -d ${D}${sysconfdir}/modules-load.d
349 install -d ${D}${sysconfdir}/modprobe.d
350}
351do_install[prefuncs] += "package_get_auto_pr"
352
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600353# Must be ran no earlier than after do_kernel_checkout or else Makefile won't be in ${S}/Makefile
354do_kernel_version_sanity_check() {
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500355 if [ "x${KERNEL_VERSION_SANITY_SKIP}" = "x1" ]; then
356 exit 0
357 fi
358
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600359 # The Makefile determines the kernel version shown at runtime
360 # Don't use KERNEL_VERSION because the headers it grabs the version from aren't generated until do_compile
361 VERSION=$(grep "^VERSION =" ${S}/Makefile | sed s/.*=\ *//)
362 PATCHLEVEL=$(grep "^PATCHLEVEL =" ${S}/Makefile | sed s/.*=\ *//)
363 SUBLEVEL=$(grep "^SUBLEVEL =" ${S}/Makefile | sed s/.*=\ *//)
364 EXTRAVERSION=$(grep "^EXTRAVERSION =" ${S}/Makefile | sed s/.*=\ *//)
365
366 # Build a string for regex and a plain version string
367 reg="^${VERSION}\.${PATCHLEVEL}"
368 vers="${VERSION}.${PATCHLEVEL}"
369 if [ -n "${SUBLEVEL}" ]; then
370 # Ignoring a SUBLEVEL of zero is fine
371 if [ "${SUBLEVEL}" = "0" ]; then
372 reg="${reg}(\.${SUBLEVEL})?"
373 else
374 reg="${reg}\.${SUBLEVEL}"
375 vers="${vers}.${SUBLEVEL}"
376 fi
377 fi
378 vers="${vers}${EXTRAVERSION}"
379 reg="${reg}${EXTRAVERSION}"
380
381 if [ -z `echo ${PV} | grep -E "${reg}"` ]; then
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500382 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 -0600383 fi
384 exit 0
385}
386
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500387addtask shared_workdir after do_compile before do_compile_kernelmodules
388addtask shared_workdir_setscene
389
390do_shared_workdir_setscene () {
391 exit 1
392}
393
394emit_depmod_pkgdata() {
395 # Stash data for depmod
396 install -d ${PKGDESTWORK}/kernel-depmod/
397 echo "${KERNEL_VERSION}" > ${PKGDESTWORK}/kernel-depmod/kernel-abiversion
398 cp ${B}/System.map ${PKGDESTWORK}/kernel-depmod/System.map-${KERNEL_VERSION}
399}
400
401PACKAGEFUNCS += "emit_depmod_pkgdata"
402
403do_shared_workdir () {
404 cd ${B}
405
406 kerneldir=${STAGING_KERNEL_BUILDDIR}
407 install -d $kerneldir
408
409 #
410 # Store the kernel version in sysroots for module-base.bbclass
411 #
412
413 echo "${KERNEL_VERSION}" > $kerneldir/kernel-abiversion
414
415 # Copy files required for module builds
416 cp System.map $kerneldir/System.map-${KERNEL_VERSION}
417 cp Module.symvers $kerneldir/
418 cp .config $kerneldir/
419 mkdir -p $kerneldir/include/config
420 cp include/config/kernel.release $kerneldir/include/config/kernel.release
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600421 if [ -e certs/signing_key.pem ]; then
422 # The signing_key.* files are stored in the certs/ dir in
423 # newer Linux kernels
424 mkdir -p $kerneldir/certs
425 cp certs/signing_key.* $kerneldir/certs/
426 elif [ -e signing_key.priv ]; then
427 cp signing_key.* $kerneldir/
428 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500429
430 # We can also copy over all the generated files and avoid special cases
431 # like version.h, but we've opted to keep this small until file creep starts
432 # to happen
433 if [ -e include/linux/version.h ]; then
434 mkdir -p $kerneldir/include/linux
435 cp include/linux/version.h $kerneldir/include/linux/version.h
436 fi
437
438 # As of Linux kernel version 3.0.1, the clean target removes
439 # arch/powerpc/lib/crtsavres.o which is present in
440 # KBUILD_LDFLAGS_MODULE, making it required to build external modules.
441 if [ ${ARCH} = "powerpc" ]; then
442 mkdir -p $kerneldir/arch/powerpc/lib/
443 cp arch/powerpc/lib/crtsavres.o $kerneldir/arch/powerpc/lib/crtsavres.o
444 fi
445
446 if [ -d include/generated ]; then
447 mkdir -p $kerneldir/include/generated/
448 cp -fR include/generated/* $kerneldir/include/generated/
449 fi
450
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500451 if [ -d arch/${ARCH}/include/generated ]; then
452 mkdir -p $kerneldir/arch/${ARCH}/include/generated/
453 cp -fR arch/${ARCH}/include/generated/* $kerneldir/arch/${ARCH}/include/generated/
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500454 fi
455}
456
457# We don't need to stage anything, not the modules/firmware since those would clash with linux-firmware
458sysroot_stage_all () {
459 :
460}
461
462KERNEL_CONFIG_COMMAND ?= "oe_runmake_call -C ${S} O=${B} oldnoconfig || yes '' | oe_runmake -C ${S} O=${B} oldconfig"
463
He Zhefe76b1e2016-05-25 04:47:16 -0400464python check_oldest_kernel() {
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500465 oldest_kernel = d.getVar('OLDEST_KERNEL')
466 kernel_version = d.getVar('KERNEL_VERSION')
467 tclibc = d.getVar('TCLIBC')
He Zhefe76b1e2016-05-25 04:47:16 -0400468 if tclibc == 'glibc':
469 kernel_version = kernel_version.split('-', 1)[0]
470 if oldest_kernel and kernel_version:
471 if bb.utils.vercmp_string(kernel_version, oldest_kernel) < 0:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500472 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 -0400473}
474
475check_oldest_kernel[vardepsexclude] += "OLDEST_KERNEL KERNEL_VERSION"
476do_configure[prefuncs] += "check_oldest_kernel"
477
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500478kernel_do_configure() {
479 # fixes extra + in /lib/modules/2.6.37+
480 # $ scripts/setlocalversion . => +
481 # $ make kernelversion => 2.6.37
482 # $ make kernelrelease => 2.6.37+
483 touch ${B}/.scmversion ${S}/.scmversion
484
485 if [ "${S}" != "${B}" ] && [ -f "${S}/.config" ] && [ ! -f "${B}/.config" ]; then
486 mv "${S}/.config" "${B}/.config"
487 fi
488
489 # Copy defconfig to .config if .config does not exist. This allows
490 # recipes to manage the .config themselves in do_configure_prepend().
491 if [ -f "${WORKDIR}/defconfig" ] && [ ! -f "${B}/.config" ]; then
492 cp "${WORKDIR}/defconfig" "${B}/.config"
493 fi
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500494
495 ${KERNEL_CONFIG_COMMAND}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500496}
497
498do_savedefconfig() {
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600499 bbplain "Saving defconfig to:\n${B}/defconfig"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500500 oe_runmake -C ${B} savedefconfig
501}
502do_savedefconfig[nostamp] = "1"
503addtask savedefconfig after do_configure
504
505inherit cml1
506
507EXPORT_FUNCTIONS do_compile do_install do_configure
508
509# kernel-base becomes kernel-${KERNEL_VERSION}
He Zhefe76b1e2016-05-25 04:47:16 -0400510# kernel-image becomes kernel-image-${KERNEL_VERSION}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500511PACKAGES = "kernel kernel-base kernel-vmlinux kernel-image kernel-dev kernel-modules"
512FILES_${PN} = ""
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500513FILES_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 -0400514FILES_kernel-image = ""
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500515FILES_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 -0500516FILES_kernel-vmlinux = "/boot/vmlinux-${KERNEL_VERSION_NAME}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500517FILES_kernel-modules = ""
518RDEPENDS_kernel = "kernel-base"
He Zhefe76b1e2016-05-25 04:47:16 -0400519# Allow machines to override this dependency if kernel image files are
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500520# not wanted in images as standard
521RDEPENDS_kernel-base ?= "kernel-image"
522PKG_kernel-image = "kernel-image-${@legitimize_package_name('${KERNEL_VERSION}')}"
523RDEPENDS_kernel-image += "${@base_conditional('KERNEL_IMAGETYPE', 'vmlinux', 'kernel-vmlinux', '', d)}"
524PKG_kernel-base = "kernel-${@legitimize_package_name('${KERNEL_VERSION}')}"
525RPROVIDES_kernel-base += "kernel-${KERNEL_VERSION}"
526ALLOW_EMPTY_kernel = "1"
527ALLOW_EMPTY_kernel-base = "1"
528ALLOW_EMPTY_kernel-image = "1"
529ALLOW_EMPTY_kernel-modules = "1"
530DESCRIPTION_kernel-modules = "Kernel modules meta package"
531
532pkg_postinst_kernel-base () {
533 if [ ! -e "$D/lib/modules/${KERNEL_VERSION}" ]; then
534 mkdir -p $D/lib/modules/${KERNEL_VERSION}
535 fi
536 if [ -n "$D" ]; then
537 depmodwrapper -a -b $D ${KERNEL_VERSION}
538 else
539 depmod -a ${KERNEL_VERSION}
540 fi
541}
542
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500543PACKAGESPLITFUNCS_prepend = "split_kernel_packages "
544
545python split_kernel_packages () {
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500546 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 -0500547}
548
549# Many scripts want to look in arch/$arch/boot for the bootable
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600550# image. This poses a problem for vmlinux and vmlinuz based
551# booting. This task arranges to have vmlinux and vmlinuz appear
552# in the normalized directory location.
553do_kernel_link_images() {
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500554 if [ ! -d "${B}/arch/${ARCH}/boot" ]; then
555 mkdir ${B}/arch/${ARCH}/boot
556 fi
557 cd ${B}/arch/${ARCH}/boot
558 ln -sf ../../../vmlinux
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600559 if [ -f ../../../vmlinuz ]; then
560 ln -sf ../../../vmlinuz
561 fi
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500562 if [ -f ../../../vmlinuz.bin ]; then
563 ln -sf ../../../vmlinuz.bin
564 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500565}
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500566addtask kernel_link_images after do_compile before do_strip
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500567
568do_strip() {
569 if [ -n "${KERNEL_IMAGE_STRIP_EXTRA_SECTIONS}" ]; then
He Zhefe76b1e2016-05-25 04:47:16 -0400570 if ! (echo "${KERNEL_IMAGETYPES}" | grep -wq "vmlinux"); then
571 bbwarn "image type(s) will not be stripped (not supported): ${KERNEL_IMAGETYPES}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500572 return
573 fi
574
575 cd ${B}
He Zhefe76b1e2016-05-25 04:47:16 -0400576 headers=`"$CROSS_COMPILE"readelf -S ${KERNEL_OUTPUT_DIR}/vmlinux | \
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500577 grep "^ \{1,\}\[[0-9 ]\{1,\}\] [^ ]" | \
578 sed "s/^ \{1,\}\[[0-9 ]\{1,\}\] //" | \
579 gawk '{print $1}'`
580
581 for str in ${KERNEL_IMAGE_STRIP_EXTRA_SECTIONS}; do {
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500582 if ! (echo "$headers" | grep -q "^$str$"); then
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500583 bbwarn "Section not found: $str";
584 fi
585
He Zhefe76b1e2016-05-25 04:47:16 -0400586 "$CROSS_COMPILE"strip -s -R $str ${KERNEL_OUTPUT_DIR}/vmlinux
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500587 }; done
588
589 bbnote "KERNEL_IMAGE_STRIP_EXTRA_SECTIONS is set, stripping sections:" \
590 "${KERNEL_IMAGE_STRIP_EXTRA_SECTIONS}"
591 fi;
592}
593do_strip[dirs] = "${B}"
594
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500595addtask strip before do_sizecheck after do_kernel_link_images
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500596
597# Support checking the kernel size since some kernels need to reside in partitions
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500598# with a fixed length or there is a limit in transferring the kernel to memory.
599# If more than one image type is enabled, warn on any that don't fit but only fail
600# if none fit.
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500601do_sizecheck() {
602 if [ ! -z "${KERNEL_IMAGE_MAXSIZE}" ]; then
603 invalid=`echo ${KERNEL_IMAGE_MAXSIZE} | sed 's/[0-9]//g'`
604 if [ -n "$invalid" ]; then
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500605 die "Invalid KERNEL_IMAGE_MAXSIZE: ${KERNEL_IMAGE_MAXSIZE}, should be an integer (The unit is Kbytes)"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500606 fi
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500607 at_least_one_fits=
He Zhefe76b1e2016-05-25 04:47:16 -0400608 for type in ${KERNEL_IMAGETYPES} ; do
609 size=`du -ks ${B}/${KERNEL_OUTPUT_DIR}/$type | awk '{print $1}'`
610 if [ $size -ge ${KERNEL_IMAGE_MAXSIZE} ]; then
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500611 bbwarn "This kernel $type (size=$size(K) > ${KERNEL_IMAGE_MAXSIZE}(K)) is too big for your device."
612 else
613 at_least_one_fits=y
He Zhefe76b1e2016-05-25 04:47:16 -0400614 fi
615 done
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500616 if [ -z "$at_least_one_fits" ]; then
617 die "All kernel images are too big for your device. Please reduce the size of the kernel by making more of it modular."
618 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500619 fi
620}
621do_sizecheck[dirs] = "${B}"
622
623addtask sizecheck before do_install after do_strip
624
He Zhefe76b1e2016-05-25 04:47:16 -0400625KERNEL_IMAGE_BASE_NAME ?= "${PKGE}-${PKGV}-${PKGR}-${MACHINE}-${DATETIME}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500626# Don't include the DATETIME variable in the sstate package signatures
627KERNEL_IMAGE_BASE_NAME[vardepsexclude] = "DATETIME"
He Zhefe76b1e2016-05-25 04:47:16 -0400628KERNEL_IMAGE_SYMLINK_NAME ?= "${MACHINE}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500629MODULE_IMAGE_BASE_NAME ?= "modules-${PKGE}-${PKGV}-${PKGR}-${MACHINE}-${DATETIME}"
630MODULE_IMAGE_BASE_NAME[vardepsexclude] = "DATETIME"
631MODULE_TARBALL_BASE_NAME ?= "${MODULE_IMAGE_BASE_NAME}.tgz"
632# Don't include the DATETIME variable in the sstate package signatures
633MODULE_TARBALL_SYMLINK_NAME ?= "modules-${MACHINE}.tgz"
634MODULE_TARBALL_DEPLOY ?= "1"
635
636kernel_do_deploy() {
He Zhefe76b1e2016-05-25 04:47:16 -0400637 for type in ${KERNEL_IMAGETYPES} ; do
638 base_name=${type}-${KERNEL_IMAGE_BASE_NAME}
639 install -m 0644 ${KERNEL_OUTPUT_DIR}/${type} ${DEPLOYDIR}/${base_name}.bin
640 done
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500641 if [ ${MODULE_TARBALL_DEPLOY} = "1" ] && (grep -q -i -e '^CONFIG_MODULES=y$' .config); then
642 mkdir -p ${D}/lib
643 tar -cvzf ${DEPLOYDIR}/${MODULE_TARBALL_BASE_NAME} -C ${D} lib
644 ln -sf ${MODULE_TARBALL_BASE_NAME} ${DEPLOYDIR}/${MODULE_TARBALL_SYMLINK_NAME}
645 fi
646
He Zhefe76b1e2016-05-25 04:47:16 -0400647 for type in ${KERNEL_IMAGETYPES} ; do
648 base_name=${type}-${KERNEL_IMAGE_BASE_NAME}
649 symlink_name=${type}-${KERNEL_IMAGE_SYMLINK_NAME}
650 ln -sf ${base_name}.bin ${DEPLOYDIR}/${symlink_name}.bin
651 ln -sf ${base_name}.bin ${DEPLOYDIR}/${type}
652 done
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500653
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500654 cd ${B}
655 # Update deploy directory
He Zhefe76b1e2016-05-25 04:47:16 -0400656 for type in ${KERNEL_IMAGETYPES} ; do
657 if [ -e "${KERNEL_OUTPUT_DIR}/${type}.initramfs" ]; then
658 echo "Copying deploy ${type} kernel-initramfs image and setting up links..."
659 initramfs_base_name=${type}-${INITRAMFS_BASE_NAME}
660 initramfs_symlink_name=${type}-initramfs-${MACHINE}
661 install -m 0644 ${KERNEL_OUTPUT_DIR}/${type}.initramfs ${DEPLOYDIR}/${initramfs_base_name}.bin
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600662 ln -sf ${initramfs_base_name}.bin ${DEPLOYDIR}/${initramfs_symlink_name}.bin
He Zhefe76b1e2016-05-25 04:47:16 -0400663 fi
664 done
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500665}
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500666do_deploy[cleandirs] = "${DEPLOYDIR}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500667do_deploy[dirs] = "${DEPLOYDIR} ${B}"
668do_deploy[prefuncs] += "package_get_auto_pr"
669
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500670addtask deploy after do_populate_sysroot do_packagedata
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500671
672EXPORT_FUNCTIONS do_deploy
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500673
674# Add using Device Tree support
675inherit kernel-devicetree