1 | #!/usr/bin/env python
|
---|
2 | #
|
---|
3 | # Copyright (c) 2006 Ondrej Palkovsky
|
---|
4 | # Copyright (c) 2009 Martin Decky
|
---|
5 | # Copyright (c) 2010 Jiri Svoboda
|
---|
6 | # All rights reserved.
|
---|
7 | #
|
---|
8 | # Redistribution and use in source and binary forms, with or without
|
---|
9 | # modification, are permitted provided that the following conditions
|
---|
10 | # are met:
|
---|
11 | #
|
---|
12 | # - Redistributions of source code must retain the above copyright
|
---|
13 | # notice, this list of conditions and the following disclaimer.
|
---|
14 | # - Redistributions in binary form must reproduce the above copyright
|
---|
15 | # notice, this list of conditions and the following disclaimer in the
|
---|
16 | # documentation and/or other materials provided with the distribution.
|
---|
17 | # - The name of the author may not be used to endorse or promote products
|
---|
18 | # derived from this software without specific prior written permission.
|
---|
19 | #
|
---|
20 | # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
21 | # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
22 | # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
23 | # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
24 | # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
25 | # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
26 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
27 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
28 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
29 | # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
30 | #
|
---|
31 |
|
---|
32 | """
|
---|
33 | HelenOS configuration system
|
---|
34 | """
|
---|
35 |
|
---|
36 | import sys
|
---|
37 | import os
|
---|
38 | import re
|
---|
39 | import time
|
---|
40 | import subprocess
|
---|
41 | import xtui
|
---|
42 | import random
|
---|
43 |
|
---|
44 | RULES_FILE = sys.argv[1]
|
---|
45 | MAKEFILE = 'Makefile.config'
|
---|
46 | MACROS = 'config.h'
|
---|
47 | PRESETS_DIR = 'defaults'
|
---|
48 |
|
---|
49 | class BinaryOp:
|
---|
50 | def __init__(self, operator, left, right):
|
---|
51 | assert operator in ('&', '|', '=', '!=')
|
---|
52 |
|
---|
53 | self._operator = operator
|
---|
54 | self._left = left
|
---|
55 | self._right = right
|
---|
56 |
|
---|
57 | def evaluate(self, config):
|
---|
58 | if self._operator == '&':
|
---|
59 | return self._left.evaluate(config) and \
|
---|
60 | self._right.evaluate(config)
|
---|
61 | if self._operator == '|':
|
---|
62 | return self._left.evaluate(config) or \
|
---|
63 | self._right.evaluate(config)
|
---|
64 |
|
---|
65 | # '=' or '!='
|
---|
66 | if not self._left in config:
|
---|
67 | config_val = ''
|
---|
68 | else:
|
---|
69 | config_val = config[self._left]
|
---|
70 | if config_val == '*':
|
---|
71 | config_val = 'y'
|
---|
72 |
|
---|
73 | if self._operator == '=':
|
---|
74 | return self._right == config_val
|
---|
75 | return self._right != config_val
|
---|
76 |
|
---|
77 | # Expression parser
|
---|
78 | class CondParser:
|
---|
79 | TOKEN_EOE = 0
|
---|
80 | TOKEN_SPECIAL = 1
|
---|
81 | TOKEN_STRING = 2
|
---|
82 |
|
---|
83 | def __init__(self, text):
|
---|
84 | self._text = text
|
---|
85 |
|
---|
86 | def parse(self):
|
---|
87 | self._position = -1
|
---|
88 | self._next_char()
|
---|
89 | self._next_token()
|
---|
90 |
|
---|
91 | res = self._parse_expr()
|
---|
92 | if self._token_type != self.TOKEN_EOE:
|
---|
93 | self._error("Expected end of expression")
|
---|
94 | return res
|
---|
95 |
|
---|
96 | def _next_char(self):
|
---|
97 | self._position += 1
|
---|
98 | if self._position >= len(self._text):
|
---|
99 | self._char = None
|
---|
100 | else:
|
---|
101 | self._char = self._text[self._position]
|
---|
102 | self._is_special_char = self._char in \
|
---|
103 | ('&', '|', '=', '!', '(', ')')
|
---|
104 |
|
---|
105 | def _error(self, msg):
|
---|
106 | raise RuntimeError("Error parsing expression: %s:\n%s\n%s^" %
|
---|
107 | (msg, self._text, " " * self._token_position))
|
---|
108 |
|
---|
109 | def _next_token(self):
|
---|
110 | self._token_position = self._position
|
---|
111 |
|
---|
112 | # End of expression
|
---|
113 | if self._char == None:
|
---|
114 | self._token = None
|
---|
115 | self._token_type = self.TOKEN_EOE
|
---|
116 | return
|
---|
117 |
|
---|
118 | # '&', '|', '=', '!=', '(', ')'
|
---|
119 | if self._is_special_char:
|
---|
120 | self._token = self._char
|
---|
121 | self._next_char()
|
---|
122 | if self._token == '!':
|
---|
123 | if self._char != '=':
|
---|
124 | self._error("Expected '='")
|
---|
125 | self._token += self._char
|
---|
126 | self._next_char()
|
---|
127 | self._token_type = self.TOKEN_SPECIAL
|
---|
128 | return
|
---|
129 |
|
---|
130 | # <var> or <val>
|
---|
131 | self._token = ''
|
---|
132 | self._token_type = self.TOKEN_STRING
|
---|
133 | while True:
|
---|
134 | self._token += self._char
|
---|
135 | self._next_char()
|
---|
136 | if self._is_special_char or self._char == None:
|
---|
137 | break
|
---|
138 |
|
---|
139 | def _parse_expr(self):
|
---|
140 | """ <expr> ::= <or_expr> ('&' <or_expr>)* """
|
---|
141 |
|
---|
142 | left = self._parse_or_expr()
|
---|
143 | while self._token == '&':
|
---|
144 | self._next_token()
|
---|
145 | left = BinaryOp('&', left, self._parse_or_expr())
|
---|
146 | return left
|
---|
147 |
|
---|
148 | def _parse_or_expr(self):
|
---|
149 | """ <or_expr> ::= <factor> ('|' <factor>)* """
|
---|
150 |
|
---|
151 | left = self._parse_factor()
|
---|
152 | while self._token == '|':
|
---|
153 | self._next_token()
|
---|
154 | left = BinaryOp('|', left, self._parse_factor())
|
---|
155 | return left
|
---|
156 |
|
---|
157 | def _parse_factor(self):
|
---|
158 | """ <factor> ::= <var> <cond> | '(' <expr> ')' """
|
---|
159 |
|
---|
160 | if self._token == '(':
|
---|
161 | self._next_token()
|
---|
162 | res = self._parse_expr()
|
---|
163 | if self._token != ')':
|
---|
164 | self._error("Expected ')'")
|
---|
165 | self._next_token()
|
---|
166 | return res
|
---|
167 |
|
---|
168 | if self._token_type == self.TOKEN_STRING:
|
---|
169 | var = self._token
|
---|
170 | self._next_token()
|
---|
171 | return self._parse_cond(var)
|
---|
172 |
|
---|
173 | self._error("Expected '(' or <var>")
|
---|
174 |
|
---|
175 | def _parse_cond(self, var):
|
---|
176 | """ <cond> ::= '=' <val> | '!=' <val> """
|
---|
177 |
|
---|
178 | if self._token not in ('=', '!='):
|
---|
179 | self._error("Expected '=' or '!='")
|
---|
180 |
|
---|
181 | oper = self._token
|
---|
182 | self._next_token()
|
---|
183 |
|
---|
184 | if self._token_type != self.TOKEN_STRING:
|
---|
185 | self._error("Expected <val>")
|
---|
186 |
|
---|
187 | val = self._token
|
---|
188 | self._next_token()
|
---|
189 |
|
---|
190 | return BinaryOp(oper, var, val)
|
---|
191 |
|
---|
192 | def read_config(fname, config):
|
---|
193 | "Read saved values from last configuration run or a preset file"
|
---|
194 |
|
---|
195 | inf = open(fname, 'r')
|
---|
196 |
|
---|
197 | for line in inf:
|
---|
198 | res = re.match(r'^(?:#!# )?([^#]\w*)\s*=\s*(.*?)\s*$', line)
|
---|
199 | if res:
|
---|
200 | config[res.group(1)] = res.group(2)
|
---|
201 |
|
---|
202 | inf.close()
|
---|
203 |
|
---|
204 | def parse_rules(fname, rules):
|
---|
205 | "Parse rules file"
|
---|
206 |
|
---|
207 | inf = open(fname, 'r')
|
---|
208 |
|
---|
209 | name = ''
|
---|
210 | choices = []
|
---|
211 |
|
---|
212 | for line in inf:
|
---|
213 |
|
---|
214 | if line.startswith('!'):
|
---|
215 | # Ask a question
|
---|
216 | res = re.search(r'!\s*(?:\[(.*?)\])?\s*([^\s]+)\s*\((.*)\)\s*$', line)
|
---|
217 |
|
---|
218 | if not res:
|
---|
219 | raise RuntimeError("Weird line: %s" % line)
|
---|
220 |
|
---|
221 | cond = res.group(1)
|
---|
222 | if cond:
|
---|
223 | cond = CondParser(cond).parse()
|
---|
224 | varname = res.group(2)
|
---|
225 | vartype = res.group(3)
|
---|
226 |
|
---|
227 | rules.append((varname, vartype, name, choices, cond))
|
---|
228 | name = ''
|
---|
229 | choices = []
|
---|
230 | continue
|
---|
231 |
|
---|
232 | if line.startswith('@'):
|
---|
233 | # Add new line into the 'choices' array
|
---|
234 | res = re.match(r'@\s*(?:\[(.*?)\])?\s*"(.*?)"\s*(.*)$', line)
|
---|
235 |
|
---|
236 | if not res:
|
---|
237 | raise RuntimeError("Bad line: %s" % line)
|
---|
238 |
|
---|
239 | choices.append((res.group(2), res.group(3)))
|
---|
240 | continue
|
---|
241 |
|
---|
242 | if line.startswith('%'):
|
---|
243 | # Name of the option
|
---|
244 | name = line[1:].strip()
|
---|
245 | continue
|
---|
246 |
|
---|
247 | if line.startswith('#') or (line == '\n'):
|
---|
248 | # Comment or empty line
|
---|
249 | continue
|
---|
250 |
|
---|
251 |
|
---|
252 | raise RuntimeError("Unknown syntax: %s" % line)
|
---|
253 |
|
---|
254 | inf.close()
|
---|
255 |
|
---|
256 | def yes_no(default):
|
---|
257 | "Return '*' if yes, ' ' if no"
|
---|
258 |
|
---|
259 | if default == 'y':
|
---|
260 | return '*'
|
---|
261 |
|
---|
262 | return ' '
|
---|
263 |
|
---|
264 | def subchoice(screen, name, choices, default):
|
---|
265 | "Return choice of choices"
|
---|
266 |
|
---|
267 | maxkey = 0
|
---|
268 | for key, val in choices:
|
---|
269 | length = len(key)
|
---|
270 | if (length > maxkey):
|
---|
271 | maxkey = length
|
---|
272 |
|
---|
273 | options = []
|
---|
274 | position = None
|
---|
275 | cnt = 0
|
---|
276 | for key, val in choices:
|
---|
277 | if (default) and (key == default):
|
---|
278 | position = cnt
|
---|
279 |
|
---|
280 | options.append(" %-*s %s " % (maxkey, key, val))
|
---|
281 | cnt += 1
|
---|
282 |
|
---|
283 | (button, value) = xtui.choice_window(screen, name, 'Choose value', options, position)
|
---|
284 |
|
---|
285 | if button == 'cancel':
|
---|
286 | return None
|
---|
287 |
|
---|
288 | return choices[value][0]
|
---|
289 |
|
---|
290 | ## Infer and verify configuration values.
|
---|
291 | #
|
---|
292 | # Augment @a config with values that can be inferred, purge invalid ones
|
---|
293 | # and verify that all variables have a value (previously specified or inferred).
|
---|
294 | #
|
---|
295 | # @param config Configuration to work on
|
---|
296 | # @param rules Rules
|
---|
297 | #
|
---|
298 | # @return True if configuration is complete and valid, False
|
---|
299 | # otherwise.
|
---|
300 | #
|
---|
301 | def infer_verify_choices(config, rules):
|
---|
302 | "Infer and verify configuration values."
|
---|
303 |
|
---|
304 | for rule in rules:
|
---|
305 | varname, vartype, name, choices, cond = rule
|
---|
306 |
|
---|
307 | if cond and not cond.evaluate(config):
|
---|
308 | continue
|
---|
309 |
|
---|
310 | if not varname in config:
|
---|
311 | value = None
|
---|
312 | else:
|
---|
313 | value = config[varname]
|
---|
314 |
|
---|
315 | if not validate_rule_value(rule, value):
|
---|
316 | value = None
|
---|
317 |
|
---|
318 | default = get_default_rule(rule)
|
---|
319 |
|
---|
320 | #
|
---|
321 | # If we don't have a value but we do have
|
---|
322 | # a default, use it.
|
---|
323 | #
|
---|
324 | if value == None and default != None:
|
---|
325 | value = default
|
---|
326 | config[varname] = default
|
---|
327 |
|
---|
328 | if not varname in config:
|
---|
329 | return False
|
---|
330 |
|
---|
331 | return True
|
---|
332 |
|
---|
333 | ## Fill the configuration with random (but valid) values.
|
---|
334 | #
|
---|
335 | # The random selection takes next rule and if the condition does
|
---|
336 | # not violate existing configuration, random value of the variable
|
---|
337 | # is selected.
|
---|
338 | # This happens recursively as long as there are more rules.
|
---|
339 | # If a conflict is found, we backtrack and try other settings of the
|
---|
340 | # variable or ignoring the variable altogether.
|
---|
341 | #
|
---|
342 | # @param config Configuration to work on
|
---|
343 | # @param rules Rules
|
---|
344 | # @param start_index With which rule to start (initial call must specify 0 here).
|
---|
345 | # @return True if able to find a valid configuration
|
---|
346 | def random_choices(config, rules, start_index):
|
---|
347 | "Fill the configuration with random (but valid) values."
|
---|
348 | if start_index >= len(rules):
|
---|
349 | return True
|
---|
350 |
|
---|
351 | varname, vartype, name, choices, cond = rules[start_index]
|
---|
352 |
|
---|
353 | # First check that this rule would make sense
|
---|
354 | if cond and not cond.evaluate(config):
|
---|
355 | return random_choices(config, rules, start_index + 1)
|
---|
356 |
|
---|
357 | # Remember previous choices for backtracking
|
---|
358 | yes_no = 0
|
---|
359 | choices_indexes = range(0, len(choices))
|
---|
360 | random.shuffle(choices_indexes)
|
---|
361 |
|
---|
362 | # Remember current configuration value
|
---|
363 | old_value = None
|
---|
364 | try:
|
---|
365 | old_value = config[varname]
|
---|
366 | except KeyError:
|
---|
367 | old_value = None
|
---|
368 |
|
---|
369 | # For yes/no choices, we ran the loop at most 2 times, for select
|
---|
370 | # choices as many times as there are options.
|
---|
371 | try_counter = 0
|
---|
372 | while True:
|
---|
373 | if vartype == 'choice':
|
---|
374 | if try_counter >= len(choices_indexes):
|
---|
375 | break
|
---|
376 | value = choices[choices_indexes[try_counter]][0]
|
---|
377 | elif vartype == 'y' or vartype == 'n':
|
---|
378 | if try_counter > 0:
|
---|
379 | break
|
---|
380 | value = vartype
|
---|
381 | elif vartype == 'y/n' or vartype == 'n/y':
|
---|
382 | if try_counter == 0:
|
---|
383 | yes_no = random.randint(0, 1)
|
---|
384 | elif try_counter == 1:
|
---|
385 | yes_no = 1 - yes_no
|
---|
386 | else:
|
---|
387 | break
|
---|
388 | if yes_no == 0:
|
---|
389 | value = 'n'
|
---|
390 | else:
|
---|
391 | value = 'y'
|
---|
392 | else:
|
---|
393 | raise RuntimeError("Unknown variable type: %s" % vartype)
|
---|
394 |
|
---|
395 | config[varname] = value
|
---|
396 |
|
---|
397 | ok = random_choices(config, rules, start_index + 1)
|
---|
398 | if ok:
|
---|
399 | return True
|
---|
400 |
|
---|
401 | try_counter = try_counter + 1
|
---|
402 |
|
---|
403 | # Restore the old value and backtrack
|
---|
404 | # (need to delete to prevent "ghost" variables that do not exist under
|
---|
405 | # certain configurations)
|
---|
406 | config[varname] = old_value
|
---|
407 | if old_value is None:
|
---|
408 | del config[varname]
|
---|
409 |
|
---|
410 | return random_choices(config, rules, start_index + 1)
|
---|
411 |
|
---|
412 |
|
---|
413 | ## Get default value from a rule.
|
---|
414 | def get_default_rule(rule):
|
---|
415 | varname, vartype, name, choices, cond = rule
|
---|
416 |
|
---|
417 | default = None
|
---|
418 |
|
---|
419 | if vartype == 'choice':
|
---|
420 | # If there is just one option, use it
|
---|
421 | if len(choices) == 1:
|
---|
422 | default = choices[0][0]
|
---|
423 | elif vartype == 'y':
|
---|
424 | default = '*'
|
---|
425 | elif vartype == 'n':
|
---|
426 | default = 'n'
|
---|
427 | elif vartype == 'y/n':
|
---|
428 | default = 'y'
|
---|
429 | elif vartype == 'n/y':
|
---|
430 | default = 'n'
|
---|
431 | else:
|
---|
432 | raise RuntimeError("Unknown variable type: %s" % vartype)
|
---|
433 |
|
---|
434 | return default
|
---|
435 |
|
---|
436 | ## Get option from a rule.
|
---|
437 | #
|
---|
438 | # @param rule Rule for a variable
|
---|
439 | # @param value Current value of the variable
|
---|
440 | #
|
---|
441 | # @return Option (string) to ask or None which means not to ask.
|
---|
442 | #
|
---|
443 | def get_rule_option(rule, value):
|
---|
444 | varname, vartype, name, choices, cond = rule
|
---|
445 |
|
---|
446 | option = None
|
---|
447 |
|
---|
448 | if vartype == 'choice':
|
---|
449 | # If there is just one option, don't ask
|
---|
450 | if len(choices) != 1:
|
---|
451 | if (value == None):
|
---|
452 | option = "? %s --> " % name
|
---|
453 | else:
|
---|
454 | option = " %s [%s] --> " % (name, value)
|
---|
455 | elif vartype == 'y':
|
---|
456 | pass
|
---|
457 | elif vartype == 'n':
|
---|
458 | pass
|
---|
459 | elif vartype == 'y/n':
|
---|
460 | option = " <%s> %s " % (yes_no(value), name)
|
---|
461 | elif vartype == 'n/y':
|
---|
462 | option =" <%s> %s " % (yes_no(value), name)
|
---|
463 | else:
|
---|
464 | raise RuntimeError("Unknown variable type: %s" % vartype)
|
---|
465 |
|
---|
466 | return option
|
---|
467 |
|
---|
468 | ## Check if variable value is valid.
|
---|
469 | #
|
---|
470 | # @param rule Rule for the variable
|
---|
471 | # @param value Value of the variable
|
---|
472 | #
|
---|
473 | # @return True if valid, False if not valid.
|
---|
474 | #
|
---|
475 | def validate_rule_value(rule, value):
|
---|
476 | varname, vartype, name, choices, cond = rule
|
---|
477 |
|
---|
478 | if value == None:
|
---|
479 | return True
|
---|
480 |
|
---|
481 | if vartype == 'choice':
|
---|
482 | if not value in [choice[0] for choice in choices]:
|
---|
483 | return False
|
---|
484 | elif vartype == 'y':
|
---|
485 | if value != 'y':
|
---|
486 | return False
|
---|
487 | elif vartype == 'n':
|
---|
488 | if value != 'n':
|
---|
489 | return False
|
---|
490 | elif vartype == 'y/n':
|
---|
491 | if not value in ['y', 'n']:
|
---|
492 | return False
|
---|
493 | elif vartype == 'n/y':
|
---|
494 | if not value in ['y', 'n']:
|
---|
495 | return False
|
---|
496 | else:
|
---|
497 | raise RuntimeError("Unknown variable type: %s" % vartype)
|
---|
498 |
|
---|
499 | return True
|
---|
500 |
|
---|
501 | def preprocess_config(config, rules):
|
---|
502 | "Preprocess configuration"
|
---|
503 |
|
---|
504 | varname_mode = 'CONFIG_BFB_MODE'
|
---|
505 | varname_width = 'CONFIG_BFB_WIDTH'
|
---|
506 | varname_height = 'CONFIG_BFB_HEIGHT'
|
---|
507 |
|
---|
508 | if varname_mode in config:
|
---|
509 | mode = config[varname_mode].partition('x')
|
---|
510 |
|
---|
511 | config[varname_width] = mode[0]
|
---|
512 | rules.append((varname_width, 'choice', 'Default framebuffer width', None, None))
|
---|
513 |
|
---|
514 | config[varname_height] = mode[2]
|
---|
515 | rules.append((varname_height, 'choice', 'Default framebuffer height', None, None))
|
---|
516 |
|
---|
517 | def create_output(mkname, mcname, config, rules):
|
---|
518 | "Create output configuration"
|
---|
519 |
|
---|
520 | varname_strip = 'CONFIG_STRIP_REVISION_INFO'
|
---|
521 | strip_rev_info = (varname_strip in config) and (config[varname_strip] == 'y')
|
---|
522 |
|
---|
523 | if strip_rev_info:
|
---|
524 | timestamp_unix = int(0)
|
---|
525 | else:
|
---|
526 | # TODO: Use commit timestamp instead of build time.
|
---|
527 | timestamp_unix = int(time.time())
|
---|
528 |
|
---|
529 | timestamp = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(timestamp_unix))
|
---|
530 |
|
---|
531 | sys.stderr.write("Fetching current revision identifier ... ")
|
---|
532 |
|
---|
533 | try:
|
---|
534 | version = subprocess.Popen(['git', 'log', '-1', '--pretty=%h'], stdout = subprocess.PIPE).communicate()[0].decode().strip()
|
---|
535 | sys.stderr.write("ok\n")
|
---|
536 | except:
|
---|
537 | version = None
|
---|
538 | sys.stderr.write("failed\n")
|
---|
539 |
|
---|
540 | if (not strip_rev_info) and (version is not None):
|
---|
541 | revision = version
|
---|
542 | else:
|
---|
543 | revision = None
|
---|
544 |
|
---|
545 | outmk = open(mkname, 'w')
|
---|
546 | outmc = open(mcname, 'w')
|
---|
547 |
|
---|
548 | outmk.write('#########################################\n')
|
---|
549 | outmk.write('## AUTO-GENERATED FILE, DO NOT EDIT!!! ##\n')
|
---|
550 | outmk.write('## Generated by: tools/config.py ##\n')
|
---|
551 | outmk.write('#########################################\n\n')
|
---|
552 |
|
---|
553 | outmc.write('/***************************************\n')
|
---|
554 | outmc.write(' * AUTO-GENERATED FILE, DO NOT EDIT!!! *\n')
|
---|
555 | outmc.write(' * Generated by: tools/config.py *\n')
|
---|
556 | outmc.write(' ***************************************/\n\n')
|
---|
557 |
|
---|
558 | defs = 'CONFIG_DEFS ='
|
---|
559 |
|
---|
560 | for varname, vartype, name, choices, cond in rules:
|
---|
561 | if cond and not cond.evaluate(config):
|
---|
562 | continue
|
---|
563 |
|
---|
564 | if not varname in config:
|
---|
565 | value = ''
|
---|
566 | else:
|
---|
567 | value = config[varname]
|
---|
568 | if (value == '*'):
|
---|
569 | value = 'y'
|
---|
570 |
|
---|
571 | outmk.write('# %s\n%s = %s\n\n' % (name, varname, value))
|
---|
572 |
|
---|
573 | if vartype in ["y", "n", "y/n", "n/y"]:
|
---|
574 | if value == "y":
|
---|
575 | outmc.write('/* %s */\n#define %s\n\n' % (name, varname))
|
---|
576 | defs += ' -D%s' % varname
|
---|
577 | else:
|
---|
578 | outmc.write('/* %s */\n#define %s %s\n#define %s_%s\n\n' % (name, varname, value, varname, value))
|
---|
579 | defs += ' -D%s=%s -D%s_%s' % (varname, value, varname, value)
|
---|
580 |
|
---|
581 | if revision is not None:
|
---|
582 | outmk.write('REVISION = %s\n' % revision)
|
---|
583 | outmc.write('#define REVISION %s\n' % revision)
|
---|
584 | defs += ' "-DREVISION=%s"' % revision
|
---|
585 |
|
---|
586 | outmk.write('TIMESTAMP_UNIX = %d\n' % timestamp_unix)
|
---|
587 | outmc.write('#define TIMESTAMP_UNIX %d\n' % timestamp_unix)
|
---|
588 | defs += ' "-DTIMESTAMP_UNIX=%d"\n' % timestamp_unix
|
---|
589 |
|
---|
590 | outmk.write('TIMESTAMP = %s\n' % timestamp)
|
---|
591 | outmc.write('#define TIMESTAMP %s\n' % timestamp)
|
---|
592 | defs += ' "-DTIMESTAMP=%s"\n' % timestamp
|
---|
593 |
|
---|
594 | outmk.write(defs)
|
---|
595 |
|
---|
596 | outmk.close()
|
---|
597 | outmc.close()
|
---|
598 |
|
---|
599 | def sorted_dir(root):
|
---|
600 | list = os.listdir(root)
|
---|
601 | list.sort()
|
---|
602 | return list
|
---|
603 |
|
---|
604 | ## Ask user to choose a configuration profile.
|
---|
605 | #
|
---|
606 | def choose_profile(root, fname, screen, config):
|
---|
607 | options = []
|
---|
608 | opt2path = {}
|
---|
609 | cnt = 0
|
---|
610 |
|
---|
611 | # Look for profiles
|
---|
612 | for name in sorted_dir(root):
|
---|
613 | path = os.path.join(root, name)
|
---|
614 | canon = os.path.join(path, fname)
|
---|
615 |
|
---|
616 | if os.path.isdir(path) and os.path.exists(canon) and os.path.isfile(canon):
|
---|
617 | subprofile = False
|
---|
618 |
|
---|
619 | # Look for subprofiles
|
---|
620 | for subname in sorted_dir(path):
|
---|
621 | subpath = os.path.join(path, subname)
|
---|
622 | subcanon = os.path.join(subpath, fname)
|
---|
623 |
|
---|
624 | if os.path.isdir(subpath) and os.path.exists(subcanon) and os.path.isfile(subcanon):
|
---|
625 | subprofile = True
|
---|
626 | options.append("%s (%s)" % (name, subname))
|
---|
627 | opt2path[cnt] = [name, subname]
|
---|
628 | cnt += 1
|
---|
629 |
|
---|
630 | if not subprofile:
|
---|
631 | options.append(name)
|
---|
632 | opt2path[cnt] = [name]
|
---|
633 | cnt += 1
|
---|
634 |
|
---|
635 | (button, value) = xtui.choice_window(screen, 'Load preconfigured defaults', 'Choose configuration profile', options, None)
|
---|
636 |
|
---|
637 | if button == 'cancel':
|
---|
638 | return None
|
---|
639 |
|
---|
640 | return opt2path[value]
|
---|
641 |
|
---|
642 | ## Read presets from a configuration profile.
|
---|
643 | #
|
---|
644 | # @param profile Profile to load from (a list of string components)
|
---|
645 | # @param config Output configuration
|
---|
646 | #
|
---|
647 | def read_presets(profile, config):
|
---|
648 | path = os.path.join(PRESETS_DIR, profile[0], MAKEFILE)
|
---|
649 | read_config(path, config)
|
---|
650 |
|
---|
651 | if len(profile) > 1:
|
---|
652 | path = os.path.join(PRESETS_DIR, profile[0], profile[1], MAKEFILE)
|
---|
653 | read_config(path, config)
|
---|
654 |
|
---|
655 | ## Parse profile name (relative OS path) into a list of components.
|
---|
656 | #
|
---|
657 | # @param profile_name Relative path (using OS separator)
|
---|
658 | # @return List of components
|
---|
659 | #
|
---|
660 | def parse_profile_name(profile_name):
|
---|
661 | profile = []
|
---|
662 |
|
---|
663 | head, tail = os.path.split(profile_name)
|
---|
664 | if head != '':
|
---|
665 | profile.append(head)
|
---|
666 |
|
---|
667 | profile.append(tail)
|
---|
668 | return profile
|
---|
669 |
|
---|
670 | def main():
|
---|
671 | profile = None
|
---|
672 | config = {}
|
---|
673 | rules = []
|
---|
674 |
|
---|
675 | # Parse rules file
|
---|
676 | parse_rules(RULES_FILE, rules)
|
---|
677 |
|
---|
678 | # Input configuration file can be specified on command line
|
---|
679 | # otherwise configuration from previous run is used.
|
---|
680 | if len(sys.argv) >= 4:
|
---|
681 | profile = parse_profile_name(sys.argv[3])
|
---|
682 | read_presets(profile, config)
|
---|
683 | elif os.path.exists(MAKEFILE):
|
---|
684 | read_config(MAKEFILE, config)
|
---|
685 |
|
---|
686 | # Default mode: check values and regenerate configuration files
|
---|
687 | if (len(sys.argv) >= 3) and (sys.argv[2] == 'default'):
|
---|
688 | if (infer_verify_choices(config, rules)):
|
---|
689 | preprocess_config(config, rules)
|
---|
690 | create_output(MAKEFILE, MACROS, config, rules)
|
---|
691 | return 0
|
---|
692 |
|
---|
693 | # Hands-off mode: check values and regenerate configuration files,
|
---|
694 | # but no interactive fallback
|
---|
695 | if (len(sys.argv) >= 3) and (sys.argv[2] == 'hands-off'):
|
---|
696 | # We deliberately test sys.argv >= 4 because we do not want
|
---|
697 | # to read implicitly any possible previous run configuration
|
---|
698 | if len(sys.argv) < 4:
|
---|
699 | sys.stderr.write("Configuration error: No presets specified\n")
|
---|
700 | return 2
|
---|
701 |
|
---|
702 | if (infer_verify_choices(config, rules)):
|
---|
703 | preprocess_config(config, rules)
|
---|
704 | create_output(MAKEFILE, MACROS, config, rules)
|
---|
705 | return 0
|
---|
706 |
|
---|
707 | sys.stderr.write("Configuration error: The presets are ambiguous\n")
|
---|
708 | return 1
|
---|
709 |
|
---|
710 | # Check mode: only check configuration
|
---|
711 | if (len(sys.argv) >= 3) and (sys.argv[2] == 'check'):
|
---|
712 | if infer_verify_choices(config, rules):
|
---|
713 | return 0
|
---|
714 | return 1
|
---|
715 |
|
---|
716 | # Random mode
|
---|
717 | if (len(sys.argv) == 3) and (sys.argv[2] == 'random'):
|
---|
718 | ok = random_choices(config, rules, 0)
|
---|
719 | if not ok:
|
---|
720 | sys.stderr.write("Internal error: unable to generate random config.\n")
|
---|
721 | return 2
|
---|
722 | if not infer_verify_choices(config, rules):
|
---|
723 | sys.stderr.write("Internal error: random configuration not consistent.\n")
|
---|
724 | return 2
|
---|
725 | preprocess_config(config, rules)
|
---|
726 | create_output(MAKEFILE, MACROS, config, rules)
|
---|
727 |
|
---|
728 | return 0
|
---|
729 |
|
---|
730 | screen = xtui.screen_init()
|
---|
731 | try:
|
---|
732 | selname = None
|
---|
733 | position = None
|
---|
734 | while True:
|
---|
735 |
|
---|
736 | # Cancel out all values which have to be deduced
|
---|
737 | for varname, vartype, name, choices, cond in rules:
|
---|
738 | if (vartype == 'y') and (varname in config) and (config[varname] == '*'):
|
---|
739 | config[varname] = None
|
---|
740 |
|
---|
741 | options = []
|
---|
742 | opt2row = {}
|
---|
743 | cnt = 1
|
---|
744 |
|
---|
745 | options.append(" --- Load preconfigured defaults ... ")
|
---|
746 |
|
---|
747 | for rule in rules:
|
---|
748 | varname, vartype, name, choices, cond = rule
|
---|
749 |
|
---|
750 | if cond and not cond.evaluate(config):
|
---|
751 | continue
|
---|
752 |
|
---|
753 | if varname == selname:
|
---|
754 | position = cnt
|
---|
755 |
|
---|
756 | if not varname in config:
|
---|
757 | value = None
|
---|
758 | else:
|
---|
759 | value = config[varname]
|
---|
760 |
|
---|
761 | if not validate_rule_value(rule, value):
|
---|
762 | value = None
|
---|
763 |
|
---|
764 | default = get_default_rule(rule)
|
---|
765 |
|
---|
766 | #
|
---|
767 | # If we don't have a value but we do have
|
---|
768 | # a default, use it.
|
---|
769 | #
|
---|
770 | if value == None and default != None:
|
---|
771 | value = default
|
---|
772 | config[varname] = default
|
---|
773 |
|
---|
774 | option = get_rule_option(rule, value)
|
---|
775 | if option != None:
|
---|
776 | options.append(option)
|
---|
777 | else:
|
---|
778 | continue
|
---|
779 |
|
---|
780 | opt2row[cnt] = (varname, vartype, name, choices)
|
---|
781 |
|
---|
782 | cnt += 1
|
---|
783 |
|
---|
784 | if (position != None) and (position >= len(options)):
|
---|
785 | position = None
|
---|
786 |
|
---|
787 | (button, value) = xtui.choice_window(screen, 'HelenOS configuration', 'Choose configuration option', options, position)
|
---|
788 |
|
---|
789 | if button == 'cancel':
|
---|
790 | return 'Configuration canceled'
|
---|
791 |
|
---|
792 | if button == 'done':
|
---|
793 | if (infer_verify_choices(config, rules)):
|
---|
794 | break
|
---|
795 | else:
|
---|
796 | xtui.error_dialog(screen, 'Error', 'Some options have still undefined values. These options are marked with the "?" sign.')
|
---|
797 | continue
|
---|
798 |
|
---|
799 | if value == 0:
|
---|
800 | profile = choose_profile(PRESETS_DIR, MAKEFILE, screen, config)
|
---|
801 | if profile != None:
|
---|
802 | read_presets(profile, config)
|
---|
803 | position = 1
|
---|
804 | continue
|
---|
805 |
|
---|
806 | position = None
|
---|
807 | if not value in opt2row:
|
---|
808 | raise RuntimeError("Error selecting value: %s" % value)
|
---|
809 |
|
---|
810 | (selname, seltype, name, choices) = opt2row[value]
|
---|
811 |
|
---|
812 | if not selname in config:
|
---|
813 | value = None
|
---|
814 | else:
|
---|
815 | value = config[selname]
|
---|
816 |
|
---|
817 | if seltype == 'choice':
|
---|
818 | config[selname] = subchoice(screen, name, choices, value)
|
---|
819 | elif (seltype == 'y/n') or (seltype == 'n/y'):
|
---|
820 | if config[selname] == 'y':
|
---|
821 | config[selname] = 'n'
|
---|
822 | else:
|
---|
823 | config[selname] = 'y'
|
---|
824 | finally:
|
---|
825 | xtui.screen_done(screen)
|
---|
826 |
|
---|
827 | preprocess_config(config, rules)
|
---|
828 | create_output(MAKEFILE, MACROS, config, rules)
|
---|
829 | return 0
|
---|
830 |
|
---|
831 | if __name__ == '__main__':
|
---|
832 | sys.exit(main())
|
---|