Optimizing iOS and macOS build times using ccache

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

  • Aaron at Spatial

6333765–702936–PostProcessBuild.cs (1.56 KB)
6333765–702939–ccache-clang.txt (331 Bytes)
6333765–702942–ccache-clang++.txt (333 Bytes)

3 Likes

it works!

Cool! Does this also work with the incremental builds?

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:

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

  1. 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

  2. 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.

1 Like

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!

1 Like