def main(args=sys.argv[1:]): # parse args parser = ArgumentParser() parser.add_argument("--output", "-o", help="Path to output file", required=True) parser.add_argument("--gen_h", "-gen_h", help="Generate gen_knobs.h", action="store_true", default=False) parser.add_argument("--gen_cpp", "-gen_cpp", help="Generate gen_knobs.cpp", action="store_true", required=False) args = parser.parse_args() cur_dir = os.path.dirname(os.path.abspath(__file__)) template_file = os.path.join(cur_dir, 'templates', 'gen_knobs.cpp') if args.gen_h: MakoTemplateWriter.to_file( template_file, args.output, cmdline=sys.argv, filename='gen_knobs', knobs=knob_defs.KNOBS, includes=['core/knobs_init.h', 'common/os.h', 'sstream', 'iomanip'], gen_header=True) if args.gen_cpp: MakoTemplateWriter.to_file( template_file, args.output, cmdline=sys.argv, filename='gen_knobs', knobs=knob_defs.KNOBS, includes=['core/knobs_init.h', 'common/os.h', 'sstream', 'iomanip'], gen_header=False) return 0
def generate_x86_h(output_dir): filename = 'gen_builder_x86.hpp' output_filename = os.path.join(output_dir, filename) functions = [] for inst in intrinsics: #print('Inst: %s, x86: %s numArgs: %d' % (inst[0], inst[1], len(inst[2]))) declargs = 'Value* ' + ', Value* '.join(inst[2]) functions.append({ 'decl': 'Value* %s(%s, const llvm::Twine& name = "")' % (inst[0], declargs), 'args': ', '.join(inst[2]), 'intrin': inst[1], }) MakoTemplateWriter.to_file(template, output_filename, cmdline=sys.argv, comment='x86 intrinsics', filename=filename, functions=functions, isX86=True)
def main(args=sys.argv[1:]): # parse args parser = ArgumentParser() parser.add_argument("--output", "-o", help="Path to output file", required=True) parser.add_argument("--gen_h", "-gen_h", help="Generate gen_knobs.h", action="store_true", default=False) parser.add_argument("--gen_cpp", "-gen_cpp", help="Generate gen_knobs.cpp", action="store_true", required=False) args = parser.parse_args() cur_dir = os.path.dirname(os.path.abspath(__file__)) template_file = os.path.join(cur_dir, 'templates', 'gen_knobs.cpp') if args.gen_h: MakoTemplateWriter.to_file(template_file, args.output, cmdline=sys.argv, filename='gen_knobs', knobs=knob_defs.KNOBS, includes=[ 'core/knobs_init.h', 'common/os.h', 'sstream', 'iomanip' ], gen_header=True) if args.gen_cpp: MakoTemplateWriter.to_file(template_file, args.output, cmdline=sys.argv, filename='gen_knobs', knobs=knob_defs.KNOBS, includes=[ 'core/knobs_init.h', 'common/os.h', 'sstream', 'iomanip' ], gen_header=False) return 0
def generate_gen_h(functions, output_dir): filename = 'gen_builder.hpp' output_filename = os.path.join(output_dir, filename) templfuncs = [] for func in functions: decl = '%s %s(%s)' % (func['return'], func['alias'], func['args']) templfuncs.append({ 'decl': decl, 'intrin': func['name'], 'args': ', '.join(func['arg_names']), }) MakoTemplateWriter.to_file(template, output_filename, cmdline=sys.argv, comment='Builder IR Wrappers', filename=filename, functions=templfuncs, isX86=False)
def generate_gen_h(functions, output_dir): filename = 'gen_builder.hpp' output_filename = os.path.join(output_dir, filename) templfuncs = [] for func in functions: decl = '%s %s(%s)' % (func['return'], func['alias'], func['args']) templfuncs.append({ 'decl' : decl, 'intrin' : func['name'], 'args' : ', '.join(func['arg_names']), }) MakoTemplateWriter.to_file( template, output_filename, cmdline=sys.argv, comment='Builder IR Wrappers', filename=filename, functions=templfuncs, isX86=False)
def generate_x86_h(output_dir): filename = 'gen_builder_x86.hpp' output_filename = os.path.join(output_dir, filename) functions = [] for inst in intrinsics: #print('Inst: %s, x86: %s numArgs: %d' % (inst[0], inst[1], len(inst[2]))) declargs = 'Value* ' + ', Value* '.join(inst[2]) functions.append({ 'decl' : 'Value* %s(%s)' % (inst[0], declargs), 'args' : ', '.join(inst[2]), 'intrin' : inst[1], }) MakoTemplateWriter.to_file( template, output_filename, cmdline=sys.argv, comment='x86 intrinsics', filename=filename, functions=functions, isX86=True)
def main(): # Parse args... parser = ArgumentParser() parser.add_argument('--proto', '-p', help='Path to proto file', required=True) parser.add_argument('--proto_private', '-pp', help='Path to private proto file', required=True) parser.add_argument('--output', '-o', help='Output filename (i.e. event.hpp)', required=True) parser.add_argument('--gen_event_hpp', help='Generate event header', action='store_true', default=False) parser.add_argument('--gen_event_cpp', help='Generate event cpp', action='store_true', default=False) parser.add_argument('--gen_eventhandler_hpp', help='Generate eventhandler header', action='store_true', default=False) parser.add_argument('--gen_eventhandlerfile_hpp', help='Generate eventhandler header for writing to files', action='store_true', default=False) args = parser.parse_args() proto_filename = args.proto proto_private_filename = args.proto_private (output_dir, output_filename) = os.path.split(args.output) if not output_dir: output_dir = '.' #print('output_dir = %s' % output_dir, file=sys.stderr) #print('output_filename = %s' % output_filename, file=sys.stderr) if not os.path.exists(proto_filename): print('Error: Could not find proto file %s' % proto_filename, file=sys.stderr) return 1 if not os.path.exists(proto_private_filename): print('Error: Could not find private proto file %s' % proto_private_filename, file=sys.stderr) return 1 protos = {} protos['events'] = {} # event dictionary containing events with their fields protos['event_names'] = [] # needed to keep events in order parsed. dict is not ordered. protos['enums'] = {} protos['enum_names'] = [] parse_protos(protos, proto_filename) parse_protos(protos, proto_private_filename) # Generate event header if args.gen_event_hpp: curdir = os.path.dirname(os.path.abspath(__file__)) template_file = os.sep.join([curdir, 'templates', 'gen_ar_event.hpp']) output_fullpath = os.sep.join([output_dir, output_filename]) MakoTemplateWriter.to_file(template_file, output_fullpath, cmdline=sys.argv, filename=output_filename, protos=protos) # Generate event implementation if args.gen_event_cpp: curdir = os.path.dirname(os.path.abspath(__file__)) template_file = os.sep.join([curdir, 'templates', 'gen_ar_event.cpp']) output_fullpath = os.sep.join([output_dir, output_filename]) MakoTemplateWriter.to_file(template_file, output_fullpath, cmdline=sys.argv, filename=output_filename, protos=protos) # Generate event handler header if args.gen_eventhandler_hpp: curdir = os.path.dirname(os.path.abspath(__file__)) template_file = os.sep.join([curdir, 'templates', 'gen_ar_eventhandler.hpp']) output_fullpath = os.sep.join([output_dir, output_filename]) MakoTemplateWriter.to_file(template_file, output_fullpath, cmdline=sys.argv, filename=output_filename, event_header='gen_ar_event.hpp', protos=protos) # Generate event handler header if args.gen_eventhandlerfile_hpp: curdir = os.path.dirname(os.path.abspath(__file__)) template_file = os.sep.join([curdir, 'templates', 'gen_ar_eventhandlerfile.hpp']) output_fullpath = os.sep.join([output_dir, output_filename]) MakoTemplateWriter.to_file(template_file, output_fullpath, cmdline=sys.argv, filename=output_filename, event_header='gen_ar_eventhandler.hpp', protos=protos) return 0
def gen_llvm_types(input_file, output_file): lines = input_file.readlines() types = [] for idx in range(len(lines)): line = lines[idx].rstrip() if 'gen_llvm_types FINI' in line: break match = re.match(r'(\s*)struct(\s*)(\w+)', line) if match: llvm_args = [] # Detect start of structure is_fwd_decl = re.search(r';', line) if not is_fwd_decl: # Extract the command name struct_name = match.group(3).strip() type_entry = { 'name': struct_name, 'members': [], } end_of_struct = False while not end_of_struct and idx < len(lines) - 1: idx += 1 line = lines[idx].rstrip() is_llvm_typedef = re.search(r'@llvm_typedef', line) if is_llvm_typedef is not None: is_llvm_typedef = True else: is_llvm_typedef = False ########################################### # Is field a llvm struct? Tells script to treat type as array of bytes that is size of structure. is_llvm_struct = re.search(r'@llvm_struct', line) if is_llvm_struct is not None: is_llvm_struct = True else: is_llvm_struct = False ########################################### # Is field the start of a function? Tells script to ignore it is_llvm_func_start = re.search(r'@llvm_func_start', line) if is_llvm_func_start is not None: while not end_of_struct and idx < len(lines) - 1: idx += 1 line = lines[idx].rstrip() is_llvm_func_end = re.search( r'@llvm_func_end', line) if is_llvm_func_end is not None: break continue ########################################### # Is field a function? Tells script to ignore it is_llvm_func = re.search(r'@llvm_func', line) if is_llvm_func is not None: continue ########################################### # Is field a llvm enum? Tells script to treat type as an enum and replaced with uint32 type. is_llvm_enum = re.search(r'@llvm_enum', line) if is_llvm_enum is not None: is_llvm_enum = True else: is_llvm_enum = False ########################################### # Is field a llvm function pointer? Tells script to treat type as an enum and replaced with uint32 type. is_llvm_pfn = re.search(r'@llvm_pfn', line) if is_llvm_pfn is not None: is_llvm_pfn = True else: is_llvm_pfn = False ########################################### # Is field const? is_const = re.search(r'\s+const\s+', line) if is_const is not None: is_const = True else: is_const = False ########################################### # Is field a pointer? is_pointer_pointer = re.search('\*\*', line) if is_pointer_pointer is not None: is_pointer_pointer = True else: is_pointer_pointer = False ########################################### # Is field a pointer? is_pointer = re.search('\*', line) if is_pointer is not None: is_pointer = True else: is_pointer = False ########################################### # Is field an array of arrays? # TODO: Can add this to a list. is_array_array = re.search('\[(\w*)\]\[(\w*)\]', line) array_count = '0' array_count1 = '0' if is_array_array is not None: array_count = is_array_array.group(1) array_count1 = is_array_array.group(2) is_array_array = True else: is_array_array = False ########################################### # Is field an array? is_array = re.search('\[(\w*)\]', line) if is_array is not None: array_count = is_array.group(1) is_array = True else: is_array = False is_scoped = re.search('::', line) if is_scoped is not None: is_scoped = True else: is_scoped = False type = None name = None if is_const and is_pointer: if is_scoped: field_match = re.match( r'(\s*)(\w+\<*\w*\>*)(\s+)(\w+::)(\w+)(\s*\**\s*)(\w+)', line) type = '%s%s' % (field_match.group(4), field_match.group(5)) name = field_match.group(7) else: field_match = re.match( r'(\s*)(\w+\<*\w*\>*)(\s+)(\w+)(\s*\**\s*)(\w+)', line) type = field_match.group(4) name = field_match.group(6) elif is_pointer: field_match = re.match( r'(\s*)(\s+)(\w+\<*\w*\>*)(\s*\**\s*)(\w+)', line) if field_match: type = field_match.group(3) name = field_match.group(5) elif is_const: field_match = re.match( r'(\s*)(\w+\<*\w*\>*)(\s+)(\w+)(\s*)(\w+)', line) if field_match: type = field_match.group(4) name = field_match.group(6) else: if is_scoped: field_match = re.match( r'\s*(\w+\<*\w*\>*)\s*::\s*(\w+\<*\w*\>*)\s+(\w+)', line) if field_match: type = field_match.group( 1) + '::' + field_match.group(2) name = field_match.group(3) else: field_match = re.match( r'(\s*)(\w+\<*\w*\>*)(\s+)(\w+)', line) if field_match: type = field_match.group(2) name = field_match.group(4) if is_llvm_typedef is False: if type is not None: type_entry['members'].append( gen_llvm_type(type, name, is_pointer, is_pointer_pointer, is_array, is_array_array, array_count, array_count1, is_llvm_struct, is_llvm_enum, is_llvm_pfn, output_file)) # Detect end of structure end_of_struct = re.match(r'(\s*)};', line) if end_of_struct: types.append(type_entry) cur_dir = os.path.dirname(os.path.abspath(__file__)) template = os.path.join(cur_dir, 'templates', 'gen_llvm.hpp') MakoTemplateWriter.to_file(template, output_file, cmdline=sys.argv, filename=os.path.basename(output_file), types=types)
def main(args=sys.argv[1:]): thisDir = os.path.dirname(os.path.realpath(__file__)) parser = ArgumentParser('Generate files and initialization functions for all permutuations of BackendPixelRate.') parser.add_argument('--dim', help='gBackendPixelRateTable array dimensions', nargs='+', type=int, required=True) parser.add_argument('--outdir', help='output directory', nargs='?', type=str, default=thisDir) parser.add_argument('--split', help='how many lines of initialization per file [0=no split]', nargs='?', type=int, default='512') parser.add_argument('--numfiles', help='how many output files to generate', nargs='?', type=int, default='0') parser.add_argument('--cpp', help='Generate cpp file(s)', action='store_true', default=False) parser.add_argument('--hpp', help='Generate hpp file', action='store_true', default=False) parser.add_argument('--cmake', help='Generate cmake file', action='store_true', default=False) parser.add_argument('--rast', help='Generate rasterizer functions instead of normal backend', action='store_true', default=False) args = parser.parse_args(args) class backendStrs : def __init__(self) : self.outFileName = 'gen_BackendPixelRate%s.cpp' self.outHeaderName = 'gen_BackendPixelRate.hpp' self.functionTableName = 'gBackendPixelRateTable' self.funcInstanceHeader = ' = BackendPixelRate<SwrBackendTraits<' self.template = 'gen_backend.cpp' self.hpp_template = 'gen_header_init.hpp' self.cmakeFileName = 'gen_backends.cmake' self.cmakeSrcVar = 'GEN_BACKEND_SOURCES' self.tableName = 'BackendPixelRate' if args.rast: self.outFileName = 'gen_rasterizer%s.cpp' self.outHeaderName = 'gen_rasterizer.hpp' self.functionTableName = 'gRasterizerFuncs' self.funcInstanceHeader = ' = RasterizeTriangle<RasterizerTraits<' self.template = 'gen_rasterizer.cpp' self.cmakeFileName = 'gen_rasterizer.cmake' self.cmakeSrcVar = 'GEN_RASTERIZER_SOURCES' self.tableName = 'RasterizerFuncs' backend = backendStrs() output_list = [] for x in args.dim: output_list.append(list(range(x))) # generate all permutations possible for template parameter inputs output_combinations = list(itertools.product(*output_list)) output_list = [] # for each permutation for x in range(len(output_combinations)): # separate each template peram into its own list member new_list = [output_combinations[x][i] for i in range(len(output_combinations[x]))] tempStr = backend.functionTableName #print each list member as an index in the multidimensional array for i in new_list: tempStr += '[' + str(i) + ']' #map each entry in the permuation as its own string member, store as the template instantiation string tempStr += backend.funcInstanceHeader + ','.join(map(str, output_combinations[x])) + '>>;' #append the line of c++ code in the list of output lines output_list.append(tempStr) # how many files should we split the global template initialization into? if (args.split == 0): numFiles = 1 else: numFiles = (len(output_list) + args.split - 1) // args.split if (args.numfiles != 0): numFiles = args.numfiles linesPerFile = (len(output_list) + numFiles - 1) // numFiles chunkedList = [output_list[x:x+linesPerFile] for x in range(0, len(output_list), linesPerFile)] # generate .cpp files if args.cpp: baseCppName = os.path.join(args.outdir, backend.outFileName) templateCpp = os.path.join(thisDir, 'templates', backend.template) for fileNum in range(numFiles): filename = baseCppName % str(fileNum) MakoTemplateWriter.to_file( templateCpp, baseCppName % str(fileNum), cmdline=sys.argv, fileNum=fileNum, funcList=chunkedList[fileNum]) if args.hpp: baseHppName = os.path.join(args.outdir, backend.outHeaderName) templateHpp = os.path.join(thisDir, 'templates', backend.hpp_template) MakoTemplateWriter.to_file( templateHpp, baseHppName, cmdline=sys.argv, numFiles=numFiles, filename=backend.outHeaderName, tableName=backend.tableName) # generate gen_backend.cmake file if args.cmake: templateCmake = os.path.join(thisDir, 'templates', 'gen_backend.cmake') cmakeFile = os.path.join(args.outdir, backend.cmakeFileName) MakoTemplateWriter.to_file( templateCmake, cmakeFile, cmdline=sys.argv, srcVar=backend.cmakeSrcVar, numFiles=numFiles, baseCppName='${RASTY_GEN_SRC_DIR}/backends/' + os.path.basename(baseCppName)) return 0
def gen_llvm_types(input_file, output_file): lines = input_file.readlines() types = [] for idx in range(len(lines)): line = lines[idx].rstrip() if 'gen_llvm_types FINI' in line: break match = re.match(r'(\s*)struct(\s*)(\w+)', line) if match: llvm_args = [] # Detect start of structure is_fwd_decl = re.search(r';', line) if not is_fwd_decl: # Extract the command name struct_name = match.group(3).strip() type_entry = { 'name' : struct_name, 'members' : [], } end_of_struct = False while not end_of_struct and idx < len(lines)-1: idx += 1 line = lines[idx].rstrip() is_llvm_typedef = re.search(r'@llvm_typedef', line) if is_llvm_typedef is not None: is_llvm_typedef = True else: is_llvm_typedef = False ########################################### # Is field a llvm struct? Tells script to treat type as array of bytes that is size of structure. is_llvm_struct = re.search(r'@llvm_struct', line) if is_llvm_struct is not None: is_llvm_struct = True else: is_llvm_struct = False ########################################### # Is field the start of a function? Tells script to ignore it is_llvm_func_start = re.search(r'@llvm_func_start', line) if is_llvm_func_start is not None: while not end_of_struct and idx < len(lines)-1: idx += 1 line = lines[idx].rstrip() is_llvm_func_end = re.search(r'@llvm_func_end', line) if is_llvm_func_end is not None: break; continue ########################################### # Is field a function? Tells script to ignore it is_llvm_func = re.search(r'@llvm_func', line) if is_llvm_func is not None: continue ########################################### # Is field a llvm enum? Tells script to treat type as an enum and replaced with uint32 type. is_llvm_enum = re.search(r'@llvm_enum', line) if is_llvm_enum is not None: is_llvm_enum = True else: is_llvm_enum = False ########################################### # Is field a llvm function pointer? Tells script to treat type as an enum and replaced with uint32 type. is_llvm_pfn = re.search(r'@llvm_pfn', line) if is_llvm_pfn is not None: is_llvm_pfn = True else: is_llvm_pfn = False ########################################### # Is field const? is_const = re.search(r'\s+const\s+', line) if is_const is not None: is_const = True else: is_const = False ########################################### # Is field a pointer? is_pointer_pointer = re.search('\*\*', line) if is_pointer_pointer is not None: is_pointer_pointer = True else: is_pointer_pointer = False ########################################### # Is field a pointer? is_pointer = re.search('\*', line) if is_pointer is not None: is_pointer = True else: is_pointer = False ########################################### # Is field an array of arrays? # TODO: Can add this to a list. is_array_array = re.search('\[(\w*)\]\[(\w*)\]', line) array_count = '0' array_count1 = '0' if is_array_array is not None: array_count = is_array_array.group(1) array_count1 = is_array_array.group(2) is_array_array = True else: is_array_array = False ########################################### # Is field an array? is_array = re.search('\[(\w*)\]', line) if is_array is not None: array_count = is_array.group(1) is_array = True else: is_array = False is_scoped = re.search('::', line) if is_scoped is not None: is_scoped = True else: is_scoped = False type = None name = None if is_const and is_pointer: if is_scoped: field_match = re.match(r'(\s*)(\w+\<*\w*\>*)(\s+)(\w+::)(\w+)(\s*\**\s*)(\w+)', line) type = '%s%s' % (field_match.group(4), field_match.group(5)) name = field_match.group(7) else: field_match = re.match(r'(\s*)(\w+\<*\w*\>*)(\s+)(\w+)(\s*\**\s*)(\w+)', line) type = field_match.group(4) name = field_match.group(6) elif is_pointer: field_match = re.match(r'(\s*)(\s+)(\w+\<*\w*\>*)(\s*\**\s*)(\w+)', line) if field_match: type = field_match.group(3) name = field_match.group(5) elif is_const: field_match = re.match(r'(\s*)(\w+\<*\w*\>*)(\s+)(\w+)(\s*)(\w+)', line) if field_match: type = field_match.group(4) name = field_match.group(6) else: if is_scoped: field_match = re.match(r'\s*(\w+\<*\w*\>*)\s*::\s*(\w+\<*\w*\>*)\s+(\w+)', line) if field_match: type = field_match.group(1) + '::' + field_match.group(2) name = field_match.group(3) else: field_match = re.match(r'(\s*)(\w+\<*\w*\>*)(\s+)(\w+)', line) if field_match: type = field_match.group(2) name = field_match.group(4) if is_llvm_typedef is False: if type is not None: type_entry['members'].append( gen_llvm_type( type, name, is_pointer, is_pointer_pointer, is_array, is_array_array, array_count, array_count1, is_llvm_struct, is_llvm_enum, is_llvm_pfn, output_file)) # Detect end of structure end_of_struct = re.match(r'(\s*)};', line) if end_of_struct: types.append(type_entry) cur_dir = os.path.dirname(os.path.abspath(__file__)) template = os.path.join(cur_dir, 'templates', 'gen_llvm.hpp') MakoTemplateWriter.to_file( template, output_file, cmdline=sys.argv, filename=os.path.basename(output_file), types=types)
def main(): # Parse args... parser = ArgumentParser() parser.add_argument('--proto', '-p', help='Path to proto file', required=True) parser.add_argument('--output', '-o', help='Output filename (i.e. event.hpp)', required=True) parser.add_argument('--gen_event_hpp', help='Generate event header', action='store_true', default=False) parser.add_argument('--gen_event_cpp', help='Generate event cpp', action='store_true', default=False) parser.add_argument('--gen_eventhandler_hpp', help='Generate eventhandler header', action='store_true', default=False) parser.add_argument( '--gen_eventhandlerfile_hpp', help='Generate eventhandler header for writing to files', action='store_true', default=False) args = parser.parse_args() proto_filename = args.proto (output_dir, output_filename) = os.path.split(args.output) if not output_dir: output_dir = '.' #print('output_dir = %s' % output_dir, file=sys.stderr) #print('output_filename = %s' % output_filename, file=sys.stderr) if not os.path.exists(proto_filename): print('Error: Could not find proto file %s' % proto_filename, file=sys.stderr) return 1 protos = parse_protos(proto_filename) # Generate event header if args.gen_event_hpp: curdir = os.path.dirname(os.path.abspath(__file__)) template_file = os.sep.join([curdir, 'templates', 'gen_ar_event.hpp']) output_fullpath = os.sep.join([output_dir, output_filename]) MakoTemplateWriter.to_file(template_file, output_fullpath, cmdline=sys.argv, filename=output_filename, protos=protos) # Generate event implementation if args.gen_event_cpp: curdir = os.path.dirname(os.path.abspath(__file__)) template_file = os.sep.join([curdir, 'templates', 'gen_ar_event.cpp']) output_fullpath = os.sep.join([output_dir, output_filename]) MakoTemplateWriter.to_file(template_file, output_fullpath, cmdline=sys.argv, filename=output_filename, protos=protos) # Generate event handler header if args.gen_eventhandler_hpp: curdir = os.path.dirname(os.path.abspath(__file__)) template_file = os.sep.join( [curdir, 'templates', 'gen_ar_eventhandler.hpp']) output_fullpath = os.sep.join([output_dir, output_filename]) MakoTemplateWriter.to_file(template_file, output_fullpath, cmdline=sys.argv, filename=output_filename, event_header='gen_ar_event.hpp', protos=protos) # Generate event handler header if args.gen_eventhandlerfile_hpp: curdir = os.path.dirname(os.path.abspath(__file__)) template_file = os.sep.join( [curdir, 'templates', 'gen_ar_eventhandlerfile.hpp']) output_fullpath = os.sep.join([output_dir, output_filename]) MakoTemplateWriter.to_file(template_file, output_fullpath, cmdline=sys.argv, filename=output_filename, event_header='gen_ar_eventhandler.hpp', protos=protos) return 0
def main(args=sys.argv[1:]): thisDir = os.path.dirname(os.path.realpath(__file__)) parser = ArgumentParser( "Generate files and initialization functions for all permutuations of BackendPixelRate." ) parser.add_argument('--dim', help="gBackendPixelRateTable array dimensions", nargs='+', type=int, required=True) parser.add_argument('--outdir', help="output directory", nargs='?', type=str, default=thisDir) parser.add_argument( '--split', help="how many lines of initialization per file [0=no split]", nargs='?', type=int, default='512') parser.add_argument('--cpp', help="Generate cpp file(s)", action='store_true', default=False) parser.add_argument('--cmake', help="Generate cmake file", action='store_true', default=False) args = parser.parse_args(args) class backendStrs: def __init__(self): self.outFileName = 'gen_BackendPixelRate%s.cpp' self.functionTableName = 'gBackendPixelRateTable' self.funcInstanceHeader = ' = BackendPixelRate<SwrBackendTraits<' self.template = 'gen_backend.cpp' self.cmakeFileName = 'gen_backends.cmake' self.cmakeSrcVar = 'GEN_BACKEND_SOURCES' backend = backendStrs() output_list = [] for x in args.dim: output_list.append(list(range(x))) # generate all permutations possible for template parameter inputs output_combinations = list(itertools.product(*output_list)) output_list = [] # for each permutation for x in range(len(output_combinations)): # separate each template peram into its own list member new_list = [ output_combinations[x][i] for i in range(len(output_combinations[x])) ] tempStr = backend.functionTableName #print each list member as an index in the multidimensional array for i in new_list: tempStr += '[' + str(i) + ']' #map each entry in the permuation as its own string member, store as the template instantiation string tempStr += backend.funcInstanceHeader + ','.join( map(str, output_combinations[x])) + '>>;' #append the line of c++ code in the list of output lines output_list.append(tempStr) # how many files should we split the global template initialization into? if (args.split == 0): numFiles = 1 else: numFiles = (len(output_list) + args.split - 1) // args.split linesPerFile = (len(output_list) + numFiles - 1) // numFiles chunkedList = [ output_list[x:x + linesPerFile] for x in range(0, len(output_list), linesPerFile) ] # generate .cpp files if args.cpp: baseCppName = os.path.join(args.outdir, backend.outFileName) templateCpp = os.path.join(thisDir, 'templates', backend.template) for fileNum in range(numFiles): filename = baseCppName % str(fileNum) #print('Generating', filename) MakoTemplateWriter.to_file(templateCpp, baseCppName % str(fileNum), cmdline=sys.argv, fileNum=fileNum, funcList=chunkedList[fileNum]) # generate gen_backend.cmake file if args.cmake: templateCmake = os.path.join(thisDir, 'templates', 'gen_backend.cmake') cmakeFile = os.path.join(args.outdir, backend.cmakeFileName) #print('Generating', cmakeFile) MakoTemplateWriter.to_file( templateCmake, cmakeFile, cmdline=sys.argv, srcVar=backend.cmakeSrcVar, numFiles=numFiles, baseCppName='${RASTY_GEN_SRC_DIR}/backends/' + os.path.basename(baseCppName)) #print("Generated %d template instantiations in %d files" % (len(output_list), numFiles)) return 0