def llexternal_use_eci(compilation_info): """Return a dummy function that, if called in a RPython program, adds the given ExternalCompilationInfo to it.""" eci = ExternalCompilationInfo(post_include_bits=['#define PYPY_NO_OP()']) eci = eci.merge(compilation_info) return llexternal('PYPY_NO_OP', [], lltype.Void, compilation_info=eci, sandboxsafe=True, _nowrapper=True, _callable=lambda: None)
def test_merge2(self): e1 = ExternalCompilationInfo(pre_include_bits=['1'], link_files=['1.c']) e2 = ExternalCompilationInfo(pre_include_bits=['2'], link_files=['1.c', '2.c']) e3 = ExternalCompilationInfo(pre_include_bits=['3'], link_files=['1.c', '2.c', '3.c']) e = e1.merge(e2) e = e.merge(e3, e3) assert e.pre_include_bits == ('1', '2', '3') assert e.link_files == ('1.c', '2.c', '3.c')
def test_merge(self): e1 = ExternalCompilationInfo(pre_include_bits=['1'], includes=['x.h'], post_include_bits=['p1']) e2 = ExternalCompilationInfo( pre_include_bits=['2'], includes=['x.h', 'y.h'], post_include_bits=['p2'], ) e3 = ExternalCompilationInfo(pre_include_bits=['3'], includes=['y.h', 'z.h'], post_include_bits=['p1', 'p3']) e = e1.merge(e2, e3) assert e.pre_include_bits == ('1', '2', '3') assert e.includes == ('x.h', 'y.h', 'z.h') assert e.post_include_bits == ('p1', 'p2', 'p3')
def test_merge2(self): e1 = ExternalCompilationInfo( pre_include_bits = ['1'], link_files = ['1.c'] ) e2 = ExternalCompilationInfo( pre_include_bits = ['2'], link_files = ['1.c', '2.c'] ) e3 = ExternalCompilationInfo( pre_include_bits = ['3'], link_files = ['1.c', '2.c', '3.c'] ) e = e1.merge(e2) e = e.merge(e3, e3) assert e.pre_include_bits == ('1', '2', '3') assert e.link_files == ('1.c', '2.c', '3.c')
def test_platforms(self): from rpython.translator.platform import Platform class Maemo(Platform): def __init__(self, cc=None): self.cc = cc eci = ExternalCompilationInfo(platform=Maemo()) eci2 = ExternalCompilationInfo() assert eci != eci2 assert hash(eci) != hash(eci2) assert repr(eci) != repr(eci2) py.test.raises(Exception, eci2.merge, eci) assert eci.merge(eci).platform == Maemo() assert (ExternalCompilationInfo(platform=Maemo(cc='xxx')) != ExternalCompilationInfo(platform=Maemo(cc='yyy'))) assert (repr(ExternalCompilationInfo(platform=Maemo(cc='xxx'))) != repr(ExternalCompilationInfo(platform=Maemo(cc='yyy'))))
def test_merge(self): e1 = ExternalCompilationInfo( pre_include_bits = ['1'], includes = ['x.h'], post_include_bits = ['p1'] ) e2 = ExternalCompilationInfo( pre_include_bits = ['2'], includes = ['x.h', 'y.h'], post_include_bits = ['p2'], ) e3 = ExternalCompilationInfo( pre_include_bits = ['3'], includes = ['y.h', 'z.h'], post_include_bits = ['p1', 'p3'] ) e = e1.merge(e2, e3) assert e.pre_include_bits == ('1', '2', '3') assert e.includes == ('x.h', 'y.h', 'z.h') assert e.post_include_bits == ('p1', 'p2', 'p3')
return vmprof_set_mainloop(pypy_execute_frame_trampoline, 0, NULL); } """], ) if DYNAMIC_VMPROF: eci_kwds['libraries'] += ['vmprof'] eci_kwds['link_extra'] = ['-Wl,-rpath,%s' % SRC, '-L%s' % SRC] else: eci_kwds['separate_module_files'] += [SRC.join('vmprof.c')] eci = ExternalCompilationInfo(**eci_kwds) check_eci = eci.merge(ExternalCompilationInfo(separate_module_files=[ SRC.join('fake_pypy_api.c')])) platform.verify_eci(check_eci) pypy_execute_frame_trampoline = rffi.llexternal( "pypy_execute_frame_trampoline", [llmemory.GCREF, llmemory.GCREF, llmemory.GCREF, lltype.Signed], llmemory.GCREF, compilation_info=eci, _nowrapper=True, sandboxsafe=True, random_effects_on_gcobjs=True) pypy_vmprof_init = rffi.llexternal("pypy_vmprof_init", [], rffi.INT, compilation_info=eci) vmprof_enable = rffi.llexternal("vmprof_enable", [rffi.INT, rffi.LONG, rffi.INT,
compile_extra.append("-DHAVE_SYS_STAT_H") if config['HAVE_SETSID']: compile_extra.append("-DHAVE_SETSID") class CConfig: _compilation_info_ = ExternalCompilationInfo(includes=['dirent.h']) HAVE_DIRENT_H = platform.Has("opendir") config = platform.configure(CConfig) if config['HAVE_DIRENT_H']: compile_extra.append("-DHAVE_DIRENT_H") eci = eci.merge(rposix.eci_inheritable, ExternalCompilationInfo(compile_extra=compile_extra)) c_child_exec = rffi.llexternal('pypy_subprocess_child_exec', [ rffi.CCHARPP, rffi.CCHARPP, rffi.CCHARPP, rffi.CCHARP, rffi.INT, rffi.INT, rffi.INT, rffi.INT, rffi.INT, rffi.INT, rffi.INT, rffi.INT, rffi.INT, rffi.INT, rffi.INT, rffi.CArrayPtr(rffi.LONG), lltype.Signed, lltype.Ptr(lltype.FuncType([rffi.VOIDP], rffi.INT)), rffi.VOIDP ], lltype.Void, compilation_info=eci, releasegil=True) c_cloexec_pipe = rffi.llexternal('pypy_subprocess_cloexec_pipe', [rffi.CArrayPtr(rffi.INT)], rffi.INT, compilation_info=eci,
char *_pypy_init_home(void) { Dl_info info; dlerror(); /* reset */ if (dladdr(&_pypy_init_home, &info) == 0) { fprintf(stderr, "PyPy initialization: dladdr() failed: %s\n", dlerror()); return NULL; } char *p = realpath(info.dli_fname, NULL); if (p == NULL) { p = strdup(info.dli_fname); } return p; } """ _eci = ExternalCompilationInfo( separate_module_sources=[_source_code], post_include_bits=['RPY_EXPORTED char *_pypy_init_home(void);']) _eci = _eci.merge(rdynload.eci) pypy_init_home = rffi.llexternal("_pypy_init_home", [], rffi.CCHARP, _nowrapper=True, compilation_info=_eci) pypy_init_free = rffi.llexternal("free", [rffi.CCHARP], lltype.Void, _nowrapper=True, compilation_info=_eci)
import py from rpython.rtyper.lltypesystem import lltype, llmemory, rffi, rstr from rpython.translator import cdir from rpython.translator.tool.cbuild import ExternalCompilationInfo cwd = py.path.local(__file__).dirpath() eci = ExternalCompilationInfo( includes=[cwd.join('faulthandler.h')], include_dirs=[str(cwd), cdir], separate_module_files=[cwd.join('faulthandler.c')]) eci_later = eci.merge(ExternalCompilationInfo( pre_include_bits=['#define PYPY_FAULTHANDLER_LATER\n'])) eci_user = eci.merge(ExternalCompilationInfo( pre_include_bits=['#define PYPY_FAULTHANDLER_USER\n'])) def direct_llexternal(*args, **kwargs): kwargs.setdefault('_nowrapper', True) kwargs.setdefault('compilation_info', eci) return rffi.llexternal(*args, **kwargs) DUMP_CALLBACK = lltype.Ptr(lltype.FuncType( [rffi.INT, rffi.SIGNEDP, lltype.Signed], lltype.Void)) pypy_faulthandler_setup = direct_llexternal( 'pypy_faulthandler_setup', [DUMP_CALLBACK], rffi.CCHARP) pypy_faulthandler_teardown = direct_llexternal( 'pypy_faulthandler_teardown', [], lltype.Void)
SYS_getrandom = None if sys.platform.startswith('linux'): eci = ExternalCompilationInfo(includes=['sys/syscall.h']) class CConfig: _compilation_info_ = eci SYS_getrandom = rffi_platform.DefinedConstantInteger( 'SYS_getrandom') globals().update(rffi_platform.configure(CConfig)) if SYS_getrandom is not None: from rpython.rlib.rposix import get_saved_errno, handle_posix_error import errno eci = eci.merge(ExternalCompilationInfo(includes=['linux/random.h'])) class CConfig: _compilation_info_ = eci GRND_NONBLOCK = rffi_platform.ConstantInteger('GRND_NONBLOCK') globals().update(rffi_platform.configure(CConfig)) # On Linux, use the syscall() function because the GNU libc doesn't # expose the Linux getrandom() syscall yet. syscall = rffi.llexternal( 'syscall', [lltype.Signed, rffi.CCHARP, rffi.LONG, rffi.INT], lltype.Signed, compilation_info=eci, save_err=rffi.RFFI_SAVE_ERRNO) class Works:
def configure_external_library(name, eci, configurations, symbol=None, _cache={}): """try to find the external library. On Unix, this simply tests and returns the given eci. On Windows, various configurations may be tried to compile the given eci object. These configurations are a list of dicts, containing: - prefix: if an absolute path, will prefix each include and library directories. If a relative path, the external directory is searched for directories which names start with the prefix. The last one in alphabetical order chosen, and becomes the prefix. - include_dir: prefix + include_dir is added to the include directories - library_dir: prefix + library_dir is added to the library directories """ if sys.platform != 'win32': configurations = [] key = (name, eci) try: return _cache[key] except KeyError: last_error = None # Always try the default configuration if {} not in configurations: configurations.append({}) for configuration in configurations: prefix = configuration.get('prefix', '') include_dir = configuration.get('include_dir', '') library_dir = configuration.get('library_dir', '') if prefix and not os.path.isabs(prefix): import glob entries = glob.glob(str(PYPY_EXTERNAL_DIR.join(prefix + '*'))) if entries: # Get last version prefix = sorted(entries)[-1] else: continue include_dir = os.path.join(prefix, include_dir) library_dir = os.path.join(prefix, library_dir) eci_lib = ExternalCompilationInfo( include_dirs=include_dir and [include_dir] or [], library_dirs=library_dir and [library_dir] or [], ) eci_lib = eci_lib.merge(eci) # verify that this eci can be compiled try: verify_eci(eci_lib) except CompilationError as e: last_error = e else: _cache[key] = eci_lib return eci_lib # Nothing found if last_error: raise last_error else: raise CompilationError("Library %s is not installed" % (name, ))
def configure_external_library(name, eci, configurations, symbol=None, _cache={}): """try to find the external library. On Unix, this simply tests and returns the given eci. On Windows, various configurations may be tried to compile the given eci object. These configurations are a list of dicts, containing: - prefix: if an absolute path, will prefix each include and library directories. If a relative path, the external directory is searched for directories which names start with the prefix. The last one in alphabetical order chosen, and becomes the prefix. - include_dir: prefix + include_dir is added to the include directories - library_dir: prefix + library_dir is added to the library directories """ if sys.platform != 'win32': configurations = [] key = (name, eci) try: return _cache[key] except KeyError: last_error = None # Always try the default configuration if {} not in configurations: configurations.append({}) for configuration in configurations: prefix = configuration.get('prefix', '') include_dir = configuration.get('include_dir', '') library_dir = configuration.get('library_dir', '') if prefix and not os.path.isabs(prefix): import glob entries = glob.glob(str(PYPY_EXTERNAL_DIR.join(prefix + '*'))) if entries: # Get last version prefix = sorted(entries)[-1] else: continue include_dir = os.path.join(prefix, include_dir) library_dir = os.path.join(prefix, library_dir) eci_lib = ExternalCompilationInfo( include_dirs=include_dir and [include_dir] or [], library_dirs=library_dir and [library_dir] or [], ) eci_lib = eci_lib.merge(eci) # verify that this eci can be compiled try: verify_eci(eci_lib) except CompilationError, e: last_error = e else: _cache[key] = eci_lib return eci_lib # Nothing found if last_error: raise last_error else: raise CompilationError("Library %s is not installed" % (name,))
config = platform.configure(CConfig) eci = ExternalCompilationInfo( includes=[thisdir.join('_posixsubprocess.h')], include_dirs=[str(thisdir), cdir], separate_module_files=[thisdir.join('_posixsubprocess.c')]) compile_extra = [] if config['HAVE_SYS_SYSCALL_H']: compile_extra.append("-DHAVE_SYS_SYSCALL_H") if config['HAVE_SETSID']: compile_extra.append("-DHAVE_SETSID") eci = eci.merge( ExternalCompilationInfo( compile_extra=compile_extra)) c_child_exec = rffi.llexternal( 'pypy_subprocess_child_exec', [rffi.CCHARPP, rffi.CCHARPP, rffi.CCHARPP, rffi.CCHARP, rffi.INT, rffi.INT, rffi.INT, rffi.INT, rffi.INT, rffi.INT, rffi.INT, rffi.INT, rffi.INT, rffi.INT, rffi.INT, rffi.CArrayPtr(rffi.LONG), lltype.Signed, lltype.Ptr(lltype.FuncType([rffi.VOIDP], rffi.INT)), rffi.VOIDP], lltype.Void, compilation_info=eci, releasegil=True) c_cloexec_pipe = rffi.llexternal( 'pypy_subprocess_cloexec_pipe', [rffi.CArrayPtr(rffi.INT)], rffi.INT,
QueryPerformanceFrequency = external( 'QueryPerformanceFrequency', [rffi.CArrayPtr(lltype.SignedLongLong)], rffi.INT, releasegil=False) class State(object): divisor = 0.0 counter_start = 0 state = State() elif CLOCK_PROCESS_CPUTIME_ID is not None: # Linux and other POSIX systems with clock_gettime() globals().update(rffi_platform.configure(CConfigForClockGetTime)) TIMESPEC = TIMESPEC CLOCK_PROCESS_CPUTIME_ID = CLOCK_PROCESS_CPUTIME_ID eci_with_lrt = eci.merge(ExternalCompilationInfo(libraries=['rt'])) c_clock_gettime = external( 'clock_gettime', [lltype.Signed, lltype.Ptr(TIMESPEC)], rffi.INT, releasegil=False, compilation_info=eci_with_lrt) if need_rusage: RUSAGE = RUSAGE RUSAGE_SELF = RUSAGE_SELF or 0 c_getrusage = external('getrusage', [rffi.INT, lltype.Ptr(RUSAGE)], rffi.INT, releasegil=False) def win_perf_counter():
#include <string.h> #include <stdio.h> #include <stdlib.h> RPY_EXPORTED char *_pypy_init_home(void) { Dl_info info; dlerror(); /* reset */ if (dladdr(&_pypy_init_home, &info) == 0) { fprintf(stderr, "PyPy initialization: dladdr() failed: %s\n", dlerror()); return NULL; } char *p = realpath(info.dli_fname, NULL); if (p == NULL) { p = strdup(info.dli_fname); } return p; } """ _eci = ExternalCompilationInfo(separate_module_sources=[_source_code], post_include_bits=['RPY_EXPORTED char *_pypy_init_home(void);']) _eci = _eci.merge(rdynload.eci) pypy_init_home = rffi.llexternal("_pypy_init_home", [], rffi.CCHARP, _nowrapper=True, compilation_info=_eci) pypy_init_free = rffi.llexternal("free", [rffi.CCHARP], lltype.Void, _nowrapper=True, compilation_info=_eci)
NULL); } """ ], ) if DYNAMIC_VMPROF: eci_kwds['libraries'] += ['vmprof'] eci_kwds['link_extra'] = ['-Wl,-rpath,%s' % SRC, '-L%s' % SRC] else: eci_kwds['separate_module_files'] += [SRC.join('vmprof.c')] eci = ExternalCompilationInfo(**eci_kwds) check_eci = eci.merge( ExternalCompilationInfo( separate_module_files=[SRC.join('fake_pypy_api.c')])) platform.verify_eci(check_eci) pypy_execute_frame_trampoline = rffi.llexternal( "pypy_execute_frame_trampoline", [llmemory.GCREF, llmemory.GCREF, llmemory.GCREF, lltype.Signed], llmemory.GCREF, compilation_info=eci, _nowrapper=True, sandboxsafe=True, random_effects_on_gcobjs=True) pypy_vmprof_init = rffi.llexternal("pypy_vmprof_init", [], rffi.INT,
QueryPerformanceCounter = external( 'QueryPerformanceCounter', [lltype.Ptr(A)], lltype.Void, releasegil=False) QueryPerformanceFrequency = external( 'QueryPerformanceFrequency', [lltype.Ptr(A)], rffi.INT, releasegil=False) class State(object): divisor = 0.0 counter_start = 0 state = State() elif CLOCK_PROCESS_CPUTIME_ID is not None: # Linux and other POSIX systems with clock_gettime() globals().update(rffi_platform.configure(CConfigForClockGetTime)) TIMESPEC = TIMESPEC CLOCK_PROCESS_CPUTIME_ID = CLOCK_PROCESS_CPUTIME_ID eci_with_lrt = eci.merge(ExternalCompilationInfo(libraries=['rt'])) c_clock_gettime = external('clock_gettime', [lltype.Signed, lltype.Ptr(TIMESPEC)], rffi.INT, releasegil=False, compilation_info=eci_with_lrt) else: RUSAGE = RUSAGE RUSAGE_SELF = RUSAGE_SELF or 0 c_getrusage = external('getrusage', [rffi.INT, lltype.Ptr(RUSAGE)], lltype.Void, releasegil=False) @replace_time_function('clock') @jit.dont_look_inside # the JIT doesn't like FixedSizeArray def clock():
import py from rpython.rtyper.lltypesystem import lltype, llmemory, rffi, rstr from rpython.translator import cdir from rpython.translator.tool.cbuild import ExternalCompilationInfo cwd = py.path.local(__file__).dirpath() eci = ExternalCompilationInfo( includes=[cwd.join('faulthandler.h')], include_dirs=[str(cwd), cdir], separate_module_files=[cwd.join('faulthandler.c')]) eci_later = eci.merge( ExternalCompilationInfo( pre_include_bits=['#define PYPY_FAULTHANDLER_LATER\n'])) eci_user = eci.merge( ExternalCompilationInfo( pre_include_bits=['#define PYPY_FAULTHANDLER_USER\n'])) def direct_llexternal(*args, **kwargs): kwargs.setdefault('_nowrapper', True) kwargs.setdefault('compilation_info', eci) return rffi.llexternal(*args, **kwargs) DUMP_CALLBACK = lltype.Ptr( lltype.FuncType([rffi.INT, rffi.SIGNEDP, lltype.Signed], lltype.Void)) pypy_faulthandler_setup = direct_llexternal('pypy_faulthandler_setup', [DUMP_CALLBACK], rffi.CCHARP)
config = platform.configure(CConfig) eci = ExternalCompilationInfo( includes=[thisdir.join('_posixsubprocess.h')], include_dirs=[str(thisdir), cdir], separate_module_files=[thisdir.join('_posixsubprocess.c')]) compile_extra = [] if config['HAVE_SYS_SYSCALL_H']: compile_extra.append("-DHAVE_SYS_SYSCALL_H") if config['HAVE_SETSID']: compile_extra.append("-DHAVE_SETSID") eci = eci.merge(ExternalCompilationInfo(compile_extra=compile_extra)) c_child_exec = rffi.llexternal('pypy_subprocess_child_exec', [ rffi.CCHARPP, rffi.CCHARPP, rffi.CCHARPP, rffi.CCHARP, rffi.INT, rffi.INT, rffi.INT, rffi.INT, rffi.INT, rffi.INT, rffi.INT, rffi.INT, rffi.INT, rffi.INT, rffi.INT, rffi.CArrayPtr(rffi.LONG), lltype.Signed, lltype.Ptr(lltype.FuncType([rffi.VOIDP], rffi.INT)), rffi.VOIDP ], lltype.Void, compilation_info=eci, releasegil=True) c_cloexec_pipe = rffi.llexternal('pypy_subprocess_cloexec_pipe', [rffi.CArrayPtr(rffi.INT)], rffi.INT, compilation_info=eci,