1. 程式人生 > >利用ccache加快android原始碼和linux核心編譯速度的方法

利用ccache加快android原始碼和linux核心編譯速度的方法

一、android原始碼編譯加速

當你刪掉out/target目錄或者使用make clean清空輸出重新編譯原始碼的時候,編譯時間通常都很漫長。
其實這個問題很容易解決,Android官方為我們帶來了解決方案–ccache編譯器快取。
官方這麼講:
You can optionally tell the build to use the ccache compilation tool. Ccache acts as a compiler cache that can be used to speed-up rebuilds. This works very well if you do “make clean” often, or if you frequently switch between different build products.

設定方法:
在你home主目錄的.bashrc中加入:
export USE_CCACHE=1
如果你需要指定一個特殊的快取目錄,也需要在.bashrc中加入,不指定則為你當前使用者目錄下的.ccache。

export CCACHE_DIR=/home/mokee/.ccache

export PATH = /usr/lib/ccache:$PATH
在MoKee OpenSource主目錄中執行,50G~100G之間手動指定:
prebuilts/misc/linux-x86/ccache/ccache -M 50G
大功告成,開始編譯吧!

二、linux核心編譯加速

修改核心根目錄的Mafile檔案,在交叉編譯變數“CROSS_COMPILE”前新增ccache即可

$ vi Makefile

export KBUILD_BUILDHOST := $(SUBARCH)
ARCH            ?= arm
CROSS_COMPILE   ?= ccache /usr/local/arm/arm-2009q3/bin/arm-none-linux-gnueabi-
#CROSS_COMPILE   ?= /usr/local/arm/4.5.1/bin/arm-none-linux-gnueabi-
CROSS_COMPILE   ?= $(CONFIG_CROSS_COMPILE:"%"=%)


三、ccache的使用

簡介

   如果你經常編譯大型的C/C++工程,不使用ccache你就out了。 

   英文說明:cache is a compiler cache. It speeds up recompilation by caching previous compilations and detecting when the same compilation is being done again. Supported languages are C, C++, Objective-C and Objective-C++.

1. 安裝 ccache

sudo apt-get install ccache

2. Usage

ccache -s   # 顯示狀態引數 (s是英語status的縮寫,表示《狀態》)
ccache -C   # 清除快取(C是大寫的,是英語Clear的縮寫,表示《清除》)

details see : man ccache
使用ccache
  • 編譯指令前增加ccache. $ ccache gcc xxx
  • 建立軟連結。 $ ln -s ccache /usr/local/bin/gcc

建議使用第一種方式,因為ccache偶爾也犯暈,當由於它出現錯誤的時候, 很難看出端倪。曾在某次編譯某份程式碼時,ccache對某個編譯選項的判斷失誤導致編譯失敗,死活查不出來為什麼原因。所以當出現某些怪異的現象時,請用正常的方式編譯試試。

3. 在交叉編譯核心時,編譯速度也快了近十倍。 

time ./build.sh

real 3m4.146s

user 10m30.640s

sys 0m37.138s

make clean -j4

time ./build.sh

real    0m43.477s

user    1m0.564s

sys     0m14.913s

4.檢視ccache的狀態用watch ccache -s

Every 2.0s: ccache -s                                                                                              Thu Nov 23 16:35:37 2017

cache directory                     /home/litin/.ccache
cache hit (direct)                   154
cache hit (preprocessed)              15
cache miss                          3197
called for link                      587
called for preprocessing              23
can't use precompiled header           1
unsupported source language          158
no input file                        515
files in cache                      8534
cache size                         353.8 Mbytes
max cache size                      40.0 Gbytes


參考資料:http://blog.csdn.net/jiulousanti/article/details/23343687

http://blog.csdn.net/zgl07/article/details/41819495

http://blog.csdn.net/qq_27062249/article/details/53642444