# Speed up recompilation with ccache

## fghellar

Update: from portage-2.0.7 up, CCACHE_DIR must be set as /var/tmp/ccache instead of /var/cache/ccache.

From the ccache ebuild:

 *Quote:*   

> Description: ccache is a fast compiler cache. It is used as a front end to your compiler to safely cache compilation output. When the same code is compiled again the cached output is used giving a significant speedup.

 

From the ccache manual page:

 *Quote:*   

> INSTALLATION
> 
> There are two ways to use ccache. You can either prefix your compile commands with "ccache" or you can create a symbolic link between ccache and the names of your compilers. The first method is most convenient if you just want to try out ccache or wish to use it for some specific projects. The second method is most useful for when you wish to use ccache for all your compiles.
> 
> To install for usage by the first method just copy ccache to somewhere in your path.
> ...

 

These are the steps to use ccache in Gentoo:

1. Update Portage, if needed. You'll need Portage version 2.0.2 or later (this is a sandbox issue).

2. Install ccache. (It's worth noticing that ccache has no dependencies, so it can be installed quite early, even before the "emerge system" install step! I wouldn't recommend installing it before bootstraping, though.)

3. Read the ccache manual.

4. Create the symbolic links:

```
# ln -s /usr/bin/ccache /bin/gcc

# ln -s /usr/bin/ccache /bin/cc

# ln -s /usr/bin/ccache /bin/g++

# ln -s /usr/bin/ccache /bin/c++
```

5. Create the ccache cache dir:

```
# mkdir /var/tmp/ccache
```

6. Add the following line to /etc/profile:

```
export CCACHE_DIR="/var/tmp/ccache"
```

7. Log out and back in, or manually run

```
# export CCACHE_DIR="/var/tmp/ccache"
```

8. Be happy!

I made some tests to see if it is really worthy. I ran the following commands twice with ccache disabled and three times with ccache enabled:

```
# ebuild /usr/portage/net-www/mozilla/mozilla-1.0-r1.ebuild clean

# ebuild /usr/portage/net-www/mozilla/mozilla-1.0-r1.ebuild unpack

# time ebuild /usr/portage/net-www/mozilla/mozilla-1.0-r1.ebuild compile
```

The results were, in order:

```
real    36m47.153s

user    32m57.496s

sys     2m18.362s

real    36m45.588s

user    32m59.537s

sys     2m17.597s

real    38m5.326s

user    33m10.850s

sys     2m22.291s

real    10m54.726s

user    6m1.358s

sys     1m37.028s

real    10m30.090s

user    5m42.772s

sys     1m37.005s
```

You can draw your own conclusions...  :Smile: 

----------

## lamer

what are you compiling this on that mozilla only takes 38 minutes?

----------

## fghellar

Athlon XP1700, 384 Mb PC2100.

----------

## asimon

Do those ccache timings with newer KDE sources and gcc 3.1 and you'll get completly different numbers  :Wink: 

I tested ccache for compiling cvs snapshots of kde 3.0.5 with gcc 3.1.

99% of all items in ccache's statistics were: "Compiler option not supported".

The compiler option in question was some obscure pre-processor flag which KDE uses when it detects gcc 3. So ccache was useless for me.

Cheers,

Andreas

----------

## fghellar

Can you provide more information about this? For example, what do you mean with "obscure pre-processor flag"? Did you link the gcc 3 binaries to ccache (they get different names if gcc 2 is installed too)? etc...

----------

## rukhoven

If you want to compile something with other cflags, then it will use your previously compiled code however wont it? It seems it will not be usefull in such a case...

----------

## fghellar

That would be a really stupid program, don't you think?  :Smile: 

Anyways, step 3 above should make it clear to you, esp. the section "How it works". (You can find it by going to Google and typing "ccache manual", if you don't have it installed.)

----------

## fghellar

Important update:

I just read in the gentoo-dev mailing list that there's been problems with ccache and newer versions of Portage. I'll see how it goes and come back with any related info.

Link: http://lists.gentoo.org/pipermail/gentoo-dev/2002-June/013248.html

----------

## asimon

 *fghellar wrote:*   

> Can you provide more information about this? For example, what do you mean with "obscure pre-processor flag"? Did you link the gcc 3 binaries to ccache (they get different names if gcc 2 is installed too)? etc...

 

I always got "argument -MT is unsupported" from ccache. This options gets added by KDEs building system when you use gcc 3. When ccache encountered this option in a compile command (and that was the case 99%) the compilation output was not put into the cache. After compiling most of KDEs modules (build dir > 2.5 GB) the cache of ccache was only 20 MB big, so it was quite useless. After two more compilations of the whole kde stuff I only had around 100 cache hits. But it was great when I used gcc 2.96 to compile KDE.

After some compilations you can check with ccache -s how efficent the cache was.

Cheers,

Andreas

----------

## rukhoven

 *fghellar wrote:*   

> That would be a really stupid program, don't you think? 
> 
> Anyways, step 3 above should make it clear to you, esp. the section "How it works". (You can find it by going to Google and typing "ccache manual", if you don't have it installed.)

 

So when I change the CFLAGS, it will not use the cached stuff if I recompile a program? Ok you seem sure of that, but I couldn't interpret the manual in such way to rule that out/in ...  :Smile:  Well I guess I'll just try that. Would some code be compiled more than once when compiling say, kdelibs ?

----------

## asimon

 *Quote:*   

> 
> 
> So when I change the CFLAGS, it will not use the cached stuff if I recompile a program? If ccache supports the flags which get used it works. Usually the options you put in CFLAGS are no problem for ccache. Would some code be compiled more than once when compiling say, kdelibs ?

 

If ccache can determine that a given compilation happend before (i.e. would result in the very same output as is already in the cache) the output in the cache is used and the compilation is not done a second time. But if you change code in the file, or change compile options, etc. the output of the new compilation would not be the same. Thus ccache cannot use the one in it s cache.

Cheers,

Andreas

----------

## Damasz

Wouldn't it be cool if the cache could be shared on a p2p network between gentoo users? (and other linux users....)

You could download the files instead of compiling them if another user with the same settings as you allready compiled the program.

Then the biggest problem a lot of people have with gentoo (long compile time) would be gone! Sweet.....

Gert-Jan.

----------

## ph317

*edit* there was a long post here about ideas for a global network-wide compiler cache - after thinking over it for a weekend I decided it was a dumb idea for a whole host of reasons, and deleted the post so that nobody else wasted brain cycles reading it and thinking about it *edit*  :Smile: Last edited by ph317 on Mon Jul 08, 2002 5:27 pm; edited 1 time in total

----------

## fghellar

 *Damasz wrote:*   

> You could download the files instead of compiling them if another user with the same settings as you allready compiled the program.

 

Not always a good idea: https://forums.gentoo.org/viewtopic.php?p=33015.

----------

## tfoss

Just a warning:

I spent a couple hours trying to figure out why emerging kdelibs kept giving some odd libSTL...  not found error.  Turns out unmerging ccache fixed it. 

-Ted

----------

## fghellar

Can you check if it has anything to do with this?

----------

## micron

I've followed all the steps but

```
/var/tmp/ccache
```

is empty...  :Shocked: 

what can I do?

----------

## MooktaKiNG

 *micron wrote:*   

> I've followed all the steps but
> 
> ```
> /var/tmp/ccache
> ```
> ...

 

This is a VERY old post.

the ccache feature has now been buil into portage.

here: 

http://www.gentoo.org/doc/en/handbook/handbook-x86.xml?part=2&chap=3#doc_chap2

----------

## micron

 *MooktaKiNG wrote:*   

> 
> 
> This is a VERY old post.
> 
> the ccache feature has now been buil into portage.
> ...

 

Thanks a lot!

----------

