Author: Armin Rigo <[email protected]>
Branch: cffi-1.0
Changeset: r1900:3d48fee148d1
Date: 2015-05-02 09:58 +0200
http://bitbucket.org/cffi/cffi/changeset/3d48fee148d1/

Log:    Add ffi.distutils_extension() and adapt a demo to show the non-
        setuptools way

diff --git a/_cffi1/recompiler.py b/_cffi1/recompiler.py
--- a/_cffi1/recompiler.py
+++ b/_cffi1/recompiler.py
@@ -815,30 +815,38 @@
                 s = s.encode('ascii')
             super(NativeIO, self).write(s)
 
-def make_c_source(ffi, module_name, preamble, target_c_file=NativeIO):
+def make_c_source(ffi, module_name, preamble, target_c_file):
     recompiler = Recompiler(ffi, module_name)
     recompiler.collect_type_table()
-    if target_c_file is NativeIO:
-        f = NativeIO()
-        recompiler.write_source_to_f(f, preamble)
-        return f.getvalue()
-    else:
-        with open(target_c_file, 'w') as f:
-            recompiler.write_source_to_f(f, preamble)
-        return None
+    f = NativeIO()
+    recompiler.write_source_to_f(f, preamble)
+    output = f.getvalue()
+    try:
+        with open(target_c_file, 'r') as f1:
+            if f1.read(len(output) + 1) != output:
+                raise IOError
+        return False     # already up-to-date
+    except IOError:
+        with open(target_c_file, 'w') as f1:
+            f1.write(output)
+        return True
 
 def _get_extension(module_name, c_file, kwds):
     source_name = ffiplatform.maybe_relative_path(c_file)
     return ffiplatform.get_extension(source_name, module_name, **kwds)
 
-def recompile(ffi, module_name, preamble, tmpdir='.', **kwds):
+def recompile(ffi, module_name, preamble, tmpdir='.',
+              call_c_compiler=True, **kwds):
     if not isinstance(module_name, str):
         module_name = module_name.encode('ascii')
     c_file = os.path.join(tmpdir, module_name + '.c')
     ext = _get_extension(module_name, c_file, kwds)
-    make_c_source(ffi, module_name, preamble, c_file)
-    outputfilename = ffiplatform.compile(tmpdir, ext)
-    return outputfilename
+    updated = make_c_source(ffi, module_name, preamble, c_file)
+    if call_c_compiler:
+        outputfilename = ffiplatform.compile(tmpdir, ext)
+        return outputfilename
+    else:
+        return ext, updated
 
 def verify(ffi, module_name, preamble, *args, **kwds):
     from _cffi1.udir import udir
diff --git a/_cffi1/setuptools_ext.py b/_cffi1/setuptools_ext.py
--- a/_cffi1/setuptools_ext.py
+++ b/_cffi1/setuptools_ext.py
@@ -42,17 +42,10 @@
     def make_mod(tmpdir):
         file_name = module_name + '.c'
         log.info("generating cffi module %r" % file_name)
-        output = recompiler.make_c_source(ffi, module_name, source)
         mkpath(tmpdir)
         c_file = os.path.join(tmpdir, file_name)
-        try:
-            with open(c_file, 'r') as f1:
-                if f1.read() != output:
-                    raise IOError
-        except IOError:
-            with open(c_file, 'w') as f1:
-                f1.write(output)
-        else:
+        updated = recompiler.make_c_source(ffi, module_name, source, c_file)
+        if not updated:
             log.info("already up-to-date")
         return c_file
 
diff --git a/cffi/api.py b/cffi/api.py
--- a/cffi/api.py
+++ b/cffi/api.py
@@ -482,6 +482,22 @@
         self._recompiler_module_name = module_name
         self._assigned_source = (source, kwds)
 
+    def distutils_extension(self, tmpdir='.'):
+        from distutils.dir_util import mkpath
+        from _cffi1 import recompile
+        #
+        if not hasattr(self, '_assigned_source'):
+            raise ValueError("set_source() must be called before"
+                             " distutils_extension()")
+        source, kwds = self._assigned_source
+        mkpath(tmpdir)
+        ext, updated = recompile(self, self._recompiler_module_name,
+                                 source, tmpdir=tmpdir,
+                                 call_c_compiler=False, **kwds)
+        if updated:
+            sys.stderr.write("generated %r\n" % (ext.sources[0],))
+        return ext
+
     def compile(self, tmpdir='.'):
         from _cffi1 import recompile
         #
diff --git a/demo/readdir2_setup.py b/demo/readdir2_setup.py
--- a/demo/readdir2_setup.py
+++ b/demo/readdir2_setup.py
@@ -1,13 +1,9 @@
-from setuptools import setup
+from distutils.core import setup
+import readdir2_build
 
 setup(
     name="readdir2",
     version="0.1",
     py_modules=["readdir2"],
-    setup_requires=["cffi>=1.0.dev0"],
-    cffi_modules=[
-        "readdir2_build:ffi",
-    ],
-    install_requires=["cffi>=1.0.dev0"],   # should maybe be "cffi-backend" 
only?
-    zip_safe=False,
+    ext_modules=[readdir2_build.ffi.distutils_extension('build')],
 )
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to