Recently I’ve been making a number of contributions to
HHVM, an open source virtual machine for both PHP and Facebook’s Hack language. Frequently my work has included squashing bugs such as segfaults or other incompatibilities. This type of work often includes frequent recompiles, after code modification, to move between release and debug branches, etc. While “make” is frequently used by projects to limit recompiles to the part of the program that has changed, it is limited to only the most recent version, and often a “make clean” is needed to remove incompatible object files. Additionally, “make” is only able to reuse object files in a single source tree, so builds in other source trees or on other computers can’t take advantage of its selective recompile.
The good news is there
IS a tool that can help address all of these shortcomings:
ccache from the author of Samba. Ccache analyses and stores your object file during compilation, so they can be automatically substituted from the cache in future cases where the same compiler, options, and code is present. The best part is ccache guaranties that the cache will provide the same result as the real compiler, including warnings, and will fall back to the real compiler if there is any ambiguity. While ccache is limited to single file C, C++, Objective-C, or Objective-C++ files from a GCC style compilers, it will transparently pass other languages, multi-file compiling, and linking onto the real compiler.
Installing & Using ccache under CentOS 7
Ccache can be found in the terrific EPEL (Extra Packages for Enterprise Linux), if you don’t already have EPEL enabled it is just a single command:
sudo rpm -Uvh http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-6.noarch.rpm
Install ccache
sudo yum install ccache -y
By default the ccache package installs symbolic links such as /usr/lib64/ccache/{gcc,g++,etc} that point to ccache and can be used directly in place of the various supported compilers. If desired you can put symbolic links in your path before your real compiler and use ccache automatically, however for this example we’ll assume you don’t wish to do that.
For small compile jobs you can now directly use /usr/lib64/ccache/{gcc,g++} to compile your code as normal.
/usr/lib64/ccache/g++ mycode.cpp
If you are using configure or make, you can override your C/C++ compiler with the following option:
{configure,make} “CC=’ccache gcc’ CXX=’ccache g++’”
Finally, for cmake simply include the following:
cmake -D CMAKE_CXX_COMPILER="/usr/lib64/ccache/g++" -D CMAKE_C_COMPILER="/usr/lib64/ccache/gcc" .
Advanced ccache
Ccache also includes a utility to report cache statistics and configure cache options.
View ccache statistics
ccache -s
Set ccache’s cache size to 5G with no item limit
ccache -M 5G –F 0
Ccache can also share a cache between users, or even between machines with NFS! This is great for a group of developers with a large code base. Of course there are some limitations, so be sure to check the ccache documentation for details.
Finally cmake can be configured to use ccache (when present) directly in your MakeLists.txt file. If you are interested please see my
cmake for HHVM commit that was recently accepted by Facebook.