利用ccache加快android源码和linux内核编译速度的方法

来源:互联网 发布:淘宝信用卡在哪申请 编辑:程序博客网 时间:2024/05/22 10:38

一、android源码编译加速

当你删掉out/target目录或者使用make clean清空输出重新编译源码的时候,编译时间通常都很漫长。

其实这个问题很容易解决,Android官方为我们带来了解决方案–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 do “make clean” often, or if you frequently switch between different build products.
设置方法:
在你home主目录的.bashrc中加入:
export USE_CCACHE=1
如果你需要指定一个特殊的缓存目录,也需要在.bashrc中加入,不指定则为你当前用户目录下的.ccache。

export CCACHE_DIR=/home/mokee/.ccache

export PATH = /usr/lib/ccache:$PATH

在MoKee OpenSource主目录中运行,50G~100G之间手动指定:
prebuilts/misc/linux-x86/ccache/ccache -M 50G
大功告成,开始编译吧!


二、linux内核编译加速

修改内核根目录的Mafile文件,在交叉编译变量“CROSS_COMPILE”前添加ccache即可

$ vi Makefileexport KBUILD_BUILDHOST := $(SUBARCH)ARCH            ?= armCROSS_COMPILE   ?= ccache /usr/local/arm/arm-2009q3/bin/arm-none-linux-gnueabi-#CROSS_COMPILE   ?= /usr/local/arm/4.5.1/bin/arm-none-linux-gnueabi-CROSS_COMPILE   ?= $(CONFIG_CROSS_COMPILE:"%"=%)




三、ccache的使用

简介

   如果你经常编译大型的C/C++工程,不使用ccache你就out了。 

   英文说明:cache is a compiler cache. It speeds up recompilation by caching previous compilations and detecting when the same compilation is being done again. Supported languages are C, C++, Objective-C and Objective-C++.


1. 安装 ccache

sudo apt-get install ccache


2. Usage


ccache -s   # 显示状态参数 (s是英语status的缩写,表示《状态》)
ccache -C   # 清除缓存(C是大写的,是英语Clear的缩写,表示《清除》)

details see : man ccache
使用ccache
  • 编译指令前增加ccache. $ ccache gcc xxx
  • 创建软链接。 $ ln -s ccache /usr/local/bin/gcc

建议使用第一种方式,因为ccache偶尔也犯晕,当由于它出现错误的时候, 很难看出端倪。曾在某次编译某份代码时,ccache对某个编译选项的判断失误导致编译失败,死活查不出来为什么原因。所以当出现某些怪异的现象时,请用正常的方式编译试试。


3. 在交叉编译内核时,编译速度也快了近十倍。 

time ./build.sh


real 3m4.146s

user 10m30.640s

sys 0m37.138s


make clean -j4


time ./build.sh

real    0m43.477s

user    1m0.564s

sys     0m14.913s

 

4.查看ccache的状态用watch ccache -s

Every 2.0s: ccache -s                                                                                              Thu Nov 23 16:35:37 2017cache directory                     /home/litin/.ccachecache hit (direct)                   154cache hit (preprocessed)              15cache miss                          3197called for link                      587called for preprocessing              23can't use precompiled header           1unsupported source language          158no input file                        515files in cache                      8534cache size                         353.8 Mbytesmax cache size                      40.0 Gbytes



参考资料:http://blog.csdn.net/jiulousanti/article/details/23343687

http://blog.csdn.net/zgl07/article/details/41819495

http://blog.csdn.net/qq_27062249/article/details/53642444


原创粉丝点击