Xcode使用CCache提高編譯和打包效率

這里以"車牛"項(xiàng)目為例
當(dāng)你遇到** 純OC**開發(fā)的項(xiàng)目,編譯打包非常慢。是否考慮過到底有沒有提高編譯速度的辦法呢。如果沒有,請(qǐng)認(rèn)真看這片文章,給大家介紹一個(gè)較好的增量編譯器CCache.

那么如何安裝CCache呢?

首先你需要在電腦上安裝 Homebrew,通過 Homebrew 安裝 CCache, 在命令行中執(zhí)行

$ brew install ccache

如何讓CCache 介入我們的項(xiàng)目呢?

一.在項(xiàng)目根目錄下,創(chuàng)建 touch ccache-clang
創(chuàng)建 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


執(zhí)行腳本 chmod 777 ccache-clang 成為可執(zhí)行文件,如果編譯報(bào)錯(cuò),需要手動(dòng)拷貝一份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 }
    
        #關(guān)閉 Enable Modules
        config.build_settings['CLANG_ENABLE_MODULES'] = 'NO'
        # 在生成的 Pods 項(xiàng)目文件中加入 CC 參數(shù)
        config.build_settings['CC'] = '$(SRCROOT)/ccache-clang'
        end
    end 
    # 拷貝主工程的ccache-clang文件到Pods下面
    FileUtils.cp('ccache-clang', 'Pods/ccache-clang')
end

如果出現(xiàn)腳本報(bào)錯(cuò),請(qǐng)使用下面的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 }

            #關(guān)閉 Enable Modules
            #config.build_settings['CLANG_ENABLE_MODULES'] = 'NO'
            # 在生成的 Pods 項(xiàng)目文件中加入 CC 參數(shù)
            #config.build_settings['CC'] = '$(SRCROOT)/ccache-clang'
                #end
        #end
        # 拷貝主工程的ccache-clang文件到Pods下面
        FileUtils.cp('ccache-clang', 'Pods/ccache-clang')
    end

三.Pod install

這一步是為了集成組件,以及ccache編譯器的介入。
完成之后,直接運(yùn)行和打包項(xiàng)目,第一次運(yùn)行比較慢,大概是Apple clang編譯器的速度的60%左右。后面隨著編譯和打包產(chǎn)生的文件的產(chǎn)生緩存之后,后面就可以增量編譯。隨著緩存命中率的提高,速度就越來越快了

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

五.常見問題處理

  • @import 問題
    可以參考Lottie-ios 組件庫(kù)的寫法,其他組件庫(kù)修改之后。進(jìn)行CCache 編譯打包測(cè)試
    CCache確定是必須關(guān)閉module引入的方式
#if __has_feature(modules)
@import Foundation;
#else
#import <Foundation/Foundation.h>
#endif

車牛GrowingIO 庫(kù) 解決辦法,由于這個(gè)庫(kù)是第三方庫(kù)。以后想辦法組件化放到公司私有庫(kù)上,這個(gè)以后做優(yōu)化

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

#ifndef __cplusplus

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

#import <WebKit/WebKit.h>
#import <CoreTelephony/CTTelephonyNetworkInfo.h>  //為判斷網(wǎng)絡(luò)制式的主要文件
#import <CoreTelephony/CTCarrier.h> //添加獲取客戶端運(yùn)營(yíng)商 支持
#import <SystemConfiguration/SystemConfiguration.h>
#import <Foundation/Foundation.h>
#import <CFNetwork/CFNetwork.h>
#import <CoreLocation/CoreLocation.h>
#import <Security/Security.h>

#endif
  • linker error,根據(jù)提示信息在Link Binary With Libraries中加入對(duì)應(yīng)的*.framework
FEF587C1-01F0-4AB0-B63A-724EDC5380AB.png
  • Pods-cheniu-resources.sh 腳本文件中參數(shù)過長(zhǎng)會(huì)導(dǎo)致編譯失敗,去掉這一行

  • 編譯運(yùn)行部分資源圖片顯示不出來

在Build phases中引用缺失的文件對(duì)應(yīng)的 *.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

整理好之后,編譯速度和打包速度都會(huì)大幅提升。編譯大概提供200%,打包提高50%左右。節(jié)省了大量不必要的等待時(shí)間
可以看到,現(xiàn)在的緩存命中率是55.11%,以前打包需要20分鐘,現(xiàn)在10分鐘內(nèi)搞定了,喝茶的時(shí)間又多了,是不是很開森,_

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容