- P1N2O / How-To-Compile-Android-Kernel.md
- EduApps-CDG / Building Linux Kernel With Android.md
- Tech Star
- 1. Download the “omap.git-android-omap-3.0.tar.gz” from Google repository https://android.googlesource.com/
- 2. Extract source:
- Getting prebuilt toolchain
- Set the path to toolchain
- CROSS_COMPILE kernel
- 1. Go inside the kernel source folder and enter following command
- Build the Kernel
- Compiling….
- Warnings:
- Errors:
- How to Build Android Kernel from source (Part 2)
- Share this:
- Like this:
- Related
- 2 comments on “ How to Build Android Kernel from source (Part 1) ”
- Leave a Reply Cancel reply
- Welcome to Tech Star World… !
- Blog Stats
- Kulani Mahadewa
- Follow Blog via Email
- Archives
- Recent Posts
P1N2O / How-To-Compile-Android-Kernel.md
This is a guide for noob’s (just like me) to get going with their Android Kernel Compilation.
NOTE: I myself am a bigginer and I’m trying to share my setup and how I do it — This guide might not be accurate (what I do might be completely wrong), experienced folks can help me and others here by suggesting changes to this gist. 😉
I use Arch Linux (beginners can try Manjaro) just because it’s has rolling-release model (install once and forget) and I don’t have to fiddle arround for packages.
Install the base-devel package group, which contains necessary packages for compilation such as make and gcc .
Downloading Kernel Source
It is recommended to create a separate build directory for your kernel(s). In this example, the directory kernelbuild will be created in the home directory: mkdir ~/kernelbuild
Guide to Compile an Android Kernel with Clang
Google compiles the Pixel 2 kernel with Clang. They shipped the device on Android 8.0 with a kernel compiled with Clang 4.0 (build.config commit and prebuilt kernel commit) and upgraded to Android 8.1 with a kernel compiled with Clang 5.0 (build.config commit and prebuilt kernel commit).
Google uses Clang’s link-time optimization and control flow integrity in the Pixel 3 kernel, hardening it against return oriented programming attacks (LTO commit, CFI commit).
Google started compiling all Chromebook 4.4 kernels with Clang in R67 (commit, LKML) and going forward, Clang is the default compiler for all future versions of the kernel (commit).
Further information including videos of talks on the motive behind compiling with Clang can be found in the ClangBuiltLinux wiki.
TL;DR: Helps find bugs, easier for Google since all of AOSP is compiled with Clang, compiler specific features such as link-time optimization, and better static analysis for higher code quality.
msm-4.14 and newer uses clang by default so there is no need for a patch stack.
How to compile the kernel with Clang (standalone)
NOTE: I am not going to write this for beginnings. I assume if you are smart enough to pick some commits, you are smart enough to know how to run git clone and know the paths of your system.
- Add the Clang commits to your kernel source
- Download/build a compatible Clang toolchain
- Download/build a copy of binutils
- Compile the kernel (for arm64, x86_64 is similar — example using AOSP’s toolchains):
make O=out ARCH=arm64 defconfig> PATH="/bin:/bin:$ " \ make -j$(nproc --all) O=out \ ARCH=arm64 \ CC=clang \ CLANG_TRIPLE=aarch64-linux-gnu- \ CROSS_COMPILE=aarch64-linux-android-
After compiling, you can verify the toolchain used by opening out/include/generated/compile.h and looking at the LINUX_COMPILER option.
- CLANG_TRIPLE is only needed when using AOSP’s version of Clang
- export CC=clang does not work. You need to pass CC=clang to make like above.
How to compile the kernel with Clang (inline with a custom ROM)
- Add the Clang commits to your kernel source
- Make sure your ROM has this commit
- Add the following to your BoardConfig.mk file in your device tree: TARGET_KERNEL_CLANG_COMPILE := true
To test and verify everything is working:
- Build a kernel image: m kernel or m bootimage
- Open the out/target/product/*/obj/KERNEL_OBJ/include/generated/compile.h file and look at the LINUX_COMPILER option.
Getting the Clang patchset
The core Clang patchset comes from mainline. It has been backported to three places:
If your kernel has 4.4.165 or 4.9.139 and newer, you automatically have the patchset and can start building with Clang!
The branches in this repository will be dedicated to adding this patchset when it does not exist and enhancing it by fixing all of the warnings from Clang (from mainline, the Pixel 2 and 3, msm-4.9/msm-4.14, and my own knowledge).
- msm-3.18-oreo — based on kernel.lnx.3.18.r33-rel. Uses msm-perf_defconfig .
- msm-3.18-pie — based on kernel.lnx.3.18.r34-rel. Uses msm-perf_defconfig .
- msm-3.18-android-10 — based on kernel.lnx.3.18.r41-rel. All relevant configs should build with -Werror .
- msm-4.4-oreo — based on kernel.lnx.4.4.r27-rel. Uses msmcortex-perf_defconfig .
- msm-4.4-pie — based on kernel.lnx.4.4.r35-rel. Uses msmcortex-perf_defconfig .
- msm-4.4-android-10 — based on kernel.lnx.4.4.r38-rel. All relevant configs should build with -Werror .
- msm-4.9-oreo — based on the latest Oreo branch for the Snapdragon 845 kernel.lnx.4.9.r7-rel. Uses sdm845-perf_defconfig .
- msm-4.9-pie — based on the latest Oreo branch for the Snapdragon 845 kernel.lnx.4.9.r11-rel. Uses sdm845-perf_defconfig .
- msm-4.9-android-10 — based on kernel.lnx.4.9.r25-rel. All relevant configs should build with -Werror .
The general structure of these commits is as follows:
- The core compilation support (if needed)
- Fixing Qualcomm specific drivers to compile with Clang
- Fixing warnings that come from code in mainline
- Fixing warnings that come from code outside of mainline
You should pick the commits that I have committed (nathanchance).
Additionally, there are fixes for:
Every time there is a branch update upstream, the branch will be rebased, there is no stable history here! Ideally, I will not need to add any commits but I will do my best to keep everything in the same order.
NOTE: 3.18 Clang is not supported officially by an OEM. I’ve merely added it here as I decided to support it with my Pixel XL kernel.
How to get a Clang toolchain
You can either use a prebuilt version of Clang (like from AOSP) or build a version yourself from the upstream sources.
For prebuilts, I recommend AOSP’s Clang, which is used for both the kernel and the platform so it is highly tested. You can git clone that repo to always have access to the latest version available, which is what I recommend. That is currently Clang 9.0.8. If you would like to just download the minimum version of Clang supported for the branches in this repo, direct tarball links are provided below:
- clang-4053586 for Oreo branches
- clang-4691093 for Pie branches
- clang-r353983c for Android 10 branches
- clang-r353983c for Android 10 branches
If you would like to build the latest version of Clang, you can do so with tc-build . There are a lot of fixes for the Linux kernel that happen in LLVM/Clang so staying up to date is critical. If you experience any issues with Clang and an Android kernel, please report them to this repo.
binutils are used for assembling (and usually linking) the Linux kernel right now. When using AOSP Clang, you should use AOSP’s GCC to avoid weird incompatibility issues. If you want source-built binutils, there is a build-binutils.py script available in tc-build
The preferred method for getting help is either opening an issue on this repository or joining the Linux kernel newbies chat on Telegram.
EduApps-CDG / Building Linux Kernel With Android.md
This guide shows how to build Linux on a Android Device and was made for people that doesn’t have s Computer.
Preparing the Environment
You need to install Termux from Google Play Store. Now we will download the Kernel so we need WGET:
apt-get update && apt-get --assume-yes install wget && cd ~ && wget https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.4.6.tar.xz
The next step is decompress that tarball with UNXZ and TAR (integrated with Termux):
unzx -d linux-5.4.6.tar.xz && tar -xf linux-5.4.6.tar
(Optional) Delete the Linux tarball:
Now we have the Linux Source on our Android, lets compile!
In Android, compile is a terror for Linux Kernel. First we need to create a config file.
If you had an Linux PC you could simply do this:
cp /boot/config-$(uname -r) linux-5.4.6/.config
But you dont have, so we need to do this:
If you have a Rooted Potato Phone you can simply:
cp /proc/config.gz ~ && gzip -d config.gz && mv config linux-5.4.6/.config && cd linux-5.46
If else, first you need to download it from our repository:
wget https://eduapps-cdg.github.io/kernel-configs/raw/master/android/$(getprop | grep -i ro.product.model | sed "s/\[ro.product.model\]: //" | sed "s/\[//" | sed "s/\]//").gz && mv $(getprop | grep -i ro.product.model | sed "s/\[ro.product.model\]: //" | sed "s/\[//" | sed "s/\]//").gz config.gz && gzip -d config.gz && mv config linux-5.4.6/.config && cd linux-5.46
If the file was not found, you must generate it. It has only one command, but this command is the worst! It’s the best choice if you are developing a new device. Complete the first command of Building and type:
Once solved the dilemma, you will not need to do it again. the next step is install the compilers:
apt-get install build-essential ncurses bison flex openssl libelf clang
This command will setup to build the Kernel with full power, if you have a single core device, it doesn’t help so much but works:
make -j $(nproc) make modules_install make install
And finally, the Last Step is: Enjoy the building time and drink a coffee!
Tech Star
I am using the kernel source of “omap” project. According to the android official web page the project is used for PandaBoard and Galaxy Nexus, and can be used as a starting point for work on TI OMAP chipsets.
1. Download the “omap.git-android-omap-3.0.tar.gz” from Google repository https://android.googlesource.com/
2. Extract source:
Open a new Terminal and enter following command.
- Create a directory structure as follows and move the downloaded omap.git-android-omap-3.0.tar.gz file into that folder
:~/android/kernel$ tar xvf omap.git-android-omap-3.0.tar.gz
Once you extracted the source: Goto ->android–>kernel–>arch–>arm–>configs folder. You can find the panda_defconfig file there.
Getting prebuilt toolchain
You can get prebuilt toolchain from the same Google repository.
2. Download the master branch and extract the source in to a new folder call toolchain, following the same steps as above.
:~/android/toolchain$ tar xvf arm-eabi-4.6-master.tar.gz
Set the path to toolchain
Here in my example the path to toolchain is : /home/tharaka/android/toolchain/bin. You can check the folder where it contains arm-linux-androideabi-gcc
CROSS_COMPILE kernel
Kernels are compiled with a program called gcc. The gcc comes with toolchain is an optimized version of gcc. Therefore here we do a cross-compile.
1. Go inside the kernel source folder and enter following command
make ARCH=arm CROSS_COMPILE=arm-linux-androideabi- panda_defconfig
Build the Kernel
Compiling….
While compiling it gives messages and warnings. We can ignore warnings but when there are errors compilation stops and terminate and we will have to fix the issuses.
Warnings:
Errors:
How to Build Android Kernel from source (Part 2)
Share this:
Like this:
Related
2 comments on “ How to Build Android Kernel from source (Part 1) ”
Leave a Reply Cancel reply
Welcome to Tech Star World… !
Blog Stats
Kulani Mahadewa
I’m a Ph.D. candidate with the Department of Computer Science, National University of Singapore. I am interested in Python, Java, and Android developments. My research interests are IoT security and privacy, program analysis, fuzzing, and protocol verification. This blog was started in 2012 while I was working as an intern in software engineering. However, I recently started posting content about my research work and research interests. I hope you enjoy my blog posts.
Follow Blog via Email
Archives
- March 2021 (3)
- October 2020 (1)
- July 2020 (1)
- May 2020 (2)
- April 2020 (2)
- March 2020 (6)
- February 2020 (3)
- January 2020 (1)
- December 2019 (1)
- June 2019 (1)
- April 2019 (3)
- March 2019 (6)
- February 2019 (8)
- January 2019 (3)
- January 2018 (9)
- November 2016 (1)
- August 2016 (2)
- May 2016 (2)
- February 2016 (1)
- January 2016 (2)
- December 2015 (3)
- September 2015 (1)
- August 2015 (1)
- May 2015 (2)
- April 2015 (1)
- March 2015 (6)
- February 2015 (4)
- January 2015 (7)
- December 2014 (2)
- October 2014 (5)
- July 2014 (1)
- June 2014 (1)
- May 2014 (1)
- April 2014 (3)
- March 2014 (1)
- September 2012 (1)
- August 2012 (5)
- July 2012 (9)