1#
2# Usage:
3# - Enable ccache
4#   Add the following line to a conffile such as conf/local.conf:
5#   INHERIT += "ccache"
6#
7# - Disable ccache for a recipe
8#   Add the following line to the recipe if it can't be built with ccache:
9#   CCACHE_DISABLE = '1'
10#
11# - Share ccache files between different builds
12#   Set CCACHE_TOP_DIR to a shared dir
13#   CCACHE_TOP_DIR = /path/to/shared_ccache/
14#
15# - TO debug ccahe
16#   export CCACHE_DEBUG = "1"
17#   export CCACHE_LOGFILE = "${CCACHE_DIR}/logfile.log"
18#   And also set PARALLEL_MAKE = "-j 1" to get make the log in order
19#
20
21# Set it to a shared location for different builds, so that cache files can
22# be shared between different builds.
23CCACHE_TOP_DIR ?= "${TMPDIR}/ccache"
24
25# ccahe removes CCACHE_BASEDIR from file path, so that hashes will be the same
26# in different builds.
27export CCACHE_BASEDIR ?= "${TMPDIR}"
28
29# Used for sharing cache files after compiler is rebuilt
30export CCACHE_COMPILERCHECK ?= "%compiler% -dumpspecs"
31
32export CCACHE_CONFIGPATH ?= "${COREBASE}/meta/conf/ccache.conf"
33
34export CCACHE_DIR ?= "${CCACHE_TOP_DIR}/${MULTIMACH_TARGET_SYS}/${PN}"
35
36# Fixed errors:
37# ccache: error: Failed to create directory /run/user/0/ccache-tmp: Permission denied
38export CCACHE_TEMPDIR ?= "${CCACHE_DIR}/tmp"
39
40# We need to stop ccache considering the current directory or the
41# debug-prefix-map target directory to be significant when calculating
42# its hash. Without this the cache would be invalidated every time
43# ${PV} or ${PR} change.
44export CCACHE_NOHASHDIR ?= "1"
45
46python() {
47    """
48    Enable ccache for the recipe
49    """
50    pn = d.getVar('PN')
51    # quilt-native doesn't need ccache since no c files
52    if not (bb.data.inherits_class("native", d) or
53            bb.utils.to_boolean(d.getVar('CCACHE_DISABLE'))):
54        d.appendVar('DEPENDS', ' ccache-native')
55        d.setVar('CCACHE', 'ccache ')
56}
57
58addtask cleanccache after do_clean
59python do_cleanccache() {
60    import shutil
61
62    ccache_dir = d.getVar('CCACHE_DIR')
63    if os.path.exists(ccache_dir):
64        bb.note("Removing %s" % ccache_dir)
65        shutil.rmtree(ccache_dir)
66    else:
67        bb.note("%s doesn't exist" % ccache_dir)
68}
69addtask cleanall after do_cleanccache
70do_cleanccache[nostamp] = "1"
71