source: rtems-source-builder/source-builder/sb/check.py @ 158ad68

4.11
Last change on this file since 158ad68 was 158ad68, checked in by Chris Johns <chrisj@…>, on 10/03/20 at 11:53:04

sb: Back port the RTEMS 5 and 6 RSB engine.

  • Build GDb first as we do for RTEMS 5 and later
  • Update GDB to 9.1 for all archs expect SPARC. The SIS patches only apply to 7.9. Disable Python for SPARC

Closes #4111

  • Property mode set to 100644
File size: 9.2 KB
Line 
1#
2# RTEMS Tools Project (http://www.rtems.org/)
3# Copyright 2010-2016 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# Check the defaults for a specific host.
22#
23
24from __future__ import print_function
25
26import fnmatch
27import os
28import re
29
30from . import error
31from . import execute
32from . import log
33from . import options
34from . import path
35from . import version
36
37def _check_none(_opts, macro, value, constraint):
38    return True
39
40
41def _check_triplet(_opts, macro, value, constraint):
42    return True
43
44
45def _check_dir(_opts, macro, value, constraint, silent = False):
46    if constraint != 'none' and not path.isdir(value):
47        if constraint == 'required':
48            if not silent:
49                log.notice('error: dir: not found: (%s) %s' % (macro, value))
50            return False
51        if not silent and _opts.warn_all():
52            log.notice('warning: dir: not found: (%s) %s' % (macro, value))
53    return True
54
55
56def _check_exe(_opts, macro, value, constraint, silent = False):
57
58    if len(value) == 0 or constraint == 'none':
59        return True
60
61    orig_value = value
62
63    if path.isabspath(value):
64        if path.isfile(value):
65            return True
66        if os.name == 'nt':
67            if path.isfile('%s.exe' % (value)):
68                return True
69        value = path.basename(value)
70        absexe = True
71    else:
72        absexe = False
73
74    paths = os.environ['PATH'].split(os.pathsep)
75
76    if _check_paths(value, paths):
77        if absexe:
78            if not silent:
79                log.notice('warning: exe: absolute exe found in path: (%s) %s' % (macro, orig_value))
80        return True
81
82    if constraint == 'optional':
83        if not silent:
84            log.trace('warning: exe: optional exe not found: (%s) %s' % (macro, orig_value))
85        return True
86
87    if not silent:
88        log.notice('error: exe: not found: (%s) %s' % (macro, orig_value))
89    return False
90
91
92def _check_paths(name, paths):
93    for p in paths:
94        exe = path.join(p, name)
95        if path.isfile(exe):
96            return True
97        if os.name == 'nt':
98            if path.isfile('%s.exe' % (exe)):
99                return True
100    return False
101
102
103def path_check(opts, silent = False):
104    if 'PATH' in os.environ:
105        paths = os.environ['PATH'].split(os.pathsep)
106        for p in paths:
107            try:
108                if len(p.strip()) == 0:
109                    if not silent:
110                        log.notice('error: environment PATH contains an empty path')
111                    return False
112                elif not options.host_windows and (p.strip() == '.' or p.strip() == '..'):
113                    if not silent:
114                        log.notice('error: environment PATH invalid path: %s' % (p))
115                    return False
116                elif not path.exists(p):
117                    if not silent and opts.warn_all():
118                        log.notice('warning: environment PATH not found: %s' % (p))
119                elif not path.isdir(p):
120                    if not silent and opts.warn_all():
121                        log.notice('warning: environment PATH not a directory: %s' % (p))
122            except Exception as e:
123                if not silent:
124                    log.notice('warning: environment PATH suspicious path: %s' % (e))
125    return True
126
127
128def host_setup(opts):
129    """ Basic sanity check. All executables and directories must exist."""
130
131    if not path_check(opts):
132        return False
133
134    checks = { 'none':    _check_none,
135               'triplet': _check_triplet,
136               'dir':     _check_dir,
137               'exe':     _check_exe }
138
139    sane = True
140
141    log.trace('--- check host set up : start"')
142    for d in list(opts.defaults.keys()):
143        try:
144            (test, constraint, value) = opts.defaults.get(d)
145        except:
146            if opts.defaults.get(d) is None:
147                raise error.general('invalid default: %s: not found' % (d))
148            else:
149                raise error.general('invalid default: %s [%r]' % (d, opts.defaults.get(d)))
150        if test != 'none':
151            value = opts.defaults.expand(value)
152            if test not in checks:
153                raise error.general('invalid check test: %s [%r]' % (test, opts.defaults.get(d)))
154            ok = checks[test](opts, d, value, constraint)
155            if ok:
156                tag = ' '
157            else:
158                tag = '*'
159            log.trace('%c %15s: %r -> "%s"' % (tag, d, opts.defaults.get(d), value))
160            if sane and not ok:
161                sane = False
162    log.trace('--- check host set up : end"')
163
164    return sane
165
166
167def check_exe(label, exe):
168    return _check_exe(None, label, exe, None, True)
169
170
171def check_orphans(opts):
172
173    def _find_files(path, globs, excludes = []):
174        ff = []
175        for root, dirs, files in os.walk(path, followlinks = True):
176            for f in files:
177                for g in globs:
178                    if fnmatch.fnmatch(f, g) and f not in excludes:
179                        ff += [os.path.join(root, f)]
180        return sorted(ff)
181
182    def _clean(line):
183        line = line[0:-1]
184        b = line.find('#')
185        if b >= 0:
186            line = line[1:b]
187        return line.strip()
188
189    def _find(name, opts):
190        ename = opts.defaults.expand(name)
191        if ':' in ename:
192            paths = path.dirname(ename).split(':')
193            name = path.basename(name)
194        else:
195            paths = opts.defaults.get_value('_configdir').split(':')
196        for p in paths:
197            n = path.join(opts.defaults.expand(p), name)
198            if path.exists(n):
199                return n
200        return None
201
202    paths = opts.defaults.get_value('_configdir').split(':')
203
204    cfgs = {}
205    for p in paths:
206        ep = opts.defaults.expand(p)
207        print('Scanning: %s (%s)' % (p, ep))
208        for f in _find_files(ep, ['*.cfg', '*.bset']):
209            root, ext = path.splitext(f)
210            cfgs[f] = { 'src': None, 'ext': ext, 'refs': 0, 'errors':[] }
211
212    wss = re.compile(r'\s+')
213
214    for c in cfgs:
215        with open(c, 'r') as f:
216            cfgs[c]['src'] = f.readlines()
217        lc = 0
218        for l in cfgs[c]['src']:
219            lc += 1
220            l = _clean(l)
221            if len(l) == 0:
222                continue
223            if l[0] == '%':
224                ls = wss.split(l, 2)
225                if ls[0] == '%include':
226                    name = _find(ls[1], opts)
227                    if name is None:
228                        cfgs[c]['errors'] += [lc]
229                    elif name not in cfgs:
230                        raise error.general('include: %s: not present' % (ls[1]))
231                    else:
232                        cfgs[name]['refs'] += 1
233            elif cfgs[c]['ext'] == '.bset' and ':' not in l:
234                for ext in ['', '.cfg', '.bset']:
235                    name = _find(l + ext, opts)
236                    if name is not None:
237                        if name not in cfgs:
238                            raise error.general('include: %s: not present' % (ls[1]))
239                        else:
240                            cfgs[name]['refs'] += 1
241                        break
242
243    topdir = opts.defaults.expand('%{_topdir}')
244
245    orphans = []
246    show = True
247
248    for c in cfgs:
249        if cfgs[c]['refs'] == 0:
250            orphans += [c]
251        if len(cfgs[c]['errors']) != 0:
252            if show:
253                print('Warnings:')
254                show = False
255            print(' %s:' % (path.relpath(c)))
256            for l in cfgs[c]['errors']:
257                print('  %3d: %s' % (l, cfgs[c]['src'][l - 1][:-1]))
258
259    show = True
260
261    for o in sorted(orphans):
262        if show:
263            print('Orphans:')
264            show = False
265        print(' %s' % (path.relpath(o)))
266
267def run():
268    import sys
269    try:
270        _opts = options.load(args = sys.argv, logfile = False)
271        log.notice('RTEMS Source Builder - Check, %s' % (version.string()))
272
273        orphans = _opts.parse_args('--check-orphans', error = False, extra = False)
274        if orphans:
275            print('Checking for orphans...')
276            check_orphans(_opts)
277        else:
278            if host_setup(_opts):
279                print('Environment is ok')
280            else:
281                print('Environment is not correctly set up')
282    except error.general as gerr:
283        print(gerr)
284        sys.exit(1)
285    except error.internal as ierr:
286        print(ierr)
287        sys.exit(1)
288    except error.exit as eerr:
289        pass
290    except KeyboardInterrupt:
291        log.notice('abort: user terminated')
292        sys.exit(1)
293    sys.exit(0)
294
295
296if __name__ == '__main__':
297    run()
Note: See TracBrowser for help on using the repository browser.