In my previous post Using GitHub Actions with C++ and CMake I have provided a GitHub Actions yaml configuration file for C++ projects using CMake.
Building a project on GitHub Actions means always a build from scratch, for any given change, big or small. This takes time and wastes resources unnecessarily.
GitHub provides a way of caching dependencies to speed up workflows. The total size of cached files per repository is 2 GiB.
By having a look at the examples of various programming languages we can see that this is meant to cache package manager dependencies e.g. pip for python, npn for node, or gradle for java.
But, as it turns out, the caching mechanism can be used to cache compilation artifacts.
ccache
ccache (or “Ccache”) is a compiler cache. It speeds up recompilation by caching previous compilations and detecting when the same compilation is being done again. Supported languages are C, C++, Objective-C and Objective-C++.
The following yaml file excerpt will enable ccache support for GitHub Actions:
This makes sure that for every build the GitHub Actions cache key is unique. It will restore
the latest tar file containing the .ccache
folder for the current configuration, and and the end of
the job it will store the updated .ccache
folder in a new tar file.
Using ccache with CMake
In the configure step one only needs to pass:
Before building the project I am configuring ccache via environment variables like this:
This will ensure that the maximum size of the cache will be 400 MiB, will use compression, and the paths will always be relative to the build directory.
ccache statistics are zeroed before starting the build (ccache -z
), and displayed after the build (ccache -s
).
Getting ccache
ccache project doesn’t have any binary releases on their github page, like CMake or ninja.
One could use brew
to install ccache
on macOS, apt get
to install ccache
on Ubuntu, but what about Windows?
I have my own ccache fork, which has three commits over the official ccache:
- CMake build system - to build on Windows
- GitHub Actions yaml file - for providing binary releases
- Visual C++ (alpha) support - for having a cross platform caching solution
Getting ccache from my fork’s binary releases is as easy as:
Visual C++ support
I used Jean-Dominique Gascuel’s work from ccache’s PR 162. He tried to build ccache with Visual C++, add support for it in ccache. His pull request had 161 commits, and in the end got closed
I just needed the last part, having support for Visual C++. I am fine with a MinGW build of ccache.
At the moment I have only tested CMake with Ninja generator in Release mode, which is exactly what I need for GitHub actions.
Debug mode is not supported since ccache should cache also the pdb files. Precompiled headers are not supported since ccache should know about them and store the pch files.
HelloWorld project
I have updated my C++ HelloWorld GitHub Actions enabled project to use ccache. The yaml file can be also downloaded from here.