Issue #154 resolved

Use FFI.verify() with mingw32 on windows

Anonymous created an issue

I want to use FFI.verify() with mingw32 on windows, I add following lines to distutils.cfg:

[build_ext]
compiler = mingw32

but when I call verify() it raises:

distutils.errors.DistutilsPlatformError: Unable to find vcvarsall.bat

I debugged this and find that I need to add following line to ffiplatform.py:

def _build(tmpdir, ext):
    # XXX compact but horrible :-(
    from distutils.core import Distribution
    import distutils.errors
    #
    dist = Distribution({'ext_modules': [ext]})
    dist.parse_config_files() #<-- add this line
    options = dist.get_option_dict('build_ext')

After the above change, I got following compile error with MingW32

__pycache__\_cffi__x3af27de4xb6529abd.c:8:16: error: conflicting types for 'int8_t'

Google the error and found that I need modify following line in vengine_cpy.py:

#include <Python.h>
#include <stddef.h>

//#ifdef MS_WIN32 // <-- comment this line
#ifndef _STDINT_H   // <-- add this line
#include <malloc.h>   /* for alloca() */
typedef __int8 int8_t;

Then I can build my extension with MingW32.

Comments (8)

  1. anatoly techtonik

    I confirm that after creating distutils.cfg with mentioned content, gcc is invoked correctly, but I am getting different error when try to run ABI example from doc page:

    __pycache__\_cffi__x234dfe1dxacacda2d.c:16:23: error: two or more data types in declaration specifiers
     typedef unsigned char _Bool;
                           ^
    __pycache__\_cffi__x234dfe1dxacacda2d.c:16:1: warning: useless type name in empty declaration
     typedef unsigned char _Bool;
     ^
    __pycache__\_cffi__x234dfe1dxacacda2d.c:180:17: fatal error: pwd.h: No such file or directory
     #include <pwd.h>
                     ^
    compilation terminated.
    Traceback (most recent call last):
      File "basic_example_duo.py", line 13, in <module>
        """, libraries=[])   # or a list of libraries to link with
      File "E:\r\pypy\cffi\cffi\api.py", line 341, in verify
        lib = self.verifier.load_library()
      File "E:\r\pypy\cffi\cffi\verifier.py", line 74, in load_library
        self._compile_module()
      File "E:\r\pypy\cffi\cffi\verifier.py", line 139, in _compile_module
        outputfilename = ffiplatform.compile(tmpdir, self.get_extension())
      File "E:\r\pypy\cffi\cffi\ffiplatform.py", line 25, in compile
        outputfilename = _build(tmpdir, ext)
      File "E:\r\pypy\cffi\cffi\ffiplatform.py", line 51, in _build
        raise VerificationError('%s: %s' % (e.__class__.__name__, e))
    cffi.ffiplatform.VerificationError: CompileError: command 'gcc' failed with exit status 1
    
  2. anatoly techtonik

    Cool. It works. =)

    from cffi import FFI
    ffi = FFI()
    ffi.cdef(""" 
        DWORD GetTickCount(void);
    """)
    C = ffi.verify("""
    #include <windows.h>
    """, libraries=[])   # or a list of libraries to link with
    p = C.GetTickCount()
    print(p)  
    
  3. Log in to comment