Xcode使用CCache提高编译和打包效率

这里以"车牛"项目为例
当你遇到** 纯OC**开发的项目,编译打包非常慢。是否考虑过到底有没有提高编译速度的办法呢。如果没有,请认真看这片文章,给大家介绍一个较好的增量编译器CCache.

那么如何安装CCache呢?

首先你需要在电脑上安装 Homebrew,通过 Homebrew 安装 CCache, 在命令行中执行

$ brew install ccache

如何让CCache 介入我们的项目呢?

一.在项目根目录下,创建 touch ccache-clang
创建 touch ccache-clang
打开脚本 open -a xcode 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
    exec ccache /usr/bin/clang "$@"
else 
    exec clang "$@"
fi


执行脚本 chmod 777 ccache-clang 成为可执行文件,如果编译报错,需要手动拷贝一份ccache-clang到Pods文件夹底下

二.CCache 在工程中Podfile 配置如下配置

target 'cheniu' do

    #post_install do |installer|
    require 'fileutils'
    post_install do |installer_representation|
            installer_representation.pods_project.targets.each do |target|
                target.build_configurations.each do |config|
  
        copy_pods_resources_path = "Pods/Target Support Files/Pods-cheniu/Pods-cheniu-resources.sh"
        string_to_replace = '--compile "${BUILT_PRODUCTS_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}"'
        assets_compile_with_app_icon_arguments = '--compile "${BUILT_PRODUCTS_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}" --app-icon "${ASSETCATALOG_COMPILER_APPICON_NAME}" --output-partial-info-plist "${BUILD_DIR}/assetcatalog_generated_info.plist"'
        text = File.read(copy_pods_resources_path)
        new_contents = text.gsub(string_to_replace, assets_compile_with_app_icon_arguments)
        File.open(copy_pods_resources_path, "w") {|file| file.puts new_contents }
    
        #关闭 Enable Modules
        config.build_settings['CLANG_ENABLE_MODULES'] = 'NO'
        # 在生成的 Pods 项目文件中加入 CC 参数
        config.build_settings['CC'] = '$(SRCROOT)/ccache-clang'
        end
    end 
    # 拷贝主工程的ccache-clang文件到Pods下面
    FileUtils.cp('ccache-clang', 'Pods/ccache-clang')
end

如果出现脚本报错,请使用下面的podfile配置


     post_install do |installer|

            copy_pods_resources_path = "Pods/Target Support Files/Pods-cheniu/Pods-cheniu-resources.sh"
            string_to_replace = '--compile "${BUILT_PRODUCTS_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}"'
            assets_compile_with_app_icon_arguments = '--compile "${BUILT_PRODUCTS_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}" --app-icon "${ASSETCATALOG_COMPILER_APPICON_NAME}" --output-partial-info-plist "${BUILD_DIR}/assetcatalog_generated_info.plist"'
            text = File.read(copy_pods_resources_path)
            new_contents = text.gsub(string_to_replace, assets_compile_with_app_icon_arguments)
            File.open(copy_pods_resources_path, "w") {|file| file.puts new_contents }

            #关闭 Enable Modules
            #config.build_settings['CLANG_ENABLE_MODULES'] = 'NO'
            # 在生成的 Pods 项目文件中加入 CC 参数
            #config.build_settings['CC'] = '$(SRCROOT)/ccache-clang'
                #end
        #end
        # 拷贝主工程的ccache-clang文件到Pods下面
        FileUtils.cp('ccache-clang', 'Pods/ccache-clang')
    end

三.Pod install

这一步是为了集成组件,以及ccache编译器的介入。
完成之后,直接运行和打包项目,第一次运行比较慢,大概是Apple clang编译器的速度的60%左右。后面随着编译和打包产生的文件的产生缓存之后,后面就可以增量编译。随着缓存命中率的提高,速度就越来越快了

四. Build Settings 修改
User-Defined 添加常量 CC {SRCROOT}/ccache-clang
Apple clang Enable Module (C and Object-C ) 设置为NO

五.常见问题处理

  • @import 问题
    可以参考Lottie-ios 组件库的写法,其他组件库修改之后。进行CCache 编译打包测试
    CCache确定是必须关闭module引入的方式
#if __has_feature(modules)
@import Foundation;
#else
#import 
#endif

车牛GrowingIO 库 解决办法,由于这个库是第三方库。以后想办法组件化放到公司私有库上,这个以后做优化

在growing.h文件中,unlock后做如下修改

#ifndef __cplusplus

//@import Foundation;
//@import WebKit;
//@import CoreTelephony;
//@import SystemConfiguration;
//@import Foundation;
//@import Security;
//@import CFNetwork;
//@import CoreLocation;
//@import Security;

#import 
#import   //为判断网络制式的主要文件
#import  //添加获取客户端运营商 支持
#import 
#import 
#import 
#import 
#import 

#endif
  • linker error,根据提示信息在Link Binary With Libraries中加入对应的*.framework
FEF587C1-01F0-4AB0-B63A-724EDC5380AB.png
  • Pods-cheniu-resources.sh 脚本文件中参数过长会导致编译失败,去掉这一行

  • 编译运行部分资源图片显示不出来

在Build phases中引用缺失的文件对应的 *.xcassets文件即可

D9F3BF19-64F9-4C5C-BC90-725B18A42E3E.png
  • Pods-cheniu-resources.sh 文件编译不过的问题
image.png

ccache 缓存命中率查看命令 ccache -s

485FD1D2-1152-4B74-9993-A4E5EAADE218.png

如何检查有没有缓存呢?
可以查看电脑根目录下.ccache文件夹

4387BCAB-813C-4899-A98D-C65CF339B0BF.png

整理好之后,编译速度和打包速度都会大幅提升。编译大概提供200%,打包提高50%左右。节省了大量不必要的等待时间
可以看到,现在的缓存命中率是55.11%,以前打包需要20分钟,现在10分钟内搞定了,喝茶的时间又多了,是不是很开森,_

你可能感兴趣的:(Xcode使用CCache提高编译和打包效率)