Search Unity

Optimizing iOS and macOS build times using ccache

Discussion in 'iOS and tvOS' started by spatial_io, Sep 21, 2020.

  1. spatial_io

    spatial_io

    Joined:
    Jul 3, 2017
    Posts:
    21
    If your project is at all like ours, compiling the il2cpp-generated C++ code in an exported Xcode project can take a few minutes, even with the simplest of code changes. We’ve found a way to incorporate ccache into Xcode builds to reduce build iteration times by over 25%!

    Step 1: install ccache, e.g. using brew install ccache
    Step 2: add these two shell scripts to a folder in your project, making sure to give them execute permissions. Also remove the .txt file extensions (this forum doesn’t allow extension-free file uploads).
    Step 3: add this PostProcessBuild.cs script to your project
    Step 4: open that build script and update the hard-coded path to wherever you saved the scripts

    And that’s it! The first time you build and run, it should take the same amount of time. Subsequent builds should blaze through compiling the C++ files. To confirm that it’s working, run ccache -s and check that the cache hit rate is above 0%.

    Note: there may not be as many cache hits if you chose Replace instead of Append when building your project.

    Hat tip to this article, which showed how easy it is to integrate with Xcode
    https://pspdfkit.com/blog/2015/ccache-for-fun-and-profit/


    - Aaron at Spatial
     

    Attached Files:

    Neonlyte, DevDunk and AntonPetrov like this.
  2. mariozhou

    mariozhou

    Joined:
    Oct 18, 2019
    Posts:
    23
    it works!
     
  3. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,059
    Cool! Does this also work with the incremental builds?
     
  4. mariozhou

    mariozhou

    Joined:
    Oct 18, 2019
    Posts:
    23
    we delete the xcode project and then re-export,works on 2020.3。
    so I think it should also work with the incremental builds

    some modifications:

    1.
    export CCACHE_SLOPPINESS=pch_defines,file_macro,time_macros,include_file_mtime,include_file_ctime

    2. PostProcessBuild.cs
    proj.SetBuildProperty(frameworkTarget, "CC", "$(SRCROOT)/ccache-clang.sh");
    proj.SetBuildProperty(frameworkTarget, "CLANG_ENABLE_MODULES", "NO");
    proj.SetBuildProperty(frameworkTarget, "GCC_PRECOMPILE_PREFIX_HEADER", "NO"); // will make the first build much more slower,may not necessary for incremental build

    3. xcode
    // this may not necessary for incremental build
    Xcode -> Preference -> Locations -> Derived Data -> Change to "Relative"


    If compiling CPP takes up a significant portion of the total compilation time, the improvement will be significant. However, in our case where we use the archive method (which is the main time-consuming process), and the machine has many CPU cores(up to 64), the compilation time only reduced by 1 minute from 4 minutes to 3 minutes.
     
    Last edited: Apr 13, 2023
    DevDunk likes this.
  5. Neonlyte

    Neonlyte

    Joined:
    Oct 17, 2013
    Posts:
    516
    FYI If you don't delete the Xcode project and only appends to the generated project, Xcode itself also already has caches for avoiding compiling extra files. Usually if my code change don't span across multiple assemblies, a rebuild only takes 5~10 seconds as opposed to a 1 minute clean build.

    But nevertheless good work on the tips!
     
    mariozhou likes this.