lib.py 4.06 KB
Newer Older
Christian Dietrich's avatar
Christian Dietrich committed
1
2
3
import os
import fnmatch
import time
4
from versuchung.execute import shell, CommandFailed, shell_failok
Christian Dietrich's avatar
Christian Dietrich committed
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 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"):
52
            shell_failok("cd %s; ./configure", path)
53
        elif self.project_name() in ("cpython",):
54
55
56
57
            shell("cd %s; mkdir -p build build/Modules;", path)
            shell("cd %s; cp -u Modules/Setup.dist build/Modules/Setup", path)
            shell("cd %s; cd build; ../configure", path)

Christian Dietrich's avatar
Christian Dietrich committed
58
        elif self.project_name() in ('mbedtls'):
59
            shell("cd %s; mkdir build; cd build; cmake .. -DCMAKE_C_COMPILER=$CC -DENABLE_PROGRAMS=OFF", path)
Christian Dietrich's avatar
Christian Dietrich committed
60
61
62
63
64
        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()
65
66
            content += "\nCC=%s\n" % (os.environ["CC"])

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


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

74
75
76
77
    def call_reconfigure(self, path):
        if self.project_name() in ('lua',):
            self.call_configure(path)
        if self.project_name() in ('cpython',):
78
            shell("cd %s; mkdir -p build/Modules; cp -u Modules/Setup.dist build/Modules/Setup", path)
79
80
            shell_failok("cd %s/build; make config.status;", path)

81

82

Christian Dietrich's avatar
Christian Dietrich committed
83
    def call_make(self, path):
84
        if self.project_name() in ("mbedtls", "cpython"):
85
86
87
            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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109

    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