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 | |
---|
24 | from __future__ import print_function |
---|
25 | |
---|
26 | import os |
---|
27 | |
---|
28 | import error |
---|
29 | import execute |
---|
30 | import log |
---|
31 | import options |
---|
32 | import path |
---|
33 | import version |
---|
34 | |
---|
35 | def _check_none(_opts, macro, value, constraint): |
---|
36 | return True |
---|
37 | |
---|
38 | |
---|
39 | def _check_triplet(_opts, macro, value, constraint): |
---|
40 | return True |
---|
41 | |
---|
42 | |
---|
43 | def _check_dir(_opts, macro, value, constraint, silent = False): |
---|
44 | if constraint != 'none' and not path.isdir(value): |
---|
45 | if constraint == 'required': |
---|
46 | if not silent: |
---|
47 | log.notice('error: dir: not found: (%s) %s' % (macro, value)) |
---|
48 | return False |
---|
49 | if not silent and _opts.warn_all(): |
---|
50 | log.notice('warning: dir: not found: (%s) %s' % (macro, value)) |
---|
51 | return True |
---|
52 | |
---|
53 | |
---|
54 | def _check_exe(_opts, macro, value, constraint, silent = False): |
---|
55 | |
---|
56 | if len(value) == 0 or constraint == 'none': |
---|
57 | return True |
---|
58 | |
---|
59 | orig_value = value |
---|
60 | |
---|
61 | if path.isabspath(value): |
---|
62 | if path.isfile(value): |
---|
63 | return True |
---|
64 | if os.name == 'nt': |
---|
65 | if path.isfile('%s.exe' % (value)): |
---|
66 | return True |
---|
67 | value = path.basename(value) |
---|
68 | absexe = True |
---|
69 | else: |
---|
70 | absexe = False |
---|
71 | |
---|
72 | paths = os.environ['PATH'].split(os.pathsep) |
---|
73 | |
---|
74 | if _check_paths(value, paths): |
---|
75 | if absexe: |
---|
76 | if not silent: |
---|
77 | log.notice('warning: exe: absolute exe found in path: (%s) %s' % (macro, orig_value)) |
---|
78 | return True |
---|
79 | |
---|
80 | if constraint == 'optional': |
---|
81 | if not silent: |
---|
82 | log.trace('warning: exe: optional exe not found: (%s) %s' % (macro, orig_value)) |
---|
83 | return True |
---|
84 | |
---|
85 | if not silent: |
---|
86 | log.notice('error: exe: not found: (%s) %s' % (macro, orig_value)) |
---|
87 | return False |
---|
88 | |
---|
89 | |
---|
90 | def _check_paths(name, paths): |
---|
91 | for p in paths: |
---|
92 | exe = path.join(p, name) |
---|
93 | if path.isfile(exe): |
---|
94 | return True |
---|
95 | if os.name == 'nt': |
---|
96 | if path.isfile('%s.exe' % (exe)): |
---|
97 | return True |
---|
98 | return False |
---|
99 | |
---|
100 | |
---|
101 | def path_check(opts, silent = False): |
---|
102 | if 'PATH' in os.environ: |
---|
103 | paths = os.environ['PATH'].split(os.pathsep) |
---|
104 | for p in paths: |
---|
105 | if len(p.strip()) == 0: |
---|
106 | if not silent: |
---|
107 | log.notice('error: environment PATH contains an empty path') |
---|
108 | return False |
---|
109 | elif not options.host_windows and (p.strip() == '.' or p.strip() == '..'): |
---|
110 | if not silent: |
---|
111 | log.notice('error: environment PATH invalid path: %s' % (p)) |
---|
112 | return False |
---|
113 | elif not path.exists(p): |
---|
114 | if not silent and opts.warn_all(): |
---|
115 | log.notice('warning: environment PATH not found: %s' % (p)) |
---|
116 | elif not path.isdir(p): |
---|
117 | if not silent and opts.warn_all(): |
---|
118 | log.notice('warning: environment PATH not a directory: %s' % (p)) |
---|
119 | return True |
---|
120 | |
---|
121 | |
---|
122 | def host_setup(opts): |
---|
123 | """ Basic sanity check. All executables and directories must exist.""" |
---|
124 | |
---|
125 | if not path_check(opts): |
---|
126 | return False |
---|
127 | |
---|
128 | checks = { 'none': _check_none, |
---|
129 | 'triplet': _check_triplet, |
---|
130 | 'dir': _check_dir, |
---|
131 | 'exe': _check_exe } |
---|
132 | |
---|
133 | sane = True |
---|
134 | |
---|
135 | for d in list(opts.defaults.keys()): |
---|
136 | try: |
---|
137 | (test, constraint, value) = opts.defaults.get(d) |
---|
138 | except: |
---|
139 | if opts.defaults.get(d) is None: |
---|
140 | raise error.general('invalid default: %s: not found' % (d)) |
---|
141 | else: |
---|
142 | raise error.general('invalid default: %s [%r]' % (d, opts.defaults.get(d))) |
---|
143 | if test != 'none': |
---|
144 | value = opts.defaults.expand(value) |
---|
145 | if test not in checks: |
---|
146 | raise error.general('invalid check test: %s [%r]' % (test, opts.defaults.get(d))) |
---|
147 | ok = checks[test](opts, d, value, constraint) |
---|
148 | if ok: |
---|
149 | tag = ' ' |
---|
150 | else: |
---|
151 | tag = '*' |
---|
152 | log.trace('%c %15s: %r -> "%s"' % (tag, d, opts.defaults.get(d), value)) |
---|
153 | if sane and not ok: |
---|
154 | sane = False |
---|
155 | |
---|
156 | return sane |
---|
157 | |
---|
158 | |
---|
159 | def check_exe(label, exe): |
---|
160 | return _check_exe(None, label, exe, None, True) |
---|
161 | |
---|
162 | |
---|
163 | def run(): |
---|
164 | import sys |
---|
165 | try: |
---|
166 | _opts = options.load(args = sys.argv) |
---|
167 | log.notice('RTEMS Source Builder - Check, %s' % (version.str())) |
---|
168 | if host_setup(_opts): |
---|
169 | print('Environment is ok') |
---|
170 | else: |
---|
171 | print('Environment is not correctly set up') |
---|
172 | except error.general as gerr: |
---|
173 | print(gerr) |
---|
174 | sys.exit(1) |
---|
175 | except error.internal as ierr: |
---|
176 | print(ierr) |
---|
177 | sys.exit(1) |
---|
178 | except error.exit as eerr: |
---|
179 | pass |
---|
180 | except KeyboardInterrupt: |
---|
181 | log.notice('abort: user terminated') |
---|
182 | sys.exit(1) |
---|
183 | sys.exit(0) |
---|
184 | |
---|
185 | |
---|
186 | if __name__ == '__main__': |
---|
187 | run() |
---|