Пример #1
0
class CConfig:
    _compilation_info_ = ExternalCompilationInfo(
        includes=["time.h"],
        libraries=libraries,
    )

    HAS_CLOCK_GETTIME = rffi_platform.Has('clock_gettime')

    CLOCK_REALTIME = rffi_platform.DefinedConstantInteger("CLOCK_REALTIME")
    CLOCK_MONOTONIC = rffi_platform.DefinedConstantInteger("CLOCK_MONOTONIC")
    CLOCK_MONOTONIC_RAW = rffi_platform.DefinedConstantInteger(
        "CLOCK_MONOTONIC_RAW")
    CLOCK_PROCESS_CPUTIME_ID = rffi_platform.DefinedConstantInteger(
        "CLOCK_PROCESS_CPUTIME_ID")
    CLOCK_THREAD_CPUTIME_ID = rffi_platform.DefinedConstantInteger(
        "CLOCK_THREAD_CPUTIME_ID")
Пример #2
0
    def test_str2chararray(self):
        eci = ExternalCompilationInfo(includes=['string.h'])
        strlen = llexternal('strlen', [CCHARP], SIZE_T, compilation_info=eci)

        def f():
            raw = str2charp("XxxZy")
            n = str2chararray("abcdef", raw, 4)
            assert raw[0] == 'a'
            assert raw[1] == 'b'
            assert raw[2] == 'c'
            assert raw[3] == 'd'
            assert raw[4] == 'y'
            lltype.free(raw, flavor='raw')
            return n

        assert interpret(f, []) == 4
Пример #3
0
    def __init__(self, ns=None, includes=[], libraries=[], include_dirs=[]):
        if ns is None:
            # map name -> lltype object
            self.ns = {}
        else:
            self.ns = ns
        self.include_dirs = include_dirs

        CConfig = type('CConfig', (object, ), {})
        CConfig._compilation_info_ = ExternalCompilationInfo(
            includes=list(includes),
            include_dirs=list(include_dirs),
            libraries=list(libraries),
        )

        self.CConfig = CConfig
Пример #4
0
def pytest_configure(config):
    if py.path.local.sysfind('genreflex') is None:
        import pypy.module._cppyy.capi.loadable_capi as lcapi
        try:
            import ctypes
            ctypes.CDLL(lcapi.backend_library)
        except Exception as e:
            if config.option.runappdirect:
                return  # "can't run dummy tests in -A"

            # build dummy backend (which has reflex info and calls hard-wired)
            import os
            from rpython.translator.tool.cbuild import ExternalCompilationInfo
            from rpython.translator.platform import platform, CompilationError
            from rpython.translator import cdir

            from rpython.rtyper.lltypesystem import rffi

            pkgpath = py.path.local(__file__).dirpath().join(os.pardir)
            srcpath = pkgpath.join('src')
            incpath = pkgpath.join('include')
            tstpath = pkgpath.join('test')

            eci = ExternalCompilationInfo(
                separate_module_files=[srcpath.join('dummy_backend.cxx')],
                include_dirs=[incpath, tstpath, cdir],
                compile_extra=[
                    '-DRPY_EXTERN=RPY_EXPORTED', '-DCPPYY_DUMMY_BACKEND',
                    '-fno-strict-aliasing', '-std=c++14'
                ],
                use_cpp_linker=True,
            )

            try:
                soname = platform.compile(
                    [],
                    eci,
                    outputfilename='libcppyy_dummy_backend',
                    standalone=False)
            except CompilationError as e:
                if '-std=c++14' in str(e):
                    global disabled
                    disabled = str(e)
                    return
                raise

            lcapi.backend_library = str(soname)
Пример #5
0
    def test_hashdefine(self):
        h_source = """
        #define X(i) (i+3)
        """

        h_file = udir.join("stuff.h")
        h_file.write(h_source)

        eci = ExternalCompilationInfo(includes=['stuff.h'],
                                      include_dirs=[udir])
        z = llexternal('X', [Signed], Signed, compilation_info=eci)

        def f():
            return z(8)

        xf = self.compile(f, [])
        assert xf() == 8 + 3
Пример #6
0
    def test_no_float_to_int_conversion(self):
        c_source = py.code.Source("""
        int someexternalfunction(int x)
        {
            return (x + 3);
        }
        """)

        eci = ExternalCompilationInfo(separate_module_sources=[c_source])
        z = llexternal('someexternalfunction', [Signed], Signed,
                       compilation_info=eci)

        def f():
            return z(8.2)

        py.test.raises(TypeError, f)
        py.test.raises(TypeError, self.compile, f, [])
Пример #7
0
    def test_const_char_star(self):
        from rpython.translator.tool.cbuild import ExternalCompilationInfo

        eci = ExternalCompilationInfo(includes=["stdlib.h"])
        atoi = rffi.llexternal('atoi', [rffi.CONST_CCHARP], rffi.INT,
                               compilation_info=eci)

        def f(n):
            s = malloc(rffi.CCHARP.TO, 2, flavor='raw')
            s[0] = '9'
            s[1] = '\0'
            res = atoi(rffi.cast(rffi.CONST_CCHARP, s))
            free(s, flavor='raw')
            return res

        fn = self.getcompiled(f, [int])
        assert fn(0) == 9
Пример #8
0
 def test_preprocess_localbase(self):
     tmpdir = udir.join('test_preprocess_localbase').ensure(dir=1)
     eci = ExternalCompilationInfo()
     os.environ['PYPY_LOCALBASE'] = '/foo/baz'
     try:
         mk = self.platform.gen_makefile(['blip.c'], eci, path=tmpdir)
         mk.write()
     finally:
         del os.environ['PYPY_LOCALBASE']
     Makefile = tmpdir.join('Makefile').read()
     include_prefix = '-I'
     lib_prefix = '-L'
     if self.platform.name == 'msvc':
         include_prefix = '/I'
         lib_prefix = '/LIBPATH:'
     assert 'INCLUDEDIRS = %s/foo/baz/include' % include_prefix in Makefile
     assert 'LIBDIRS = %s/foo/baz/lib' % lib_prefix in Makefile
Пример #9
0
 def build_eci(self):
     all_sources = []
     for cts in self.includes:
         all_sources.extend(cts.sources)
     all_sources.extend(self.sources)
     all_headers = self.headers
     for x in self.includes:
         for hdr in x.headers:
             if hdr not in all_headers:
                 all_headers.append(hdr)
     if sys.platform == 'win32':
         compile_extra = ['-Dssize_t=long']
     else:
         compile_extra = []
     return ExternalCompilationInfo(
         post_include_bits=all_sources, includes=all_headers,
         compile_extra=compile_extra)
Пример #10
0
    def test_longsize(self):
        if not is_x86():
            py.test.skip("i386 only")
        cfile = udir.join('test_int_size.c')
        cfile.write(r'''
        #include <stdio.h>
        #include <limits.h>

        int main() {
                printf("%ld\n", LONG_MAX);
                return 0;
        }
        ''')
        eci = ExternalCompilationInfo()
        executable = self.platform.compile([cfile], eci)
        res = self.platform.execute(executable)
        self.check_res(res, str(sys.maxint) + '\n')
Пример #11
0
def compile_so_file():
    from rpython.translator.platform import platform
    from rpython.translator.tool.cbuild import ExternalCompilationInfo
    from rpython.translator import cdir
    udir = pytest.ensuretemp('_ctypes_test')
    cfile = py.path.local(__file__).dirpath().join("_ctypes_test.c")

    if sys.platform == 'win32':
        libraries = ['oleaut32']
    else:
        libraries = []
    eci = ExternalCompilationInfo(libraries=libraries, include_dirs=[cdir])

    return platform.compile([cfile],
                            eci,
                            str(udir.join('_ctypes_test')),
                            standalone=False)
Пример #12
0
    def test_basic(self):
        c_source = py.code.Source("""
        int someexternalfunction(int x)
        {
            return (x + 3);
        }
        """)

        eci = ExternalCompilationInfo(separate_module_sources=[c_source])
        z = llexternal('someexternalfunction', [Signed], Signed,
                       compilation_info=eci)

        def f():
            return z(8)

        xf = self.compile(f, [])
        assert xf() == 8+3
Пример #13
0
def compile_extension_module(space, modname, **kwds):
    """
    Build an extension module and return the filename of the resulting native
    code file.

    modname is the name of the module, possibly including dots if it is a module
    inside a package.

    Any extra keyword arguments are passed on to ExternalCompilationInfo to
    build the module (so specify your source with one of those).
    """
    state = space.fromcache(State)
    api_library = state.api_lib
    if sys.platform == 'win32':
        kwds["libraries"] = [api_library]
        # '%s' undefined; assuming extern returning int
        kwds["compile_extra"] = ["/we4013"]
        # prevent linking with PythonXX.lib
        w_maj, w_min = space.fixedview(space.sys.get('version_info'), 5)[:2]
        kwds["link_extra"] = [
            "/NODEFAULTLIB:Python%d%d.lib" %
            (space.int_w(w_maj), space.int_w(w_min))
        ]
    elif sys.platform == 'darwin':
        kwds["link_files"] = [str(api_library + '.dylib')]
    else:
        kwds["link_files"] = [str(api_library + '.so')]
        if sys.platform.startswith('linux'):
            kwds["compile_extra"] = [
                "-Werror=implicit-function-declaration", "-g", "-O0"
            ]
            kwds["link_extra"] = ["-g"]

    modname = modname.split('.')[-1]
    eci = ExternalCompilationInfo(include_dirs=api.include_dirs, **kwds)
    eci = eci.convert_sources_to_files()
    dirname = (udir / uniquemodulename('module')).ensure(dir=1)
    soname = platform.platform.compile([],
                                       eci,
                                       outputfilename=str(dirname / modname),
                                       standalone=False)
    from pypy.module.imp.importing import get_so_extension
    pydname = soname.new(purebasename=modname, ext=get_so_extension(space))
    soname.rename(pydname)
    return str(pydname)
Пример #14
0
 def test_use_eci(self):
     tmpdir = udir.join('use_eci').ensure(dir=1)
     hfile = tmpdir.join('needed.h')
     hfile.write('#define SOMEHASHDEFINE 42\n')
     eci = ExternalCompilationInfo(include_dirs=[tmpdir])
     cfile = udir.join('use_eci_c.c')
     cfile.write('''
     #include <stdio.h>
     #include "needed.h"
     int main()
     {
         printf("%d\\n", SOMEHASHDEFINE);
         return 0;
     }
     ''')
     executable = self.platform.compile([cfile], eci)
     res = self.platform.execute(executable)
     self.check_res(res)
Пример #15
0
    def test_variadic_args(self):
        from rpython.translator.tool.cbuild import ExternalCompilationInfo
        from rpython.translator.platform import platform
        from rpython.tool.udir import udir

        c_file = udir.ensure("test_libffi", dir=1).join("xlib.c")
        c_file.write(py.code.Source('''
        #include "src/precommondefs.h"
        #include <stdarg.h>
        #include <stdio.h>
        RPY_EXPORTED
        Signed fun(Signed n, ...) {
            va_list ptr;
            int sum = 0;
            printf("n: %ld\\n", n);

            va_start(ptr, n);
            for (int i = 0; i < n; i++) {
                Signed foo = va_arg(ptr, Signed);
                sum += foo;
                printf("Arg %d: %ld\\n", i, foo);
            }
            va_end(ptr);
            return sum;
        }
        '''))

        eci = ExternalCompilationInfo(include_dirs=[cdir])
        lib_name = str(platform.compile([c_file], eci, 'x4', standalone=False))

        lib = CDLL(lib_name)
        signed = cast_type_to_ffitype(rffi.SIGNED)
        fun = lib.getrawpointer('fun', [signed, signed, signed], signed, variadic_args=2)

        buffer = lltype.malloc(rffi.SIGNEDP.TO, 4, flavor='raw')
        buffer[0] = 2
        buffer[1] = 3
        buffer[2] = 15
        buffer[3] = 100
        fun.call([rffi.cast(rffi.VOIDP, buffer), rffi.cast(rffi.VOIDP, rffi.ptradd(buffer, 1)),
                  rffi.cast(rffi.VOIDP, rffi.ptradd(buffer, 2))],
                 rffi.cast(rffi.VOIDP, rffi.ptradd(buffer, 3)))
        assert buffer[3] == 3 + 15
        lltype.free(buffer, flavor='raw')
Пример #16
0
    def test_opaque_type(self):
        h_source = py.code.Source("""
        #ifndef _OPAQUE_H
        #define _OPAQUE_H
        struct stuff {
           char data[38];
        };
        #endif /* _OPAQUE_H */
        """)

        c_source = py.code.Source("""
        #include "opaque.h"

        char get(struct stuff* x)
        {
           x->data[13] = 'a';
           return x->data[13];
        }
        """)


        # if it doesn't segfault, than we probably malloced it :-)
        h_file = udir.join("opaque.h")
        h_file.write(h_source)

        from rpython.rtyper.tool import rffi_platform
        eci = ExternalCompilationInfo(
            includes=['opaque.h'],
            include_dirs=[str(udir)],
            separate_module_sources=[c_source]
        )
        STUFFP = COpaquePtr('struct stuff', compilation_info=eci)

        ll_get = llexternal('get', [STUFFP], CHAR, compilation_info=eci)

        def f():
            ll_stuff = lltype.malloc(STUFFP.TO, flavor='raw')
            result = ll_get(ll_stuff)
            lltype.free(ll_stuff, flavor='raw')
            return result

        f1 = self.compile(f, [])
        assert f1() == 'a'
Пример #17
0
    def test_struct(self):
        h_source = """
        #ifndef _MY_SOURCE_H
        #define _MY_SOURCE_H
        struct xx {
           int one;
           char two;
           int three;
        };
        #endif
        """
        h_file = udir.join("structxx.h")
        h_file.write(h_source)

        c_source = """
        #include <structxx.h>

        int f(struct xx* z)
        {
          return (z->one + z->three);
        }
        """
        TP = CStructPtr('xx', ('one', INT), ('two', Char), ('three', INT))

        eci = ExternalCompilationInfo(
            includes=['structxx.h'],
            include_dirs=[udir],
            separate_module_sources=[c_source]
        )
        z = llexternal('f', [TP], INT, compilation_info=eci)

        def f():
            struct = lltype.malloc(TP.TO, flavor='raw')
            struct.c_one = cast(INT, 3)
            struct.c_two = '\x33'
            struct.c_three = cast(INT, 5)
            result = z(struct)
            lltype.free(struct, flavor='raw')
            return cast(SIGNED, result)

        fn = self.compile(f, [], backendopt=False)
        assert fn() == 8
Пример #18
0
    def test_opaque_typedef(self):
        code = """
        #include <stddef.h>
        struct stuff;
        typedef struct stuff *stuff_ptr;
        static int get(stuff_ptr ptr) { return (ptr != NULL); }
        """

        eci = ExternalCompilationInfo(post_include_bits=[code])

        STUFFP = COpaquePtr(typedef='stuff_ptr', compilation_info=eci)
        ll_get = llexternal('get', [STUFFP],
                            lltype.Signed,
                            compilation_info=eci)

        def f():
            return ll_get(lltype.nullptr(STUFFP.TO))

        f1 = self.compile(f, [])
        assert f1() == 0
Пример #19
0
def test_execute_code_show_runtime_error():
    f = localudir.join('z.c')
    f.write("""
    #include <stdio.h>
    int main()
    {
       fprintf(stderr, "hello\\n");
       return 0;
    }
    """)
    for i in range(2):
        eci = ExternalCompilationInfo()
        oldstderr = sys.stderr
        try:
            sys.stderr = capture = cStringIO.StringIO()
            output = build_executable_cache([f], eci, True)
        finally:
            sys.stderr = oldstderr
        assert 'hello' in capture.getvalue()
        assert output == ''
Пример #20
0
def add_extra_files(eci):
    srcdir = py.path.local(__file__).join('..', 'src')
    files = [
        srcdir / 'entrypoint.c',  # ifdef PYPY_STANDALONE
        srcdir / 'mem.c',
        srcdir / 'exception.c',
        srcdir / 'rtyper.c',  # ifdef HAVE_RTYPER
        srcdir / 'support.c',
        srcdir / 'profiling.c',
        srcdir / 'debug_print.c',
        srcdir / 'debug_traceback.c',  # ifdef HAVE_RTYPER
        srcdir / 'asm.c',
        srcdir / 'instrument.c',
        srcdir / 'int.c',
        srcdir / 'stack.c',
        srcdir / 'threadlocal.c',
    ]
    if _CYGWIN:
        files.append(srcdir / 'cygwin_wait.c')
    return eci.merge(ExternalCompilationInfo(separate_module_files=files))
Пример #21
0
    def test_extra_include_dirs(self):
        udir.ensure("incl", dir=True)
        udir.join("incl", "incl.h").write("#define C 3")
        c_source = py.code.Source("""
        #include <incl.h>
        int fun ()
        {
            return (C);
        }
        """)
        eci = ExternalCompilationInfo(includes=['incl.h'],
                                      include_dirs=[str(udir.join('incl'))],
                                      separate_module_sources=[c_source])
        z = llexternal('fun', [], Signed, compilation_info=eci)

        def f():
            return z()

        res = self.compile(f, [])
        assert res() == 3
Пример #22
0
 def test_two_files(self):
     cfile = udir.join('test_two_files.c')
     cfile.write('''
     #include <stdio.h>
     int func();
     int main()
     {
         printf("%d\\n", func());
         return 0;
     }
     ''')
     cfile2 = udir.join('implement1.c')
     cfile2.write('''
     int func()
     {
         return 42;
     }
     ''')
     executable = self.platform.compile([cfile, cfile2], ExternalCompilationInfo())
     res = self.platform.execute(executable)
     self.check_res(res)
Пример #23
0
 def init(self, tmpdir):
     eci = ExternalCompilationInfo(compile_extra=['-g', '-O0'],
                                   separate_module_sources=[
                                       """
             RPY_EXTERN int native_func(int d) {
                 int j = 0;
                 if (d > 0) {
                     return native_func(d-1);
                 } else {
                     for (int i = 0; i < 42000; i++) {
                         j += 1;
                     }
                 }
                 return j;
             }
             """
                                   ])
     self.native_func = rffi.llexternal("native_func", [rffi.INT],
                                        rffi.INT,
                                        compilation_info=eci)
     super(TestNative, self).init(tmpdir)
Пример #24
0
 def test_make_shared_lib(self):
     eci = ExternalCompilationInfo(separate_module_sources=[
         '''
         RPY_EXTERN int get()
         {
             return 42;
         }
         int shouldnt_export()
         {
             return 43;
         }'''
     ], )
     neweci = eci.compile_shared_lib()
     assert len(neweci.libraries) == 1
     try:
         import ctypes
     except ImportError:
         py.test.skip("Need ctypes for that test")
     assert ctypes.CDLL(neweci.libraries[0]).get() == 42
     assert not hasattr(ctypes.CDLL(neweci.libraries[0]), 'shouldnt_export')
     assert not neweci.separate_module_sources
     assert not neweci.separate_module_files
Пример #25
0
    def prepare_c_example(cls):
        from rpython.tool.udir import udir
        from rpython.translator.tool.cbuild import ExternalCompilationInfo
        from rpython.translator.platform import platform

        c_file = udir.ensure("test__ffi", dir=1).join("foolib.c")
        # automatically collect the C source from the docstrings of the tests
        snippets = ["""
        #include "src/precommondefs.h"
        #define DLLEXPORT RPY_EXPORTED
        """]
        for name in dir(cls):
            if name.startswith('test_'):
                meth = getattr(cls, name)
                # the heuristic to determine it it's really C code could be
                # improved: so far we just check that there is a '{' :-)
                if meth.__doc__ is not None and '{' in meth.__doc__:
                    snippets.append(meth.__doc__)
        #
        c_file.write(py.code.Source('\n'.join(snippets)))
        eci = ExternalCompilationInfo(include_dirs=[cdir])
        return str(platform.compile([c_file], eci, 'x', standalone=False))
Пример #26
0
    def setup_class(cls):
        import ctypes
        from rpython.tool.udir import udir
        from rpython.translator.platform import platform
        from rpython.translator.tool.cbuild import ExternalCompilationInfo

        c_source = """
        #include "src/precommondefs.h"

        RPY_EXPORTED
        void *int_to_void_p(int arg) {}

        RPY_EXPORTED
        struct random_structure* int_int_to_struct_p(int one, int two) {}
        """

        c_file = udir.join('rffilib.c')
        c_file.write(c_source)
        eci = ExternalCompilationInfo(include_dirs=[cdir])
        libname = platform.compile([c_file], eci,
                                   standalone=False)
        cls.lib = ctypes.CDLL(str(libname))
Пример #27
0
def add_extra_files(database, eci):
    srcdir = py.path.local(__file__).join('..', 'src')
    files = [
        srcdir / 'entrypoint.c',       # ifdef PYPY_STANDALONE
        srcdir / 'mem.c',
        srcdir / 'exception.c',
        srcdir / 'rtyper.c',           # ifdef HAVE_RTYPER
        srcdir / 'support.c',
        srcdir / 'profiling.c',
        srcdir / 'debug_print.c',
        srcdir / 'debug_traceback.c',  # ifdef HAVE_RTYPER
        srcdir / 'asm.c',
        srcdir / 'instrument.c',
        srcdir / 'int.c',
        srcdir / 'stack.c',
        srcdir / 'threadlocal.c',
    ]
    if _CYGWIN:
        files.append(srcdir / 'cygwin_wait.c')
    if database.reverse_debugger:
        from rpython.translator.revdb import gencsupp
        files += gencsupp.extra_files()
    return eci.merge(ExternalCompilationInfo(separate_module_files=files))
Пример #28
0
    def test_64_32_results(self):
        if not is_x86():
            py.test.skip("i386 only")
        plat32 = Darwin_i386()
        plat64 = Darwin_x86_64()
        cfile = udir.join('test_int_size.c')
        cfile.write(r'''
        #include <stdio.h>
        #include <limits.h>

        int main() {
                printf("%d\n", INT_MAX < LONG_MAX);
                return 0;
        }
        ''')
        eci = ExternalCompilationInfo()
        executable = plat32.compile([cfile], eci)
        res = plat32.execute(executable)
        self.check_res(res, '0\n')
        if host_factory == Darwin_x86_64:
            executable = plat64.compile([cfile], eci)
            res = plat64.execute(executable)
            self.check_res(res, '1\n')
Пример #29
0
def ask_gcc(question, add_source="", ignore_errors=False):
    from rpython.translator.platform import platform
    includes = ['stdlib.h', 'stdio.h', 'sys/types.h']
    if platform.name != 'msvc':
        includes += ['inttypes.h', 'stddef.h']
    include_string = "\n".join(["#include <%s>" % i for i in includes])
    c_source = py.code.Source('''
    // includes
    %s

    %s

    // checking code
    int main(void)
    {
       %s
       return (0);
    }
    ''' % (include_string, add_source, str(question)))
    c_file = udir.join("gcctest.c")
    c_file.write(str(c_source) + '\n')
    eci = ExternalCompilationInfo()
    return build_executable_cache([c_file], eci, ignore_errors=ignore_errors)
Пример #30
0
def COpaque(name=None, ptr_typedef=None, hints=None, compilation_info=None):
    if compilation_info is None:
        compilation_info = ExternalCompilationInfo()
    if hints is None:
        hints = {}
    else:
        hints = hints.copy()
    hints['external'] = 'C'
    if name is not None:
        hints['c_name'] = name
    if ptr_typedef is not None:
        hints['c_pointer_typedef'] = ptr_typedef
    def lazy_getsize(cache={}):
        from rpython.rtyper.tool import rffi_platform
        try:
            return cache[name]
        except KeyError:
            val = rffi_platform.sizeof(name, compilation_info)
            cache[name] = val
            return val

    hints['getsize'] = lazy_getsize
    return lltype.OpaqueType(name, hints)