def write_simulators(self, stream, debug=0, individual=False): ## Emit source macros dontexpand = set( [smac for smac, uses in self.macro_uses.items() if len(uses) > 1]) SU.emit_debug( debug, 2, "{0}: dontexpand {1}".format(self.__class__.__name__, dontexpand)) if len(dontexpand) > 0: smac_sorted = list(dontexpand) smac_sorted.sort() for smac in smac_sorted: stream.write('\n\n') stream.write('set({0}\n'.format(smac)) stream.write('\n'.join( [' ' * 4 + f for f in self.source_macros[smac]])) stream.write(')') stream.write('\n\n') ## Emit the simulators simnames = list(self.simulators.keys()) simnames.sort() SU.emit_debug( debug, 2, "{0}: Writing {1}".format(self.__class__.__name__, simnames)) for simname in simnames: sim = self.simulators[simname] ## Patch up the simulator source lists, expanding macros that aren't ## in the macro sources: sim.sources = self.expand_sources(sim.sources, dontexpand, debug) stream.write('\n') sim.write_simulator(stream, 0, individual)
def get_simulator_vars(self, debug=0): simvars = set() ignored = set(self.source_macros.keys()) for macval in self.source_macros.values(): ## This could be replaced by a functools.reduce() for val in macval: simvars = simvars.union(set(SPM.extract_variables(val))) for sim in self.simulators.values(): simvars = simvars.union(sim.get_source_vars().union( sim.get_include_vars())) simvars = simvars.difference(ignored).difference(SPM._special_vars) SU.emit_debug(debug, 2, 'simvars {0}'.format(simvars)) return simvars
def inner_sources(srcmacro): for v in srcmacro: if not self.source_elems_to_ignore(v): m = SPM._var_rx.match(v) if m is not None: vname = m.group(1) vardef = defs.get(vname) if vardef is not None: ## Convert the macro variable into a list vardef = [ SPM.normalize_variables(v) \ for v in vardef.split() if not self.source_elems_to_ignore(v) ] SU.emit_debug( debug, 3, '{0}source macro: {1} -> {2}'.format( depth, vname, vardef)) simcoll.add_source_macro(vname, vardef, sim) ## Continue into the macro variable's definitions inner_sources(vardef)
def collect_source_macros(self, comp, defs, varname, simcoll, sim, debug=0, depth=''): def inner_sources(srcmacro): for v in srcmacro: if not self.source_elems_to_ignore(v): m = SPM._var_rx.match(v) if m is not None: vname = m.group(1) vardef = defs.get(vname) if vardef is not None: ## Convert the macro variable into a list vardef = [ SPM.normalize_variables(v) \ for v in vardef.split() if not self.source_elems_to_ignore(v) ] SU.emit_debug( debug, 3, '{0}source macro: {1} -> {2}'.format( depth, vname, vardef)) simcoll.add_source_macro(vname, vardef, sim) ## Continue into the macro variable's definitions inner_sources(vardef) vardef = defs.get(varname) if vardef is not None: vardef = [ SPM.normalize_variables(v) \ for v in vardef.split() if not self.source_elems_to_ignore(v) ] SU.emit_debug( debug, 3, '{0}source macro: {1} -> {2}'.format(depth, varname, vardef)) simcoll.add_source_macro(varname, vardef, sim) inner_sources(vardef) SU.emit_debug(debug, 3, '{0}source added: {1}'.format(depth, comp)) sim.add_source(comp) else: print('{0}undefined make macro: {1}'.format(depth, varname))
def expand_sources(self, srcs, dontexpand, debug=0): updated_srcs = [] for src in srcs: SU.emit_debug( debug, 2, "{0}: Source {1}".format(self.__class__.__name__, src)) m = SPM._var_rx.match(src) if m and m[1] not in dontexpand.union(SPM._special_vars): SU.emit_debug( debug, 2, "{0}: Expanding {1}".format(self.__class__.__name__, m[1])) varexp = self.source_macros.get(m[1]) if varexp is not None: updated_srcs.extend(self.source_macros[m[1]]) else: print('!! Could not expand {0}'.format(m[1])) else: updated_srcs.append(src) if updated_srcs == srcs: return srcs else: return self.expand_sources(updated_srcs, dontexpand, debug)
def extract(self, compile_action, test_name, sim_dir, sim_name, defs, debug=0, depth=''): """Extract sources, defines, includes and flags from the simulator's compile action in the makefile. """ sim_dir_path = SPM.expand_vars(sim_dir, defs).replace('./', '') simcoll = self.dirs.get(sim_dir_path) if simcoll is None: simcoll = SC.SimCollection(sim_dir) self.dirs[sim_dir_path] = simcoll sim = simcoll.get_simulator(sim_name, sim_dir, sim_dir_path, test_name) # Remove compile command line elements and do one level of variable expansion, split the resulting # string into a list. all_comps = [] for comp in [ SPM.normalize_variables(act) for act in compile_action.split() if not self.compile_elems_to_ignore(act) ]: all_comps.extend(comp.split()) if debug >= 3: print('{0}all_comps after filtering:'.format(depth)) pprint.pp(all_comps) # Iterate through the final compile component list and extract source files, includes # and defines. # # Deferred: Looking for options that set the i64, z64, video library options. while all_comps: comp = all_comps[0] # print(':: comp {0}'.format(comp)) if self.is_source(comp): # Source file... ## sim.add_source(comp.replace(sim_dir + '/', '')) sim.add_source(comp) elif comp.startswith('-I'): all_comps = self.process_flag(all_comps, defs, sim.add_include, depth) elif comp.startswith('-D'): all_comps = self.process_flag(all_comps, defs, sim.add_define, depth) elif comp.startswith('-L') or comp.startswith('-l'): ## It's a library path or library. Skip. pass else: # Solitary variable expansion? m = SPM._var_rx.match(comp) if m: varname = m.group(1) if varname not in SPM._special_vars: if not self.is_source_macro(comp, defs): expand = [ SPM.normalize_variables(elem) for elem in SPM.shallow_expand_vars( comp, defs).split() if not self.source_elems_to_ignore(elem) ] SU.emit_debug( debug, 3, '{0}var expanded {1} -> {2}'.format( depth, comp, expand)) all_comps[1:] = expand + all_comps[1:] else: ## Source macro self.collect_source_macros(comp, defs, varname, simcoll, sim, debug, depth) else: sim.add_source(comp) else: # Nope. print('{0}unknown component: {1}'.format(depth, comp)) all_comps = all_comps[1:] sim.scan_for_flags(defs) sim.cleanup_defines() if debug >= 2: pprint.pprint(sim)