iOS-CCache提升编译和打包速度

2,200 阅读1分钟

安装

终端命令:brew install ccache

制作脚本文件

注意:带有c++文件的再写一份ccache-clang++文件,里面的clang也改成clang++

终端命令:先进入项目的根目录下
touch ccache-clang
open /User/xxx(你的mac用户名字)/ccache-clang
粘贴下面的代码
#!/bin/sh
export PATH=$PATH:/usr/local/bin
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 "$@"

xcode配置

  • Build Settings搜索框旁边的+号,选择Add User-Defined Setting,输入CC,然后右侧加入ccache-clang的路径,比如放在项目根目录下$(SRCROOT)/ccache-clang
  • Build Settings搜索Enable Modules修改为NO,然后编译

解决错误

"_kUTTypeImage", referenced from,"_kUTTagClassMIMEType", referenced from: 等

在Build Phases下Link Binary With Libraries添加MobileCoreServices

"_NSSQLiteStoreType", referenced from:等带SQLite的,"OBJC_CLASS$_NSManagedObjectModel", referenced from:

添加CoreData库

"OBJC_CLASS$_CADisplayLink", referenced from

添加QuartzCore库

CGRect,UIImageView,OBJC_CLASS$_UIResponder", referenced from:等

添加CoreGraphics库

Cocoapods的问题

Cocoapods如果也需要CCache,那么也需要将Enable Modules设置为NO,但是Cocoapods里的库都依赖多种系统库,如果关闭了Enable Modules那么关联的系统库就需要我们手动加入Link Binary With Libraries才能编译通过

post_install do |installer_representation|
  installer_representation.pods_project.targets.each do |target|
    target.build_configurations.each do |config|
      #关闭 Enable Modules
      config.build_settings['CLANG_ENABLE_MODULES'] = 'NO'
      #在生成的 Pods 项目文件中加入 CC 参数,路径的值根据你自己的项目来修改
      config.build_settings['CC'] = '$(PODS_ROOT)/ccache-clang'
    end
  end
end

提升速度

由于当前项目Cocoapods库比较多,需要加入的系统库比较多所以就先没有改,先看看效果,所以在没有关闭CocoaPods的Enable Modules情况下的提升速度为:
缓存命中率68%
build提升速度100s -> 58s
archive提升速度142s -> 73s

总结

我们项目的代码不太多,不是很庞大,所以本来速度就不太慢,经过优化后提升了50%的编译和打包速度,对于其他的老旧项目,我相信提升的更多。而且我的设备是老款的mac,对于硬件有差距的情况下,硬件更好的速度会更快。