我们从Python开源项目中,提取了以下27个代码示例,用于说明如何使用setuptools.Extension()。
def get_device_extension(dev_name: str, libraries: list, library_dirs: list, include_dirs: list, use_cython=False): try: language = DEVICES[dev_name]["language"] except KeyError: language = "c++" file_ext = "pyx" if use_cython else "cpp" if language == "c++" else "c" cur_dir = os.path.dirname(os.path.realpath(__file__)) if USE_RELATIVE_PATHS: # We need relative paths on windows cpp_file_path = "src/urh/dev/native/lib/{0}.{1}".format(dev_name, file_ext) else: cpp_file_path = os.path.join(cur_dir, "lib", "{0}.{1}".format(dev_name, file_ext)) return Extension("urh.dev.native.lib." + dev_name, [cpp_file_path], libraries=libraries, library_dirs=library_dirs, include_dirs=include_dirs, language=language)
def extension(name): return Extension( 'phorth.' + name, ['phorth/{name}.cc'.format(name=name)], libraries=['py'], library_dirs=['../../c++/libpy'], include_dirs=['../../c++/libpy/include', 'phorth/include'], language='C++', extra_compile_args=[ '-Wall', '-Wextra', '-Wno-unused-parameter', '-Wno-missing-field-initializers', '-Wno-write-strings', '-std=gnu++14', '-O0', ], )
def initialize(self): if not getattr(self, 'initialized', False): self.initialized = True from Cython.Build import cythonize include_dirs = os.environ.get('CYTHON_INCLUDE_DIRS','.').split(':') extension_modules = [ Extension('sharedbuffers.mapped_struct', ['sharedbuffers/mapped_struct.py'], depends = ['sharedbuffers/mapped_struct.pxd']), ] ext_modules = cythonize(extension_modules, include_path = include_dirs) self.extend(ext_modules)
def Extension(*args, **kwds): """ Simple wrapper about distutils.core.Extension that adds additional PyObjC specific flags. """ os_level = get_sdk_level() if os_level is None: os_level = get_os_level() cflags = ["-DPyObjC_BUILD_RELEASE=%02d%02d"%(tuple(map(int, os_level.split('.'))))] ldflags = [] if 'clang' in get_config_var('CC'): cflags.append('-Wno-deprecated-declarations') CFLAGS = get_config_var('CFLAGS') if '-isysroot' not in CFLAGS and os.path.exists('/usr/include/stdio.h'): # We're likely on a system with de Xcode Command Line Tools. # Explicitly use the most recent problems to avoid compile problems. data = os.popen('xcodebuild -version -sdk macosx Path').read() data = data.strip() if data: cflags.append('-isysroot') cflags.append(data) if os_level == '10.4': cflags.append('-DNO_OBJC2_RUNTIME') if 'extra_compile_args' in kwds: kwds['extra_compile_args'] = kwds['extra_compile_args'] + cflags else: kwds['extra_compile_args'] = cflags if 'extra_link_args' in kwds: kwds['extra_link_args'] = kwds['extra_link_args'] + ldflags else: kwds['extra_link_args'] = ldflags return _Extension(*args, **kwds)
def get_cython_extensions(): files = ["capnpy/segment/base.pyx", "capnpy/segment/segment.py", "capnpy/segment/builder.pyx", "capnpy/blob.py", "capnpy/enum.py", "capnpy/visit.py", "capnpy/struct_.py", "capnpy/list.py", "capnpy/type.py", "capnpy/message.py", "capnpy/buffered.py", "capnpy/filelike.py", "capnpy/ptr.pyx", "capnpy/packing.pyx", "capnpy/_hash.pyx", "capnpy/_util.pyx" ] root_dir = os.path.abspath(os.path.dirname(__file__)) capnpy_dir = os.path.join(root_dir, 'capnpy') def getext(fname): extname = fname.replace('/', '.').replace('.pyx', '').replace('.py', '') if DEBUG: extra_compile_args = ['-O0', '-g'] else: extra_compile_args = ['-O3'] return Extension( extname, [fname], include_dirs = [capnpy_dir], extra_compile_args = extra_compile_args, ) return [getext(f) for f in files]
def build_extension(self, ext): """Overridden method. Builds each Extension.""" ext.runtime_library_dirs = self.rpath # Unfortunately things don't work as they should on the Mac... if platform.system() == 'Darwin': for path in self.rpath: ext.extra_link_args.append('-Wl,-rpath,' + path) # Call the base class method. build_ext.build_extension(self, ext)
def run_setup(with_extensions): setup_kwargs_tmp = dict(setup_kwargs) if with_extensions: setup_kwargs_tmp['ext_modules'] = [Extension( 'elasticapm.utils.wrapt._wrappers', ['elasticapm/utils/wrapt/_wrappers.c'] )] setup_kwargs_tmp['cmdclass']['build_ext'] = optional_build_ext setup(**setup_kwargs_tmp) # Figure out if we should build the wrapt C extensions
def define_extensions(use_cython=False): if sys.platform.startswith("win"): # compile args from # https://msdn.microsoft.com/en-us/library/fwkeyyhe.aspx compile_args = ['/O2', '/openmp'] link_args = [] else: compile_args = ['-Wno-unused-function', '-Wno-maybe-uninitialized', '-O3', '-ffast-math'] link_args = [] if use_openmp: compile_args.append("-fopenmp") link_args.append("-fopenmp") src_ext = '.pyx' if use_cython else '.cpp' modules = [Extension("implicit." + name, [os.path.join("implicit", name + src_ext)], language='c++', extra_compile_args=compile_args, extra_link_args=link_args) for name in ['_als', '_nearest_neighbours']] if CUDA: modules.append(Extension("implicit.cuda._cuda", [os.path.join("implicit", "cuda", "_cuda" + src_ext), os.path.join("implicit", "cuda", "als.cu"), os.path.join("implicit", "cuda", "matrix.cu")], language="c++", extra_compile_args=compile_args, extra_link_args=link_args, library_dirs=[CUDA['lib64']], libraries=['cudart', 'cublas'], runtime_library_dirs=[CUDA['lib64']], include_dirs=[CUDA['include'], '.'])) if use_cython: return cythonize(modules) else: return modules # set_gcc copied from glove-python project # https://github.com/maciejkula/glove-python
def ext_modules(): import numpy walks_ext = Extension('jwalk.walks', ['jwalk/src/walks.pyx'], include_dirs=[numpy.get_include()]) return [walks_ext]
def extensions(): from numpy import get_include from Cython.Build import cythonize ext_core = Extension( "pydpc.core", sources=["ext/core.pyx", "ext/_core.c"], include_dirs=[get_include()], extra_compile_args=["-O3", "-std=c99"]) exts = [ext_core] return cythonize(exts)
def extension_modules(): import numpy as np libraries, library_dirs = BuildFortranThenExt.get_library_dirs() extensions = [] for name, dependencies in FORTRAN_MODULES.items(): if name in ('types', 'status'): # No speedup. continue mod_name = 'bezier._{}_speedup'.format(name) path = SPEEDUP_FILENAME.format(name) if BuildFortranThenExt.USE_SHARED_LIBRARY: # Here we don't depend on object files since the functionality # is contained in the shared library. extra_objects = [] else: # NOTE: These may be treated as relative paths and replaced # before the extension is actually built. extra_objects = [ OBJECT_FILENAME.format(dependency) for dependency in dependencies ] # NOTE: Copy ``libraries`` and ``library_dirs`` so they # aren't shared (and mutable) between extensions. extension = setuptools.Extension( mod_name, [path], extra_objects=extra_objects, include_dirs=[ np.get_include(), os.path.join('src', 'bezier', 'include'), ], libraries=copy.deepcopy(libraries), library_dirs=copy.deepcopy(library_dirs), ) extensions.append(extension) return extensions
def stdchannel_redirected(stdchannel): """ Redirects stdout or stderr to a StringIO object. As of python 3.4, there is a standard library contextmanager for this, but backwards compatibility! """ try: s = io.StringIO() old = getattr(sys, stdchannel) setattr(sys, stdchannel, s) yield s finally: setattr(sys, stdchannel, old) # Subclass setuptools Extension to add a parameter specifying where the shared # library should be placed after being compiled
def __init__(self, libdest, *args, **kwargs): self.libdest = libdest setuptools.Extension.__init__(self, *args, **kwargs) # Subclass setuptools build_ext to put the compiled shared library in the # appropriate place in the source tree.
def read(fname): return open(os.path.join(os.path.dirname(__file__), fname)).read() # cos_module_np = Extension('cos_module_np', # sources=['PcgComp/kernels/cos_module_np.c'], # include_dirs=[numpy.get_include()])
def extensions(): try: from Cython.Build import cythonize except ImportError: def cythonize(*args, **kwargs): print("Hint: Wrapping import of cythonize in extensions()") from Cython.Build import cythonize return cythonize(*args, **kwargs) try: import numpy lstIncludes = [numpy.get_include()] except ImportError: lstIncludes = [] extensionArguments = { 'include_dirs': lstIncludes + ['fastmat/core', 'fastmat/inspect', 'util'], 'extra_compile_args': compilerArguments, 'extra_link_args': linkerArguments, 'define_macros': defineMacros } return cythonize( [Extension("*", ["fastmat/*.pyx"], **extensionArguments), Extension("*", ["fastmat/algs/*.pyx"], **extensionArguments), Extension("*", ["fastmat/core/*.pyx"], **extensionArguments)], compiler_directives=cythonDirectives) # determine requirements for install and setup
def add_extensions(setup_path, folder, extensions): """ Adds .pyx files in the specified folder to the list of extensions. """ source_path = path.join(setup_path, folder) for root, dirs, files in os.walk(source_path): for file in files: if path.splitext(file)[1] == ".pyx": pyx_file = path.relpath(path.join(root, file), setup_path) module = path.splitext(pyx_file)[0].replace("/", ".") extensions.append(Extension(module, [pyx_file], include_dirs=compilation_includes),)
def get_ext_modules(): filenames = [os.path.splitext(f)[0] for f in os.listdir("src/urh/cythonext") if f.endswith(EXT)] extensions = [Extension("urh.cythonext." + f, ["src/urh/cythonext/" + f + EXT], extra_compile_args=[OPEN_MP_FLAG], extra_link_args=[OPEN_MP_FLAG], language="c++") for f in filenames] return extensions
def make_cpp_extension_libfwdbwd(): ext = Extension( 'bnpy.allocmodel.hmm.lib.libfwdbwdcpp', sources=['bnpy/allocmodel/hmm/lib/FwdBwdRowMajor.cpp'], include_dirs=[get_path_to_eigen()], ) return ext
def make_cpp_extension_libsparsemix(): ext = Extension( 'bnpy.util.lib.sparseResp.libsparsemix', sources=['bnpy/util/lib/sparseResp/SparsifyRespCPPX.cpp'], include_dirs=[get_path_to_eigen()], ) return ext
def make_cpp_extension_libsparsetopics(): ext = Extension( 'bnpy.util.lib.sparseResp.libsparsetopics', sources=['bnpy/util/lib/sparseResp/TopicModelLocalStepCPPX.cpp'], include_dirs=[get_path_to_eigen(), get_path_to_boost()], ) return ext
def make_cpp_extension_libsparsetopicsmanydocs(): ext = Extension( 'bnpy.util.lib.sparseResp.libsparsetopicsmanydocs', sources=[ 'bnpy/util/lib/sparseResp/TopicModelLocalStepManyDocsCPPX.cpp'], include_dirs=[get_path_to_eigen(), get_path_to_boost()], ) return ext
def make_cython_extension_EntropyUtilX(): ext = Extension( "bnpy.util.EntropyUtilX", sources=["bnpy/util/EntropyUtilX.pyx"], libraries=["m"], extra_compile_args = ["-O3", "-ffast-math"], ) return add_directives_to_cython_ext(ext)
def _extension(modpath): """Make setuptools.Extension.""" return setuptools.Extension(modpath, [modpath.replace('.', '/') + '.py'])
def main(): from imp import load_source from os import path from sys import argv if "--setuptools" in argv: argv.remove("--setuptools") from setuptools import setup, Extension else: from distutils.core import setup, Extension cptrace_ext = Extension('cptrace', sources=SOURCES) cptrace = load_source("version", path.join("cptrace", "version.py")) install_options = { "name": cptrace.PACKAGE, "version": cptrace.VERSION, "url": cptrace.WEBSITE, "download_url": cptrace.WEBSITE, "license": cptrace.LICENSE, "author": "Victor Stinner", "description": "python binding of ptrace written in C", "long_description": LONG_DESCRIPTION, "classifiers": CLASSIFIERS, "ext_modules": [cptrace_ext], } setup(**install_options)
def make_extensions(options, compiler): """Produce a list of Extension instances which passed to cythonize().""" no_cuda = options['no_cuda'] settings = build.get_compiler_setting() include_dirs = settings['include_dirs'] settings['include_dirs'] = [ x for x in include_dirs if path.exists(x)] settings['library_dirs'] = [ x for x in settings['library_dirs'] if path.exists(x)] if sys.platform != 'win32': settings['runtime_library_dirs'] = settings['library_dirs'] if options['linetrace']: settings['define_macros'].append(('CYTHON_TRACE', '1')) settings['define_macros'].append(('CYTHON_TRACE_NOGIL', '1')) if no_cuda: settings['define_macros'].append(('CUPY_NO_CUDA', '1')) ret = [] for module in MODULES: print('Include directories:', settings['include_dirs']) print('Library directories:', settings['library_dirs']) if not no_cuda: if not check_library(compiler, includes=module['include'], include_dirs=settings['include_dirs']): utils.print_warning( 'Include files not found: %s' % module['include'], 'Skip installing %s support' % module['name'], 'Check your CPATH environment variable') continue if not check_library(compiler, libraries=module['libraries'], library_dirs=settings['library_dirs']): utils.print_warning( 'Cannot link libraries: %s' % module['libraries'], 'Skip installing %s support' % module['name'], 'Check your LIBRARY_PATH environment variable') continue if 'check_method' in module and \ not module['check_method'](compiler, settings): continue s = settings.copy() if not no_cuda: s['libraries'] = module['libraries'] ret.extend([ setuptools.Extension(f, [path.join(*f.split('.')) + '.pyx'], **s) for f in module['file']]) return ret
def pkgconfig(*packages, **kw): """ Query pkg-config for library compile and linking options. Return configuration in distutils Extension format. Usage: pkgconfig('opencv') pkgconfig('opencv', 'libavformat') pkgconfig('opencv', optional='--static') pkgconfig('opencv', config=c) returns e.g. {'extra_compile_args': [], 'extra_link_args': [], 'include_dirs': ['/usr/include/ffmpeg'], 'libraries': ['avformat'], 'library_dirs': []} Intended use: distutils.core.Extension('pyextension', sources=['source.cpp'], **c) Set PKG_CONFIG_PATH environment variable for nonstandard library locations. based on work of Micah Dowty (http://code.activestate.com/recipes/502261-python-distutils-pkg-config/) """ config = kw.setdefault('config', {}) optional_args = kw.setdefault('optional', '') # { <distutils Extension arg>: [<pkg config option>, <prefix length to strip>], ...} flag_map = {'include_dirs': ['--cflags-only-I', 2], 'library_dirs': ['--libs-only-L', 2], 'libraries': ['--libs-only-l', 2], 'extra_compile_args': ['--cflags-only-other', 0], 'extra_link_args': ['--libs-only-other', 0], } for package in packages: for distutils_key, (pkg_option, n) in flag_map.items(): items = subprocess.check_output(['pkg-config', optional_args, pkg_option, package]).decode('utf8').split() config.setdefault(distutils_key, []).extend([i[n:] for i in items]) return config