[98376de] | 1 | #!/usr/bin/env python
|
---|
[44882c8] | 2 | #
|
---|
[5a55ae6] | 3 | # Copyright (c) 2006 Ondrej Palkovsky
|
---|
[9a0367f] | 4 | # Copyright (c) 2009 Martin Decky
|
---|
[62bb73e] | 5 | # Copyright (c) 2010 Jiri Svoboda
|
---|
[44882c8] | 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 | #
|
---|
[3c80f2b] | 31 |
|
---|
[98376de] | 32 | """
|
---|
[9a0367f] | 33 | HelenOS configuration system
|
---|
[98376de] | 34 | """
|
---|
[3c80f2b] | 35 |
|
---|
[98376de] | 36 | import sys
|
---|
| 37 | import os
|
---|
| 38 | import re
|
---|
[5a8fbcb9] | 39 | import time
|
---|
| 40 | import subprocess
|
---|
[27fb3d6] | 41 | import xtui
|
---|
[98376de] | 42 |
|
---|
[62bb73e] | 43 | RULES_FILE = sys.argv[1]
|
---|
[84266669] | 44 | MAKEFILE = 'Makefile.config'
|
---|
| 45 | MACROS = 'config.h'
|
---|
[62bb73e] | 46 | PRESETS_DIR = 'defaults'
|
---|
[98376de] | 47 |
|
---|
[62bb73e] | 48 | def read_config(fname, config):
|
---|
[d40ffbb] | 49 | "Read saved values from last configuration run or a preset file"
|
---|
[9a0367f] | 50 |
|
---|
[28f4adb] | 51 | inf = open(fname, 'r')
|
---|
[9a0367f] | 52 |
|
---|
| 53 | for line in inf:
|
---|
| 54 | res = re.match(r'^(?:#!# )?([^#]\w*)\s*=\s*(.*?)\s*$', line)
|
---|
[ba8de9c3] | 55 | if res:
|
---|
[62bb73e] | 56 | config[res.group(1)] = res.group(2)
|
---|
[9a0367f] | 57 |
|
---|
| 58 | inf.close()
|
---|
[98376de] | 59 |
|
---|
[62bb73e] | 60 | def check_condition(text, config, rules):
|
---|
[81c8d54] | 61 | "Check that the condition specified on input line is True (only CNF and DNF is supported)"
|
---|
[9a0367f] | 62 |
|
---|
| 63 | ctype = 'cnf'
|
---|
| 64 |
|
---|
[ba8de9c3] | 65 | if (')|' in text) or ('|(' in text):
|
---|
[9a0367f] | 66 | ctype = 'dnf'
|
---|
| 67 |
|
---|
[ba8de9c3] | 68 | if ctype == 'cnf':
|
---|
[9a0367f] | 69 | conds = text.split('&')
|
---|
| 70 | else:
|
---|
| 71 | conds = text.split('|')
|
---|
| 72 |
|
---|
| 73 | for cond in conds:
|
---|
[ba8de9c3] | 74 | if cond.startswith('(') and cond.endswith(')'):
|
---|
[9a0367f] | 75 | cond = cond[1:-1]
|
---|
| 76 |
|
---|
[62bb73e] | 77 | inside = check_inside(cond, config, ctype)
|
---|
[9a0367f] | 78 |
|
---|
| 79 | if (ctype == 'cnf') and (not inside):
|
---|
| 80 | return False
|
---|
| 81 |
|
---|
[ba8de9c3] | 82 | if (ctype == 'dnf') and inside:
|
---|
[9a0367f] | 83 | return True
|
---|
| 84 |
|
---|
[ba8de9c3] | 85 | if ctype == 'cnf':
|
---|
[9a0367f] | 86 | return True
|
---|
[1f5c9c96] | 87 |
|
---|
[9a0367f] | 88 | return False
|
---|
[98376de] | 89 |
|
---|
[62bb73e] | 90 | def check_inside(text, config, ctype):
|
---|
[81c8d54] | 91 | "Check for condition"
|
---|
[9a0367f] | 92 |
|
---|
[ba8de9c3] | 93 | if ctype == 'cnf':
|
---|
[9a0367f] | 94 | conds = text.split('|')
|
---|
| 95 | else:
|
---|
| 96 | conds = text.split('&')
|
---|
| 97 |
|
---|
| 98 | for cond in conds:
|
---|
| 99 | res = re.match(r'^(.*?)(!?=)(.*)$', cond)
|
---|
[ba8de9c3] | 100 | if not res:
|
---|
[9a0367f] | 101 | raise RuntimeError("Invalid condition: %s" % cond)
|
---|
| 102 |
|
---|
| 103 | condname = res.group(1)
|
---|
| 104 | oper = res.group(2)
|
---|
| 105 | condval = res.group(3)
|
---|
| 106 |
|
---|
[ba8de9c3] | 107 | if not condname in config:
|
---|
[9a0367f] | 108 | varval = ''
|
---|
| 109 | else:
|
---|
[62bb73e] | 110 | varval = config[condname]
|
---|
[7aef7ee] | 111 | if (varval == '*'):
|
---|
| 112 | varval = 'y'
|
---|
[9a0367f] | 113 |
|
---|
[ba8de9c3] | 114 | if ctype == 'cnf':
|
---|
[9a0367f] | 115 | if (oper == '=') and (condval == varval):
|
---|
| 116 | return True
|
---|
| 117 |
|
---|
| 118 | if (oper == '!=') and (condval != varval):
|
---|
| 119 | return True
|
---|
| 120 | else:
|
---|
| 121 | if (oper == '=') and (condval != varval):
|
---|
| 122 | return False
|
---|
| 123 |
|
---|
| 124 | if (oper == '!=') and (condval == varval):
|
---|
| 125 | return False
|
---|
| 126 |
|
---|
[ba8de9c3] | 127 | if ctype == 'cnf':
|
---|
[9a0367f] | 128 | return False
|
---|
| 129 |
|
---|
| 130 | return True
|
---|
[98376de] | 131 |
|
---|
[62bb73e] | 132 | def parse_rules(fname, rules):
|
---|
| 133 | "Parse rules file"
|
---|
[9a0367f] | 134 |
|
---|
[28f4adb] | 135 | inf = open(fname, 'r')
|
---|
[9a0367f] | 136 |
|
---|
| 137 | name = ''
|
---|
| 138 | choices = []
|
---|
| 139 |
|
---|
| 140 | for line in inf:
|
---|
| 141 |
|
---|
[ba8de9c3] | 142 | if line.startswith('!'):
|
---|
[9a0367f] | 143 | # Ask a question
|
---|
| 144 | res = re.search(r'!\s*(?:\[(.*?)\])?\s*([^\s]+)\s*\((.*)\)\s*$', line)
|
---|
| 145 |
|
---|
[ba8de9c3] | 146 | if not res:
|
---|
[9a0367f] | 147 | raise RuntimeError("Weird line: %s" % line)
|
---|
| 148 |
|
---|
| 149 | cond = res.group(1)
|
---|
| 150 | varname = res.group(2)
|
---|
| 151 | vartype = res.group(3)
|
---|
| 152 |
|
---|
[62bb73e] | 153 | rules.append((varname, vartype, name, choices, cond))
|
---|
[9a0367f] | 154 | name = ''
|
---|
| 155 | choices = []
|
---|
| 156 | continue
|
---|
| 157 |
|
---|
[ba8de9c3] | 158 | if line.startswith('@'):
|
---|
[9a0367f] | 159 | # Add new line into the 'choices' array
|
---|
| 160 | res = re.match(r'@\s*(?:\[(.*?)\])?\s*"(.*?)"\s*(.*)$', line)
|
---|
| 161 |
|
---|
| 162 | if not res:
|
---|
| 163 | raise RuntimeError("Bad line: %s" % line)
|
---|
| 164 |
|
---|
| 165 | choices.append((res.group(2), res.group(3)))
|
---|
| 166 | continue
|
---|
| 167 |
|
---|
[ba8de9c3] | 168 | if line.startswith('%'):
|
---|
[9a0367f] | 169 | # Name of the option
|
---|
| 170 | name = line[1:].strip()
|
---|
| 171 | continue
|
---|
| 172 |
|
---|
[ba8de9c3] | 173 | if line.startswith('#') or (line == '\n'):
|
---|
[9a0367f] | 174 | # Comment or empty line
|
---|
| 175 | continue
|
---|
| 176 |
|
---|
| 177 |
|
---|
| 178 | raise RuntimeError("Unknown syntax: %s" % line)
|
---|
| 179 |
|
---|
| 180 | inf.close()
|
---|
[98376de] | 181 |
|
---|
[9a0367f] | 182 | def yes_no(default):
|
---|
| 183 | "Return '*' if yes, ' ' if no"
|
---|
| 184 |
|
---|
[ba8de9c3] | 185 | if default == 'y':
|
---|
[9a0367f] | 186 | return '*'
|
---|
| 187 |
|
---|
| 188 | return ' '
|
---|
[98376de] | 189 |
|
---|
[27fb3d6] | 190 | def subchoice(screen, name, choices, default):
|
---|
[9a0367f] | 191 | "Return choice of choices"
|
---|
| 192 |
|
---|
[27fb3d6] | 193 | maxkey = 0
|
---|
| 194 | for key, val in choices:
|
---|
| 195 | length = len(key)
|
---|
| 196 | if (length > maxkey):
|
---|
| 197 | maxkey = length
|
---|
[9a0367f] | 198 |
|
---|
| 199 | options = []
|
---|
[27fb3d6] | 200 | position = None
|
---|
| 201 | cnt = 0
|
---|
| 202 | for key, val in choices:
|
---|
[ba8de9c3] | 203 | if (default) and (key == default):
|
---|
[27fb3d6] | 204 | position = cnt
|
---|
| 205 |
|
---|
| 206 | options.append(" %-*s %s " % (maxkey, key, val))
|
---|
| 207 | cnt += 1
|
---|
[9a0367f] | 208 |
|
---|
[27fb3d6] | 209 | (button, value) = xtui.choice_window(screen, name, 'Choose value', options, position)
|
---|
[9a0367f] | 210 |
|
---|
[ba8de9c3] | 211 | if button == 'cancel':
|
---|
[9a0367f] | 212 | return None
|
---|
| 213 |
|
---|
[27fb3d6] | 214 | return choices[value][0]
|
---|
[98376de] | 215 |
|
---|
[e4d540b] | 216 | ## Infer and verify configuration values.
|
---|
| 217 | #
|
---|
[62bb73e] | 218 | # Augment @a config with values that can be inferred, purge invalid ones
|
---|
[e4d540b] | 219 | # and verify that all variables have a value (previously specified or inferred).
|
---|
| 220 | #
|
---|
[d40ffbb] | 221 | # @param config Configuration to work on
|
---|
| 222 | # @param rules Rules
|
---|
[e4d540b] | 223 | #
|
---|
[d40ffbb] | 224 | # @return True if configuration is complete and valid, False
|
---|
| 225 | # otherwise.
|
---|
[e4d540b] | 226 | #
|
---|
[62bb73e] | 227 | def infer_verify_choices(config, rules):
|
---|
[e4d540b] | 228 | "Infer and verify configuration values."
|
---|
[9a0367f] | 229 |
|
---|
[ba8de9c3] | 230 | for rule in rules:
|
---|
| 231 | varname, vartype, name, choices, cond = rule
|
---|
[d40ffbb] | 232 |
|
---|
[ba8de9c3] | 233 | if cond and (not check_condition(cond, config, rules)):
|
---|
[9a0367f] | 234 | continue
|
---|
| 235 |
|
---|
[ba8de9c3] | 236 | if not varname in config:
|
---|
[4756634] | 237 | value = None
|
---|
[e4d540b] | 238 | else:
|
---|
[4756634] | 239 | value = config[varname]
|
---|
[d40ffbb] | 240 |
|
---|
| 241 | if not validate_rule_value(rule, value):
|
---|
[4756634] | 242 | value = None
|
---|
[d40ffbb] | 243 |
|
---|
| 244 | default = get_default_rule(rule)
|
---|
[1f5c9c96] | 245 |
|
---|
[c0bd08d] | 246 | #
|
---|
| 247 | # If we don't have a value but we do have
|
---|
| 248 | # a default, use it.
|
---|
| 249 | #
|
---|
| 250 | if value == None and default != None:
|
---|
| 251 | value = default
|
---|
[4756634] | 252 | config[varname] = default
|
---|
[d40ffbb] | 253 |
|
---|
[ba8de9c3] | 254 | if not varname in config:
|
---|
[9a0367f] | 255 | return False
|
---|
| 256 |
|
---|
| 257 | return True
|
---|
[98376de] | 258 |
|
---|
[e4d540b] | 259 | ## Get default value from a rule.
|
---|
[d40ffbb] | 260 | def get_default_rule(rule):
|
---|
[e4d540b] | 261 | varname, vartype, name, choices, cond = rule
|
---|
[d40ffbb] | 262 |
|
---|
[e4d540b] | 263 | default = None
|
---|
[d40ffbb] | 264 |
|
---|
[ba8de9c3] | 265 | if vartype == 'choice':
|
---|
[e4d540b] | 266 | # If there is just one option, use it
|
---|
[ba8de9c3] | 267 | if len(choices) == 1:
|
---|
[e4d540b] | 268 | default = choices[0][0]
|
---|
[ba8de9c3] | 269 | elif vartype == 'y':
|
---|
[e4d540b] | 270 | default = '*'
|
---|
[ba8de9c3] | 271 | elif vartype == 'n':
|
---|
[e4d540b] | 272 | default = 'n'
|
---|
[ba8de9c3] | 273 | elif vartype == 'y/n':
|
---|
[e4d540b] | 274 | default = 'y'
|
---|
[ba8de9c3] | 275 | elif vartype == 'n/y':
|
---|
[e4d540b] | 276 | default = 'n'
|
---|
| 277 | else:
|
---|
| 278 | raise RuntimeError("Unknown variable type: %s" % vartype)
|
---|
[d40ffbb] | 279 |
|
---|
[e4d540b] | 280 | return default
|
---|
| 281 |
|
---|
| 282 | ## Get option from a rule.
|
---|
| 283 | #
|
---|
[d40ffbb] | 284 | # @param rule Rule for a variable
|
---|
| 285 | # @param value Current value of the variable
|
---|
[e4d540b] | 286 | #
|
---|
| 287 | # @return Option (string) to ask or None which means not to ask.
|
---|
| 288 | #
|
---|
[d40ffbb] | 289 | def get_rule_option(rule, value):
|
---|
[e4d540b] | 290 | varname, vartype, name, choices, cond = rule
|
---|
[d40ffbb] | 291 |
|
---|
[e4d540b] | 292 | option = None
|
---|
[d40ffbb] | 293 |
|
---|
[ba8de9c3] | 294 | if vartype == 'choice':
|
---|
[e4d540b] | 295 | # If there is just one option, don't ask
|
---|
[ba8de9c3] | 296 | if len(choices) != 1:
|
---|
[e4d540b] | 297 | if (value == None):
|
---|
| 298 | option = "? %s --> " % name
|
---|
| 299 | else:
|
---|
| 300 | option = " %s [%s] --> " % (name, value)
|
---|
[ba8de9c3] | 301 | elif vartype == 'y':
|
---|
[e4d540b] | 302 | pass
|
---|
[ba8de9c3] | 303 | elif vartype == 'n':
|
---|
[e4d540b] | 304 | pass
|
---|
[ba8de9c3] | 305 | elif vartype == 'y/n':
|
---|
[e4d540b] | 306 | option = " <%s> %s " % (yes_no(value), name)
|
---|
[ba8de9c3] | 307 | elif vartype == 'n/y':
|
---|
[e4d540b] | 308 | option =" <%s> %s " % (yes_no(value), name)
|
---|
| 309 | else:
|
---|
| 310 | raise RuntimeError("Unknown variable type: %s" % vartype)
|
---|
[d40ffbb] | 311 |
|
---|
[e4d540b] | 312 | return option
|
---|
| 313 |
|
---|
| 314 | ## Check if variable value is valid.
|
---|
| 315 | #
|
---|
[d40ffbb] | 316 | # @param rule Rule for the variable
|
---|
| 317 | # @param value Value of the variable
|
---|
[e4d540b] | 318 | #
|
---|
[d40ffbb] | 319 | # @return True if valid, False if not valid.
|
---|
[e4d540b] | 320 | #
|
---|
[d40ffbb] | 321 | def validate_rule_value(rule, value):
|
---|
[e4d540b] | 322 | varname, vartype, name, choices, cond = rule
|
---|
| 323 |
|
---|
| 324 | if value == None:
|
---|
| 325 | return True
|
---|
[d40ffbb] | 326 |
|
---|
[ba8de9c3] | 327 | if vartype == 'choice':
|
---|
| 328 | if not value in [choice[0] for choice in choices]:
|
---|
[e4d540b] | 329 | return False
|
---|
[ba8de9c3] | 330 | elif vartype == 'y':
|
---|
[e4d540b] | 331 | if value != 'y':
|
---|
| 332 | return False
|
---|
[ba8de9c3] | 333 | elif vartype == 'n':
|
---|
[e4d540b] | 334 | if value != 'n':
|
---|
| 335 | return False
|
---|
[ba8de9c3] | 336 | elif vartype == 'y/n':
|
---|
[e4d540b] | 337 | if not value in ['y', 'n']:
|
---|
| 338 | return False
|
---|
[ba8de9c3] | 339 | elif vartype == 'n/y':
|
---|
[e4d540b] | 340 | if not value in ['y', 'n']:
|
---|
| 341 | return False
|
---|
| 342 | else:
|
---|
| 343 | raise RuntimeError("Unknown variable type: %s" % vartype)
|
---|
[d40ffbb] | 344 |
|
---|
[e4d540b] | 345 | return True
|
---|
| 346 |
|
---|
[1f5c9c96] | 347 | def preprocess_config(config, rules):
|
---|
| 348 | "Preprocess configuration"
|
---|
| 349 |
|
---|
| 350 | varname_mode = 'CONFIG_BFB_MODE'
|
---|
| 351 | varname_width = 'CONFIG_BFB_WIDTH'
|
---|
| 352 | varname_height = 'CONFIG_BFB_HEIGHT'
|
---|
| 353 |
|
---|
| 354 | if varname_mode in config:
|
---|
| 355 | mode = config[varname_mode].partition('x')
|
---|
| 356 |
|
---|
| 357 | config[varname_width] = mode[0]
|
---|
| 358 | rules.append((varname_width, 'choice', 'Default framebuffer width', None, None))
|
---|
| 359 |
|
---|
| 360 | config[varname_height] = mode[2]
|
---|
| 361 | rules.append((varname_height, 'choice', 'Default framebuffer height', None, None))
|
---|
| 362 |
|
---|
[62bb73e] | 363 | def create_output(mkname, mcname, config, rules):
|
---|
[9a0367f] | 364 | "Create output configuration"
|
---|
| 365 |
|
---|
[5a8fbcb9] | 366 | timestamp = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
|
---|
[fe12f9f4] | 367 |
|
---|
| 368 | sys.stderr.write("Fetching current revision identifier ... ")
|
---|
[7b76744] | 369 |
|
---|
| 370 | try:
|
---|
[28f4adb] | 371 | version = subprocess.Popen(['bzr', 'version-info', '--custom', '--template={clean}:{revno}:{revision_id}'], stdout = subprocess.PIPE).communicate()[0].decode().split(':')
|
---|
[7b76744] | 372 | sys.stderr.write("ok\n")
|
---|
| 373 | except:
|
---|
| 374 | version = [1, "unknown", "unknown"]
|
---|
| 375 | sys.stderr.write("failed\n")
|
---|
[5a8fbcb9] | 376 |
|
---|
[ba8de9c3] | 377 | if len(version) == 3:
|
---|
[5a8fbcb9] | 378 | revision = version[1]
|
---|
[ba8de9c3] | 379 | if version[0] != 1:
|
---|
[5a8fbcb9] | 380 | revision += 'M'
|
---|
| 381 | revision += ' (%s)' % version[2]
|
---|
| 382 | else:
|
---|
| 383 | revision = None
|
---|
[84266669] | 384 |
|
---|
[28f4adb] | 385 | outmk = open(mkname, 'w')
|
---|
| 386 | outmc = open(mcname, 'w')
|
---|
[84266669] | 387 |
|
---|
| 388 | outmk.write('#########################################\n')
|
---|
| 389 | outmk.write('## AUTO-GENERATED FILE, DO NOT EDIT!!! ##\n')
|
---|
| 390 | outmk.write('#########################################\n\n')
|
---|
[9a0367f] | 391 |
|
---|
[84266669] | 392 | outmc.write('/***************************************\n')
|
---|
| 393 | outmc.write(' * AUTO-GENERATED FILE, DO NOT EDIT!!! *\n')
|
---|
| 394 | outmc.write(' ***************************************/\n\n')
|
---|
| 395 |
|
---|
[4e9aaf5] | 396 | defs = 'CONFIG_DEFS ='
|
---|
[9a0367f] | 397 |
|
---|
[62bb73e] | 398 | for varname, vartype, name, choices, cond in rules:
|
---|
[ba8de9c3] | 399 | if cond and (not check_condition(cond, config, rules)):
|
---|
[9a0367f] | 400 | continue
|
---|
| 401 |
|
---|
[ba8de9c3] | 402 | if not varname in config:
|
---|
[4756634] | 403 | value = ''
|
---|
[9a0367f] | 404 | else:
|
---|
[4756634] | 405 | value = config[varname]
|
---|
| 406 | if (value == '*'):
|
---|
| 407 | value = 'y'
|
---|
[9a0367f] | 408 |
|
---|
[4756634] | 409 | outmk.write('# %s\n%s = %s\n\n' % (name, varname, value))
|
---|
[84266669] | 410 |
|
---|
[ba8de9c3] | 411 | if vartype in ["y", "n", "y/n", "n/y"]:
|
---|
| 412 | if value == "y":
|
---|
[84266669] | 413 | outmc.write('/* %s */\n#define %s\n\n' % (name, varname))
|
---|
[4e9aaf5] | 414 | defs += ' -D%s' % varname
|
---|
[84266669] | 415 | else:
|
---|
[4756634] | 416 | outmc.write('/* %s */\n#define %s %s\n#define %s_%s\n\n' % (name, varname, value, varname, value))
|
---|
| 417 | defs += ' -D%s=%s -D%s_%s' % (varname, value, varname, value)
|
---|
[9a0367f] | 418 |
|
---|
[ba8de9c3] | 419 | if revision is not None:
|
---|
[5a8fbcb9] | 420 | outmk.write('REVISION = %s\n' % revision)
|
---|
| 421 | outmc.write('#define REVISION %s\n' % revision)
|
---|
[4e9aaf5] | 422 | defs += ' "-DREVISION=%s"' % revision
|
---|
[84266669] | 423 |
|
---|
[5a8fbcb9] | 424 | outmk.write('TIMESTAMP = %s\n' % timestamp)
|
---|
[84266669] | 425 | outmc.write('#define TIMESTAMP %s\n' % timestamp)
|
---|
[4e9aaf5] | 426 | defs += ' "-DTIMESTAMP=%s"\n' % timestamp
|
---|
| 427 |
|
---|
| 428 | outmk.write(defs)
|
---|
[84266669] | 429 |
|
---|
| 430 | outmk.close()
|
---|
| 431 | outmc.close()
|
---|
[98376de] | 432 |
|
---|
[31fb9a0] | 433 | def sorted_dir(root):
|
---|
| 434 | list = os.listdir(root)
|
---|
| 435 | list.sort()
|
---|
| 436 | return list
|
---|
| 437 |
|
---|
[6ec0acd] | 438 | ## Ask user to choose a configuration profile.
|
---|
[62bb73e] | 439 | #
|
---|
[d40ffbb] | 440 | def choose_profile(root, fname, screen, config):
|
---|
[31fb9a0] | 441 | options = []
|
---|
| 442 | opt2path = {}
|
---|
| 443 | cnt = 0
|
---|
| 444 |
|
---|
| 445 | # Look for profiles
|
---|
| 446 | for name in sorted_dir(root):
|
---|
| 447 | path = os.path.join(root, name)
|
---|
| 448 | canon = os.path.join(path, fname)
|
---|
| 449 |
|
---|
[ba8de9c3] | 450 | if os.path.isdir(path) and os.path.exists(canon) and os.path.isfile(canon):
|
---|
[31fb9a0] | 451 | subprofile = False
|
---|
| 452 |
|
---|
| 453 | # Look for subprofiles
|
---|
| 454 | for subname in sorted_dir(path):
|
---|
| 455 | subpath = os.path.join(path, subname)
|
---|
| 456 | subcanon = os.path.join(subpath, fname)
|
---|
| 457 |
|
---|
[ba8de9c3] | 458 | if os.path.isdir(subpath) and os.path.exists(subcanon) and os.path.isfile(subcanon):
|
---|
[31fb9a0] | 459 | subprofile = True
|
---|
| 460 | options.append("%s (%s)" % (name, subname))
|
---|
[6ec0acd] | 461 | opt2path[cnt] = [name, subname]
|
---|
[31fb9a0] | 462 | cnt += 1
|
---|
| 463 |
|
---|
[ba8de9c3] | 464 | if not subprofile:
|
---|
[31fb9a0] | 465 | options.append(name)
|
---|
[6ec0acd] | 466 | opt2path[cnt] = [name]
|
---|
[31fb9a0] | 467 | cnt += 1
|
---|
| 468 |
|
---|
| 469 | (button, value) = xtui.choice_window(screen, 'Load preconfigured defaults', 'Choose configuration profile', options, None)
|
---|
| 470 |
|
---|
[ba8de9c3] | 471 | if button == 'cancel':
|
---|
[31fb9a0] | 472 | return None
|
---|
| 473 |
|
---|
[6ec0acd] | 474 | return opt2path[value]
|
---|
| 475 |
|
---|
| 476 | ## Read presets from a configuration profile.
|
---|
| 477 | #
|
---|
[d40ffbb] | 478 | # @param profile Profile to load from (a list of string components)
|
---|
| 479 | # @param config Output configuration
|
---|
[6ec0acd] | 480 | #
|
---|
[d40ffbb] | 481 | def read_presets(profile, config):
|
---|
[6ec0acd] | 482 | path = os.path.join(PRESETS_DIR, profile[0], MAKEFILE)
|
---|
| 483 | read_config(path, config)
|
---|
[d40ffbb] | 484 |
|
---|
[6ec0acd] | 485 | if len(profile) > 1:
|
---|
| 486 | path = os.path.join(PRESETS_DIR, profile[0], profile[1], MAKEFILE)
|
---|
| 487 | read_config(path, config)
|
---|
| 488 |
|
---|
| 489 | ## Parse profile name (relative OS path) into a list of components.
|
---|
| 490 | #
|
---|
[d40ffbb] | 491 | # @param profile_name Relative path (using OS separator)
|
---|
| 492 | # @return List of components
|
---|
[6ec0acd] | 493 | #
|
---|
| 494 | def parse_profile_name(profile_name):
|
---|
| 495 | profile = []
|
---|
[d40ffbb] | 496 |
|
---|
[6ec0acd] | 497 | head, tail = os.path.split(profile_name)
|
---|
| 498 | if head != '':
|
---|
| 499 | profile.append(head)
|
---|
[d40ffbb] | 500 |
|
---|
[6ec0acd] | 501 | profile.append(tail)
|
---|
| 502 | return profile
|
---|
[31fb9a0] | 503 |
|
---|
[98376de] | 504 | def main():
|
---|
[6ec0acd] | 505 | profile = None
|
---|
[62bb73e] | 506 | config = {}
|
---|
| 507 | rules = []
|
---|
[9a0367f] | 508 |
|
---|
[62bb73e] | 509 | # Parse rules file
|
---|
| 510 | parse_rules(RULES_FILE, rules)
|
---|
[9a0367f] | 511 |
|
---|
[421250e] | 512 | # Input configuration file can be specified on command line
|
---|
| 513 | # otherwise configuration from previous run is used.
|
---|
| 514 | if len(sys.argv) >= 4:
|
---|
[6ec0acd] | 515 | profile = parse_profile_name(sys.argv[3])
|
---|
[d40ffbb] | 516 | read_presets(profile, config)
|
---|
[6ec0acd] | 517 | elif os.path.exists(MAKEFILE):
|
---|
| 518 | read_config(MAKEFILE, config)
|
---|
[9a0367f] | 519 |
|
---|
[e3c3172] | 520 | # Default mode: check values and regenerate configuration files
|
---|
[ba8de9c3] | 521 | if (len(sys.argv) >= 3) and (sys.argv[2] == 'default'):
|
---|
[62bb73e] | 522 | if (infer_verify_choices(config, rules)):
|
---|
[1f5c9c96] | 523 | preprocess_config(config, rules)
|
---|
[62bb73e] | 524 | create_output(MAKEFILE, MACROS, config, rules)
|
---|
[9a0367f] | 525 | return 0
|
---|
| 526 |
|
---|
[e3c3172] | 527 | # Hands-off mode: check values and regenerate configuration files,
|
---|
| 528 | # but no interactive fallback
|
---|
| 529 | if (len(sys.argv) >= 3) and (sys.argv[2] == 'hands-off'):
|
---|
| 530 | # We deliberately test sys.argv >= 4 because we do not want
|
---|
| 531 | # to read implicitly any possible previous run configuration
|
---|
| 532 | if len(sys.argv) < 4:
|
---|
| 533 | sys.stderr.write("Configuration error: No presets specified\n")
|
---|
| 534 | return 2
|
---|
| 535 |
|
---|
| 536 | if (infer_verify_choices(config, rules)):
|
---|
[1f5c9c96] | 537 | preprocess_config(config, rules)
|
---|
[e3c3172] | 538 | create_output(MAKEFILE, MACROS, config, rules)
|
---|
| 539 | return 0
|
---|
| 540 |
|
---|
| 541 | sys.stderr.write("Configuration error: The presets are ambiguous\n")
|
---|
| 542 | return 1
|
---|
| 543 |
|
---|
[62bb73e] | 544 | # Check mode: only check configuration
|
---|
[ba8de9c3] | 545 | if (len(sys.argv) >= 3) and (sys.argv[2] == 'check'):
|
---|
| 546 | if infer_verify_choices(config, rules):
|
---|
[48c3d50] | 547 | return 0
|
---|
| 548 | return 1
|
---|
| 549 |
|
---|
[27fb3d6] | 550 | screen = xtui.screen_init()
|
---|
[9a0367f] | 551 | try:
|
---|
| 552 | selname = None
|
---|
[31fb9a0] | 553 | position = None
|
---|
[9a0367f] | 554 | while True:
|
---|
| 555 |
|
---|
[62bb73e] | 556 | # Cancel out all values which have to be deduced
|
---|
| 557 | for varname, vartype, name, choices, cond in rules:
|
---|
[ba8de9c3] | 558 | if (vartype == 'y') and (varname in config) and (config[varname] == '*'):
|
---|
[62bb73e] | 559 | config[varname] = None
|
---|
[81c8d54] | 560 |
|
---|
[9a0367f] | 561 | options = []
|
---|
| 562 | opt2row = {}
|
---|
[31fb9a0] | 563 | cnt = 1
|
---|
| 564 |
|
---|
| 565 | options.append(" --- Load preconfigured defaults ... ")
|
---|
| 566 |
|
---|
[62bb73e] | 567 | for rule in rules:
|
---|
[e4d540b] | 568 | varname, vartype, name, choices, cond = rule
|
---|
[9a0367f] | 569 |
|
---|
[ba8de9c3] | 570 | if cond and (not check_condition(cond, config, rules)):
|
---|
[9a0367f] | 571 | continue
|
---|
| 572 |
|
---|
[ba8de9c3] | 573 | if varname == selname:
|
---|
[9a0367f] | 574 | position = cnt
|
---|
| 575 |
|
---|
[ba8de9c3] | 576 | if not varname in config:
|
---|
[4756634] | 577 | value = None
|
---|
[9a0367f] | 578 | else:
|
---|
[4756634] | 579 | value = config[varname]
|
---|
[9a0367f] | 580 |
|
---|
[d40ffbb] | 581 | if not validate_rule_value(rule, value):
|
---|
[4756634] | 582 | value = None
|
---|
[d40ffbb] | 583 |
|
---|
| 584 | default = get_default_rule(rule)
|
---|
[1f5c9c96] | 585 |
|
---|
[c0bd08d] | 586 | #
|
---|
| 587 | # If we don't have a value but we do have
|
---|
| 588 | # a default, use it.
|
---|
| 589 | #
|
---|
| 590 | if value == None and default != None:
|
---|
| 591 | value = default
|
---|
| 592 | config[varname] = default
|
---|
[d40ffbb] | 593 |
|
---|
| 594 | option = get_rule_option(rule, value)
|
---|
[e4d540b] | 595 | if option != None:
|
---|
| 596 | options.append(option)
|
---|
[8fe3f832] | 597 | else:
|
---|
| 598 | continue
|
---|
[9a0367f] | 599 |
|
---|
[27fb3d6] | 600 | opt2row[cnt] = (varname, vartype, name, choices)
|
---|
[9a0367f] | 601 |
|
---|
| 602 | cnt += 1
|
---|
| 603 |
|
---|
[28f4adb] | 604 | if (position != None) and (position >= len(options)):
|
---|
[31fb9a0] | 605 | position = None
|
---|
| 606 |
|
---|
[27fb3d6] | 607 | (button, value) = xtui.choice_window(screen, 'HelenOS configuration', 'Choose configuration option', options, position)
|
---|
[9a0367f] | 608 |
|
---|
[ba8de9c3] | 609 | if button == 'cancel':
|
---|
[9a0367f] | 610 | return 'Configuration canceled'
|
---|
| 611 |
|
---|
[ba8de9c3] | 612 | if button == 'done':
|
---|
[62bb73e] | 613 | if (infer_verify_choices(config, rules)):
|
---|
[6346efd] | 614 | break
|
---|
| 615 | else:
|
---|
| 616 | xtui.error_dialog(screen, 'Error', 'Some options have still undefined values. These options are marked with the "?" sign.')
|
---|
| 617 | continue
|
---|
| 618 |
|
---|
[ba8de9c3] | 619 | if value == 0:
|
---|
[d40ffbb] | 620 | profile = choose_profile(PRESETS_DIR, MAKEFILE, screen, config)
|
---|
[6ec0acd] | 621 | if profile != None:
|
---|
[d40ffbb] | 622 | read_presets(profile, config)
|
---|
[31fb9a0] | 623 | position = 1
|
---|
| 624 | continue
|
---|
| 625 |
|
---|
| 626 | position = None
|
---|
[ba8de9c3] | 627 | if not value in opt2row:
|
---|
[27fb3d6] | 628 | raise RuntimeError("Error selecting value: %s" % value)
|
---|
| 629 |
|
---|
| 630 | (selname, seltype, name, choices) = opt2row[value]
|
---|
[9a0367f] | 631 |
|
---|
[ba8de9c3] | 632 | if not selname in config:
|
---|
[4756634] | 633 | value = None
|
---|
[27fb3d6] | 634 | else:
|
---|
[4756634] | 635 | value = config[selname]
|
---|
[9a0367f] | 636 |
|
---|
[ba8de9c3] | 637 | if seltype == 'choice':
|
---|
[4756634] | 638 | config[selname] = subchoice(screen, name, choices, value)
|
---|
[ba8de9c3] | 639 | elif (seltype == 'y/n') or (seltype == 'n/y'):
|
---|
| 640 | if config[selname] == 'y':
|
---|
[62bb73e] | 641 | config[selname] = 'n'
|
---|
[9a0367f] | 642 | else:
|
---|
[62bb73e] | 643 | config[selname] = 'y'
|
---|
[9a0367f] | 644 | finally:
|
---|
[27fb3d6] | 645 | xtui.screen_done(screen)
|
---|
[9a0367f] | 646 |
|
---|
[1f5c9c96] | 647 | preprocess_config(config, rules)
|
---|
[62bb73e] | 648 | create_output(MAKEFILE, MACROS, config, rules)
|
---|
[9a0367f] | 649 | return 0
|
---|
[98376de] | 650 |
|
---|
| 651 | if __name__ == '__main__':
|
---|
[43a10c4] | 652 | sys.exit(main())
|
---|