分類
基本教學

MacBook Pro 編譯 Android 核心

上一篇已建立並編譯 Android Source Tree ,這篇記錄 Android Kernel 的編譯
上篇提到的 Android Source Tree 並不包含 Kernel 的部分,因此當你想對 Kernel 作修改時 必須另外下載 Kernel 的部分
這篇也是照著 Google 提示的操作
先掛載 android.dmg 並移動到之前建立的 android 資料夾中,建立新資料夾(Android_KERNEL)以放置 Kernel 原始碼,如果想針對不同廠商核心編譯可以再建立方便識別的資料夾(panda)
使用 git 下載 Kernel

git clone https://android.googlesource.com/device/ti/panda

移動到 panda資料夾

cd panda

取得 commit message

git log --max-count=1 kernel

出現的訊息會類似下圖,我們需要的部分是 Built from kernel 後面那一串訊息
(cb5fc502c60be9305c5a007be335e860d9e7c0cb)

下載想編譯的kernel ,以google的例子下載 omap

git clone https://android.googlesource.com/kernel/common.git
git clone https://android.googlesource.com/kernel/exynos.git
git clone https://android.googlesource.com/kernel/goldfish.git
git clone https://android.googlesource.com/kernel/msm.git
git clone https://android.googlesource.com/kernel/omap.git
git clone https://android.googlesource.com/kernel/samsung.git
git clone https://android.googlesource.com/kernel/tegra.git

 
確認prebuilts-gcc在path中

export PATH=$(pwd)/prebuilts/gcc/linux-x86/arm/arm-eabi-4.6/bin:$PATH

編譯,其中 commit_from_first_step 填入(cb5fc502c60be9305c5a007be335e860d9e7c0cb)

export ARCH=arm
export SUBARCH=arm
export CROSS_COMPILE=arm-eabi-
cd omap
git checkout <commit_from_first_step>
make panda_defconfig
make
分類
基本教學

MacBook Pro 建立 Android Source Environment (不需 Ubuntu)

注意此篇不需建立 Ubuntu
規格: MacBook Pro (Lion) , Mac OS X 10.7 , Xcode 4.6.1
參考資料:AOSP官網
步驟直接從 Setting up Mac OS X build environment 開始即可
1.
建立 case-sensitive disk image
首先提到 Mac OS 是 case-insensitive (不區分大小寫) 但這會造成部分 git 指令的錯誤
所以建議我們製作 case-sensitive 的 disk image
你可以使用
A.launch Disk Utility and select “New Image”. A size of 25GB is the minimum to complete the build, larger numbers are more future-proof. Using sparse images saves space while allowing to grow later as the need arises. Be sure to select “case sensitive, journaled” as the volume format.
或者是
B.開啓終端機,輸入以下指令

hdiutil create -type SPARSE -fs 'Case-sensitive Journaled HFS+' -size 40g ~/android.dmg

(強烈建議指令用複製貼上)
完成後在 /User/使用者 會出現 android.dmg.sparseimage ,當掛載這個disk Image 在桌面上便會出現一個磁碟空間
預設掛載disk image後出現的磁碟空間名稱為 untitled ,可改為 android
2.
編輯 ~/.bash_profile
開啓終端機輸入

vi ~/.bash_profile

並加入以下內容
(注意 android.dmg 要看實際的名稱修改,有可能是android.dmg.sparsefile
而 /Volumes/android 中的android也是一樣,視情況修改)

mount the android file image
function mountAndroid { hdiutil attach ~/android.dmg -mountpoint /Volumes/android; }

提示: 操作的步驟為先按 a 進入INSERT模式,再貼上,離開時先按esc結束編輯,再輸入:wq 儲存離開
3.
安裝需要的套件
3-1.
首先安裝 Xcode ,建議 Xcode 版本需要 3.1.4(含)以上, 到 Apple Developer Site 下載
如果已經安裝的 Xcode 版本太高(e.g. 4.x),還需要 Mac OS 10.5 SDK
Mac OS X 10.5 SDK 安裝方式為:先到這裡左邊搜尋框輸入 xcode 3.1.4 並下載,下載完成後點擊 xcode314_2809_developerdvd.dmg
接著在桌面上會出現 Xcode Tools -> Packages -> MacOSX10.5.pkg ,點擊後更改安裝位置選擇 Developer ,完成後可以看到 Developer/SDKs 中多了  MacOSX10.5.sdk
3-2.
安裝 MacPorts,這部分也是照著google的提示
先把

export PATH=/opt/local/bin:$PATH

加到 ~/.bach_profile 中
接著取得 make , git , gpg packages
開啓終端機輸入

POSIXLY_CORRECT=1 sudo port install gmake libsdl git-core gnupg

如果系統是 MacOS 10.4,還要加入以下指令

POSIXLY_CORRECT=1 sudo port install bison

3-3.
為了消除 gmake 3.82 的bug, 建議安裝 gmake 3.81,
首先將以下指令加入 /opt/local/etc/macports/sources.conf 

file:///Users/Shared/dports

建立目錄

mkdir /Users/Shared/dports

移動到 /Users/Shared/dports 中,輸入

svn co --revision 50980 http://svn.macports.org/repository/macports/trunk/dports/devel/gmake/ devel/gmake/

輸入

portindex /Users/Shared/dports

最後安裝 gmake 3.81

sudo port install gmake @3.81


4.
解除 file descriptor limit
在 ~/.bash_profile 加入

set the number of open files to be 1024
ulimit -S -n 1024


5.
下載 Android Source Tree
5-1.
安裝 Repo
開啓終端機輸入

mkdir ~/bin
PATH=~/bin:$PATH

下載 Repo

curl https://dl-ssl.google.com/dl/googlesource/git-repo/repo > ~/bin/repo
chmod a+x ~/bin/repo

5-2.
初始化 Repo
先掛載步驟1.建立的disk image,接著桌面上會出現一個磁碟空間
開啓終端機輸入指令移動到該磁碟空間中並建立一個空的目錄(WORKING_DIRECTORY)

mkdir WORKING_DIRECTORY
cd WORKING_DIRECTORY

(WORKING_DIRECTORY,名稱可自取)
接著輸入初始化指令(這個是下載 master 版本,如果Mac OS 版本為10.7 強烈建議選擇)

repo init -u https://android.googlesource.com/platform/manifest

也可以看看別的分支版本,使用 -b 指定版本(這個是下載指定版本 android-4.0.1_r1)

repo init -u https://android.googlesource.com/platform/manifest -b android-4.0.1_r1

在過程中會出現輸入名字和email以及顏色測試,就按照需求輸入吧
(使用 repo 指令若出現 command not found,請輸入整個路徑如 ~/bin/repo init ….)
5-3.
下載 Android Source Tree (此下載時間相當久,3m速度大概要3~4小時)
終端機輸入

repo sync

6.
建立和執行
6-1.
初始化環境,終端機輸入

source build/envsetup.sh

6-2.
選擇 Target
終端機輸入

lunch full-eng

可以自由選擇輸入的參數

Build name Device Notes
full emulator fully configured with all languages, apps, input methods
full_maguro maguro full build running on Galaxy Nexus GSM/HSPA+ (“maguro”)
full_panda panda full build running on PandaBoard (“panda”)
and the BUILDTYPE is one of the following:
Buildtype Use
user limited access; suited for production
userdebug like “user” but with root access and debuggability; preferred for debugging
eng development configuration with additional debugging tools

6-3.
編譯
終端機輸入

make -j4

(4代表行程數)

7.
錯誤處理
第一次編譯會出現 strnlen 錯誤,

搜尋發現 此篇 有提到解決方法
簡單的說就是修改 ./external/elfutils/config-compat-darwin.h 中的 strnlen 方法為
#if __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 1070

static inline size_t strnlen (const char *__string, size_t __maxlen)
{
int len = 0;
while (__maxlen– && *__string++)
len++;
return len;
}

#endif
修正以後重新編譯會得到第二次錯誤

這次的錯誤是在 ld: illegal text-relocoation 開始,google發現老外有一樣的錯誤,原因眾說紛紜,有人說也許跟 Android Source 版本不同有關,這次只好選擇下載master版本
使用以下指令來取得 master 版本(原先發生錯誤版本為 Android 4.0.1_r1)

repo init -u https://android.googlesource.com/platform/manifest

一樣重複下載…(3~4個小時)

repo sync

初始化

source build/envsetup.sh

選擇 Target

lunch full-eng

編譯

make -j4

編譯完成!!!

跑模擬器
終端機輸入

emulator

基本上建立環境並不難,只是步驟稍微多了點,請先注意本地端Mac OS SDK版本,還有在
下載 Android Source Tree 以及編譯的時候通常都要2~3個小時,時間的花費相當驚人@@
下一篇會介紹 MacBook Pro 編譯 Android kernel