1from setuptools import setup, Extension
2import glob
3import platform
4import os
5import sys
6import re
7
8#Does gcc compile with this header and library?
9def compile_test(header, library):
10    dummy_path = os.path.join(os.path.dirname(__file__), "dummy")
11    command = "bash -c \"g++ -include " + header + " -l" + library + " -x c++ - <<<'int main() {}' -o " + dummy_path + " >/dev/null 2>/dev/null && rm " + dummy_path + " 2>/dev/null\""
12    return os.system(command) == 0
13
14max_order = "6"
15is_max_order = [s for s in sys.argv if "--max_order" in s]
16for element in is_max_order:
17    max_order = re.split('[= ]',element)[1]
18    sys.argv.remove(element)
19
20FILES = glob.glob('util/*.cc') + glob.glob('lm/*.cc') + glob.glob('util/double-conversion/*.cc') + glob.glob('python/*.cc')
21FILES = [fn for fn in FILES if not (fn.endswith('main.cc') or fn.endswith('test.cc'))]
22
23if platform.system() == 'Linux':
24    LIBS = ['stdc++', 'rt']
25elif platform.system() == 'Darwin':
26    LIBS = ['c++']
27else:
28    LIBS = []
29
30#We don't need -std=c++11 but python seems to be compiled with it now.  https://github.com/kpu/kenlm/issues/86
31ARGS = ['-O3', '-DNDEBUG', '-DKENLM_MAX_ORDER='+max_order, '-std=c++11']
32
33#Attempted fix to https://github.com/kpu/kenlm/issues/186 and https://github.com/kpu/kenlm/issues/197
34if platform.system() == 'Darwin':
35    ARGS += ["-stdlib=libc++", "-mmacosx-version-min=10.7"]
36
37if compile_test('zlib.h', 'z'):
38    ARGS.append('-DHAVE_ZLIB')
39    LIBS.append('z')
40
41if compile_test('bzlib.h', 'bz2'):
42    ARGS.append('-DHAVE_BZLIB')
43    LIBS.append('bz2')
44
45if compile_test('lzma.h', 'lzma'):
46    ARGS.append('-DHAVE_XZLIB')
47    LIBS.append('lzma')
48
49ext_modules = [
50    Extension(name='kenlm',
51        sources=FILES + ['python/kenlm.cpp'],
52        language='C++',
53        include_dirs=['.'],
54        libraries=LIBS,
55        extra_compile_args=ARGS)
56]
57
58setup(
59    name='kenlm',
60    ext_modules=ext_modules,
61    include_package_data=True,
62)
63