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