speedup will be higher when I/O is fast (e.g., when files are in the disk
cache). The direct mode can be disabled by setting +CCACHE_NODIRECT+.
- - When hashing the output from the preprocessor, absolute paths are
- rewritten to relative paths, but only for paths under the directory
- specified by `CCACHE_BASEDIR`. Paths specified by `-I` and similar
- options get the same treatment. This is done to get cache hits even when
- compiling with `-g` and when using absolute include directory paths.
- Absolute paths in the standard error text will also be more accurate. The
- default value of `CCACHE_BASEDIR` is the current working directory. To
- disable the rewriting, set `CCACHE_BASEDIR` to the empty string.
+ - Support has been added for rewriting absolute paths to relative paths
+ when hashing, in order to increase cache hit rate when building the same
+ source code in different directories even when compiling with `-g` and
+ when using absolute include directory paths. This is done by setting the
+ `CCACHE_BASEDIR` environment variable to an absolute path that specifies
+ which paths to rewrite.
- Object files are now by default stored compressed in the cache. The
runtime cost is negligible, and more files will fit in the ccache
*CCACHE_BASEDIR*::
- When hashing the output from the preprocessor, absolute paths are rewritten
- to relative paths, but only for paths under the directory specified by
- *CCACHE_BASEDIR*. Paths specified by *-I* and similar options get the same
- treatment. This is done to get cache hits even when compiling with *-g* and
- when using absolute include directory paths. The default value of
- *CCACHE_BASEDIR* is the current working directory. To disable the
- rewriting, set *CCACHE_BASEDIR* to the empty string.
+ If you set the environment variable *CCACHE_BASEDIR* to an absolute path,
+ ccache rewrites absolute paths into relative paths before computing the
+ hash that identifies the compilation, but only for paths under the
+ specified directory. See the discussion under
+ <<_compiling_in_different_directories,COMPILING IN DIFFERENT DIRECTORIES>>.
*CCACHE_CC*::
preprocessor output if they have any effect at all)
* the real compiler's size and modification time (unless
*CCACHE_COMPILERCHECK* says something else)
-* Any stderr output generated by the preprocessor
-
-
-USING CCACHE WITH DISTCC
-------------------------
-
-``distcc'' is a very useful program for distributing compilation across a range
-of compiler servers. It is often useful to combine distcc with ccache so that
-compilations that are done are sped up by distcc, but ccache avoids the
-compilation completely where possible.
-
-The recommended way of combining distcc and ccache is by using the
-*CCACHE_PREFIX* option. You just need to set the environment variable
-*CCACHE_PREFIX* to *distcc* and ccache will prefix the command line used with
-the compiler with the command ``distcc''.
+* any standard error output generated by the preprocessor
+
+
+COMPILING IN DIFFERENT DIRECTORIES
+----------------------------------
+
+Some information included in the hash that identifies a unique compilation may
+contain absolute paths:
+
+* The preprocessed source code may contain absolute paths to include files if
+ the compiler option *-g* is used or if absolute paths are given to *-I* and
+ similar compiler options.
+* Paths specified by compiler options (such as *-I*, *-MF*, etc) may be
+ absolute.
+* The source code file path may be absolute, and that path may be included in
+ warnings emitted to standard error by the preprocessor.
+
+This means that if you compile the same code in different locations, you can't
+share compilation results between the different build directories since you get
+cache misses because of the absolute build directory paths that are part of the
+hash. To mitigate this problem, you can specify a ``base directory'' by setting
+the *CCACHE_BASEDIR* variable to an absolute path. ccache will then rewrite
+absolute paths that are under the base directory (i.e., paths that have the
+base directory as a prefix) to relative paths when constructing the hash. The
+typical path to use as base directory is your home directory or another
+directory that is a parent of your build directories. (Don't use +/+ as the
+base directory since that will make ccache also rewrite paths to system header
+files, which doesn't gain anything.)
+
+The only drawback of using *CCACHE_BASEDIR* is that if you compile with *-g*
+and use absolute paths to the source code files, the source code paths that are
+stored in the object files may point to the wrong directory, which may prevent
+debuggers like GDB from finding the source code. Sometimes, a work-around is to
+change the directory explicitly with the ``cd'' command in GDB.
SHARING A CACHE
Typically, users will see that their libraries and binaries are relinked
without reason.
+You may also want to make sure that the developers have *CCACHE_BASEDIR* set
+appropriately, as discussed in the previous section.
+
+
+USING CCACHE WITH DISTCC
+------------------------
+
+``distcc'' is a very useful program for distributing compilation across a range
+of compiler servers. It is often useful to combine distcc with ccache so that
+compilations that are done are sped up by distcc, but ccache avoids the
+compilation completely where possible.
+
+The recommended way of combining distcc and ccache is by using the
+*CCACHE_PREFIX* option. You just need to set the environment variable
+*CCACHE_PREFIX* to *distcc* and ccache will prefix the command line used with
+the compiler with the command ``distcc''.
+
MORE INFORMATION
----------------
export CCACHE_NODIRECT
##################################################################
- # CCACHE_BASEDIR="$PWD" is the default.
+ # CCACHE_BASEDIR="" is the default.
testname="default CCACHE_BASEDIR"
cd dir1
$CCACHE -z >/dev/null
$CCACHE $COMPILER -I$PWD/include -c src/test.c
checkstat 'cache hit (direct)' 0
- checkstat 'cache hit (preprocessed)' 1
- checkstat 'cache miss' 0
+ checkstat 'cache hit (preprocessed)' 0
+ checkstat 'cache miss' 1
cd ..
##################################################################
cd dir1
$CCACHE -z >/dev/null
unset CCACHE_NODIRECT
- $CCACHE $COMPILER -I$PWD//include -c $PWD//.///src/test.c
+ CCACHE_BASEDIR=$PWD $CCACHE $COMPILER -I$PWD//include -c $PWD//.///src/test.c
export CCACHE_NODIRECT=1
checkstat 'cache hit (direct)' 1
checkstat 'cache hit (preprocessed)' 0
# Check that rewriting triggered by CCACHE_BASEDIR also affects stderr.
testname="stderr"
$CCACHE -z >/dev/null
- $CCACHE $COMPILER -Wall -W -I$PWD -c $PWD/stderr.c -o $PWD/stderr.o 2>stderr.txt
+ CCACHE_BASEDIR=$PWD $CCACHE $COMPILER -Wall -W -I$PWD -c $PWD/stderr.c -o $PWD/stderr.o 2>stderr.txt
checkstat 'cache hit (direct)' 0
checkstat 'cache hit (preprocessed)' 0
checkstat 'cache miss' 1
test_failed "Base dir ($PWD) found in stderr:\n`cat stderr.txt`"
fi
- $CCACHE $COMPILER -Wall -W -I$PWD -c $PWD/stderr.c -o $PWD/stderr.o 2>stderr.txt
+ CCACHE_BASEDIR=$PWD $CCACHE $COMPILER -Wall -W -I$PWD -c $PWD/stderr.c -o $PWD/stderr.o 2>stderr.txt
checkstat 'cache hit (direct)' 0
checkstat 'cache hit (preprocessed)' 1
checkstat 'cache miss' 1