setup.py 1.5 KB
Newer Older
Y
Yibing Liu 已提交
1 2 3 4 5 6 7 8 9 10 11 12
from setuptools import setup, Extension
import glob
import platform
import os


def compile_test(header, library):
    dummy_path = os.path.join(os.path.dirname(__file__), "dummy")
    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\""
    return os.system(command) == 0


13 14
FILES = glob.glob('kenlm/util/*.cc') + glob.glob('kenlm/lm/*.cc') + glob.glob(
    'kenlm/util/double-conversion/*.cc')
Y
Yibing Liu 已提交
15 16 17 18 19 20 21 22
FILES = [
    fn for fn in FILES if not (fn.endswith('main.cc') or fn.endswith('test.cc'))
]

LIBS = ['stdc++']
if platform.system() != 'Darwin':
    LIBS.append('rt')

23
ARGS = ['-O3', '-DNDEBUG', '-DKENLM_MAX_ORDER=6', '-std=c++11']
Y
Yibing Liu 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36

if compile_test('zlib.h', 'z'):
    ARGS.append('-DHAVE_ZLIB')
    LIBS.append('z')

if compile_test('bzlib.h', 'bz2'):
    ARGS.append('-DHAVE_BZLIB')
    LIBS.append('bz2')

if compile_test('lzma.h', 'lzma'):
    ARGS.append('-DHAVE_XZLIB')
    LIBS.append('lzma')

37
os.system('swig -python -c++ ./decoders.i')
Y
Yibing Liu 已提交
38 39 40

ctc_beam_search_decoder_module = [
    Extension(
41 42
        name='_swig_decoders',
        sources=FILES + glob.glob('*.cxx') + glob.glob('*.cpp'),
Y
Yibing Liu 已提交
43
        language='C++',
44
        include_dirs=['.', './kenlm', './openfst-1.6.3/src/include'],
Y
Yibing Liu 已提交
45 46 47 48 49
        libraries=LIBS,
        extra_compile_args=ARGS)
]

setup(
50
    name='swig_decoders',
Y
Yibing Liu 已提交
51
    version='0.1',
52
    description="""CTC decoders""",
Y
Yibing Liu 已提交
53
    ext_modules=ctc_beam_search_decoder_module,
54
    py_modules=['swig_decoders'], )