Build the CFFI lib in a nice way when possible.

This commit is contained in:
Jason Madden 2016-09-22 16:14:58 -05:00
parent c9467db5c7
commit 0fb95054d3
No known key found for this signature in database
GPG Key ID: 349F84431A08B99E
4 changed files with 52 additions and 12 deletions

1
.gitignore vendored
View File

@ -5,6 +5,7 @@
*.egg-info
*.pyc
*.pyo
*.so
*.sublime-*
.coverage
htmlcov/

36
relstorage/cache/_cache_ring_build.py vendored Normal file
View File

@ -0,0 +1,36 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# Copyright (c) 2016 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
from __future__ import absolute_import, print_function, division
import os
from cffi import FFI
this_dir = os.path.dirname(os.path.abspath(__file__))
ffi = FFI()
with open(os.path.join(this_dir, 'cache_ring.h')) as f:
ffi.cdef(f.read())
ffi.set_source('relstorage.cache._cache_ring',
'#include "cache_ring.c"',
include_dirs=[this_dir])
def verify():
return ffi.verify('#include "cache_ring.c"',
include_dirs=[this_dir])
if __name__ == '__main__':
ffi.compile()

View File

@ -19,18 +19,17 @@ Segmented LRU implementations.
"""
import os
from cffi import FFI
this_dir = os.path.dirname(os.path.abspath(__file__))
ffi = FFI()
with open(os.path.join(this_dir, 'cache_ring.h')) as f:
ffi.cdef(f.read())
_FFI_RING = ffi.verify("""
#include "cache_ring.c"
""", include_dirs=[this_dir])
try:
from relstorage.cache import _cache_ring
except ImportError:
# Must be on an old pypy version, stuck with an old
# CFFI
from relstorage.cache import _cache_ring_build
_FFI_RING = _cache_ring_build.verify()
ffi = _cache_ring_build.ffi
else:
ffi = _cache_ring.ffi
_FFI_RING = _cache_ring.lib
_ring_move_to_head = _FFI_RING.rsc_ring_move_to_head
_ring_del = _FFI_RING.rsc_ring_del

View File

@ -84,6 +84,10 @@ setup(
'zc.lockfile',
# ZODB and ZEO are handled as environment-markers in extras.
],
# If a new-enough CFFI is installed, build the module as part of installation.
# Otherwise, we'll build it at import time. The wheels we distribute should have
# cffi installed so we distribute the built binaries.
cffi_modules = ['relstorage/cache/_cache_ring_build.py:ffi'],
tests_require=tests_require,
extras_require={
# We previously used MySQL-python (C impl) on CPython 2.7,