CCache是一个编译工具,可以将xcode编译文件缓存起来,从而达到编译提速。
优点
能大幅度地提升编译速度,起码要减少掉 50% 的编译时间
不需要对项目作出重大调整
不需要改变开发工具链
缺点
不支持clang的modules,需要手动引入链接库
不支持oc++
不支持swift
1.安装 CCache
brew install ccache
2 创建 CCache 编译脚本
需要两个脚本,需要对c++和oc分别处理
进入到项目文件夹中,生成文件ccache-clang,vi ccache-clang
#!/bin/sh
if type -p ccache >/dev/null 2>&1; then
export CCACHE_MAXSIZE=10G
export CCACHE_CPP2=true
export CCACHE_HARDLINK=true
export CCACHE_SLOPPINESS=file_macro,time_macros,include_file_mtime,include_file_ctime,file_stat_matches
# 指定日志文件路径到桌面,等下排查集成问题有用,集成成功后删除,否则很占磁盘空间
export CCACHE_LOGFILE='~/Desktop/CCache.log'
exec ccache /usr/bin/clang "$@"
else
exec clang "$@"
fi
进入到项目文件夹中,生成文件ccache-clang++,vi ccache-clang++
#!/bin/sh
if type -p ccache >/dev/null 2>&1; then
export CCACHE_MAXSIZE=10G
export CCACHE_CPP2=true
export CCACHE_HARDLINK=true
export CCACHE_SLOPPINESS=file_macro,time_macros,include_file_mtime,include_file_ctime,file_stat_matches
# 指定日志文件路径到桌面,等下排查集成问题有用,集成成功后删除,否则很占磁盘空间
export CCACHE_LOGFILE='~/Desktop/CCache.log'
exec ccache /usr/bin/clang++ "$@"
else
exec clang++ "$@"
fi
修改两个文件的权限
$ chmod 777 ccache-clang
3 build Setting
添加CC编译 Xcode 在编译时把执行路径的可执行文件当做 C 编译器
4 xcode关闭Enable Modules CCache 不支持 Clang Modules
5. ccache -s 查看缓存是否生效
cache directory /Users/frank/.ccache
primary config /Users/frank/.ccache/ccache.conf
secondary config (readonly) /usr/local/Cellar/ccache/3.6/etc/ccache.conf
stats updated Thu Apr 25 17:20:42 2019
cache hit (direct) 0
cache hit (preprocessed) 1
cache miss 855
cache hit rate 0.12 %
called for link 1
compile failed 4
unsupported compiler option 487
no input file 2
cleanups performed 0
files in cache 4134
cache size 133.5 MB
max cache size 10.0 GB
6. 进一步优化:
移除PCH,CCache是通过md4进行缓存查找,因此当你修改了 PCH 或者 PCH 引用到的头文件的内容时,会造成全部缓存失效,只能全体重新编译。












网友评论