source: rtems-source-builder/source-builder/sb/setbuilder.py @ 2802080

4.104.114.95
Last change on this file since 2802080 was 2802080, checked in by Chris Johns <chrisj@…>, on 09/03/13 at 22:45:04

sb: Do not create a build set tar file on error.

If creating build set tar files and using --keep-going do not create
the tar file if any part of the set build fails.

  • Property mode set to 100644
File size: 18.3 KB
Line 
1#
2# RTEMS Tools Project (http://www.rtems.org/)
3# Copyright 2010-2013 Chris Johns (chrisj@rtems.org)
4# All rights reserved.
5#
6# This file is part of the RTEMS Tools package in 'rtems-tools'.
7#
8# Permission to use, copy, modify, and/or distribute this software for any
9# purpose with or without fee is hereby granted, provided that the above
10# copyright notice and this permission notice appear in all copies.
11#
12# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
20#
21# This code builds a package compiler tool suite given a tool set. A tool
22# set lists the various tools. These are specific tool configurations.
23#
24
25import copy
26import datetime
27import glob
28import operator
29import os
30import sys
31
32try:
33    import build
34    import check
35    import error
36    import log
37    import mailer
38    import options
39    import path
40    import reports
41    import version
42except KeyboardInterrupt:
43    print 'abort: user terminated'
44    sys.exit(1)
45except:
46    print 'error: unknown application load error'
47    sys.exit(1)
48
49class buildset:
50    """Build a set builds a set of packages."""
51
52    def __init__(self, bset, _configs, opts, macros = None):
53        log.trace('_bset: %s: init' % (bset))
54        self.configs = _configs
55        self.opts = opts
56        if macros is None:
57            self.macros = copy.copy(opts.defaults)
58        else:
59            self.macros = copy.copy(macros)
60        self.bset = bset
61        self.bset_pkg = '%s-%s-set' % (self.macros.expand('%{_target}'), self.bset)
62        self.mail_header = ''
63        self.mail_report = ''
64        self.build_failure = None
65
66    def write_mail_header(self, text, prepend = False):
67        if len(text) == 0 or text[-1] != '\n' or text[-1] != '\r':
68            text += os.linesep
69        if prepend:
70            self.mail_header = text + self.mail_header
71        else:
72            self.mail_header += text
73
74    def write_mail_report(self, text, prepend = False):
75        if len(text) == 0 or text[-1] != '\n' or text[-1] != '\r':
76            text += os.linesep
77        if prepend:
78            self.mail_report = text + self.mail_report
79        else:
80            self.mail_report += text
81
82    def copy(self, src, dst):
83        log.output('copy: %s => %s' % (path.host(src), path.host(dst)))
84        if not self.opts.dry_run():
85            path.copy_tree(src, dst)
86
87    def report(self, _config, _build):
88        if not _build.opts.get_arg('--no-report') \
89           and not _build.macros.get('%{_disable_reporting}') \
90           and _build.opts.get_arg('--mail'):
91            format = _build.opts.get_arg('--report-format')
92            if format is None:
93                format = 'html'
94                ext = '.html'
95            else:
96                if len(format) != 2:
97                    raise error.general('invalid report format option: %s' % ('='.join(format)))
98                if format[1] == 'text':
99                    format = 'text'
100                    ext = '.txt'
101                elif format[1] == 'asciidoc':
102                    format = 'asciidoc'
103                    ext = '.txt'
104                elif format[1] == 'html':
105                    format = 'html'
106                    ext = '.html'
107                else:
108                    raise error.general('invalid report format: %s' % (format[1]))
109            buildroot = _build.config.abspath('%{buildroot}')
110            prefix = _build.macros.expand('%{_prefix}')
111            name = _build.main_package().name() + ext
112            log.notice('reporting: %s -> %s' % (_config, name))
113            if not _build.opts.get_arg('--no-report'):
114                outpath = path.host(path.join(buildroot, prefix, 'share', 'rtems-source-builder'))
115                outname = path.host(path.join(outpath, name))
116                r = reports.report(format, self.configs, _build.opts, _build.macros)
117                r.setup()
118                r.introduction(_build.config.file_name())
119                r.config(_build.config, _build.opts, _build.macros)
120                if not _build.opts.dry_run():
121                    _build.mkdir(outpath)
122                    r.write(outname)
123                del r
124            if not _build.macros.get('%{_disable_reporting}') \
125               and _build.opts.get_arg('--mail'):
126                r = reports.report('text', self.configs, _build.opts, _build.macros)
127                r.setup()
128                r.introduction(_build.config.file_name())
129                r.config(_build.config, _build.opts, _build.macros)
130                self.write_mail_report(r.out)
131                del r
132
133    def root_copy(self, src, dst):
134        what = '%s -> %s' % \
135            (os.path.relpath(path.host(src)), os.path.relpath(path.host(dst)))
136        log.trace('_bset: %s: collecting: %s' % (self.bset, what))
137        self.copy(src, dst)
138
139    def install(self, name, buildroot, prefix):
140        dst = prefix
141        src = path.join(buildroot, prefix)
142        log.notice('installing: %s -> %s' % (name, path.host(dst)))
143        self.copy(src, dst)
144
145    def canadian_cross(self, _build):
146        # @fixme Switch to using a private macros map.
147        macros_to_save = ['%{_prefix}',
148                          '%{_tmproot}',
149                          '%{buildroot}',
150                          '%{_builddir}',
151                          '%{_host}']
152        macros_to_copy = [('%{_host}',     '%{_build}'),
153                          ('%{_tmproot}',  '%{_tmpcxcroot}'),
154                          ('%{buildroot}', '%{buildcxcroot}'),
155                          ('%{_builddir}', '%{_buildcxcdir}')]
156        orig_macros = {}
157        for m in macros_to_save:
158            orig_macros[m] = _build.config.macro(m)
159        for m in macros_to_copy:
160            _build.config.set_define(m[0], _build.config.macro(m[1]))
161        _build.make()
162        for m in macros_to_save:
163            _build.config.set_define(m, orig_macros[m])
164        if not _build.macros.get('%{_disable_collecting}'):
165            self.root_copy(_build.config.expand('%{buildcxcroot}'),
166                           _build.config.expand('%{_tmpcxcroot}'))
167
168    def build_package(self, _config, _build):
169        if not _build.disabled():
170            if _build.canadian_cross():
171                self.canadian_cross(_build)
172            _build.make()
173            self.report(_config, _build)
174            if not _build.macros.get('%{_disable_collecting}'):
175                self.root_copy(_build.config.expand('%{buildroot}'),
176                               _build.config.expand('%{_tmproot}'))
177
178    def bset_tar(self, _build):
179        tardir = _build.config.expand('%{_tardir}')
180        if self.opts.get_arg('--bset-tar-file') \
181           and not _build.macros.get('%{_disable_packaging}'):
182            path.mkdir(tardir)
183            tar = path.join(tardir, _build.config.expand('%s.tar.bz2' % (self.bset_pkg)))
184            log.notice('tarball: %s' % (os.path.relpath(path.host(tar))))
185            if not self.opts.dry_run():
186                tmproot = _build.config.expand('%{_tmproot}')
187                cmd = _build.config.expand("'cd " + tmproot + \
188                                               " && %{__tar} -cf - . | %{__bzip2} > " + tar + "'")
189                _build.run(cmd, shell_opts = '-c', cwd = tmproot)
190
191    def parse(self, bset):
192
193        def _clean(line):
194            line = line[0:-1]
195            b = line.find('#')
196            if b >= 0:
197                line = line[1:b]
198            return line.strip()
199
200        bsetname = bset
201
202        if not path.exists(bsetname):
203            for cp in self.macros.expand('%{_configdir}').split(':'):
204                configdir = path.abspath(cp)
205                bsetname = path.join(configdir, bset)
206                if path.exists(bsetname):
207                    break
208                bsetname = None
209            if bsetname is None:
210                raise error.general('no build set file found: %s' % (bset))
211        try:
212            log.trace('_bset: %s: open: %s' % (self.bset, bsetname))
213            bset = open(path.host(bsetname), 'r')
214        except IOError, err:
215            raise error.general('error opening bset file: %s' % (bsetname))
216
217        configs = []
218
219        try:
220            lc = 0
221            for l in bset:
222                lc += 1
223                l = _clean(l)
224                if len(l) == 0:
225                    continue
226                log.trace('_bset: %s: %03d: %s' % (self.bset, lc, l))
227                ls = l.split()
228                if ls[0][-1] == ':' and ls[0][:-1] == 'package':
229                    self.bset_pkg = self.macros.expand(ls[1].strip())
230                    self.macros['package'] = self.bset_pkg
231                elif ls[0][0] == '%':
232                    if ls[0] == '%define':
233                        if len(ls) > 2:
234                            self.macros.define(ls[1].strip(),
235                                               ' '.join([f.strip() for f in ls[2:]]))
236                        else:
237                            self.macros.define(ls[1].strip())
238                    elif ls[0] == '%undefine':
239                        if len(ls) > 2:
240                            raise error.general('%s:%d: %undefine requires just the name' % \
241                                                    (self.bset, lc))
242                        self.macros.undefine(ls[1].strip())
243                    elif ls[0] == '%include':
244                        configs += self.parse(ls[1].strip())
245                    else:
246                        raise error.general('%s:%d: invalid directive in build set files: %s' % \
247                                                (self.bset, lc, l))
248                else:
249                    l = l.strip()
250                    c = build.find_config(l, self.configs)
251                    if c is None:
252                        raise error.general('%s:%d: cannot find file: %s' % (self.bset, lc, l))
253                    configs += [c]
254        except:
255            bset.close()
256            raise
257
258        bset.close()
259
260        return configs
261
262    def load(self):
263
264        #
265        # If the build set file ends with .cfg the user has passed to the
266        # buildset builder a configuration so we just return it.
267        #
268        if self.bset.endswith('.cfg'):
269            configs = [self.bset]
270        else:
271            exbset = self.macros.expand(self.bset)
272            self.macros['_bset'] = exbset
273            root, ext = path.splitext(exbset)
274            if exbset.endswith('.bset'):
275                bset = exbset
276            else:
277                bset = '%s.bset' % (exbset)
278            configs = self.parse(bset)
279        return configs
280
281    def build(self, deps = None, nesting_count = 0):
282
283        nesting_count += 1
284
285        log.trace('_bset: %s: make' % (self.bset))
286        log.notice('Build Set: %s' % (self.bset))
287
288        if self.opts.get_arg('--mail'):
289            mail_report_subject = '%s %s' % (self.bset, self.macros.expand('%{_host}'))
290
291        configs = self.load()
292
293        log.trace('_bset: %s: configs: %s'  % (self.bset, ','.join(configs)))
294
295        current_path = os.environ['PATH']
296
297        start = datetime.datetime.now()
298
299        mail_report = False
300        have_errors = False
301
302        try:
303            builds = []
304            for s in range(0, len(configs)):
305                b = None
306                try:
307                    #
308                    # Each section of the build set gets a separate set of
309                    # macros so we do not contaminate one configuration with
310                    # another.
311                    #
312                    opts = copy.copy(self.opts)
313                    macros = copy.copy(self.macros)
314                    if configs[s].endswith('.bset'):
315                        log.trace('_bset: == %2d %s' % (nesting_count + 1, '=' * 75))
316                        bs = buildset(configs[s], self.configs, opts, macros)
317                        bs.build(deps, nesting_count)
318                        del bs
319                    elif configs[s].endswith('.cfg'):
320                        mail_report = self.opts.get_arg('--mail')
321                        log.trace('_bset: -- %2d %s' % (nesting_count + 1, '-' * 75))
322                        b = build.build(configs[s], self.opts.get_arg('--pkg-tar-files'),
323                                        opts, macros)
324                        if b.macros.get('%{_disable_reporting}'):
325                            mail_report = False
326                        if deps is None:
327                            self.build_package(configs[s], b)
328                            if s == len(configs) - 1 and not have_errors:
329                                self.bset_tar(b)
330                        else:
331                            deps += b.config.includes()
332                        builds += [b]
333                    else:
334                        raise error.general('invalid config type: %s' % (configs[s]))
335                except error.general, gerr:
336                    have_errors = True
337                    if b is not None:
338                        if self.build_failure is None:
339                            self.build_failure = b.name()
340                        self.write_mail_header('')
341                        self.write_mail_header('= ' * 40)
342                        self.write_mail_header('Build FAILED: %s' % (b.name()))
343                        self.write_mail_header('- ' * 40)
344                        self.write_mail_header(str(log.default))
345                        self.write_mail_header('- ' * 40)
346                        if self.opts.keep_going():
347                            log.notice(str(gerr))
348                            if self.opts.always_clean():
349                                builds += [b]
350                        else:
351                            raise
352                    else:
353                        raise
354            if deps is None and not self.opts.no_install():
355                for b in builds:
356                    if not b.disabled() \
357                       and not b.macros.get('%{_disable_installing}'):
358                        self.install(b.name(),
359                                     b.config.expand('%{buildroot}'),
360                                     b.config.expand('%{_prefix}'))
361            if deps is None and \
362                    (not self.opts.no_clean() or self.opts.always_clean()):
363                for b in builds:
364                    if not b.disabled():
365                        log.notice('cleaning: %s' % (b.name()))
366                        b.cleanup()
367            for b in builds:
368                del b
369        except error.general, gerr:
370            raise
371        except KeyboardInterrupt:
372            mail_report = False
373            raise
374        except:
375            self.build_failure = 'RSB general failure'
376            raise
377        finally:
378            end = datetime.datetime.now()
379            os.environ['PATH'] = current_path
380            build_time = str(end - start)
381            if mail_report:
382                to_addr = self.opts.get_arg('--mail-to')
383                if to_addr is not None:
384                    to_addr = to_addr[1]
385                else:
386                    to_addr = self.macros.expand('%{_mail_tools_to}')
387                log.notice('Mailing report: %s' % (to_addr))
388                self.write_mail_header('Build Time %s' % (build_time), True)
389                self.write_mail_header('')
390                m = mailer.mail(self.opts)
391                if self.build_failure is not None:
392                    mail_report_subject = 'Build: FAILED %s (%s)' %\
393                        (mail_report_subject, self.build_failure)
394                    pass_fail = 'FAILED'
395                else:
396                    mail_report_subject = 'Build: PASSED %s' % (mail_report_subject)
397                if not self.opts.dry_run():
398                    m.send(to_addr, mail_report_subject,
399                           self.mail_header + self.mail_report)
400            log.notice('Build Set: Time %s' % (build_time))
401
402def list_bset_cfg_files(opts, configs):
403    if opts.get_arg('--list-configs') or opts.get_arg('--list-bsets'):
404        if opts.get_arg('--list-configs'):
405            ext = '.cfg'
406        else:
407            ext = '.bset'
408        for p in configs['paths']:
409            print 'Examining: %s' % (os.path.relpath(p))
410        for c in configs['files']:
411            if c.endswith(ext):
412                print '    %s' % (c)
413        return True
414    return False
415
416def run():
417    import sys
418    try:
419        optargs = { '--list-configs':  'List available configurations',
420                    '--list-bsets':    'List available build sets',
421                    '--list-deps':     'List the dependent files.',
422                    '--bset-tar-file': 'Create a build set tar file',
423                    '--pkg-tar-files': 'Create package tar files',
424                    '--no-report':     'Do not create a package report.',
425                    '--report-format': 'The report format (text, html, asciidoc).' }
426        mailer.append_options(optargs)
427        opts = options.load(sys.argv, optargs)
428        log.notice('RTEMS Source Builder - Set Builder, v%s' % (version.str()))
429        opts.log_info()
430        if not check.host_setup(opts):
431            raise error.general('host build environment is not set up correctly')
432        configs = build.get_configs(opts)
433        if opts.get_arg('--list-deps'):
434            deps = []
435        else:
436            deps = None
437        if not list_bset_cfg_files(opts, configs):
438            prefix = opts.defaults.expand('%{_prefix}')
439            if not opts.dry_run() and not opts.no_install() and \
440                    not path.ispathwritable(prefix):
441                raise error.general('prefix is not writable: %s' % (path.host(prefix)))
442            for bset in opts.params():
443                b = buildset(bset, configs, opts)
444                b.build(deps)
445                del b
446        if deps is not None:
447            c = 0
448            for d in sorted(set(deps)):
449                c += 1
450                print 'dep[%d]: %s' % (c, d)
451    except error.general, gerr:
452        log.notice(str(gerr))
453        print >> sys.stderr, 'Build FAILED'
454        sys.exit(1)
455    except error.internal, ierr:
456        log.notice(str(ierr))
457        sys.exit(1)
458    except error.exit, eerr:
459        pass
460    except KeyboardInterrupt:
461        log.notice('abort: user terminated')
462        sys.exit(1)
463    sys.exit(0)
464
465if __name__ == "__main__":
466    run()
Note: See TracBrowser for help on using the repository browser.