android kernel编译过程详细

下载源代码

https://source.android.com/source/building-kernels
承接之前编译的android4.4.4的系统源码,所以说是模拟平台,用goldfish

1
sakura@ubuntu:~$ git clone https://aosp.tuna.tsinghua.edu.cn/kernel/goldfish

查看各种版本的goldfish

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
sakura@ubuntu:~$ cd goldfish/
sakura@ubuntu:~/goldfish$ git branch -a
* master
remotes/origin/HEAD -> origin/master
remotes/origin/android-3.10
remotes/origin/android-3.18
remotes/origin/android-3.4
remotes/origin/android-goldfish-2.6.29
remotes/origin/android-goldfish-3.10
remotes/origin/android-goldfish-3.10-k-dev
remotes/origin/android-goldfish-3.10-l-mr1-dev
remotes/origin/android-goldfish-3.10-m-dev
remotes/origin/android-goldfish-3.10-n-dev
remotes/origin/android-goldfish-3.18
remotes/origin/android-goldfish-3.18-dev
remotes/origin/android-goldfish-3.4
remotes/origin/android-goldfish-3.4-l-mr1-dev
remotes/origin/android-goldfish-4.4-dev
remotes/origin/heads/for/android-goldfish-3.18-dev
remotes/origin/linux-goldfish-3.0-wip
remotes/origin/master
sakura@ubuntu:~/goldfish$

我们选择3.4版本

切换分支

1
2
3
4
sakura@ubuntu:~/goldfish$ git checkout remotes/origin/android-goldfish-3.4 -b goldfish3.4
Checking out files: 100% (38854/38854), done.
Branch goldfish3.4 set up to track remote branch android-3.4 from origin.
Switched to a new branch 'goldfish3.4'

配置交叉编译链

首先,要翻墙,mac及其虚拟机可以参考我的博客
然后获取交叉编译链

1
sakura@ubuntu:~$ git clone https://android.googlesource.com/platform/prebuilts/gcc/linux-x86/arm/arm-eabi-4.6

设置环境变量

1
sakura@ubuntu:~$ sudo vim /etc/profile

在打开的文件最末添加

1
export PATH=/home/sakura/arm-eabi-4.6/bin:$PATH

然后使配置生效

1
sakura@ubuntu:~$ source /etc/profile

确认一下

1
2
sakura@ubuntu:~$ echo $PATH
/home/sakura/arm-eabi-4.6/bin:/home/sakura/Android/Sdk/platform-tools:/usr/local/java/jdk1.6.0_45/bin:/usr/local/java/jdk1.6.0_45/jre/bin:/usr/local/java/jdk1.6.0_45:/home/sakura/Android/Sdk/platform-tools:/usr/local/java/jdk1.6.0_45/bin:/usr/local/java/jdk1.6.0_45/jre/bin:/usr/local/java/jdk1.6.0_45:/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

配置编译选项,进行编译

1
2
3
4
5
6
7
8
9
10
11
12
13
14
sakura@ubuntu:~/goldfish$ export ARCH=arm
sakura@ubuntu:~/goldfish$ export CROSS_COMPILE=arm-eabi-
sakura@ubuntu:~/goldfish$ export SUBARCH=arm
sakura@ubuntu:~/goldfish$ make goldfish_armv7_defconfig
HOSTCC scripts/basic/fixdep
HOSTCC scripts/kconfig/conf.o
SHIPPED scripts/kconfig/zconf.tab.c
SHIPPED scripts/kconfig/zconf.lex.c
SHIPPED scripts/kconfig/zconf.hash.c
HOSTCC scripts/kconfig/zconf.tab.o
HOSTLD scripts/kconfig/conf
#
# configuration written to .config
#

增加内核编译选项,修改goldfish/.config配置文件

1
sakura@ubuntu:~/goldfish$ vim /home/sakura/goldfish/.config

添加以下两行

1
2
CONFIG_DEBUG_INFO=y #显示vmlinux符号
CONFIG_KGDB=y #开启kgdb

执行 make 命令进行编译

1
sakura@ubuntu:~/goldfish$ make

启动

编译成功后会显示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
OBJCOPY arch/arm/boot/Image
Kernel: arch/arm/boot/Image is ready
AS arch/arm/boot/compressed/head.o
GZIP arch/arm/boot/compressed/piggy.gzip
AS arch/arm/boot/compressed/piggy.gzip.o
CC arch/arm/boot/compressed/misc.o
CC arch/arm/boot/compressed/decompress.o
CC arch/arm/boot/compressed/string.o
SHIPPED arch/arm/boot/compressed/lib1funcs.S
AS arch/arm/boot/compressed/lib1funcs.o
SHIPPED arch/arm/boot/compressed/ashldi3.S
AS arch/arm/boot/compressed/ashldi3.o
LD arch/arm/boot/compressed/vmlinux
OBJCOPY arch/arm/boot/zImage
Kernel: arch/arm/boot/zImage is ready

以指定的内核启动模拟器

1
emulator -verbose -show-kernel -kernel ~/goldfish/arch/arm/boot/zImage

错误处理

输入emulator的时候报错

1
2
3
No command 'emulator' found, did you mean:
Command 'qemulator' from package 'qemulator' (universe)
emulator: command not found

我至今不知道为什么经常emulator就没了。。但是只要输入lunch,然后再make一下,几分钟就好了……