def cpp_evaluator(expr, ns): compiler, extra_compile_args = get_compiler_and_args() library_dirs = prefs['codegen.cpp.library_dirs'] extra_link_args = prefs['codegen.cpp.extra_link_args'] update_for_cross_compilation(library_dirs, extra_compile_args, extra_link_args) with std_silent(): return weave.inline('return_val = %s;' % expr, ns.keys(), local_dict=ns, support_code=CPPCodeGenerator.universal_support_code, compiler=compiler, extra_compile_args=extra_compile_args, extra_link_args=extra_link_args, library_dirs=library_dirs, include_dirs=prefs['codegen.cpp.include_dirs'] )
def cpp_evaluator(expr, ns): compiler, extra_compile_args = get_compiler_and_args() library_dirs = prefs['codegen.cpp.library_dirs'] extra_link_args = prefs['codegen.cpp.extra_link_args'] update_for_cross_compilation(library_dirs, extra_compile_args, extra_link_args) with std_silent(): return weave.inline('return_val = %s;' % expr, ns.keys(), local_dict=ns, support_code=CPPCodeGenerator.universal_support_code, compiler=compiler, extra_compile_args=extra_compile_args, extra_link_args=extra_link_args, library_dirs=library_dirs, include_dirs=prefs['codegen.cpp.include_dirs'] )
def _load_module(self, module_path, define_macros, include_dirs, library_dirs, extra_compile_args, extra_link_args, libraries, code, lib_dir, module_name, runtime_library_dirs, compiler, key, sources): have_module = os.path.isfile(module_path) if not have_module: if define_macros is None: define_macros = [] if include_dirs is None: include_dirs = [] if library_dirs is None: library_dirs = [] if extra_compile_args is None: extra_compile_args = [] if extra_link_args is None: extra_link_args = [] if libraries is None: libraries = [] c_include_dirs = include_dirs if 'numpy' in code: import numpy c_include_dirs.append(numpy.get_include()) # TODO: We should probably have a special folder just for header # files that are shared between different codegen targets import brian2.synapses as synapses synapses_dir = os.path.dirname(synapses.__file__) c_include_dirs.append(synapses_dir) pyx_file = os.path.join(lib_dir, module_name + '.pyx') # ignore Python 3 unicode stuff for the moment #pyx_file = py3compat.cast_bytes_py2(pyx_file, encoding=sys.getfilesystemencoding()) #with io.open(pyx_file, 'w', encoding='utf-8') as f: # f.write(code) with open(pyx_file, 'w') as f: f.write(code) update_for_cross_compilation(library_dirs, extra_compile_args, extra_link_args, logger=logger) for source in sources: if not source.lower().endswith('.pyx'): raise ValueError('Additional Cython source files need to ' 'have an .pyx ending') # Copy source and header file (if present) to library directory shutil.copyfile( source, os.path.join(lib_dir, os.path.basename(source))) name_without_ext = os.path.splitext( os.path.basename(source))[0] header_name = name_without_ext + '.pxd' if os.path.exists( os.path.join(os.path.dirname(source), header_name)): shutil.copyfile( os.path.join(os.path.dirname(source), header_name), os.path.join(lib_dir, header_name)) final_sources = [ os.path.join(lib_dir, os.path.basename(source)) for source in sources ] extension = Extension(name=module_name, sources=[pyx_file], define_macros=define_macros, include_dirs=c_include_dirs, library_dirs=library_dirs, runtime_library_dirs=runtime_library_dirs, extra_compile_args=extra_compile_args, extra_link_args=extra_link_args, libraries=libraries, language='c++') build_extension = self._get_build_extension(compiler=compiler) try: opts = dict( quiet=True, annotate=False, force=True, ) # suppresses the output on stdout with std_silent(): build_extension.extensions = Cython_Build.cythonize( [extension] + final_sources, **opts) build_extension.build_temp = os.path.dirname(pyx_file) build_extension.build_lib = lib_dir build_extension.run() if prefs['codegen.runtime.cython.delete_source_files']: # we can delete the source files to save disk space cpp_file = os.path.join(lib_dir, module_name + '.cpp') try: os.remove(pyx_file) os.remove(cpp_file) temp_dir = os.path.join( lib_dir, os.path.dirname(pyx_file)[1:], module_name + '.*') for fname in glob.glob(temp_dir): os.remove(fname) except (OSError, IOError) as ex: logger.debug( 'Deleting Cython source files failed with error: %s' % str(ex)) except Cython_Compiler.Errors.CompileError: return # Temporarily insert the Cython directory to the Python path so that # code importing from an external module that was declared via # sources works sys.path.insert(0, lib_dir) spec = importlib.util.spec_from_file_location(module_name, module_path) module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) sys.path.pop(0) self._code_cache[key] = module return module
def _load_module(self, module_path, define_macros, include_dirs, library_dirs, extra_compile_args, extra_link_args, libraries, code, lib_dir, module_name, runtime_library_dirs, compiler, key): have_module = os.path.isfile(module_path) if not have_module: if define_macros is None: define_macros = [] if include_dirs is None: include_dirs = [] if library_dirs is None: library_dirs = [] if extra_compile_args is None: extra_compile_args = [] if extra_link_args is None: extra_link_args = [] if libraries is None: libraries = [] c_include_dirs = include_dirs if 'numpy' in code: import numpy c_include_dirs.append(numpy.get_include()) # TODO: We should probably have a special folder just for header # files that are shared between different codegen targets import brian2.synapses as synapses synapses_dir = os.path.dirname(synapses.__file__) c_include_dirs.append(synapses_dir) pyx_file = os.path.join(lib_dir, module_name + '.pyx') # ignore Python 3 unicode stuff for the moment #pyx_file = py3compat.cast_bytes_py2(pyx_file, encoding=sys.getfilesystemencoding()) #with io.open(pyx_file, 'w', encoding='utf-8') as f: # f.write(code) with open(pyx_file, 'w') as f: f.write(code) update_for_cross_compilation(library_dirs, extra_compile_args, extra_link_args, logger=logger) extension = Extension( name=module_name, sources=[pyx_file], define_macros=define_macros, include_dirs=c_include_dirs, library_dirs=library_dirs, runtime_library_dirs=runtime_library_dirs, extra_compile_args=extra_compile_args, extra_link_args=extra_link_args, libraries=libraries, language='c++', ) build_extension = self._get_build_extension(compiler=compiler) try: opts = dict( quiet=True, annotate=False, force=True, ) # suppresses the output on stdout with std_silent(): build_extension.extensions = Cython_Build.cythonize( [extension], **opts) build_extension.build_temp = os.path.dirname(pyx_file) build_extension.build_lib = lib_dir build_extension.run() except Cython_Compiler.Errors.CompileError: return module = imp.load_dynamic(module_name, module_path) self._code_cache[key] = module return module
def _load_module(self, module_path, define_macros, include_dirs, library_dirs, extra_compile_args, extra_link_args, libraries, code, lib_dir, module_name, runtime_library_dirs, compiler, key, sources): have_module = os.path.isfile(module_path) if not have_module: if define_macros is None: define_macros = [] if include_dirs is None: include_dirs = [] if library_dirs is None: library_dirs = [] if extra_compile_args is None: extra_compile_args = [] if extra_link_args is None: extra_link_args = [] if libraries is None: libraries = [] c_include_dirs = include_dirs if 'numpy' in code: import numpy c_include_dirs.append(numpy.get_include()) # TODO: We should probably have a special folder just for header # files that are shared between different codegen targets import brian2.synapses as synapses synapses_dir = os.path.dirname(synapses.__file__) c_include_dirs.append(synapses_dir) pyx_file = os.path.join(lib_dir, module_name + '.pyx') # ignore Python 3 unicode stuff for the moment #pyx_file = py3compat.cast_bytes_py2(pyx_file, encoding=sys.getfilesystemencoding()) #with io.open(pyx_file, 'w', encoding='utf-8') as f: # f.write(code) with open(pyx_file, 'w') as f: f.write(code) update_for_cross_compilation(library_dirs, extra_compile_args, extra_link_args, logger=logger) for source in sources: if not source.lower().endswith('.pyx'): raise ValueError('Additional Cython source files need to ' 'have an .pyx ending') # Copy source and header file (if present) to library directory shutil.copyfile(source, os.path.join(lib_dir, os.path.basename(source))) name_without_ext = os.path.splitext(os.path.basename(source))[0] header_name = name_without_ext + '.pxd' if os.path.exists(os.path.join(os.path.dirname(source), header_name)): shutil.copyfile(os.path.join(os.path.dirname(source), header_name), os.path.join(lib_dir, header_name)) final_sources = [os.path.join(lib_dir, os.path.basename(source)) for source in sources] extension = Extension( name=module_name, sources=[pyx_file], define_macros=define_macros, include_dirs=c_include_dirs, library_dirs=library_dirs, runtime_library_dirs=runtime_library_dirs, extra_compile_args=extra_compile_args, extra_link_args=extra_link_args, libraries=libraries, language='c++') build_extension = self._get_build_extension(compiler=compiler) try: opts = dict( quiet=True, annotate=False, force=True, ) # suppresses the output on stdout with std_silent(): build_extension.extensions = Cython_Build.cythonize([extension] + final_sources, **opts) build_extension.build_temp = os.path.dirname(pyx_file) build_extension.build_lib = lib_dir build_extension.run() if prefs['codegen.runtime.cython.delete_source_files']: # we can delete the source files to save disk space cpp_file = os.path.join(lib_dir, module_name + '.cpp') try: os.remove(pyx_file) os.remove(cpp_file) temp_dir = os.path.join(lib_dir, os.path.dirname(pyx_file)[1:], module_name + '.*') for fname in glob.glob(temp_dir): os.remove(fname) except (OSError, IOError) as ex: logger.debug('Deleting Cython source files failed with error: %s' % str(ex)) except Cython_Compiler.Errors.CompileError: return # Temporarily insert the Cython directory to the Python path so that # code importing from an external module that was declared via # sources works sys.path.insert(0, lib_dir) module = imp.load_dynamic(module_name, module_path) sys.path.pop(0) self._code_cache[key] = module return module
def _load_module(self, module_path, include_dirs, library_dirs, extra_compile_args, extra_link_args, libraries, code, lib_dir, module_name, runtime_library_dirs, compiler, key): have_module = os.path.isfile(module_path) if not have_module: if include_dirs is None: include_dirs = [] if library_dirs is None: library_dirs = [] if extra_compile_args is None: extra_compile_args = [] if extra_link_args is None: extra_link_args = [] if libraries is None: libraries = [] c_include_dirs = include_dirs if 'numpy' in code: import numpy c_include_dirs.append(numpy.get_include()) # TODO: We should probably have a special folder just for header # files that are shared between different codegen targets import brian2.synapses as synapses synapses_dir = os.path.dirname(synapses.__file__) c_include_dirs.append(synapses_dir) pyx_file = os.path.join(lib_dir, module_name + '.pyx') # ignore Python 3 unicode stuff for the moment #pyx_file = py3compat.cast_bytes_py2(pyx_file, encoding=sys.getfilesystemencoding()) #with io.open(pyx_file, 'w') as f:#, encoding='utf-8') as f: # f.write(code) open(pyx_file, 'w').write(code) update_for_cross_compilation(library_dirs, extra_compile_args, extra_link_args, logger=logger) extension = Extension( name=module_name, sources=[pyx_file], include_dirs=c_include_dirs, library_dirs=library_dirs, runtime_library_dirs=runtime_library_dirs, extra_compile_args=extra_compile_args, extra_link_args=extra_link_args, libraries=libraries, language='c++', ) build_extension = self._get_build_extension(compiler=compiler) try: opts = dict( quiet=True, annotate=False, force=True, ) # suppresses the output on stdout with std_silent(): build_extension.extensions = Cython_Build.cythonize([extension], **opts) build_extension.build_temp = os.path.dirname(pyx_file) build_extension.build_lib = lib_dir build_extension.run() except Cython_Compiler.Errors.CompileError: return module = imp.load_dynamic(module_name, module_path) self._code_cache[key] = module return module