您的位置:首页 > 移动开发 > Android开发

Android使用ccache减少编译时间

2015-10-16 16:09 645 查看
先看看官方说明:

Optimizing a build environment (optional)

Setting up 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 use make clean often, or if you frequently switch between different build products.

Put the following in your .bashrc (or equivalent):

export USE_CCACHE=1


By default the cache will be stored in ~/.ccache. If your home directory is on NFS or some other non-local filesystem, you will want to specify the directory in your .bashrc file as well:

export CCACHE_DIR=<path-to-your-cache-directory>


The suggested cache size is 50-100GB. You will need to run the following command once you have downloaded the source code:

prebuilts/misc/linux-x86/ccache/ccache -M 50G


On Mac OS, you should replace linux-x86 with darwin-x86:

prebuilts/misc/darwin-x86/ccache/ccache -M 50G


When building Ice Cream Sandwich (4.0.x) or older, ccache is in a different location:

prebuilt/linux-x86/ccache/ccache -M 50G


This setting is stored in the CCACHE_DIR and is persistent.

On Linux, you can watch ccache being used by doing the following:

$ watch -n1 -d prebuilts/misc/linux-x86/ccache/ccache -s


ccache(“compiler cache”的缩写)是一个编译器缓存,该工具会高速缓存编译生成的信息,并在编译的特定部分使用高速缓存的信息, 比如头文件,这样就节省了通常使用cpp解析这些信息所需要的时间。如果某头文件中包含对其他头文件的引用,ccache会用那个文件的 cpp-parsed版本来取代include声明。ccache只是将最终的文本拷贝到文件中,使得它可以立即被编译,而不是真正去读取、理解并解释其内容。ccache是以空间换取速度,ccache非常适合经常make clean(或删除out目录)后重新编译的情况。

配置方法如下:

1. 在~/.bashrc中添加(或者/etc/profile文件中):

#ccache
export USE_CCACHE=1
export CCACHE_DIR=/<path_of_your_choice>/.ccache


默认情况下cache(缓存)会保存在~/.ccache目录下,如果主目录位于NFS或其他非本地文件系统上, 设置cache目录位置:

export CCACHE_DIR=<path-to-your-cache-directory>


注:配置.bashrc后注意source改文件,否则cache(缓存)会保存在~/.ccache目录下,而不是你设置的目录。

2. 使用android源码prebuilts目录下面的ccache工具初始化该文件夹

推荐的cache目录大小为50-100GB,在命令行执行以下命令:

prebuilt/linux-x86/ccache/ccache -M 50G


注:以上命令需要在你所下载的代码的根目录下面执行

该设置会保存到CCACHE_DIR中,且该命令是长效的,不会因系统重启而失效。使用ccache第一次编译后能够明显提高make clean以后再次的编译速度。使用ccache之后,第一次编译会时间久一点,之后每次编译速度都会有提升,降低所有使用gcc的编译时间,大约25%~30%。

3. 查看ccahe使用情况

$ watch -n1 -d prebuilts/misc/linux-x86/ccache/ccache -s


以上命令需要在你所下载的代码的根目录下面执行

注:可以通过ccache -s查看cache信息,如果满了可以用ccache -C清除所有。在满了状态下编译新codebase,将会旧的那个清除,这会降低效率,所以cache一定要设置正确!

4. 开启kernel ccache

(1). 以上ccache只对编译android有效,kernel部分需要额外设置才行

(2). alps/kernel/Makefile文件中的

AS      = $(CROSS_COMPILE)as
LD      = $(CROSS_COMPILE)ld
CC      = $(CROSS_COMPILE)gcc
CPP     = $(CC) -E


之后添加:

ifneq ($(USE_CCACHE),)
export CCACHE_COMPILERCHECK := content
export CCACHE_SLOPPINESS := time_macros,include_file_mtime,file_macro
export CCACHE_BASEDIR := /
ccache := $(strip $(wildcard $(PWD)/../prebuilts/misc/linux-x86/ccache/ccache))
ifdef ccache
ifneq ($(ccache),$(firstword $(CC)))
CC := $(ccache) $(CC)
endif
ccache =
endif
endif


(3). 这样在编译kernel时也可以享受到ccache带来编译时间的缩短。

以上方法优化后大约可降低50%的编译时间,也就是说优化前需要1小时编译时间,优化后半小时就完成了。

5. 共享ccache的缓存

所有需要共享的用户请添加以下环境变量:

export CCACHE_DIR=<ccache缓存路径>
export CACHE_UMASK=002
unset CCACHE_HARDLINK


可以将以上添加到$HOME/.bashrc,保证打开的终端都已经加载以上的环境变量。其中< ccache缓存路径 >请替换为指定的绝对路径(必须是需要共享的用户都能读写的)。比如:/proj/.ccache

注意:由于多人同时写ccache缓存可能引起IO带宽不够反而降低效率,强烈建议服务器编译可以此配置,但是其他共享用户最好增加下面配置, 仅只读缓存内容。

export CCACHE_READONLY=1


设置好环境变量后,重新登入终端使环境变量生效,然后创建< ccache缓存路径 >,设置< ccache缓存路径 >的共享权限,在终端输入:

find $CCACHE_DIR -type d | xargs chmod g+s


完成前面的步骤后,就可以开始共享使用了,注意ccache空间要足够大,否则反而拉长编译时间。

如果遇到问题可以参考ccache的使用说明:http://ccache.samba.org/manual.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android ccache 编译