[pypy-commit] cffi default: Real test of a pkgconfig integration

vyskocilm pypy.commits at gmail.com
Thu Jan 31 04:26:55 EST 2019


Author: Michal Vyskocil <michal.vyskocil at gmail.com>
Branch: 
Changeset: r3197:e0251d298a40
Date: 2019-01-08 11:08 +0100
http://bitbucket.org/cffi/cffi/changeset/e0251d298a40/

Log:	Real test of a pkgconfig integration

	Fix encoding errors

	Given testing Python program ``` from cffi import FFI ffibuilder =
	FFI()

	ffibuilder.cdef( "char* zsys_hostname();" )

	ffibuilder.set_source( "_czmq", "#include <czmq.h>",
	pkgconfig=["libczmq"] )

	 if __name__ == "__main__": ffibuilder.compile(verbose=True) ```

	We can run ffibuilder from source dir of czmq

	``` PKG_CONFIG_PATH=`pwd`/src python3 t.py generating ./_czmq.c ...
	gcc -pthread -shared -flto -fuse-linker-plugin -ffat-lto-objects
	-flto-partition=none ./_czmq.o -L/usr/local/lib64 -L/usr/lib64
	-lczmq -lzmq -lpython3.6m -o ./_czmq.cpython-36m-x86_64-linux-gnu.so
	```

	``` python3 t.py generating ./_czmq.c ... gcc -pthread -shared -flto
	-fuse-linker-plugin -ffat-lto-objects -flto-partition=none ./_czmq.o
	-L/usr/lib64 -lczmq -lpython3.6m -o ./_czmq.cpython-36m-x86_64
	-linux-gnu.so ```

	Note that in the first case `/usr/local` has been added to the
	compiler path as provided by local pkg-config file.

diff --git a/cffi/api.py b/cffi/api.py
--- a/cffi/api.py
+++ b/cffi/api.py
@@ -645,7 +645,7 @@
             if "libraries" in kwds:
                 del kwds["libraries"]  # real library names are going to be
                                         # provided by pkg-config
-            pkgconfig.merge_flags(kwds, pkgconfig.kwargs(kwds["pkgconfig"]))
+            pkgconfig.merge_flags(kwds, pkgconfig.flags(kwds["pkgconfig"]))
             del kwds["pkgconfig"]
         self._assigned_source = (str(module_name), source,
                                  source_extension, kwds)
diff --git a/cffi/pkgconfig.py b/cffi/pkgconfig.py
--- a/cffi/pkgconfig.py
+++ b/cffi/pkgconfig.py
@@ -1,5 +1,6 @@
 # pkg-config, https://www.freedesktop.org/wiki/Software/pkg-config/ integration for cffi
 import subprocess
+import sys
 
 def is_installed():
     """Check if pkg-config is installed or not"""
@@ -53,28 +54,29 @@
     # drop starting -I -L -l from cflags
     def dropILl(string):
         def _dropILl(string):
-            if string.startswith(b"-I") or string.startswith(b"-L") or string.startswith(b"-l"):
+            if string.startswith(u"-I") or string.startswith(u"-L") or string.startswith(u"-l"):
                 return string [2:]
         return [_dropILl(x) for x in string.split()]
 
     # convert -Dfoo=bar to list of tuples [("foo", "bar")] expected by cffi
     def macros(string):
         def _macros(string):
-            return tuple(string [2:].split(b"=", 2))
-        return [_macros(x) for x in string.split() if x.startswith(b"-D")]
+            return tuple(string [2:].split(u"=", 2))
+        return [_macros(x) for x in string.split() if x.startswith(u"-D")]
 
     def drop_macros(string):
-        return [x for x in string.split() if not x.startswith(b"-D")]
+        return [x for x in string.split() if not x.startswith(u"-D")]
 
     # return kwargs for given libname
     def kwargs(libname):
+        fse = sys.getfilesystemencoding()
         return {
-                "include_dirs" : dropILl(call(libname, "--cflags-only-I")),
-                "library_dirs" : dropILl(call(libname, "--libs-only-L")),
-                "libraries" : dropILl(call(libname, "--libs-only-l")),
-                "define_macros" : macros(call(libname, "--cflags-only-other")),
-                "extra_compile_args" : drop_macros(call(libname, "--cflags-only-other")),
-                "extra_link_args" : call(libname, "--libs-only-other").split()
+                "include_dirs" : dropILl(call(libname, "--cflags-only-I").decode(fse)),
+                "library_dirs" : dropILl(call(libname, "--libs-only-L").decode(fse)),
+                "libraries" : dropILl(call(libname, "--libs-only-l").decode('ascii')),
+                "define_macros" : macros(call(libname, "--cflags-only-other").decode('ascii')),
+                "extra_compile_args" : drop_macros(call(libname, "--cflags-only-other").decode('ascii')),
+                "extra_link_args" : call(libname, "--libs-only-other").decode('ascii').split()
                 }
 
     # merge all arguments together
diff --git a/testing/cffi1/test_pkgconfig.py b/testing/cffi1/test_pkgconfig.py
--- a/testing/cffi1/test_pkgconfig.py
+++ b/testing/cffi1/test_pkgconfig.py
@@ -34,10 +34,10 @@
 def test_pkgconfig():
     kwargs = pkgconfig.flags("python-3.6")
     assert kwargs == {
-        'include_dirs': [b'/usr/include/python3.6m'],
-        'library_dirs': [b'/usr/lib64'],
-        'libraries': [b'python3.6'],
-        'define_macros': [(b'CFFI_TEST', b'1')],
-        'extra_compile_args': [b'-O42'],
-        'extra_link_args': [b'-lm']
+        'include_dirs': [u'/usr/include/python3.6m'],
+        'library_dirs': [u'/usr/lib64'],
+        'libraries': [u'python3.6'],
+        'define_macros': [(u'CFFI_TEST', u'1')],
+        'extra_compile_args': [u'-O42'],
+        'extra_link_args': [u'-lm']
     }


More information about the pypy-commit mailing list