summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--_cffi1/recompiler.py34
-rw-r--r--_cffi1/setuptools_ext.py11
-rw-r--r--cffi/api.py16
-rw-r--r--demo/readdir2_setup.py10
4 files changed, 42 insertions, 29 deletions
diff --git a/_cffi1/recompiler.py b/_cffi1/recompiler.py
index 481572d..eb96656 100644
--- a/_cffi1/recompiler.py
+++ b/_cffi1/recompiler.py
@@ -815,30 +815,38 @@ else:
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
index 346fd71..b9ee87c 100644
--- a/_cffi1/setuptools_ext.py
+++ b/_cffi1/setuptools_ext.py
@@ -42,17 +42,10 @@ def add_cffi_module(dist, mod_spec):
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
index 66fc30a..19c210d 100644
--- a/cffi/api.py
+++ b/cffi/api.py
@@ -482,6 +482,22 @@ class FFI(object):
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
index e38ebf8..bd8c19f 100644
--- 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')],
)