'Using populate_sdk to include kernel headers

How do I include the linux kernel headers as part of the SDK package in Yocto?

I'm using Yocto 1.8 (fido) in an embedded project and want to do out-of-tree kernel module development. Currently, I can build my kernel modules (apart from bitbake) by pointing my $KERNEL_PATH to the poky/build/tmp/work-shared/<machine>/kernel-source/ directory when I run make. I don't want to do this in the long term, however, since others need to easily build the modules without installing and building a full image from bitbake.

I can generate an SDK using bitbake myimage -c populate_sdk. However, this doesn't include the kernel headers (all I ever see is sysroots/<mach>/usr/include/linux). How do I cause the kernel headers to be included in the SDK package? Also, I don't want the kernel headers to show up as part of my target image.

[Edit] My image recipe is as follows:

EXTRA_IMAGE_FEATURES_append = " eclipse-debug debug-tweaks"
TOOLCHAIN_HOST_TASK_append = " nativesdk-cmake"
IMAGE_INSTALL = "packagegroup-core-boot  ${CORE_IMAGE_EXTRA_INSTALL} util-linux kernel-modules netbase busybox base-passwd base-files sysvinit initscripts bash gdbserver strace sysfsutils dtc gawk ethtool grep sed wget  iptables oprofile net-tools dropbear rsync stress-ng rt-tests i2c-tools"
inherit core-image

The kernel I'm using is linux-altera-ltsi-rt in the meta-altera layer.



Solution 1:[1]

From the fido release, the handling of kernel builds has been changed. In previous releases, you could usually just skip down to the usage example below.

In fido or any 1.8+, if you want the kernel src and build system available in the SDK, you should add

TOOLCHAIN_TARGET_TASK_append = " kernel-devsrc"

to your image recipe. That will ensure that the new, kernel-devsrc package is installed into your toolchain.

The procedure below is just to ensure that the rest of the workflow is fully understood (even though it's not strictly part of the original question).

Usage Example

Lets assume a module Makefile as follows:

obj-m += hello-1.o
all:
    make -C  $(KERNEL_SRC) M=$(PWD) modules

clean:
    make -C  $(KERNEL_SRC) M=$(PWD) clean

Example taken from The Linux Kernel Module Programming Guide (Note that the actual commands needs to have a tab character for indentation).

Then you'll have to define KERNEL_SRC to be sysroots/<mach>/usr/src/kernel/, either in the Makefile, or from your make call. (Using a variable like KERNEL_SRC will ensure that your module recipe automatically picks the right location when building using bitbake).

To manually build your kernel module:

  1. Source the environment-* file for your SDK.

  2. Go to you modules directory.

  3. KERNEL_SRC=<sdk-install-path>/sysroots/<mach>/usr/src/kernel LDFLAGS="" make However, this will fail, as fixdep can't be found. We'll fix this manually.

  4. cd <sdk-install-path>/sysroots/<mach>/usr/src/kernel

  5. make modules_prepare

    If this needs to be run with sudo, be sure to source the environment file in the sudo environment: sudo bash -c "source <sdk-install-path>/environment-setup-<mach> && make modules_prepare"

  6. Go back to your modules directory.

  7. KERNEL_SRC=<sdk-install-path>/sysroots/<mach>/usr/src/kernel LDFLAGS="" make

This should now allow you to build your modules.

If you don't have the kernel source under sysroots/<mach>/usr/src/kernel/, we'll have to look into that.

Solution 2:[2]

anders answer is very good, but in recent versions of yocto the way to add kernel-devsrc seems to be

IMAGE_INSTALL += "kernel-devsrc"

which I found here: https://www.mail-archive.com/[email protected]/msg36448.html

Solution 3:[3]

With Yocto Zeus (3.0.x) add this to your image recipe:

    TOOLCHAIN_TARGET_TASK += "kernel-devsrc"

EDIT: Same for Gatesgarth (3.2.x) but the make scripts command has a new host dependency to libyaml-dev

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 bookmarc
Solution 2 othiman
Solution 3