Improving Build Speed of Mbed Projects with CCache

This tutorial shows how to use CCache to improve the build speed of mbed projects with VisualGDB.

CCache is a tool that maintains a global cache (one per user account) of commonly compiled sources. E.g. if projects A and B both compile a shared source called adc.cpp with exactly the same parameters, CCache will automatically reuse the result of compilation even if the file timestamp changes. CCache automatically checks the contents of all included files as well, making sure that actual file changes do trigger a rebuild.

In practice, this helps speed up rebuilding of projects when common configuration files are regenerated without changing their contents.

We will now show how to create a basic mbed-based project and configure it to use CCache for building.

  1. Start Visual Studio and locate the VisualGDB Mbed Project Wizard:
  2. Enter the name and location for your project:
  3. As each mbed project uses its own unique configuration file, CCache won’t be able to reuse built files between different projects, however it can considerably speed up subsequent builds of the same project when the mbed tools would otherwise rebuild all the sources. We will demonstrate this using the regular project layout (mbed repository cloned inside the project folder). Make sure you check the “speed up build using CCache” flag:
  4. On the next page of the wizard, select the ARM toolchain and choose your mbed target:
  5. Finally, specify the debug method that works with your target and click “Finish” to generate the project:
  6. If this is the first time you are using CCache, you can configure the folder where it will store the cached object files via Tools->Options->VisualGDB->CCache:
  7. If you try to use CCache on the unmodified mbed-cli build logic, it will not be of much use because mbed reshuffles the preprocessor macros passed to the gcc on each build. This can be fixed by patching the mbed-os\tools\toolchains\gcc.py file. Locate cmd = cc + … line in the compile() function in gcc.py and replace it with this code:
            #CCACHE_WORKAROUND_PRESENT
            symbols = self.get_symbols()
            symbols.sort()
     
            extra_macros = [x for x in cc if x[0:2] == "-D"]
            cc = [x for x in cc if x[0:2] != "-D"]
            extra_macros.sort()
     
            cmd = cc + extra_macros + self.get_compile_options(symbols, includes)

    Watch that the tabs/spaces in the pasted code match the original “cmd = cc + …” line, as otherwise Python will trigger an error.

  8. Now you can start building the project:
  9. The initial build will take slightly longer, as CCache will copy each built object file into the cache directory:
  10. However, if you try rebuilding the project (even if you delete the object directory), the build will be much faster (25x faster in this example), as ccache will reuse the previously built files:
  11. You can turn CCache on and off for individual projects via VisualGDB Project Properties -> Mbed Project -> Common Build Settings: