以下步骤在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工程也适用