Commit 07673635 authored by Denis Bilenko's avatar Denis Bilenko

add a few options to setup.py that allow selection of libevent to compile against

Windows support in setup.py is dropped because it's not tested and probably does
not work anyway.
parent dcd37c15
#!/usr/bin/env python #!/usr/bin/env python
"""
gevent build & installation script
----------------------------------
If you have more than one libevent installed or it is installed in a
non-standard location, use the options to point to the right dirs:
-Idir add include dir
-Ldir add library dir
-1 prefer libevent1
-2 prefer libevent2
Alternatively,
setup.py build DIR
is a shortcut for
setup.py build -IDIR -IDIR/include LDIR/.libs
"""
import sys import sys
import os import os
import glob
import re import re
from distutils.core import Extension, setup from distutils.core import Extension, setup
from os.path import join, exists, isdir
try:
import ctypes
except ImportError:
ctypes = None
name = 'gevent.core' __version__ = re.search("__version__\s*=\s*'(.*)'", open('gevent/__init__.py').read(), re.M).group(1).strip()
sources = ['gevent/core.c'] assert __version__
ev_dir = None # override to skip searching for libevent
include_dirs = []
library_dirs = []
extra_compile_args = []
LIBEVENT_MAJOR = None # 1 or 2
VERBOSE = '-v' in sys.argv
system_dirs = ['/usr/lib/libevent.*',
'/usr/lib64/libevent.*',
'/usr/local/lib/libevent.*',
'/usr/local/lib64/libevent.*']
def check_dir(path, must_exist):
if not isdir(path):
msg = 'Not a directory: %s' % path
if must_exist:
sys.exit(msg)
try: def add_include_dir(path, must_exist=True):
any check_dir(path, must_exist)
except NameError: include_dirs.append(path)
def any(iterable):
for i in iterable: def add_library_dir(path, must_exist=True):
if i: check_dir(path, must_exist)
return True library_dirs.append(path)
def get_version_from_include_path(d):
if ev_dir is None and any(glob.glob(system_dir) for system_dir in system_dirs): if VERBOSE:
print 'found system libevent for', sys.platform print 'checking %s for event2/event.h (libevent 2) and event.h (libevent 1)' % d
gevent_core = Extension(name=name, event_h = join(d, 'event2', 'event.h')
sources=sources, if exists(event_h):
libraries=['event']) print 'Using libevent 2: %s' % event_h
elif ev_dir is None and glob.glob('%s/lib/libevent.*' % sys.prefix): return 2
print 'found installed libevent in', sys.prefix event_h = join(d, 'event.h')
gevent_core = Extension(name=name, if exists(event_h):
sources=sources, print 'Using libevent 1: %s' % event_h
include_dirs=['%s/include' % sys.prefix], return 1
library_dirs=['%s/lib' % sys.prefix],
libraries=['event']) def get_version_from_ctypes(cdll, path):
try:
get_version = cdll.event_get_version
get_version.restype = ctypes.c_char_p
except AttributeError:
pass
else:
version = get_version()
print 'Using libevent %s: %s' % (version, path)
if version.startswith('1'):
return 1
elif version.startswith('2'):
return 2
else:
print 'Wierd response from %s get_version(): %r' % (path, version)
def get_version_from_path(path):
v1 = re.search('[^\d]1\.', path)
v2 = re.search('[^\d]2\.', path)
if v1 is not None:
if v2 is None:
print 'Using libevent 1: "%s"' % path
return 1
elif v2 is not None:
print 'Using libevent 2: "%s"' % path
return 2
def get_version_from_library_path(d):
if VERBOSE:
print 'checking %s for libevent.so' % d
libevent_so = join(d, 'libevent.so')
if exists(libevent_so):
if ctypes:
return get_version_from_ctypes( ctypes.CDLL(libevent_so), libevent_so )
else:
return get_version_from_path(d)
def unique(lst):
result = []
for item in lst:
if item not in result:
result.append(item)
return result
# parse options: -I name / Iname / -L name / -Lname / -1 / -2
i = 1
while i < len(sys.argv):
arg = sys.argv[i]
if arg == '-I':
del sys.argv[i]
add_include_dir(sys.argv[i])
elif arg.startswith('-I'):
add_include_dir(arg[2:])
elif arg == '-L':
del sys.argv[i]
add_library_dir(sys.argv[i])
elif arg.startswith('-L'):
add_library_dir(arg[2:])
elif arg == '-1':
LIBEVENT_MAJOR = 1
elif arg == '-2':
LIBEVENT_MAJOR = 2
else:
i = i+1
continue
del sys.argv[i]
if len(sys.argv)>=3 and isdir(sys.argv[-1]):
libevent_source_path = sys.argv[-1]
del sys.argv[-1]
add_include_dir(join(libevent_source_path, 'include'), must_exist=False)
add_include_dir(libevent_source_path, must_exist=False)
add_library_dir(join(libevent_source_path, '.libs'), must_exist=False)
if not sys.argv[1:] or '-h' in sys.argv or '--help' in sys.argv:
print __doc__
else: else:
if ev_dir is None: # try to figure out libevent version from -I and -L options
l = glob.glob('../libevent*') for d in include_dirs:
l.reverse() if LIBEVENT_MAJOR is not None:
for path in l:
if os.path.isdir(path):
ev_dir = path
break break
if ev_dir: LIBEVENT_MAJOR = get_version_from_include_path(d)
print 'found libevent build directory', ev_dir
ev_incdirs = [ev_dir, ev_dir + '/compat'] for d in library_dirs:
ev_extargs = [] if LIBEVENT_MAJOR is not None:
ev_extobjs = [] break
ev_libraries = ['event'] LIBEVENT_MAJOR = get_version_from_library_path(d)
if sys.platform == 'win32': if LIBEVENT_MAJOR is None and ctypes:
ev_incdirs.extend(['%s/WIN32-Code' % ev_dir, libevent = ctypes.cdll.LoadLibrary('libevent.so')
'%s/compat' % ev_dir]) LIBEVENT_MAJOR = get_version_from_ctypes(libevent, 'libevent.so')
sources.extend(['%s/%s' % (ev_dir, x) for x in [
'WIN32-Code/misc.c', 'WIN32-Code/win32.c', # search system library dirs (unless explicit library directory was provided)
'log.c', 'event.c']]) if LIBEVENT_MAJOR is None and not library_dirs:
ev_extargs = ['-DWIN32', '-DHAVE_CONFIG_H'] library_paths = os.environ.get('LD_LIBRARY_PATH', '').split(':')
ev_libraries = ['wsock32'] library_paths += ['%s/lib' % sys.prefix,
else: '%s/lib64' % sys.prefix,
ev_extobjs = glob.glob('%s/*.o' % dir) '/usr/lib/',
'/usr/lib64/',
gevent_core = Extension(name=name, '/usr/local/lib/',
sources=sources, '/usr/local/lib64/']
include_dirs=ev_incdirs,
extra_compile_args=ev_extargs, for x in unique(library_paths):
extra_objects=ev_extobjs, LIBEVENT_MAJOR = get_version_from_library_path(x)
libraries=ev_libraries) if LIBEVENT_MAJOR is not None:
add_library_dir(x)
break
if LIBEVENT_MAJOR is None:
print 'Cannot guess the version of libevent installed on your system.'
else: else:
sys.stderr.write("\nWARNING: couldn't find libevent installation or build directory: assuming system-wide libevent is installed.\n\n") extra_compile_args.append( '-DUSE_LIBEVENT_%s' % LIBEVENT_MAJOR )
gevent_core = Extension(name=name,
sources=sources,
libraries=['event']) gevent_core = Extension(name = 'gevent.core',
sources=['gevent/core.c'],
include_dirs=include_dirs,
library_dirs=library_dirs,
libraries=['event'],
extra_compile_args=extra_compile_args)
version = re.search("__version__\s*=\s*'(.*)'", open('gevent/__init__.py').read(), re.M).group(1).strip()
assert version, version
if __name__ == '__main__': if __name__ == '__main__':
setup( setup(
name='gevent', name='gevent',
version=version, version=__version__,
description='Python network library that uses greenlet and libevent for easy and scalable concurrency', description='Python network library that uses greenlet and libevent for easy and scalable concurrency',
author='Denis Bilenko', author='Denis Bilenko',
author_email='denis.bilenko@gmail.com', author_email='denis.bilenko@gmail.com',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment