def __init__(self, name, sources, **kwargs): # the goal is to rely on original Extension # to do so we convert the .py to .cpp with pythran # and register the .cpp in place of the .py # That's stage 0, and it's enough if you get the source # from github and `python setup.py install it` # # *But* if you want to distribute the source through # `python setup.py sdist` then the .py no longer exists # and only the .cpp is distributed. That's stage 1 cxx_sources = [] for source in sources: base, ext = os.path.splitext(source) output_file = base + '.cpp' # target name # stage 0 when we have the .py if os.path.exists(source): stage = 0 # stage 1 otherwise. `.cpp' should already be there # as generated upon stage 0 else: assert os.path.exists(output_file) stage = 1 ext = '.cpp' source = output_file # stage-dependant processing if stage == 0: tc.compile_pythranfile(source, output_file, module_name=name, cpponly=True) cxx_sources.append(output_file) extension_args = cfg.make_extension() Extension.__init__(self, name, cxx_sources, **extension_args)
def __init__(self, name, sources, *args, **kwargs): cfg_ext = cfg.make_extension(python=True, **kwargs) self.cxx = cfg_ext.pop('cxx', None) self.cc = cfg_ext.pop('cc', None) self._sources = sources Extension.__init__(self, name, sources, *args, **cfg_ext) self.__dict__.pop("sources", None)
def compile_cxxfile(module_name, cxxfile, output_binary=None, **kwargs): '''c++ file -> native module Return the filename of the produced shared library Raises CompileError on failure ''' builddir = mkdtemp() buildtmp = mkdtemp() extension_args = make_extension(python=True, **kwargs) extension = PythranExtension(module_name, [cxxfile], **extension_args) try: setup(name=module_name, ext_modules=[extension], cmdclass={"build_ext": PythranBuildExt}, # fake CLI call script_name='setup.py', script_args=['--verbose' if logger.isEnabledFor(logging.INFO) else '--quiet', 'build_ext', '--build-lib', builddir, '--build-temp', buildtmp] ) except SystemExit as e: raise CompileError(str(e)) def copy(src_file, dest_file): # not using shutil.copy because it fails to copy stat across devices with open(src_file, 'rb') as src: with open(dest_file, 'wb') as dest: dest.write(src.read()) ext = sysconfig.get_config_var('SO') # Copy all generated files including the module name prefix (.pdb, ...) for f in glob.glob(os.path.join(builddir, module_name + "*")): if f.endswith(ext): if not output_binary: output_binary = os.path.join(os.getcwd(), module_name + ext) copy(f, output_binary) else: if not output_binary: output_directory = os.getcwd() else: output_directory = os.path.dirname(output_binary) copy(f, os.path.join(output_directory, os.path.basename(f))) shutil.rmtree(builddir) shutil.rmtree(buildtmp) logger.info("Generated module: " + module_name) logger.info("Output: " + output_binary) return output_binary
def compile_cxxfile(cxxfile, module_so=None, **kwargs): '''c++ file -> native module Return the filename of the produced shared library Raises CompileError on failure ''' if module_so: module_name, _ = os.path.splitext(os.path.basename(module_so)) else: module_name, _ = os.path.splitext(os.path.basename(cxxfile)) builddir = mkdtemp() buildtmp = mkdtemp() extension_args = make_extension(**kwargs) extension = Extension(module_name, [cxxfile], language="c++", **extension_args) try: setup(name=module_name, ext_modules=[extension], # fake CLI call script_name='setup.py', script_args=['--verbose' if logger.isEnabledFor(logging.INFO) else '--quiet', 'build_ext', '--build-lib', builddir, '--build-temp', buildtmp, ] ) except SystemExit as e: raise CompileError(e.args) [target] = glob.glob(os.path.join(builddir, module_name + "*")) if module_so: shutil.move(target, module_so) else: shutil.move(target, os.getcwd()) module_so = os.path.join(os.getcwd(), os.path.basename(target)) shutil.rmtree(builddir) shutil.rmtree(buildtmp) logger.info("Generated module: " + module_name) logger.info("Output: " + module_so) return module_so
def compile_cxxfile(module_name, cxxfile, output_binary=None, **kwargs): '''c++ file -> native module Return the filename of the produced shared library Raises CompileError on failure ''' builddir = mkdtemp() buildtmp = mkdtemp() extension_args = make_extension(python=True, **kwargs) extension = PythranExtension(module_name, [cxxfile], **extension_args) try: setup(name=module_name, ext_modules=[extension], cmdclass={"build_ext": PythranBuildExt}, # fake CLI call script_name='setup.py', script_args=['--verbose' if logger.isEnabledFor(logging.INFO) else '--quiet', 'build_ext', '--build-lib', builddir, '--build-temp', buildtmp] ) except SystemExit as e: raise CompileError(str(e)) target, = glob.glob(os.path.join(builddir, module_name + "*")) if not output_binary: output_binary = os.path.join(os.getcwd(), module_name + os.path.splitext(target)[1]) # not using shutil.copy because it fails to copy stat across devices with open(target, 'rb') as src: with open(output_binary, 'wb') as dest: dest.write(src.read()) shutil.rmtree(builddir) shutil.rmtree(buildtmp) logger.info("Generated module: " + module_name) logger.info("Output: " + output_binary) return output_binary
def __init__(self, name, sources, *args, **kwargs): # the goal is to rely on original Extension # to do so we convert the .py to .cpp with pythran # and register the .cpp in place of the .py # That's stage 0, and it's enough if you get the source # from github and `python setup.py install it` # # *But* if you want to distribute the source through # `python setup.py sdist` then the .py no longer exists # and only the .cpp is distributed. That's stage 1 import pythran.toolchain as tc cxx_sources = [] for source in sources: base, ext = os.path.splitext(source) if ext == ".cpp": output_file = source stage = 1 else: output_file = base + '.cpp' # target name # stage 0 when we have the .py if os.path.exists(source): stage = 0 # stage 1 otherwise. `.cpp' should already be there # as generated upon stage 0 else: assert os.path.exists(output_file) stage = 1 source = output_file # stage-dependant processing if stage == 0: # get the last name in the path if '.' in name: module_name = os.path.splitext(name)[-1][1:] else: module_name = name tc.compile_pythranfile(source, output_file, module_name, cpponly=True) cxx_sources.append(output_file) cfg_ext = cfg.make_extension(python=True, **kwargs) self.cxx = cfg_ext.pop('cxx') Extension.__init__(self, name, cxx_sources, *args, **cfg_ext)
def compile_cxxfile(module_name, cxxfile, output_binary=None, **kwargs): '''c++ file -> native module Return the filename of the produced shared library Raises CompileError on failure ''' builddir = mkdtemp() buildtmp = mkdtemp() extension_args = make_extension(**kwargs) extension = Extension(module_name, [cxxfile], language="c++", **extension_args) try: setup( name=module_name, ext_modules=[extension], # fake CLI call script_name='setup.py', script_args=[ '--verbose' if logger.isEnabledFor(logging.INFO) else '--quiet', 'build_ext', '--build-lib', builddir, '--build-temp', buildtmp, ]) except SystemExit as e: raise CompileError(e.args) [target] = glob.glob(os.path.join(builddir, module_name + "*")) if not output_binary: output_binary = os.path.join(os.getcwd(), module_name + os.path.splitext(target)[1]) shutil.move(target, output_binary) shutil.rmtree(builddir) shutil.rmtree(buildtmp) logger.info("Generated module: " + module_name) logger.info("Output: " + output_binary) return output_binary
def __init__(self, name, sources): # the goal is to rely on original Extension # to do so we convert the .py to .cpp with pythran # and register the .cpp in place of the .py # That's stage 0, and it's enough if you get the source # from github and `python setup.py install it` # # *But* if you want to distribute the source through # `python setup.py sdist` then the .py no longer exists # and only the .cpp is distributed. That's stage 1 cxx_sources = [] for source in sources: base, _ = os.path.splitext(source) output_file = base + '.cpp' # target name # stage 0 when we have the .py if os.path.exists(source): stage = 0 # stage 1 otherwise. `.cpp' should already be there # as generated upon stage 0 else: assert os.path.exists(output_file) stage = 1 source = output_file # stage-dependant processing if stage == 0: tc.compile_pythranfile(source, output_file, module_name=name, cpponly=True) cxx_sources.append(output_file) extension_args = cfg.make_extension() Extension.__init__(self, name, cxx_sources, **extension_args)