如何使用 CCache 进行 Cocos2d-x 编译加速

CCache是C/C++的编译器缓存。当相同的编译被再次编译时,进行检测并通过之前编译的缓存进行编译加速。所以我们可以通过这种方法给Cocos2d-x Android的编译加速。使用这种方法,在i7 CPU的Macbook Pro Retina上编译,我们的Cocos2d-x Android工程编译时间可以从10分钟减少到30秒!!

注意:以下操作仅对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/
注意:默认的安装路径是 /usr/local/bin,我们需要拷贝ccache到/usr/bin/,不然ndk-build会找不到并报错

如果终端报错说找不到autoheader,这说明我们需要先安装automake:
1
brew install automake
但是,如果如果报错是说找不到brew,我们就必须先安装brew:
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
这个命令将缓存大小设置为10G。当然,如果你的硬盘有足够的空间,你也可以将此设置为50G。
然后,我们跳转到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
如果缓存的高速缓存命中(cache hit)和缓存大小(cache size)均为0,这就表示ccache没有运行,我们必须从新检查配置。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值