peeyush
peeyush

Reputation: 2921

How to use ccache with Make?

I have a source directory which uses makefile to compile the code. This makefile/configure file is not written for ccache compatibility. So I thought to use ccache. I created alias in .bashrc as alias gcc='ccache gcc', but Makefile is still not considering this definition of gcc. So is there anything I can do without touching Makefile/configure file such that it takes ccache gcc instead of gcc. Also CC='ccache gcc' ./configure is not an option, since it does not ask for CC.

If I write Makefile then I can provide ${gcc), but this is not an option, since I am not writing Makefile. Is there any way by which we don't need to change anything in source file, but still enable ccache compiling.

Upvotes: 10

Views: 20750

Answers (3)

Add packaged ccache to PATH

PATH="/usr/lib/ccache:${PATH}" make

This is a versatile method, that:

  • works for all compilers at once: C, C++, etc.
  • depends less on the actual Makefile setup like CC could
  • does not break people's build if your are automating something and the target user does not have ccache installed

Mentioned at man ccache:

To use the second method on a Debian system, it's easiest to just prepend /usr/lib/ccache to your PATH. /usr/lib/ccache contains symlinks for all compilers currently installed as Debian packages.

And you can confirm that with:

ls -l /usr/lib/ccache

which contains a ton of possible GCC names, including for installed cross compilers:

total 0
lrwxrwxrwx 1 root root 16 May  6 13:51 aarch64-linux-gnu-gcc -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 May  6 13:51 aarch64-linux-gnu-gcc-7 -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 Jun 23 18:25 arm-linux-gnueabi-g++ -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 Jun 23 18:25 arm-linux-gnueabi-g++-7 -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 May  6 13:51 arm-linux-gnueabi-gcc -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 May  6 13:51 arm-linux-gnueabi-gcc-7 -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 May 28 22:11 arm-linux-gnueabihf-gcc -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 May 28 22:11 arm-linux-gnueabihf-gcc-7 -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 May  6 13:51 arm-none-eabi-g++ -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 May  6 13:51 arm-none-eabi-gcc -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 May  6 13:51 arm-none-eabi-gcc-6.3.1 -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 May  6 13:51 c++ -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 May  6 13:51 c89-gcc -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 May  6 13:51 c99-gcc -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 May  6 13:51 cc -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 May  6 13:51 clang -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 May  6 13:51 clang++ -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 May  6 13:51 g++ -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 May  6 13:51 g++-5 -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 May  6 13:51 g++-6 -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 May  6 13:51 g++-7 -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 May  6 13:51 gcc -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 May  6 13:51 gcc-5 -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 May  6 13:51 gcc-6 -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 May  6 13:51 gcc-7 -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 May  6 13:51 x86_64-linux-gnu-g++ -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 May  6 13:51 x86_64-linux-gnu-g++-5 -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 May  6 13:51 x86_64-linux-gnu-g++-6 -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 May  6 13:51 x86_64-linux-gnu-g++-7 -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 May  6 13:51 x86_64-linux-gnu-gcc -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 May  6 13:51 x86_64-linux-gnu-gcc-5 -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 May  6 13:51 x86_64-linux-gnu-gcc-6 -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 May  6 13:51 x86_64-linux-gnu-gcc-7 -> ../../bin/ccache

Tested on Ubuntu 16.04.

Upvotes: 5

slowdog
slowdog

Reputation: 6216

As described by the fine manual: Create a symlink named "gcc" in a directory that is listed in your PATH before the one containing the real gcc. This will cause ccache to be used transparently, with no need for any change to the makefile.

Upvotes: 9

MadScientist
MadScientist

Reputation: 100866

Aliases are local to the shell they are created in; unlike environment variables they are not passed to any programs that the shell invokes (including make). Make invokes /bin/sh, not /bin/bash, and /bin/sh doesn't read your ~/.bashrc etc. so no aliases defined there will be helpful to you.

I'm not exactly sure why you have placed some of the restrictions you mention on yourself: these things work fine and you haven't given a reason to avoid them that I understand. For example, providing a different CC with configure will work, if the version of autoconf is not really ancient. You can do this:

./configure CC='ccache gcc'

for example and that will set the default value of CC in your makefile to be ccache gcc. I don't know what you mean by "it does not ask for CC".

If you would prefer, you can also override the setting of CC on the make command line, like this:

make CC='ccache gcc'

which also works fine.

Upvotes: 10

Related Questions