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

4.104.114.95
Last change on this file since ce0f7a1 was ce0f7a1, checked in by Chris Johns <chrisj@…>, on 11/09/13 at 22:55:37

Add the host to the build set tar file name if no target.

If building a host package use the host as the package tar file
name prefix. This means the tar files will all start with the host
name.

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