CCache是C/C++的编译器缓存。当相同的编译被再次编译时,进行检测并通过之前编译的缓存进行编译加速。所以我们可以通过这种方法给Cocos2d-x Android的编译加速。使用这种方法,在i7 CPU的Macbook Pro Retina上编译,我们的Cocos2d-x Android工程编译时间可以从10分钟减少到30秒!!
注意:以下操作仅对Mac OS X有效。
或者通过源码安装:
注意:默认的安装路径是 /usr/local/bin,我们需要拷贝ccache到/usr/bin/,不然ndk-build会找不到并报错
如果终端报错说找不到autoheader,这说明我们需要先安装automake:
但是,如果如果报错是说找不到brew,我们就必须先安装brew:
如果你看到帮助信息,恭喜你,安装成功了!
运行命令行:
这个命令将缓存大小设置为10G。当然,如果你的硬盘有足够的空间,你也可以将此设置为50G。
然后,我们跳转到NDK目录,如果你忘了这个路径,你可以使用以下命令来查看:
新建一个终端窗口,运行:
如果缓存的高速缓存命中(cache hit)和缓存大小(cache size)均为0,这就表示ccache没有运行,我们必须从新检查配置。
注意:以下操作仅对Mac OS X有效。
安装
我们使用homebrew:
1
|
brew install --HEAD ccache
|
1
2
3
4
5
6
7
|
git clone https:
//github.com/jrosdahl/ccache.git
cd ccache
./autogen.sh
./configure
make
make install
cp /usr/local/bin/ccache /usr/bin/
|
如果终端报错说找不到autoheader,这说明我们需要先安装automake:
1
|
brew install automake
|
ruby -e
"$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"
1
|
ccache
|
在android上配置编译环境
我们必须先设置以下环境变量来使用ccache:
1
2
3
4
|
vim ~/.bash_profile
Add following lines:
export USE_CCACHE=1
export NDK_CCACHE=/usr/local/bin/ccache
|
1
|
ccache -M 10G
|
然后,我们跳转到NDK目录,如果你忘了这个路径,你可以使用以下命令来查看:
1
|
which ndk-build
|
以下是我的输出结果:
1
|
/developer/android/android-ndk-r9b/ndk-build
|
所以,我的NDK_ROOT是:
1
|
/developer/android/android-ndk-r9b
|
打开文件:$NDK_ROOT/build/core/default-build-commands.mk
找到以下章节并加入ccache,如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
ifneq ($(findstring ccc-analyzer,$(CC)),)
TARGET_CC = $(CC)
else
TARGET_CC = ccache $(TOOLCHAIN_PREFIX)gcc #Add ccache support
endif
TARGET_CFLAGS =
TARGET_CONLYFLAGS =
ifneq ($(findstring c++-analyzer,$(CXX)),)
TARGET_CXX = $(CXX)
else
TARGET_CXX = ccache $(TOOLCHAIN_PREFIX)g++ #Add ccache support
endif
TARGET_CXXFLAGS = $(TARGET_CFLAGS) -fno-exceptions -fno-rtti
|
编译cocos2d-x游戏
选择cocos2d-x根目录,运行:
1
|
python build/android-build.py -p 10 cpp-tests
|
1
|
ccache -s
|
这个命令会打印出ccache状态,如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
cache directory /Users/heliclei/.ccache
primary config /Users/heliclei/.ccache/ccache.conf
secondary config (readonly) /usr/local/etc/ccache.conf
cache hit (direct) 13588
cache hit (preprocessed) 11145
cache miss 696
called
for
link 1
called
for
preprocessing 14
preprocessor error 1
can't use precompiled header 129
no input file 5
files in cache 32222
cache size 5.4 GB
max cache size 30.0 GB
|