summaryrefslogtreecommitdiff
path: root/bench/Build.py
blob: 67d28174b957036fd67620c2a4dfe960d1907547 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# distcc/benchmark -- automated system for testing distcc correctness
# and performance on various source trees.

# Copyright (C) 2002, 2003 by Martin Pool

# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.

# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA

from Project import Project
from compiler import CompilerSpec
import buildutil
from buildutil import make_dir, run_cmd, rm_files
import re, os, sys, time



class Build:
    """A Build is a combination of a Project and CompilerSpec.

    """
    def __init__(self, project, compiler, n_repeats):
        self.project = project
        self.compiler = compiler
        self.n_repeats = n_repeats

        self.base_dir = os.path.join(os.getcwd(), "build", self.project.name, self.compiler.name)
        self.unpacked_dir = os.path.join(self.base_dir, self.project.unpacked_subdir)

        # Some packages need to be started from a subdirectory of their
        # unpacked form.  For example, Samba is compiled from the "source/"
        # subdirectory of the unpacked source.
        if self.project.build_subdir:
            self.build_dir = os.path.join(self.unpacked_dir, project.build_subdir)
        else:
            self.build_dir = self.unpacked_dir

        self.log_dir = self.build_dir

    def __repr__(self):
        return "Build(%s, %s)" % (`self.project`, `self.compiler`)


    def unpack(self):
        """Unpack from source tarball into build directory"""
        if re.search(r"\.tar\.bz2$", self.project.package_file):
            tar_fmt = "tar xf %s --bzip2"
        else:
            tar_fmt = "tar xfz %s"

        tar_cmd = tar_fmt % os.path.join(os.getcwd(), self.project.package_dir,
                                         self.project.package_file)

        make_dir(self.base_dir)
        print "** Unpacking..."
        run_cmd("cd %s && %s" % (self.base_dir, tar_cmd))


    def configure(self, compiler):
        """Run configuration command for this tree, if any."""
        self.compiler = compiler

        make_dir(self.log_dir)

        configure_log = os.path.join(self.log_dir, "bench-configure.log")
        distcc_log = os.path.join(self.log_dir, "bench-configure-distcc.log")

        rm_files((configure_log, distcc_log))

        print "** Configuring..."
        run_cmd("cd %s && \\\nDISTCC_LOG='%s' \\\nCC='%s' \\\nCXX='%s' \\\n%s \\\n>%s 2>&1" %
                (self.build_dir, distcc_log, self.compiler.cc,
                 self.compiler.cxx,
                 self.project.configure_cmd, configure_log))


    def build(self, sum):
        """Actually build the package."""

        build_log = os.path.join(self.log_dir, "bench-build.log")
        prebuild_log = os.path.join(self.log_dir, "bench-prebuild.log")

        distcc_log = os.path.join(self.log_dir, "bench-build-distcc.log")

        rm_files((build_log, distcc_log))

        print "** Building..."
        if self.project.pre_build_cmd:
            cmd = ("cd %s && %s > %s 2>&1" % (self.build_dir,
                                              self.project.pre_build_cmd,
                                              prebuild_log))
            run_cmd(cmd)
            
        cmd = ("cd %s && \\\n%s \\\nDISTCC_LOG='%s' \\\nCC='%s' \\\nCXX='%s' \\\n%s \\\n>%s 2>&1" % 
               (self.build_dir, self.project.build_cmd, distcc_log,
                self.compiler.cc,
                self.compiler.cxx,
                self.compiler.make_opts,
                build_log))
        result, elapsed = run_cmd(cmd)
        return elapsed


    def clean(self):
        clean_log = os.path.join(self.log_dir, "bench-clean.log")
        print "** Cleaning build directory"
        cmd = "cd %s && make clean >%s 2>&1" % (self.build_dir, clean_log)
        run_cmd(cmd)
        

    def scrub(self):
        print "** Removing build directory"
        run_cmd("rm -rf %s" % self.unpacked_dir)


    def build_actions(self, actions, summary):
        """Carry out selected actions.

        Catch exceptions and handle."""
        try:
            times = []
            if 'sweep' in actions:
                self.scrub()
            if 'unpack' in actions:
                self.unpack()
            if 'configure' in actions:
                self.configure(self.compiler)
            for i in range(self.n_repeats):
                if 'build' in actions:
                    times.append(self.build(summary))
                if 'clean' in actions:
                    self.clean()
            if 'scrub' in actions:
                self.scrub()
            summary.store(self.project, self.compiler, times)
        except KeyboardInterrupt:
            raise
        except:
            apply(sys.excepthook, sys.exc_info()) # print traceback
            summary.store(self.project, self.compiler, 'FAIL')