The CCACHE is a compiler cache for C/C++ code. The Android framework distribution consists of several native libraries that live in the /external
directory. You may have noticed that on a clean build, these libraries are rebuilt, even if they were never changed. The rebuilding of these libraries takes a lot of time. To save you that time, you can use the CCACHE mechanism. In this recipe, we will highlight certain tips and tricks that make the life of an Android systems developer easier. In particular, we will explain how to use the CCACHE mechanism, how to selectively compile modules, and how to test them.
Navigate to your Android sources root and issue the make clean
command. We want to build up the cache from scratch.
In a terminal at the Android sources root, execute the following commands:
export USE_CCACHE=1 export CCACHE_DIR=/<path_of_your_choice>/.ccache
The preceding two environment variables control whether we use CCACHE, and if so, where the CCACHE directory is located. You are free to use any directory for the CCACHE.
Inside
prebuilt/linux-x86
, there is theccache
directory that contains the ccache binary. Just executing the following command presents us with options of what this binary can do:prebuilt/linux-x86/ccache/ccache
This command provides an output like:
ccache, a compiler cache. Version 2.4 Copyright Andrew Tridgell, 2002 Usage: ccache [options] ccache compiler [compile options] compiler [compile options] (via symbolic link) Options: -s show statistics summary -z zero statistics -c run a cache cleanup -C clear the cache completely -F <maxfiles> set maximum files in cache -M <maxsize> set maximum size of cache (use G, M or K) -h this help page -V print version number
Therefore, we can set the maximum cache size with the help of the following command:
prebuilt/linux-x86/ccache/ccache -M 20G
This will set it to be 20 GB.
Now, if you start a make for any target (choose emulator since it's the easiest), we can watch how the CCACHE is being used. In another terminal, use the following command:
watch -n1 -d prebuilt/linux-x86/ccache/ccache -s
The
watch
command is used to monitor the cache status and usage.The helper file should be saved as
build_helper.sh
:#!/bin/sh source build/envsetup.sh export USE_CCACHE=1 export CCACHE_DIR=<YOUR_CACHE_PATH>/.ccache prebuilt/linux-x86/ccache/ccache -M 20G lunch $1
An example invocation for the emulator target is:
./build_helper.sh 1
and it outputs the following:
including device/htc/passion/vendorsetup.sh including device/samsung/crespo/vendorsetup.sh Set cache size limit to 20971520k ========================================= PLATFORM_VERSION_CODENAME=REL PLATFORM_VERSION=2.3.4 TARGET_PRODUCT=generic TARGET_BUILD_VARIANT=eng TARGET_SIMULATOR=false TARGET_BUILD_TYPE=release TARGET_BUILD_APPS= TARGET_ARCH=arm HOST_ARCH=x86 HOST_OS=linux HOST_BUILD_TYPE=release BUILD_ID=GRJ22 =========================================
The Android build system is written in such a way that if CCACHE is activated by the environment variable, it will be used automatically.
The above shell script is really simple and simplifies the developer's task of initializing an environment for further work. The commands within the shell script have to be executed whenever a new terminal is opened for Android systems development. Hence it is easier to execute one shell script instead of several individual commands.