def generate_pyx(p, pxd_files): """The main function of this module for generating code This function generates the .pyx file with name given by PYX_FILE_NAME and stores it in the current directory. Cython will use the .pyx file to build a wrapper for all generated C functions. """ from mmgroup.dev.mm_op import mm_op def pyx_comment(text, f): print("\n" + "#"*70 + "\n### %s\n" % text + "#"*70 + "\n\n",file=f) f_pyx = open(os.path.join(PXD_DIR, PYX_FILE_NAME.format(P = p)), "wt") declarations = PYX_DECLARATIONS.format(INT_BITS = INT_BITS, P = 0) print(declarations, file = f_pyx) for pxd_f in pxd_files: pyx_comment("Wrappers for C functions from file %s" % pxd_f, f_pyx) pyx_code = pxd_to_pyx( os.path.join(PXD_DIR, pxd_f), None, translate_pxd, nogil=True ) print(pyx_code, file = f_pyx) pyx_comment("Constants", f_pyx) basis = mm_op.MM_Op(p) for name in MM_BASICS_CONSTANTS: value = getattr(basis, name) declaration = "%s = %d\n" % (name, value) print(declaration, file = f_pyx) f_pyx.close()
def generate_files(): """The main function of this module for generating code This function generates all reqired .c files and also the required headers. It also generates the .pxi file with name given by PYX_FILE_NAME and stores it in the current directory. Cython will use the .pxi file to build a wrapper for all generated C functions. """ def pxi_comment(text, f): print("\n" + "#"*70 + "\n### %s\n" % text + "#"*70 + "\n\n",file=f) c_files, pxd_files = make_reduce() print("Creating %s" % PXI_FILE_NAME) f_pxi = open(os.path.join(PXD_DIR, PXI_FILE_NAME), "wt") print(PXD_DECLARATIONS, file = f_pxi) for pxd_f in pxd_files: pxi_comment("Wrappers for C functions from file %s" % pxd_f, f_pxi) pxi_content = pxd_to_pyx( os.path.join(PXD_DIR, pxd_f), os.path.split(pxd_f)[0] ) print(pxi_content, file = f_pxi) f_pxi.close() print("Code generation for module mm_reduce terminated successfully") return c_files
def generators_make_c_code(): """Create .c and .h file with the functionality of class Mat24Xi """ print("Creating C sources for the 'generators' extension\n") # Setp table and directives for code generation GenXi.tables["GenXi_doc"] = GenXi # can't do this earlier tables = {} directives = {} for table_class in GENERATORS_TABLE_CLASSES: table_instance = table_class() tables.update(table_instance.tables) directives.update(table_instance.directives) print(tables.keys()) tg = TableGenerator(tables, directives) # Generate c files all_ske_files = [ os.path.join(SKE_DIR, name) for name in GENERATORS_H_FILES ] for name in GENERATORS_C_FILES: ske_file = name + ".ske" ske_path = os.path.join(SKE_DIR, ske_file) c_file = name + ".c" c_path = os.path.join(C_DIR, c_file) print("Creating %s from %s" % (c_file, ske_file)) tg.generate(ske_path, c_path) all_ske_files.append(ske_path) # generate .h file all_ske_files.append(GENERATORS_H_END) h_file = H_GENERATORS_NAME h_path = os.path.join(C_DIR, h_file) pxd_file = PXD_GENERATORS_NAME print("Creating %s from previous .ske files" % h_file) tg.generate(all_ske_files, None, h_path) # generate .pxd file tg.generate_pxd(os.path.join(PXD_DIR, PXD_GENERATORS_NAME), h_file, PXD_DECLARATIONS) print("C files for extension 'generators' have been created") # generate .pxi file def pxi_comment(text, f): print("\n" + "#" * 70 + "\n### %s\n" % text + "#" * 70 + "\n\n", file=f) f_pxi = open(os.path.join(PXD_DIR, PXI_GENERATORS_NAME), "wt") pxi_comment("Wrappers for C functions from file %s" % PXD_GENERATORS_NAME, f_pxi) print(PXD_DECLARATIONS, file=f_pxi) pxi_content = pxd_to_pyx(os.path.join(PXD_DIR, PXD_GENERATORS_NAME), os.path.split(PXD_GENERATORS_NAME)[0], select=True) print(pxi_content, file=f_pxi) f_pxi.close()