source: rtems-source-builder/source-builder/sb/setbuilder.py @ 8d7624e

4.104.114.95
Last change on this file since 8d7624e was 8d7624e, checked in by Chris Johns <chrisj@…>, on 02/17/13 at 00:50:02

Clean _tmproot. Move config listing to the setbuilder module.

Add the mising _tmprool clean.

Move the list printing out of the build which now just returns a
list of config files to the location of the options.

  • Property mode set to 100644
File size: 9.2 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 distutils.dir_util
26import glob
27import operator
28import os
29
30import build
31import check
32import defaults
33import error
34import log
35import path
36
37#
38# Version of Source Builder Set Builder.
39#
40version = '0.1'
41
42def _trace(opts, text):
43    if opts.trace():
44        print text
45
46def _notice(opts, text):
47    if not opts.quiet() and not log.default.has_stdout():
48        print text
49    log.output(text)
50    log.flush()
51
52class buildset:
53    """Build a set builds a set of packages."""
54
55    def __init__(self, bset, _defaults, opts):
56        _trace(opts, '_bset:%s: init' % (bset))
57        self.opts = opts
58        self.defaults = _defaults
59        self.bset = bset
60        self.bset_pkg = '%s-%s-set' % (self.opts.expand('%{_target}', _defaults),
61                                       self.bset)
62
63    def _output(self, text):
64        if not self.opts.quiet():
65            log.output(text)
66
67    def copy(self, src, dst):
68        if os.path.isdir(path.host(src)):
69            topdir = self.opts.expand('%{_topdir}', self.defaults)
70            what = '%s -> %s' % \
71                (path.host(src[len(topdir) + 1:]), path.host(dst[len(topdir) + 1:]))
72            if self.opts.trace():
73                _notice(self.opts, 'installing: %s' % (what))
74            if not self.opts.dry_run():
75                try:
76                    files = distutils.dir_util.copy_tree(path.host(src),
77                                                         path.host(dst))
78                    for f in files:
79                        self._output(f)
80                except IOError, err:
81                    raise error.general('installing tree: %s: %s' % (what, str(err)))
82                except distutils.errors.DistutilsFileError, err:
83                    raise error.general('installing tree: %s' % (str(err)))
84
85    def first_package(self, _build):
86        tmproot = path.abspath(_build.config.expand('%{_tmproot}'))
87        _build.rmdir(tmproot)
88        _build.mkdir(tmproot)
89        prefix = _build.config.expand('%{_prefix}')
90        if prefix[0] == os.sep:
91            prefix = prefix[1:]
92        tmpprefix = path.join(tmproot, prefix)
93        tmpbindir = path.join(tmpprefix, 'bin')
94        # exporting to the environment
95        os.environ['SB_TMPPREFIX'] = tmpprefix
96        os.environ['SB_TMPBINDIR'] = tmpbindir
97        os.environ['PATH'] = path.host(tmpbindir) + os.pathsep + os.environ['PATH']
98        self._output('path: ' + os.environ['PATH'])
99        # shell format
100        return tmproot
101
102    def every_package(self, _build, tmproot):
103        self.copy(_build.config.abspath('%{buildroot}'), tmproot)
104
105    def last_package(self, _build, tmproot):
106        tar = path.join(_build.config.expand('%{_tardir}'),
107                        _build.config.expand('%s.tar.bz2' % (self.bset_pkg)))
108        _notice(self.opts, 'tarball: %s' % path.host(tar))
109        if not self.opts.dry_run():
110            cmd = _build.config.expand("'cd " + tmproot + \
111                                           " && %{__tar} -cf - . | %{__bzip2} > " + tar + "'")
112            _build.run(cmd, shell_opts = '-c', cwd = tmproot)
113
114    def parse(self, bset):
115
116        def _clean(line):
117            line = line[0:-1]
118            b = line.find('#')
119            if b >= 0:
120                line = line[1:b]
121            return line.strip()
122
123        bsetname = bset
124
125        if not path.exists(bsetname):
126            for cp in self.opts.expand('%{_configdir}', self.defaults).split(':'):
127                configdir = path.abspath(cp)
128                bsetname = path.join(configdir, bset)
129                if path.exists(bsetname):
130                    break
131                bsetname = None
132            if bsetname is None:
133                raise error.general('no build set file found: %s' % (bset))
134        try:
135            if self.opts.trace():
136                print '_bset:%s: open: %s' % (self.bset, bsetname)
137            bset = open(path.host(bsetname), 'r')
138        except IOError, err:
139            raise error.general('error opening bset file: %s' % (bsetname))
140
141        configs = []
142
143        try:
144            lc = 0
145            for l in bset:
146                lc += 1
147                l = _clean(l)
148                if len(l) == 0:
149                    continue
150                if self.opts.trace():
151                    print '%03d: %s' % (lc, l)
152                if ':' in l:
153                    ls = l.split(':')
154                    if ls[0].strip() == 'package':
155                        self.bset_pkg = self.opts.expand(ls[1].strip(), self.defaults)
156                        self.defaults['package'] = ('none', 'none', self.bset_pkg)
157                elif l[0] == '%':
158                    if l.startswith('%define'):
159                        ls = l.split()
160                        if len(ls) > 2:
161                            self.defaults[ls[1].strip()] = ('none', 'none', ls[2].strip())
162                        else:
163                            self.defaults[ls[1].strip()] = ('none', 'none', '1')
164                    elif l.startswith('%include'):
165                        ls = l.split(' ')
166                        configs += self.parse(ls[1].strip())
167                    else:
168                        raise error.general('invalid directive in build set files: %s' % (l))
169                else:
170                    configs += [l.strip()]
171        except:
172            bset.close()
173            raise
174
175        bset.close()
176
177        return configs
178
179    def load(self):
180
181        exbset = self.opts.expand(self.bset, self.defaults)
182
183        self.defaults['_bset'] = ('none', 'none', exbset)
184
185        root, ext = path.splitext(exbset)
186
187        if exbset.endswith('.bset'):
188            bset = exbset
189        else:
190            bset = '%s.bset' % (exbset)
191
192        return self.parse(bset)
193
194    def make(self):
195
196        _trace(self.opts, '_bset:%s: make' % (self.bset))
197        _notice(self.opts, 'Build Set: %s' % (self.bset))
198
199        configs = self.load()
200
201        _trace(self.opts, '_bset:%s: configs: %s'  % (self.bset, ','.join(configs)))
202
203        current_path = os.environ['PATH']
204        try:
205            builds = []
206            for s in range(0, len(configs)):
207                b = build.build(configs[s], _defaults = self.defaults, opts = self.opts)
208                if s == 0:
209                    tmproot = self.first_package(b)
210                b.make()
211                self.every_package(b, tmproot)
212                if s == len(configs) - 1:
213                    self.last_package(b, tmproot)
214                builds += [b]
215            if not self.opts.no_clean():
216                for b in builds:
217                    _notice(self.opts, 'cleaning: %s' % (b.name()))
218                    b.cleanup()
219            for b in builds:
220                del b
221        except:
222            os.environ['PATH'] = current_path
223            raise
224        os.environ['PATH'] = current_path
225
226def run():
227    import sys
228    try:
229        optargs = { '--list-configs': 'List available configurations',
230                    '--list-bsets': 'List available build sets'}
231        opts, _defaults = defaults.load(sys.argv, optargs)
232        log.default = log.log(opts.logfiles())
233        _notice(opts, 'Source Builder - Set Builder, v%s' % (version))
234        if not check.host_setup(opts, _defaults):
235            if not opts.force():
236                raise error.general('host build environment is not set up correctly (use --force to proceed)')
237            _notice(opts, 'warning: forcing build with known host setup problems')
238        if opts.get_arg('--list-configs') or opts.get_arg('--list-bsets'):
239            if opts.get_arg('--list-configs'):
240                ext = '.cfg'
241            else:
242                ext = '.bset'
243            paths, configs = build.get_configs(opts, _defaults, ext = ext)
244            for p in paths:
245                print 'Examining: %s' % (os.path.relpath(p))
246            for c in configs:
247                print '    %s' % (c)
248        else:
249            for bset in opts.params():
250                c = buildset(bset, _defaults = _defaults, opts = opts)
251                c.make()
252                del c
253    except error.general, gerr:
254        print gerr
255        sys.exit(1)
256    except error.internal, ierr:
257        print ierr
258        sys.exit(1)
259    except error.exit, eerr:
260        pass
261    except KeyboardInterrupt:
262        _notice(opts, 'user terminated')
263        sys.exit(1)
264    sys.exit(0)
265
266if __name__ == "__main__":
267    run()
Note: See TracBrowser for help on using the repository browser.