Difference between revisions of "Compiling kernels with envkernel.sh"

From postmarketOS
Jump to navigation Jump to search
m (Set correct config folder)
(add "see also" from Mainlining_Guide#envkernel.sh)
Line 84: Line 84:
$ pmbootstrap flasher flash_kernel
$ pmbootstrap flasher flash_kernel
== See also ==
* {{MR|1703}} optional gcc6 argument
* {{MR|1747}} pmbootstrap build --envkernel
* [https://gitlab.com/postmarketOS/pmbootstrap/issues/1672#note_105668759 a quick Q&A about using envkernel.sh for the Nexus 5] (if you have more questions, please open a new issue or ask in the chat).

Revision as of 19:13, 27 February 2019

This page details how to compile a kernel manually using the envkernel.sh helper script. This workflow can used to quickly compile and test changes to the kernel source.

Usually when compiling a kernel, you would install all dependencies and a cross compiler in your host system. To make it easier, we have the envkernel.sh script (for fish shell: envkernel.fish). It sets up an Alpine Linux chroot with all the dependencies by using pmbootstrap internally, exports the environment variables to use the right cross compiler and creates an alias to make. This means, whenever you type make after sourcing that script, it will actually run make in the chroot.

Selecting a kernel

This section details how to fetch the kernel source of an existing postmarketOS device. Existing kernel aports can be found in the aports/device directory.

In the following example we will use the oneplus-bacon device. The kernel aport is in the aports/device/linux-oneplus-bacon directory. Within the APKBUILD is the source variable which lists the kernel source repository.

After determining the kernel you want to work with, download the source code.

$ git clone https://github.com/LineageOS/android_kernel_oneplus_msm8974.git
$ cd android_kernel_oneplus_msm8974

Preparing the source directory

To compile the current version of the kernel, check out the git revision found in the APKBUILD.

$ git checkout dd65620ba04a8c6ba0e30553c9c95388daefae02

Applying patches

Some kernel aports have patches applied on top of the remote source. You may want to apply these patches if they aren't in the source tree you have checked out.

For example oneplus-bacon has a patch which isn't in the git revision in the prior example.

$ git apply /code/pmboostrap/aports/device/linux-oneplus-bacon/02_gpu-msm-fix-gcc5-compile.patch

Kernels which can only be compiled with older GCC versions may need the compiler-gcc6.h. If this header exists in the kernel aport then copy it to the kernel source directory.

$ cp /code/pmboostrap/aports/device/linux-oneplus-bacon/compiler-gcc6.h include/linux/

Compile the kernel

The envkernel.sh script will set up a build environment based on the target device configured for pmbootstrap.

In this example we are using the oneplus-bacon device. So we should be sure to have run pmbootstrap init and select oneplus-bacon as the target device. Run the envkernel.sh script once in the kernel source directory to initialize the build environment. If the kernel can only be compiled with GCC6 then pass the --gcc6 flag.

$ source /code/pmbootstrap/envkernel.sh --gcc6

Copy the defconfig from the kernel aport to the kernel source directory.

$ cp /code/pmboostrap/aports/device/linux-oneplus-bacon/config-oneplus-bacon.armhf arch/arm/configs/oneplus_bacon_defconfig

Build the defconfig and kernel with the following commands.

$ make oneplus_bacon_defconfig
$ make

Run the kernel

Post make scripts

Some kernels require extra packaging steps such as dtbtool. The envkernel.sh build environment works within the pmbootstrap chroots. The run-script alias can be used to execute scripts within the source or build directory in the chroot.

In this example we are using the oneplus-bacon which requires dtbtool. Create a script with the dtbtool command named post-make.sh in the kernel source directory.

dtbTool -s 2048 -p "${srcdir}/scripts/dtc/" -o \
	"${builddir}/arch/arm/boot/dt.img" \

Run the script.

$ run-script post-make.sh

Packaging and flashing the kernel

A kernel compiled under envkernel.sh can be packaged by pmbootstrap by using the --envkernel build option.

$ pmbootstrap build --envkernel linux-oneplus-bacon

After compiling and packaging the kernel, it can be flashed to device using the standard pmbootstrap flash commands.

$ pmbootstrap flasher flash_kernel

See also