如何下載並編譯Android內核源碼goldfish(圖文)

參考文章:

http://blog.csdn.net/flydream0/article/details/7070392


開發環境:

Mac OS X 10.6.8


關於如何下載Android4.0源碼,請查看我的博客內另一篇文章(同樣是圖文教程):

http://blog.csdn.net/flydream0/article/details/7036156

如何編譯Android4.0源碼請看:

http://blog.csdn.net/flydream0/article/details/7046612


下面進入正題:

第一步:下載goldfish源碼 

假設Andorid源碼目錄爲${WORK_DIRECTORY}=/Volumes/AndroidDisk/android_2.3.7_r1

在Android源碼根目錄下新建kernel文件夾

[html] view plaincopy
  1. $mkdir kernel  
  2. $cd kernel  
下載源碼:

[html] view plaincopy
  1. $git clone http://android.googlesource.com/kernel/goldfish.git   

下載完畢如下圖:

此時在kernel目錄下會生成一個goldfish文件夾。進入此目錄:

[html] view plaincopy
  1. $cd goldfish  

此目錄下有一個隱藏的目錄.git,通過

[html] view plaincopy
  1. $ls -al  

可看到此目錄:


查看所有分支:

[html] view plaincopy
  1. git  branch -a  
如下圖:


check out:

[html] view plaincopy
  1. $git checkout remotes/origin/android-goldfish-2.6.29  


此時你會看到goldfish目錄下會出現很多文件:

[html] view plaincopy
  1. $ls  

 這個時候goldfish源碼就已經下下來了,接下來的事情就是編譯了.


第二步:編譯goldfish

導出交叉編譯工具目錄到$PATH環境變量中去.

[html] view plaincopy
  1. export PATH=$PATH:${WORK_DIRECTORY}/prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/bin  
  2. // Mac OS X下環境目錄設置不同,請注意改爲darwin-x86
  3. export PATH=$PATH:${WORK_DIRECTORY}/prebuilt/darwin-x86/toolchain/arm-eabi-4.4.3/bin

我們將使用上述這個目錄下的交叉編譯器arm-eabi-gcc

然後在glodfish目錄下用gedit打開Makefile文件,找到這兩行文字:

 # ARCH ?= (SUBARCH)
 # CROSS_COMPILE?=
修改爲:
ARCH ?= arm
CROSS_COMPILE     ?= arm-eabi-


[html] view plaincopy
  1. $gedit Makefile  

注意:ARTH ?=arm,的arm後邊不要有空格,不然就會出現如下類似錯誤:
Make:...../kernel/goldfish/arch/arm: Is a directory.  Stop.
害我白白浪費了幾個小時.

爲了確保環境參數正確,接下來執行下面兩條指令,否則有可能不能正常啓動模擬器.
  1. $ source build/envsetup.sh  //缺少這條指令,可能會導致無法編譯通過  
  2. $ lunch full-eng            //缺少這條指令,可能會導致無法啓動模塊器,系統報無法找到AVD,並要求你創建AVD。  

關閉gedit,接下來就開始make了,執行如下指令:
[html] view plaincopy
  1. $ make goldfish_armv7_defconfig  
  2. $ make goldfish_defconfig  // goldfish_armv7_defconfig文件估計有問題,在make後無法啓動模擬器,改成goldfish_defconfig就OK
  3. $ make  
注:用$make goldfish_defconfig這樣配置也可以編譯通過,模擬器也可以啓動,但是Android的開機畫機就顯示不了,$adb shell也死活連不上,原因就是這個goldfish_defconfig這個配置文件問題.

提示:
$make goldfish_armv7_defconfig指令的意思是將目錄WORKING_DIRECTORY/kernel/goldfish/arch/arm/configs/下的goldfish_armv7_defconfig文件內的Kconfig配置內容複製到WORKING_DIRECTORY/kernel/goldfish/目錄下的.config文件中,.config文件是一個隱藏目錄,保存着各個目錄下Kconfig文件的配置.

Mac OS X上編譯內核會在make過程中報錯:缺少elf.h
scripts/mod/mk_elfconfig.c:4:17: error: elf.h: No such file or directory
scripts/mod/mk_elfconfig.c: In function ‘main’:
scripts/mod/mk_elfconfig.c:9: error: ‘EI_NIDENT’ undeclared (first use in this function)
scripts/mod/mk_elfconfig.c:9: error: (Each undeclared identifier is reported only once
scripts/mod/mk_elfconfig.c:9: error: for each function it appears in.)
scripts/mod/mk_elfconfig.c:19: error: ‘ELFMAG’ undeclared (first use in this function)
scripts/mod/mk_elfconfig.c:19: error: ‘SELFMAG’ undeclared (first use in this function)
scripts/mod/mk_elfconfig.c:23: error: ‘EI_CLASS’ undeclared (first use in this function)
scripts/mod/mk_elfconfig.c:24: error: ‘ELFCLASS32’ undeclared (first use in this function)
scripts/mod/mk_elfconfig.c:27: error: ‘ELFCLASS64’ undeclared (first use in this function)
scripts/mod/mk_elfconfig.c:33: error: ‘EI_DATA’ undeclared (first use in this function)
scripts/mod/mk_elfconfig.c:34: error: ‘ELFDATA2LSB’ undeclared (first use in this function)
scripts/mod/mk_elfconfig.c:37: error: ‘ELFDATA2MSB’ undeclared (first use in this function)
scripts/mod/mk_elfconfig.c:9: warning: unused variable ‘ei’
make[2]: *** [scripts/mod/mk_elfconfig] Error 1
make[1]: *** [scripts/mod] Error 2
make: *** [scripts] Error 2
解決方案:
這是因爲 Mac  的 include 文件少了一個 elf.h
從網上(例如:http://www.rockbox.org/tracker/9006?getfile=16683)下載一個放到 scripts/mod 目錄,並且修改 mod 目錄裏面引用了 elf.h 的兩個文件(mk_elfconfig.c、modpost.h)就可以了。

最終結果如下圖所示:


這樣就表示編譯成功了.

[html] view plaincopy
  1. $ ls arch/arm/boot/  
可以看到zImage文件.


第三步:在模擬器中啓動編譯好的內核
 
  1. $ export PATH=$PATH:${WORK_DIRECTORY}/out/host/linux-x86/bin  
  2. $ export ANDROID_PRODUCT_OUT=${WORK_DIRECTORY}/out/target/product/generic  
  3. $ emulator -kernel ${WORK_DIRECTORY}/kernel/goldfish/arch/arm/boot/zImage -debug-all  

模擬器啓動界面:



進入模擬器從設置裏看版本信息:

從上圖可以看出當前Android版本是4.0.1,內核版本是2.6.29,說明成功了.

同樣也可以通過adb shell來查看內核版本信息,如下圖:

[html] view plaincopy
  1. $adb shell  
  2. #cd proc  
  3. #cat version  



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章