Speeding up compilation with ccache [xcode tip]


#1

hi.

ccache helps speed up rebuilds a lot. it’s not noticably faster on normal compiles for me.
but if you also clean-all/build-all a lot, this is for you.

all info taken from https://pspdfkit.com/blog/2015/ccache-for-fun-and-profit/.

These instructions will set up ccache for all xcode projects within an OF folder.

1. Install with homebrew

brew install ccache

2. Create two scripts to replace clang and clang++

libs/openFrameworksCompiled/project/osx/ccache-clang.sh

#!/bin/sh
if type -p /usr/local/bin/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 /usr/local/bin/ccache /usr/bin/clang "$@"
else
  exec clang "$@"
fi

libs/openFrameworksCompiled/project/osx/ccache-clang++.sh

#!/bin/sh
if type -p /usr/local/bin/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 /usr/local/bin/ccache /usr/bin/clang++ "$@"
else
  exec clang++ "$@"
fi

the sh files need to be executable (e.g. by running chmod a+x ccache*.sh)

3. Edit CoreOF.xcconfig

At the very bottom, add those two lines:

CC = /Users/hansi/Documents/OF/of_v0093_osx_release/libs/openFrameworksCompiled/project/osx/ccache-clang.sh
CXX = /Users/hansi/Documents/OF/of_v0093_osx_release/libs/openFrameworksCompiled/project/osx/ccache-clang++.sh

Make sure to use absolute paths! The use of $(OF_PATH) in CC/CXX crashes my xcode, not sure why.

CoreOF.xcconfig is reachable from XCode directly, so enabling/disabling is easy.

4. Test it

Now do two cycles of clean all/build all. The second cycle should only take a few seconds.

clang directly: oF build time 1:48, project build time 0:35
ccache+clang: oF build time 0:05, project build time 0:03


apparently a ccache for VS does exist, but i haven’t tried it: https://github.com/frerich/clcache


#2

Seems really cool but there are a number of caveats on the page you link to. Would any of them affect a typical openFrameworks project?


#3

Seems really cool but there are a number of caveats on the page you link to. Would any of them affect a typical openFrameworks project?

  • clang modules: OF is not using those at the moment, as far as i can tell.
  • precompiled headers: not on by default, and apple discourages using them

if you start having problems with this approach you would notice right away (it wont compile!). in that case you can just remove your CC/CXX variables from CoreOF.xcconfig.


#4

cheers. i’ll give it a go.


#5

I’ve just tried both on OSX and iOS project and indeed it’s surprisingly fast.
*I saw Permission Denied error because I made .sh file via Xcode.


#6

Confirming that it’s much faster for me too. Also had to chmod 0777 the scripts to avoid permissions error.


#7

@clem @hrs good catch. textmate does this by itself when it saves a sh file the first times. i’ve edited the post accordingly.


#8

From 8 minutes to 8 seconds! Amazing. Thank you!!


#9

This Trick is awesome.
thanks @kritzikratzi
just tried it and works really fast. Although I need some testing to see how it behaves in general with OF.

cheers!