summaryrefslogtreecommitdiffstats
path: root/py/mock/plugins/ccache.py
blob: 77a9130b958c5da30549eb9cfd84b40cc193fa1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# vim:expandtab:autoindent:tabstop=4:shiftwidth=4:filetype=python:textwidth=0:
# License: GPL2 or later see COPYING
# Written by Michael Brown
# Copyright (C) 2007 Michael E Brown <mebrown@michaels-house.net>

# python library imports
import os

# our imports
from mock.trace_decorator import decorate, traceLog, getLog
import mock.util

requires_api_version = "1.0"

# plugin entry point
decorate(traceLog())
def init(rootObj, conf):
    CCache(rootObj, conf)

# classes
class CCache(object):
    """enables ccache in buildroot/rpmbuild"""
    decorate(traceLog())
    def __init__(self, rootObj, conf):
        self.rootObj = rootObj
        self.ccache_opts = conf
        self.ccachePath = self.ccache_opts['dir'] % self.ccache_opts
        rootObj.ccacheObj = self
        rootObj.preExistingDeps = rootObj.preExistingDeps + " ccache "
        rootObj.addHook("prebuild", self._ccacheBuildHook)
        rootObj.addHook("preinit",  self._ccachePreInitHook)
        rootObj.umountCmds.append('umount -n %s' % rootObj.makeChrootPath("/tmp/ccache"))
        rootObj.mountCmds.append('mount -n --bind %s  %s' % (self.ccachePath, rootObj.makeChrootPath("/tmp/ccache")))

    # =============
    # 'Private' API
    # =============
    # set the max size before we actually use it during a build.
    # ccache itself manages size and settings.
    decorate(traceLog())
    def _ccacheBuildHook(self):
        self.rootObj.doChroot("ccache -M %s" % self.ccache_opts['max_cache_size'])

    # basic idea here is that we add 'cc', 'gcc', 'g++' shell scripts to
    # to /tmp/ccache, which is bind-mounted from a shared location.
    # we then add this to the front of the path.
    # we also set a few admin variables used by ccache to find the shared
    # cache.
    decorate(traceLog())
    def _ccachePreInitHook(self):
        mock.util.mkdirIfAbsent(self.rootObj.makeChrootPath('/tmp/ccache'))
        mock.util.mkdirIfAbsent(self.ccachePath)
        os.environ['PATH'] = "/tmp/ccache:%s" % (os.environ['PATH'])
        os.environ['CCACHE_DIR'] = "/tmp/ccache"
        os.environ['CCACHE_UMASK'] = "002"
        for i in ("cc", "gcc", "gcc296", "gcc32", "gcc33", "gcc34",
                "g++", "c++", "c++32", "c++33", "c++34", "g++296", "g++32", "g++33", "g++34",
                "g++-libstdc++-so_7",):
            forceLink("/usr/bin/ccache", os.path.join(self.ccachePath, "%s" % i))
            forceLink("/usr/bin/ccache", os.path.join(self.ccachePath, "x86_64-redhat-linux-%s" % i))
            forceLink("/usr/bin/ccache", os.path.join(self.ccachePath, "i386-redhat-linux-%s" % i))

decorate(traceLog())
def forceLink( existing, linkname ):
    try:
        os.unlink(linkname)
    except OSError:
        pass

    os.symlink(existing, linkname)