def evalWeakConditionDontRaise(e, target=None, add_dict=None): """Same as evalWeakCondition() but returns None instead of raising an exception.""" try: errors.pushCtx(e) if 'cond' not in e.props: return 1 condstr = e.props['cond'] typ = mk.evalCondition(condstr, target=target, add_dict=add_dict) # Condition never met when generating this target: if typ == '0': return 0 # Condition always met: elif typ == '1': return 1 else: return None finally: errors.popCtx()
def handleTarget(e): if e.name not in rules: raise ReaderError(e, "unknown target type") rule = rules[e.name] if rule.pseudo and 'id' not in e.props: global _pseudoTargetLastID id = 'pseudotgt%i' % _pseudoTargetLastID _pseudoTargetLastID += 1 else: if 'id' not in e.props: raise ReaderError(e, "target doesn't have id") id = e.props['id'] cond = None if 'cond' in e.props: isCond = 1 # Handle conditional targets: condstr = evalConstExpr(e, e.props['cond']) typ = mk.evalCondition(condstr) # Condition never met, ignore the target: if typ == '0': utils.deadTargets.append(id) return # Condition always met: elif typ == '1': isCond = 0 elif typ != None: raise ReaderError(e, "malformed condition: '%s'" % condstr) if isCond: checkConditionsSupport(e) cond = mk.makeCondition(condstr) if cond == None: raise ReaderError(e, "malformed condition: '%s'" % condstr) tags = rule.getTagsDict() e = applyTemplates(e, rule.getTemplates() + extractTemplates(e, post=0), extractTemplates(e, post=1)) if id in mk.targets: raise ReaderError(e, "duplicate target name '%s'" % id) if 'category' in e.props: try: cats = { 'all' : mk.Target.CATEG_ALL, 'normal' : mk.Target.CATEG_NORMAL, 'automatic' : mk.Target.CATEG_AUTOMATIC } category = cats[e.props['category']] except KeyError: raise ReaderError(e, "unknown category '%s'" % e.props['category']) else: category = mk.Target.CATEG_NORMAL target = mk.Target(e.name, id, cond, rule.pseudo, category) mk.addTarget(target) errors.pushCtx("when processing target '%s' at %s" % (target.id, e.location())) _processTargetNodes(e, target, tags, None) errors.popCtx()
def handleSet(e, target=None, add_dict=None): try: errors.pushCtx("in <set> at %s" % e.location()) name = basename = evalConstExpr(e, e.props['var'], target) if (name in mk.override_vars) and target == None: return # can't change value of variable overriden with -D=xxx doEval = not ('eval' in e.props and e.props['eval'] == '0') overwrite = not ('overwrite' in e.props and e.props['overwrite'] == '0') isCond = (len(e.children) > 0) isMakeVar = 'make_var' in e.props and e.props['make_var'] == '1' value = e.value if 'hints' in e.props: hints = e.props['hints'] else: hints = '' # Handle conditions: if isCond: if e.value: raise ReaderError(e, "cannot set unconditional value when <if> is used") noValueSet = 1 for e_if in e.children: try: errors.pushCtx(e_if) if e_if.name != 'if': raise ReaderError(e_if, "malformed <set> command") # Preprocess always true or always false conditions: condstr = evalConstExpr(e_if, e_if.props['cond'], target=target, add_dict=add_dict) condstr = translateSpecialCondition(e_if, condstr, target) typ = mk.evalCondition(condstr) # Condition never met when generating this target: if typ == '0': if config.debug: print "[dbg] removing never-met condition '%s' for variable '%s'" % (condstr, name) continue # Condition always met: elif typ == '1': if config.debug: print "[dbg] condition '%s' for variable '%s' is always met" % (condstr, name) noValueSet = 0 isCond = 0 value = e_if.value break elif typ != None: raise ReaderError(e, "malformed condition '%s': doesn't evaluate to boolean value" % condstr) cond = mk.makeCondition(condstr) noValueSet = 0 # Real conditions: checkConditionsSupport(e) if 'scope' in e.props: raise ReaderError(e, "conditional variable can't have nondefault scope ('%s')" % e.props['scope']) if target != None: if (not overwrite) and (name in target.vars): return name = '__%s_%s' % (target.id.replace('-','_').replace('.','_').replace('/','_'), basename) mk.setVar(e.props['var'], '$(%s)' % name, eval=0, target=target, add_dict=add_dict, hints=hints) if cond == None: raise ReaderError(e, "malformed condition: '%s': must be constant expression, equality test or conjunction of them" % condstr) if name in mk.cond_vars: if not overwrite: return var = mk.cond_vars[name] else: var = mk.CondVar(name, target) mk.addCondVar(var, hints) if doEval: value = mk.evalExpr(e_if.value,target=target,add_dict=add_dict) else: value = e_if.value var.add(cond, value) finally: errors.popCtx() if noValueSet: isCond = 0 value = '' if isCond: return # Non-conditional variables: if value == None: value = '' if 'append' in e.props and e.props['append'] == '1': doAppend = 1 else: doAppend = 0 if 'prepend' in e.props and e.props['prepend'] == '1': doPrepend = 1 else: doPrepend = 0 store_in = None if 'scope' in e.props: sc = evalConstExpr(e, e.props['scope'], target=target) if sc == 'local': pass elif sc == 'global': store_in = mk.vars else: if sc in mk.targets: store_in = mk.targets[sc].vars else: raise ReaderError(e, "invalid scope '%s': must be 'global', 'local' or target name" % sc) if isMakeVar: if doAppend or store_in != None or not doEval: raise ReaderError(e, "make variable (%s) can't be appended or stored in nondefault scope or not evaluated" % name) mk.setVar(name, value, eval=doEval, target=target, add_dict=add_dict, store_in=store_in, append=doAppend, prepend=doPrepend, overwrite=overwrite, makevar=isMakeVar, hints=hints) finally: errors.popCtx()
def flattenConfig(cfg): # make copy of mk.vars, we'll need to restore it later: orig_vars = mk.vars mk.vars = copy.deepcopy(mk.vars) orig_targets = mk.targets mk.targets = copy.deepcopy(mk.targets) orig_make_vars = mk.make_vars mk.make_vars = {} orig_cond_vars = mk.cond_vars mk.cond_vars = {} if 'configs' in mk.vars: del mk.vars['configs'] for t in mk.targets.values(): if 'configs' in t.vars: del t.vars['configs'] # add option values in this configuration: for opt in cfg: mk.vars[opt] = cfg[opt] # add conditional variables: for cv in orig_cond_vars.values(): mk.vars[cv.name] = '' for val in cv.values: ok = 1 for e in val.cond.exprs: if e.option.values == None and e.option.default != e.value: ok = 0 break if cfg[e.option.name] != e.value: ok = 0 break if not ok: continue mk.vars[cv.name] = val.value break finalize.finalEvaluation() # Remove targets that are not part of this configuration: toDel = [] for t in mk.targets: tar = mk.targets[t] if tar.cond == None: use = '1' else: use = mk.evalCondition(tar.cond.tostr()) assert use != None if use == '0': toDel.append(t) else: orig_targets[t].vars['configs'][__cfg2str(cfg)] = tar.vars for t in toDel: del mk.targets[t] finalize.replaceEscapeSequences() myvars = mk.vars mytgt = mk.targets mk.vars = orig_vars mk.targets = orig_targets mk.cond_vars = orig_cond_vars mk.make_vars = orig_make_vars return (myvars, mytgt)
def flattenConfig(cfg): # make copy of mk.vars, we'll need to restore it later: orig_vars = mk.vars mk.vars = copy.deepcopy(mk.vars) orig_targets = mk.targets mk.targets = copy.deepcopy(mk.targets) orig_make_vars = mk.make_vars mk.make_vars = {} orig_cond_vars = mk.cond_vars mk.cond_vars = {} if "configs" in mk.vars: del mk.vars["configs"] for t in mk.targets.values(): if "configs" in t.vars: del t.vars["configs"] # add option values in this configuration: for opt in cfg: mk.vars[opt] = cfg[opt] # add conditional variables: for cv in orig_cond_vars.values(): mk.vars[cv.name] = "" for val in cv.values: ok = 1 for e in val.cond.exprs: if e.option.values == None and e.option.default != e.value: ok = 0 break if cfg[e.option.name] != e.value: ok = 0 break if not ok: continue mk.vars[cv.name] = val.value break finalize.finalEvaluation() # Remove targets that are not part of this configuration: toDel = [] for t in mk.targets: tar = mk.targets[t] if tar.cond == None: use = "1" else: use = mk.evalCondition(tar.cond.tostr()) assert use != None if use == "0": toDel.append(t) else: orig_targets[t].vars["configs"][__cfg2str(cfg)] = tar.vars for t in toDel: del mk.targets[t] finalize.replaceEscapeSequences() myvars = mk.vars mytgt = mk.targets mk.vars = orig_vars mk.targets = orig_targets mk.cond_vars = orig_cond_vars mk.make_vars = orig_make_vars return (myvars, mytgt)
def handleTarget(e): if e.name not in rules: raise ReaderError(e, "unknown target type") rule = rules[e.name] if rule.pseudo and 'id' not in e.props: global _pseudoTargetLastID id = 'pseudotgt%i' % _pseudoTargetLastID _pseudoTargetLastID += 1 else: if 'id' not in e.props: raise ReaderError(e, "target doesn't have id") id = e.props['id'] cond = None if 'cond' in e.props: isCond = 1 # Handle conditional targets: condstr = evalConstExpr(e, e.props['cond']) typ = mk.evalCondition(condstr) # Condition never met, ignore the target: if typ == '0': utils.deadTargets.append(id) return # Condition always met: elif typ == '1': isCond = 0 elif typ != None: raise ReaderError(e, "malformed condition: '%s'" % condstr) if isCond: checkConditionsSupport(e) cond = mk.makeCondition(condstr) if cond == None: raise ReaderError(e, "malformed condition: '%s'" % condstr) tags = rule.getTagsDict() e = applyTemplates(e, rule.getTemplates() + extractTemplates(e, post=0), extractTemplates(e, post=1)) if id in mk.targets: raise ReaderError(e, "duplicate target name '%s'" % id) if 'category' in e.props: try: cats = { 'all': mk.Target.CATEG_ALL, 'normal': mk.Target.CATEG_NORMAL, 'automatic': mk.Target.CATEG_AUTOMATIC } category = cats[e.props['category']] except KeyError: raise ReaderError(e, "unknown category '%s'" % e.props['category']) else: category = mk.Target.CATEG_NORMAL target = mk.Target(e.name, id, cond, rule.pseudo, category) mk.addTarget(target) errors.pushCtx("when processing target '%s' at %s" % (target.id, e.location())) _processTargetNodes(e, target, tags, None) errors.popCtx()
def handleSet(e, target=None, add_dict=None): try: errors.pushCtx("in <set> at %s" % e.location()) name = basename = evalConstExpr(e, e.props['var'], target) if (name in mk.override_vars) and target == None: return # can't change value of variable overriden with -D=xxx doEval = not ('eval' in e.props and e.props['eval'] == '0') overwrite = not ('overwrite' in e.props and e.props['overwrite'] == '0') isCond = (len(e.children) > 0) isMakeVar = 'make_var' in e.props and e.props['make_var'] == '1' value = e.value if 'hints' in e.props: hints = e.props['hints'] else: hints = '' # Handle conditions: if isCond: if e.value: raise ReaderError( e, "cannot set unconditional value when <if> is used") noValueSet = 1 for e_if in e.children: try: errors.pushCtx(e_if) if e_if.name != 'if': raise ReaderError(e_if, "malformed <set> command") # Preprocess always true or always false conditions: condstr = evalConstExpr(e_if, e_if.props['cond'], target=target, add_dict=add_dict) condstr = translateSpecialCondition(e_if, condstr, target) typ = mk.evalCondition(condstr) # Condition never met when generating this target: if typ == '0': if config.debug: print "[dbg] removing never-met condition '%s' for variable '%s'" % ( condstr, name) continue # Condition always met: elif typ == '1': if config.debug: print "[dbg] condition '%s' for variable '%s' is always met" % ( condstr, name) noValueSet = 0 isCond = 0 value = e_if.value break elif typ != None: raise ReaderError( e, "malformed condition '%s': doesn't evaluate to boolean value" % condstr) cond = mk.makeCondition(condstr) noValueSet = 0 # Real conditions: checkConditionsSupport(e) if 'scope' in e.props: raise ReaderError( e, "conditional variable can't have nondefault scope ('%s')" % e.props['scope']) if target != None: if (not overwrite) and (name in target.vars): return name = '__%s_%s' % (target.id.replace( '-', '_').replace('.', '_').replace('/', '_'), basename) mk.setVar(e.props['var'], '$(%s)' % name, eval=0, target=target, add_dict=add_dict, hints=hints) if cond == None: raise ReaderError( e, "malformed condition: '%s': must be constant expression, equality test or conjunction of them" % condstr) if name in mk.cond_vars: if not overwrite: return var = mk.cond_vars[name] else: var = mk.CondVar(name, target) mk.addCondVar(var, hints) if doEval: value = mk.evalExpr(e_if.value, target=target, add_dict=add_dict) else: value = e_if.value var.add(cond, value) finally: errors.popCtx() if noValueSet: isCond = 0 value = '' if isCond: return # Non-conditional variables: if value == None: value = '' if 'append' in e.props and e.props['append'] == '1': doAppend = 1 else: doAppend = 0 if 'prepend' in e.props and e.props['prepend'] == '1': doPrepend = 1 else: doPrepend = 0 store_in = None if 'scope' in e.props: sc = evalConstExpr(e, e.props['scope'], target=target) if sc == 'local': pass elif sc == 'global': store_in = mk.vars else: if sc in mk.targets: store_in = mk.targets[sc].vars else: raise ReaderError( e, "invalid scope '%s': must be 'global', 'local' or target name" % sc) if isMakeVar: if doAppend or store_in != None or not doEval: raise ReaderError( e, "make variable (%s) can't be appended or stored in nondefault scope or not evaluated" % name) mk.setVar(name, value, eval=doEval, target=target, add_dict=add_dict, store_in=store_in, append=doAppend, prepend=doPrepend, overwrite=overwrite, makevar=isMakeVar, hints=hints) finally: errors.popCtx()