Speeding up embedded development with CCache transplants

There are two different schools of thought when it comes to embedded development such as working on Ubuntu phone. The first group feels that compiling on the device is better and the other group prefers doing cross-builds. Both have their advantages and disadvantages.

One of the biggest disadvantages for compiling on-device is the fact that a reflash wipes all your state and you need to compile your project from scratch again. This is unfortunate if a fresh build takes 20 minutes (which is not that uncommon).

The question then becomes whether you can avoid this. It turns out that you can by doing a ccache transplant. Here’s how to do it.

First you install ccache and set up your project:

sudo apt install ccache
CC='ccache gcc' CXX='ccache g++' cmake <your flags>

and develop as normal. Just before reflashing do a backup:

tar czf ccachedir.tar.gz .ccache/ (on device) 
adb pull /home/phablet/ccachedir.tar.gz (on desktop)

Then flash and put your ccachedir back:

adb push ccachedir.tar.gz /home/phablet/ccachedir.tar.gz (on desktop) 
tar xf ccachedir.tar.gz (on device)

Then set up your project with the same CMake stanza as above, run your build command and you are back to having a fully up-to-date build on your reflashed device.

A test done with a small-to-medium Qt5 project dropped the build time from 6 minutes to 30 seconds.