以下步驟在MAC下測試通過:
首先是安裝CCache,
可以用homebrew
brew install --HEAD ccache
也可以用源碼安裝
git clone https://github.com/jrosdahl/ccache.git
cd ccache
./autogen.sh
./configure
make
make install
如果提示autoheader找不到,要先裝個automake
brew install automake
當然,如果提示brew找不到,要先裝一個homebrew
ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"
CCache裝好以後,需要配置一下環境變量:
vim ~/.bash_profile
加上如下配置:
export USE_CCACHE=1
export CCACHE_DIR=/Developer/ccache
export NDK_CCACHE=/usr/local/bin/ccache
保存退出
然後在bash下運行:
source ~/.bash_profile
讓設置生效。
然後再運行:
ccache -F 10G
這個命令是設置編譯文件緩存的大小,如果硬盤夠大,可以設50G。
最後找到NDK目錄,編輯$NDK_ROOT/build/core/default-build-commands.mk文件:
#
# IMPORTANT: The following definitions must use lazy assignment because
# the value of TOOLCHAIN_PREFIX or TARGET_CFLAGS can be changed later by
# the toolchain's setup.mk script.
#
ifneq ($(findstring ccc-analyzer,$(CC)),)
TARGET_CC = $(CC)
else
TARGET_CC = ccache $(TOOLCHAIN_PREFIX)gcc
endif
TARGET_CFLAGS =
TARGET_CONLYFLAGS =
ifneq ($(findstring c++-analyzer,$(CXX)),)
TARGET_CXX = $(CXX)
else
TARGET_CXX = ccache $(TOOLCHAIN_PREFIX)g++
endif
TARGET_CXXFLAGS = $(TARGET_CFLAGS) -fno-exceptions -fno-rtti
在如上所示位置加上ccache
配置完畢
測試一下效果:
切到coco2d-x根目錄,運行:
python build/android-build.py -p 10 cpp-tests
然後再開一個bash窗口,運行
ccache -s
這個命令是用來查看ccache的統計數據的
第一次編譯是建立緩存,在我的mbp i7 SSD下大概要7分鐘,會比沒有加速慢一些
如果出現編譯錯誤:
ccache找不到
需要檢查一下ccache是否安裝正確,可以在命令行上輸入ccache -V測試一下,如果有沒有輸出版本信息,就表明ccache沒有安裝成功,如果命令行裏測試通過,但編譯時仍提示ccache找不到,有可能是path設置不對,可以用絕對路徑試試。
用git clean -xdf 把編譯結果清掉(請注意:此命令慎用,會一併刪除所有沒有加入git管理的文件)
再編譯一次,就無比快了,不到半分鐘
用ccache -s 查看下數據:
cocos2dxs-Mac-mini:core cocos2dx$ ccache -s
cache directory /Developer/ccache
primary config /Developer/ccache/ccache.conf
secondary config (readonly) /usr/local/etc/ccache.conf
cache hit (direct) 8328
cache hit (preprocessed) 1
cache miss 2609
called for link 31
multiple source files 10939
compile failed 1
couldn't find the compiler 3
files in cache 6618
cache size 1.6 GB
max cache size 5.0 GB
如果cache hit/cache size/files in cache都是0,說明ccache沒有生效。
UPDATE: 這個辦法對其他NDK工程也適用