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