Get the FULL version

Android: Use ccache with Android NDK on Cygwin

Android: Use ccache with Android NDK on Cygwin thumbnail

This Android tutorial explains how to set-up ccache to work with the NDK on Cygwin environments. Ccache is a great tool that detects unnecessary code recompilation by comparing the current source compilation with previously cached results, thus reducing the time it takes to complete. The support for ccache has been officially added to the 7th revision of the NDK.

The instructions in this posts have been tested on Windows 7 Ultimate 64 bits and Windows 7 Home Premium 64 bits, using Cygwin 6.1, Eclipse 3.5.2 and obviously, Android NDK r7. The commands to compile Android native code are from this post.

The first thing necessary to get ccache working is to install ccache. To do this in Cygwin, just run the setup again (available at the Cygwin Install page). After it starts, select your connection method and mirror. At the package list, select the Devel category and find ccache and check that you want to download the compiled binary of ccache, like this:

Installing ccache on Cygwin screenshot

After selecting ccache, proceed with the installation by clicking on the 'Next' button.

With ccache installed, it’s time to configure the system to make this tool work with the NDK. Just as explained in the Android NDK’s official documentation, create an environment variable named NDK_CCACHE that points to the ccache binary. To accomplish that in Windows 7, just open ‘My Computer‘, right click anywhere and select Properties. At this recently opened dialog select Advanced system settings. This will open another dialog with some tabs; select the one labeled Advanced and click on the Environment Variables button.

System Properties - Advanced Tab

Select the 'Advanced' tab. Click on the 'Environment Variables...' button.

At this dialog create an user environment variable by clicking at the New button:

Environment Variables dialog screenshot

Click on the 'New' button as shown above to create an user variable.

It must be named NDK_CCACHE and the value is going to be the absolute path pointing to the ccache binary (in this case, the ccache.exe file). This file is at inside Cygwin’s folder, under the bin directory. For this tutorial, let’s assume that Cygwin is installed at the root of the C drive, so the variable’s value will be:

/cygdrive/c/cygwin/bin/ccache.exe

Note that this is a POSIX-style bsolute path and not a Win32 one. Now, create another variable to define where ccache should store the compilation results. This variable must be named CCACHE_DIR and the value has to be an absolute path to an existing directory on the system, in the Win32 format. As an example, to place the cached results at an Cygwin’s user directory, just create a directory there and pass it’s absolute path as this variable’s value, like this:

C:\cygwin\home\<user>\ccache

Where <user> must be replaced by the name of your Cygwin user. Again, this is just a suggestion: this variable can point to any existing folder on the system, so create the cache directory before setting it. After creating those variables, the Environment Variables dialog will look like this:

Screenshot of recently created User Variables

The recently created user variables should be something like the ones pictured above. Click on 'OK' to confirm the changes.

As explained by Daniel Smith at this thread on the Android-NDK Google Group, in order to make ccache work, some lines at the default-build-commands.mk must be changed. This file is located at the <ndk>/build/core folder, where <ndk> is the Android NDK directory. For the r7 lines 85 and 88 must be altered from:

//Line 85
TARGET_CC       = $(TOOLCHAIN_PREFIX)gcc

//LINE 88
TARGET_CXX      = $(TOOLCHAIN_PREFIX)g++

To:

//Line 85
TARGET_CC       = ccache $(TOOLCHAIN_PREFIX)gcc

//LINE 88
TARGET_CXX      = ccache $(TOOLCHAIN_PREFIX)g++

Save the changes made to this file. Finally, all that’s left to be done is to give administrator rights to three different executables. The first one is the eclipse.exe located inside the folder where you have installed the Eclipse. The second and third programs are ccache.exe and the bash.exe, both located at the /bin folder. To give administration rights to a program, just right click on it and then select Properties->Compatibility Tab. Check the ‘Run program as Administrator’ box. Like this:

Asssigning privileges

Assigning administrator privileges to 'bash.exe'. Repeat this process to 'ccache.exe' and 'eclipse.exe' .

Don’t worry, depending on your system, it will be possible to remove the administrator rights after getting the ccache to work properly. To check whether everything is correctly configured open Cygwin’s bash shell terminal and type:

ccache -s

It will output some information about the current cache, such as it’s maximum size and current folder. It’s a great way to double check everything. Now open Eclipse and compile an NDK project. Run ccache -s at Cygwin again. If everything worked as expected the Files in Cache and Cache Size should be different than zero. Recompile the same NDK project and wait until it’s finished. Run the same command at Cygwin one last time. Case there are some hits registered by the the Cache Hit counter, like the screenshot below:

Ccache usage statistics screenshot

Some statistics displayed by ccache after correctly configuring it to work with the Android NDK.

Congratulations, ccache is running as it supposed to.

At this point, it’s possible to try to remove the administration rights from eclipse.exe, bash.exe and ccache.exe and test if ccache works without it.

One last tip: as hinted by CyanogenMod’s Wiki that explains how to speed up builds, try to use a some of the RAM as a disk to speed up compilations. There are some great third-party software out there to create RAM disks, such as the Dataram RAMDisk , ImDisk and Gavotte Ramdisk. Just set the ccache folder with the CCACHE_DIR variable to the ram disk to benefit from it’s performance.

Troubleshooting and other tips

If there were some errors during the compilation process, such as messages telling that the cache directory couldn’t be found, and you are sure that the path at the CCACHE_DIR variable is correct, try passing it as a POSIX-style path, instead of using a Win32 path. Also, check if eclipse.exe, ccache.exe and bash.exe have administrator rights. As default, ccache tries to create a .ccache folder inside Cygwin’s user folder. Windows doesn’t like folders that starts with a dot, so as the NDK. This could be simply fixed by setting ccache to use another folder.

Also, remember that changes made to environment variable will only take effect after closing and re-launching a software. So, case you want to check if ccache is reading the CCACHE_DIR variable correctly, you must close Cygwin’s bash and repeat the ccache -s command. This is also valid for Eclipse.

After trying this configuration on two different Windows installations it was possible to remove the administration rights from the three mentioned executables, however this made ccache stop working on one of the machines. Also, one accepted the CCACHE_DIR value as an absolute POSIX-style path, while the other one required it formatted as a Win32 path.

For large projects, you might also want to increase the cache size. To do so, look at ccache’s help by typing ccache -h on how to do so.

That’s it!

What are your experiences with the newest NDK revision? Got ccache working and have some useful tips? Please share it in the comments below.

One Comments to “Android: Use ccache with Android NDK on Cygwin”

  1. chris samson says:

    Absolute load of rubbish – you CANNOT use a cygwin version of ccache with the NDK, FAIL.

Leave a Comment

Post Comments RSS