fadedbee
fadedbee

Reputation: 44745

How do you build a Linux Kernel for Android from the AOSP?

I've found that AOSP build.sh dist creates:

This newly-created msm-4.14 includes a .config file which looks like normal kernel configuration.

How can I compile this kernel for aarch64?

When I use make it prompts to regenerate an x86_64 config file, as .config correctly contains Aarch64 settings.

Perhaps I'm trying the wrong thing, but mm in kernel/msm-4.14 ignored changes to kernel configs, and build.sh dist took hours.

I want to be able to change kernel configuration and source code, and build new kernels in just a few minutes, as I can for desktop machines.

How should I do this for Android?

Upvotes: 5

Views: 7780

Answers (1)

Lakindu
Lakindu

Reputation: 950

This is the way to compile the kernel for your hardware and create the boot image :

$ cd <aosp_root_dir>
$ source ./build/envsetup.sh
$ lunch <product_name>-<build_variant>
# Example: lunch sdm660_64-userdebug

$ make bootimage -j4
# This compiles the kernel and copies it to 
# <aosp_root_dir>/out/target/product/<product_name>/kernel,
# and creates boot image at 
# <aosp_root_dir>/out/target/product/<product_name>/boot.img

If you still need to reduce the time it takes, then you have to find the individual commands that AOSP build system executes to compile the kernel and create the boot image.

You can find the commands from <aosp_root_dir>/out/verbose.log.gz. It is the compressed package that contains the verbose log of your last build. So, build the boot image using make bootimage command first, then extract the verbose.log.gz package and you will get verbose.log file.

Inside that file, find a log line that contains the text : defconfig, and that would most probably be the command that AOSP build system executes to compile the kernel.

In my case, these 2 are the commands I found from verbose log, which is used to compile the kernel :

# make sdm660_defconfig
${ANDROID_BUILD_TOP}/prebuilts/build-tools/linux-x86/bin/make -j1 \
    -C kernel/msm-4.4 \
    O=${ANDROID_PRODUCT_OUT}/obj/kernel/msm-4.4 \
    HOSTCC=${ANDROID_BUILD_TOP}/prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.17-4.8/bin/x86_64-linux-gcc \
    HOSTAR=${ANDROID_BUILD_TOP}/prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.17-4.8/bin/x86_64-linux-ar \
    HOSTLD=${ANDROID_BUILD_TOP}/prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.17-4.8/bin/x86_64-linux-ld \
    HOSTCFLAGS="-I/usr/include -I/usr/include/x86_64-linux-gnu -L/usr/lib -L/usr/lib/x86_64-linux-gnu" \
    HOSTLDFLAGS="-L/usr/lib -L/usr/lib/x86_64-linux-gnu" \
    ARCH=arm64 \
    CROSS_COMPILE=${ANDROID_BUILD_TOP}/prebuilts/gcc/linux-x86/aarch64/aarch64-linux-android-4.9/bin/aarch64-linux-android- \
    sdm660_defconfig

# make
${ANDROID_BUILD_TOP}/prebuilts/build-tools/linux-x86/bin/make -j4 \
    -C kernel/msm-4.4 \
    O=${ANDROID_PRODUCT_OUT}/obj/kernel/msm-4.4 \
    HOSTCC=${ANDROID_BUILD_TOP}/prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.17-4.8/bin/x86_64-linux-gcc \
    HOSTAR=${ANDROID_BUILD_TOP}/prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.17-4.8/bin/x86_64-linux-ar \
    HOSTLD=${ANDROID_BUILD_TOP}/prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.17-4.8/bin/x86_64-linux-ld \
    HOSTCFLAGS="-I/usr/include -I/usr/include/x86_64-linux-gnu -L/usr/lib -L/usr/lib/x86_64-linux-gnu" \
    HOSTLDFLAGS="-L/usr/lib -L/usr/lib/x86_64-linux-gnu" \
    ARCH=arm64 \
    CROSS_COMPILE=${ANDROID_BUILD_TOP}/prebuilts/gcc/linux-x86/aarch64/aarch64-linux-android-4.9/bin/aarch64-linux-android-

After compiling the kernel using above commands, according to verbose log it copies the compile output to <aosp_root_dir>/out/target/product/<product_name>/kernel. That is the kernel files used to create the boot image.

cp "${ANDROID_PRODUCT_OUT}/obj/kernel/msm-4.4/arch/arm64/boot/Image.gz" \
    "${ANDROID_PRODUCT_OUT}/kernel"

Finally, you can find the command that creates the boot image. According to my verbose log, following was the command :

${ANDROID_BUILD_TOP}/out/host/linux-x86/bin/mkbootimg  \
    --kernel ${ANDROID_PRODUCT_OUT}/kernel  \
    --ramdisk ${ANDROID_PRODUCT_OUT}/ramdisk-recovery.img \
    --cmdline "console=ttyMSM0,115200,n8 androidboot.console=ttyMSM0 earlycon=msm_serial_dm,0xc170000 androidboot.hardware=qcom user_debug=31 msm_rtb.filter=0x37 ehci-hcd.park=3 lpm_levels.sleep_disabled=1 sched_enable_hmp=1 sched_enable_power_aware=1 service_locator.enable=1 swiotlb=1 loop.max_part=7 buildvariant=eng veritykeyid=id:`openssl x509 -in build/target/product/security/verity.x509.pem -text | grep keyid | sed 's/://g' | tr -d '[:space:]' | tr '[:upper:]' '[:lower:]' | sed 's/keyid//g'`" \
    --base 0x00000000 \
    --pagesize 4096 \
    --os_version 10 \
    --os_patch_level yyyy-mm-dd \
    --header_version 1 \
    --output  ${ANDROID_PRODUCT_OUT}/boot.img

Like this, you can find the commands to compile the kernel for your hardware and create the boot image.

Upvotes: 7

Related Questions