lib.py 3.58 KB
Newer Older
Christian Dietrich's avatar
Christian Dietrich committed
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
import os
import fnmatch
import time
from versuchung.execute import shell, CommandFailed
import logging

def read_hash_directory(hash_dir, remove_keys = []):
    """Read in all records from a hash dir
    """
    ret = []
    for root, dirnames, filenames in os.walk(hash_dir):
        for filename in fnmatch.filter(filenames, '*.info'):
            with open(os.path.join(root, filename)) as fd:
                data = "[%s]" % (",".join(fd.readlines()))
                data = eval(data)
                for record in data:
                    for key in remove_keys:
                        del record[key]
                ret.extend(data)
                print len(ret)
    return ret


class ClangHashHelper:
    def project_name(self):
        return os.path.basename(self.metadata['project-clone-url'])

    def setup_compiler_paths(self, clang_path):
        if "ccache" in self.mode.value:
            cache_dir = os.path.join(self.tmp_directory.path, "ccache")
            os.mkdir(cache_dir)
            os.environ["CCACHE_DIR"] = cache_dir

        if self.mode.value == "normal":
            CC = os.path.join(clang_path, "build/wrappers/clang-normal")
        elif self.mode.value == "clang-hash":
            CC = os.path.join(clang_path, "build/wrappers/clang-hash-stop")
        elif self.mode.value == "ccache-clang-hash":
            CC = os.path.join(clang_path, "build/wrappers/clang-ccache-hash-stop")
        elif self.mode.value == "ccache":
            CC = os.path.join(clang_path, "build/wrappers/clang-ccache")
        else:
            raise RuntimeError("Not a valid mode")

        os.environ['CC'] = CC
        self.CC = CC

    def call_configure(self, path):
        if self.project_name() == "postgresql":
            shell("cd %s; ./configure --enable-depend", path)
51
        elif self.project_name() in ("musl", "bash", "samba"):
Christian Dietrich's avatar
Christian Dietrich committed
52
            shell("cd %s; ./configure", path)
53
54
        elif self.project_name() in ("cpython",):
            shell("cd %s; mkdir build; cd build; ../configure", path)
Christian Dietrich's avatar
Christian Dietrich committed
55
        elif self.project_name() in ('mbedtls'):
56
            shell("cd %s; mkdir build; cd build; cmake .. -DCMAKE_C_COMPILER=$CC -DENABLE_PROGRAMS=OFF", path)
Christian Dietrich's avatar
Christian Dietrich committed
57
58
59
60
61
        elif self.project_name() in ('lua',):
            # This is an ugly hack to make it possible to override the
            # CC variable from the outsite.
            with open("%s/makefile" % path) as fd:
                content = fd.readlines()
62
63
            content += "\nCC=%s\n" % (os.environ["CC"])

Christian Dietrich's avatar
Christian Dietrich committed
64
65
66
67
68
69
70
71
            with open("%s/makefile" % path, "w") as fd:
                fd.write("".join(content))


        else:
            raise RuntimeError("Not a valid project")

    def call_make(self, path):
72
        if self.project_name() in ("mbedtls", "cpython"):
73
74
75
            return shell("cd %s/build; make -j %s", path, str(self.jobs.value))
        else:
            return shell("cd %s; make -j %s", path, str(self.jobs.value))
Christian Dietrich's avatar
Christian Dietrich committed
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97

    def rebuild(self, path, info, fail_ok=False):
        # Recompile!
        start_time = time.time()
        try:
            ret = self.call_make(path)
        except CommandFailed as e:
            if not fail_ok:
                raise
            else:
                info['failed'] = True
                ret = ("", 1)
        end_time = time.time()

        # Account only nano seconds, everywhere
        build_time = int((end_time - start_time) * 1e9)
        info['build-time'] = build_time
        logging.info("Rebuild done[%s]: %s s; failed=%s",
                     info.get("filename") or info.get("commit"),
                     build_time / 1e9,
                     info.get("failed", False))
        return info