`python setup.py install` fails with MinGW

Issue #159 resolved
anatoly techtonik
created an issue
```running install
running bdist_egg
running egg_info
writing requirements to cffi.egg-info\requires.txt
writing cffi.egg-info\PKG-INFO
writing top-level names to cffi.egg-info\top_level.txt
writing manifest file 'cffi.egg-info\SOURCES.txt'
installing library code to build\bdist.win32\egg
running install_lib
running build_py
running build_ext
building '_cffi_backend' extension
E:\mingw32\bin\gcc.exe -mdll -O -Wall -Ic/libffi_msvc -IC:\Python27\include -IC:\Python27\PC -c c/_cffi_backend.c -o build\temp.win32-2.7\Release\c\_cffi_backend.o
c/_cffi_backend.c: In function 'convert_array_from_object':
c/_cffi_backend.c:1108:26: warning: unknown conversion type character 'z' in format [-Wformat=]
ct->ct_name, n);
^
c/_cffi_backend.c:1108:26: warning: too many arguments for format [-Wformat-extra-args]
c/_cffi_backend.c:1133:30: warning: unknown conversion type character 'z' in format [-Wformat=]
"(got %zd characters)", ct->ct_name, n);
^
c/_cffi_backend.c:1133:30: warning: too many arguments for format [-Wformat-extra-args]
c/_cffi_backend.c:1153:30: warning: unknown conversion type character 'z' in format [-Wformat=]
"(got %zd characters)", ct->ct_name, n);
^
c/_cffi_backend.c:1153:30: warning: too many arguments for format [-Wformat-extra-args]
c/_cffi_backend.c: In function 'convert_struct_from_object':
c/_cffi_backend.c:1186:26: warning: unknown conversion type character 'z' in format [-Wformat=]
ct->ct_name, n);
^
c/_cffi_backend.c:1186:26: warning: too many arguments for format [-Wformat-extra-args]
c/_cffi_backend.c:1199:30: warning: unknown conversion type character 'z' in format [-Wformat=]
ct->ct_name, n);
^
c/_cffi_backend.c:1199:30: warning: too many arguments for format [-Wformat-extra-args]
c/_cffi_backend.c: In function 'cdata_repr':
c/_cffi_backend.c:1586:13: warning: unknown conversion type character 'L' in format [-Wformat=]
sprintf(buffer, "%LE", lvalue);
^
c/_cffi_backend.c:1586:13: warning: too many arguments for format [-Wformat-extra-args]
c/_cffi_backend.c:1598:9: warning: unknown conversion type character 'z' in format [-Wformat=]
s = PyText_FromFormat("sliced length %zd", get_array_length(cd));
^
c/_cffi_backend.c:1598:9: warning: too many arguments for format [-Wformat-extra-args]
c/_cffi_backend.c: In function 'cdataowning_repr':
c/_cffi_backend.c:1650:30: warning: unknown conversion type character 'z' in format [-Wformat=]
cd->c_type->ct_name, size);
^
c/_cffi_backend.c:1650:30: warning: too many arguments for format [-Wformat-extra-args]
c/_cffi_backend.c: In function '_cdata_get_indexed_ptr':
c/_cffi_backend.c:1823:26: warning: unknown conversion type character 'z' in format [-Wformat=]
i, get_array_length(cd));
^
c/_cffi_backend.c:1823:26: warning: unknown conversion type character 'z' in format [-Wformat=]
c/_cffi_backend.c:1823:26: warning: too many arguments for format [-Wformat-extra-args]
c/_cffi_backend.c: In function '_cdata_getslicearg':
c/_cffi_backend.c:1875:26: warning: unknown conversion type character 'z' in format [-Wformat=]
stop, get_array_length(cd));
^
c/_cffi_backend.c:1875:26: warning: unknown conversion type character 'z' in format [-Wformat=]
c/_cffi_backend.c:1875:26: warning: too many arguments for format [-Wformat-extra-args]
c/_cffi_backend.c: In function 'cdata_ass_slice':
c/_cffi_backend.c:1954:26: warning: unknown conversion type character 'z' in format [-Wformat=]
length, PyBytes_GET_SIZE(v));
^
c/_cffi_backend.c:1954:26: warning: unknown conversion type character 'z' in format [-Wformat=]
c/_cffi_backend.c:1954:26: warning: too many arguments for format [-Wformat-extra-args]
c/_cffi_backend.c:1972:30: warning: unknown conversion type character 'z' in format [-Wformat=]
length, i);
^
c/_cffi_backend.c:1972:30: warning: unknown conversion type character 'z' in format [-Wformat=]
c/_cffi_backend.c:1972:30: warning: too many arguments for format [-Wformat-extra-args]
c/_cffi_backend.c:1986:22: warning: unknown conversion type character 'z' in format [-Wformat=]
"got more than %zd values to unpack", length);
^
c/_cffi_backend.c:1986:22: warning: too many arguments for format [-Wformat-extra-args]
c/_cffi_backend.c: In function 'cdata_call':
c/_cffi_backend.c:2370:30: warning: unknown conversion type character 'z' in format [-Wformat=]
i + 1, Py_TYPE(obj)->tp_name);
^
c/_cffi_backend.c:2370:30: warning: format '%s' expects argument of type 'char *', but argument 3 has type 'int' [-Wformat=]
c/_cffi_backend.c:2370:30: warning: too many arguments for format [-Wformat-extra-args]
c/_cffi_backend.c: In function 'cast_to_integer_or_char':
c/_cffi_backend.c:2919:26: warning: unknown conversion type character 'z' in format [-Wformat=]
PyString_GET_SIZE(ob), ct->ct_name);
^
c/_cffi_backend.c:2919:26: warning: format '%s' expects argument of type 'char *', but argument 3 has type 'Py_ssize_t' [-Wformat=]
c/_cffi_backend.c:2919:26: warning: too many arguments for format [-Wformat-extra-args]
c/_cffi_backend.c:2931:26: warning: unknown conversion type character 'z' in format [-Wformat=]
PyUnicode_GET_SIZE(ob), ct->ct_name);
^
c/_cffi_backend.c:2931:26: warning: format '%s' expects argument of type 'char *', but argument 3 has type 'Py_ssize_t' [-Wformat=]
c/_cffi_backend.c:2931:26: warning: too many arguments for format [-Wformat-extra-args]
c/_cffi_backend.c: In function 'new_array_type':
c/_cffi_backend.c:3483:9: warning: unknown conversion type character 'l' in format [-Wformat=]
sprintf(extra_text, "[%llu]", (unsigned PY_LONG_LONG)length);
^
c/_cffi_backend.c:3483:9: warning: too many arguments for format [-Wformat-extra-args]
c/_cffi_backend.c: In function 'b_complete_struct_or_union':
c/_cffi_backend.c:3912:22: warning: unknown conversion type character 'z' in format [-Wformat=]
"up to %zd", ct->ct_name, totalsize, boffsetmax);
^
c/_cffi_backend.c:3912:22: warning: unknown conversion type character 'z' in format [-Wformat=]
c/_cffi_backend.c:3912:22: warning: too many arguments for format [-Wformat-extra-args]
E:\mingw32\bin\gcc.exe -mdll -O -Wall -Ic/libffi_msvc -IC:\Python27\include -IC:\Python27\PC -c c/libffi_msvc\ffi.c -o build\temp.win32-2.7\Release\c\libffi_msvc\ffi.o
E:\mingw32\bin\gcc.exe -mdll -O -Wall -Ic/libffi_msvc -IC:\Python27\include -IC:\Python27\PC -c c/libffi_msvc\prep_cif.c -o build\temp.win32-2.7\Release\c\libffi_msvc\prep_cif.o
E:\mingw32\bin\gcc.exe -mdll -O -Wall -Ic/libffi_msvc -IC:\Python27\include -IC:\Python27\PC -c c/libffi_msvc\types.c -o build\temp.win32-2.7\Release\c\libffi_msvc\types.o
E:\mingw32\bin\gcc.exe -mdll -O -Wall -Ic/libffi_msvc -IC:\Python27\include -IC:\Python27\PC -c c/libffi_msvc\win32.c -o build\temp.win32-2.7\Release\c\libffi_msvc\win32.o
c/libffi_msvc\win32.c:47:1: warning: 'naked' attribute directive ignored [-Wattributes]
{
^
c/libffi_msvc\win32.c: In function 'ffi_call_x86':
c/libffi_msvc\win32.c:48:2: error: '_asm' undeclared (first use in this function)
_asm {
^
c/libffi_msvc\win32.c:48:2: note: each undeclared identifier is reported only once for each function it appears in
c/libffi_msvc\win32.c:48:7: error: expected ';' before '{' token
_asm {
^
c/libffi_msvc\win32.c:162:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
error: command 'gcc' failed with exit status 1
```

This is with `gcc.EXE (i686-win32-sjlj-rev1, Built by MinGW-W64 project) 4.9.0`

Won't fix it myself. If someone with mingw feels like it, I would accept a patch.

1. reporter

So, we need a preprocessor that turns the VC __asm section format to GCC asm format. The task will be mush easier if it is possible to move the inline Assembly section into external .asm file. `ffi_call_win32_x86.asm` Is that possible?

FWIW I managed to get it working on an MSYS2 / MinGW (32 bit) environment by modifying the source as follows:

```diff -r 630f22d1d446 c/misc_win32.h
--- a/c/misc_win32.h    Tue Sep 27 11:37:38 2016 +0200
+++ b/c/misc_win32.h    Fri Oct 07 14:56:28 2016 +0200
@@ -238,4 +238,4 @@
/************************************************************/
/* obscure */

-#define ffi_prep_closure(a,b,c,d)  ffi_prep_closure_loc(a,b,c,d,a)
+/*#define ffi_prep_closure(a,b,c,d)  ffi_prep_closure_loc(a,b,c,d,a)*/
diff -r 630f22d1d446 setup.py
--- a/setup.py  Tue Sep 27 11:37:38 2016 +0200
+++ b/setup.py  Fri Oct 07 14:56:28 2016 +0200
@@ -58,7 +58,8 @@
config = Distribution().get_command_obj('config')
if ok:
+               pass
else:
ok1 = config.try_compile('int some_regular_variable_42;')
if not ok1:
@@ -88,6 +89,7 @@

if sys.platform == 'win32':
COMPILE_LIBFFI = 'c/libffi_msvc'    # from the CPython distribution
+    COMPILE_LIBFFI = None
else:
COMPILE_LIBFFI = None
```

Thanks Saúl! I committed something in c10bc64fa183 that tries to follow your patch but also continues to work on MSVC. Can you check if MinGW compiles now with c10bc64fa183? Thanks!