summaryrefslogtreecommitdiffstats
path: root/python/mozbuild/mozbuild/controller/clobber.py
blob: 02f75c6adaada3f6c38a29e9fc6037baad5aab40 (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

from __future__ import absolute_import, print_function

r'''This module contains code for managing clobbering of the tree.'''

import errno
import os
import subprocess
import sys

from mozfile.mozfile import remove as mozfileremove
from textwrap import TextWrapper


CLOBBER_MESSAGE = ''.join([TextWrapper().fill(line) + '\n' for line in
'''
The CLOBBER file has been updated, indicating that an incremental build since \
your last build will probably not work. A full/clobber build is required.

The reason for the clobber is:

{clobber_reason}

Clobbering can be performed automatically. However, we didn't automatically \
clobber this time because:

{no_reason}

The easiest and fastest way to clobber is to run:

 $ mach clobber

If you know this clobber doesn't apply to you or you're feeling lucky -- \
Well, are ya? -- you can ignore this clobber requirement by running:

 $ touch {clobber_file}
'''.splitlines()])

class Clobberer(object):
    def __init__(self, topsrcdir, topobjdir):
        """Create a new object to manage clobbering the tree.

        It is bound to a top source directory and to a specific object
        directory.
        """
        assert os.path.isabs(topsrcdir)
        assert os.path.isabs(topobjdir)

        self.topsrcdir = os.path.normpath(topsrcdir)
        self.topobjdir = os.path.normpath(topobjdir)
        self.src_clobber = os.path.join(topsrcdir, 'CLOBBER')
        self.obj_clobber = os.path.join(topobjdir, 'CLOBBER')

        # Try looking for mozilla/CLOBBER, for comm-central
        if not os.path.isfile(self.src_clobber):
            self.src_clobber = os.path.join(topsrcdir, 'mozilla', 'CLOBBER')

        assert os.path.isfile(self.src_clobber)

    def clobber_needed(self):
        """Returns a bool indicating whether a tree clobber is required."""

        # No object directory clobber file means we're good.
        if not os.path.exists(self.obj_clobber):
            return False

        # Object directory clobber older than current is fine.
        if os.path.getmtime(self.src_clobber) <= \
            os.path.getmtime(self.obj_clobber):

            return False

        return True

    def clobber_cause(self):
        """Obtain the cause why a clobber is required.

        This reads the cause from the CLOBBER file.

        This returns a list of lines describing why the clobber was required.
        Each line is stripped of leading and trailing whitespace.
        """
        with open(self.src_clobber, 'rt') as fh:
            lines = [l.strip() for l in fh.readlines()]
            return [l for l in lines if l and not l.startswith('#')]

    def have_winrm(self):
        # `winrm -h` should print 'winrm version ...' and exit 1
        try:
            p = subprocess.Popen(['winrm.exe', '-h'],
                                 stdout=subprocess.PIPE,
                                 stderr=subprocess.STDOUT)
            return p.wait() == 1 and p.stdout.read().startswith('winrm')
        except:
            return False

    def remove_objdir(self, full=True):
        """Remove the object directory.

        ``full`` controls whether to fully delete the objdir. If False,
        some directories (e.g. Visual Studio Project Files) will not be
        deleted.
        """
        # Top-level files and directories to not clobber by default.
        no_clobber = {
            '.mozbuild',
            'msvc',
        }

        if full:
            # mozfile doesn't like unicode arguments (bug 818783).
            paths = [self.topobjdir.encode('utf-8')]
        else:
            try:
                paths = []
                for p in os.listdir(self.topobjdir):
                    if p not in no_clobber:
                        paths.append(os.path.join(self.topobjdir, p).encode('utf-8'))
            except OSError as e:
                if e.errno != errno.ENOENT:
                    raise
                return

        procs = []
        for p in sorted(paths):
            path = os.path.join(self.topobjdir, p)
            if sys.platform.startswith('win') and self.have_winrm() and os.path.isdir(path):
                procs.append(subprocess.Popen(['winrm', '-rf', path]))
            else:
                # We use mozfile because it is faster than shutil.rmtree().
                mozfileremove(path)

        for p in procs:
            p.wait()

    def ensure_objdir_state(self):
        """Ensure the CLOBBER file in the objdir exists.

        This is called as part of the build to ensure the clobber information
        is configured properly for the objdir.
        """
        if not os.path.exists(self.topobjdir):
            os.makedirs(self.topobjdir)

        if not os.path.exists(self.obj_clobber):
            # Simply touch the file.
            with open(self.obj_clobber, 'a'):
                pass

    def maybe_do_clobber(self, cwd, allow_auto=False, fh=sys.stderr):
        """Perform a clobber if it is required. Maybe.

        This is the API the build system invokes to determine if a clobber
        is needed and to automatically perform that clobber if we can.

        This returns a tuple of (bool, bool, str). The elements are:

          - Whether a clobber was/is required.
          - Whether a clobber was performed.
          - The reason why the clobber failed or could not be performed. This
            will be None if no clobber is required or if we clobbered without
            error.
        """
        assert cwd
        cwd = os.path.normpath(cwd)

        if not self.clobber_needed():
            print('Clobber not needed.', file=fh)
            self.ensure_objdir_state()
            return False, False, None

        # So a clobber is needed. We only perform a clobber if we are
        # allowed to perform an automatic clobber (off by default) and if the
        # current directory is not under the object directory. The latter is
        # because operating systems, filesystems, and shell can throw fits
        # if the current working directory is deleted from under you. While it
        # can work in some scenarios, we take the conservative approach and
        # never try.
        if not allow_auto:
            return True, False, \
               self._message('Automatic clobbering is not enabled\n'
                              '  (add "mk_add_options AUTOCLOBBER=1" to your '
                              'mozconfig).')

        if cwd.startswith(self.topobjdir) and cwd != self.topobjdir:
            return True, False, self._message(
                'Cannot clobber while the shell is inside the object directory.')

        print('Automatically clobbering %s' % self.topobjdir, file=fh)
        try:
            self.remove_objdir(False)
            self.ensure_objdir_state()
            print('Successfully completed auto clobber.', file=fh)
            return True, True, None
        except (IOError) as error:
            return True, False, self._message(
                'Error when automatically clobbering: ' + str(error))

    def _message(self, reason):
        lines = [' ' + line for line in self.clobber_cause()]

        return CLOBBER_MESSAGE.format(clobber_reason='\n'.join(lines),
            no_reason='  ' + reason, clobber_file=self.obj_clobber)


def main(args, env, cwd, fh=sys.stderr):
    if len(args) != 2:
        print('Usage: clobber.py topsrcdir topobjdir', file=fh)
        return 1

    topsrcdir, topobjdir = args

    if not os.path.isabs(topsrcdir):
        topsrcdir = os.path.abspath(topsrcdir)

    if not os.path.isabs(topobjdir):
        topobjdir = os.path.abspath(topobjdir)

    auto = True if env.get('AUTOCLOBBER', False) else False
    clobber = Clobberer(topsrcdir, topobjdir)
    required, performed, message = clobber.maybe_do_clobber(cwd, auto, fh)

    if not required or performed:
        if performed and env.get('TINDERBOX_OUTPUT'):
            print('TinderboxPrint: auto clobber', file=fh)
        return 0

    print(message, file=fh)
    return 1


if __name__ == '__main__':
    sys.exit(main(sys.argv[1:], os.environ, os.getcwd(), sys.stdout))