| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- #!/usr/bin/env python
- # Copyright (c) 2017-present, Facebook, Inc.
- # All rights reserved.
- #
- # This source code is licensed under the BSD-style license found in the
- # LICENSE file in the root directory of this source tree. An additional grant
- # of patent rights can be found in the PATENTS file in the same directory.
- from __future__ import absolute_import
- from __future__ import division
- from __future__ import print_function
- from __future__ import unicode_literals
- from setuptools import setup, Extension
- from setuptools.command.build_ext import build_ext
- import sys
- import setuptools
- import os
- __version__ = '0.0.2'
- FASTTEXT_SRC = "src"
- # Based on https://github.com/pybind/python_example
- class get_pybind_include(object):
- """Helper class to determine the pybind11 include path
- The purpose of this class is to postpone importing pybind11
- until it is actually installed, so that the ``get_include()``
- method can be invoked. """
- def __init__(self, user=False):
- self.user = user
- def __str__(self):
- import pybind11
- return pybind11.get_include(self.user)
- fasttext_src_files = map(str, os.listdir(FASTTEXT_SRC))
- fasttext_src_cc = list(filter(lambda x: x.endswith('.cc'), fasttext_src_files))
- fasttext_src_cc = list(
- map(lambda x: str(os.path.join(FASTTEXT_SRC, x)), fasttext_src_cc)
- )
- ext_modules = [
- Extension(
- str('fasttext_pybind'),
- [
- str('python/fastText/pybind/fasttext_pybind.cc'),
- ] + fasttext_src_cc,
- include_dirs=[
- # Path to pybind11 headers
- get_pybind_include(),
- get_pybind_include(user=True),
- # Path to fasttext source code
- FASTTEXT_SRC,
- ],
- language='c++',
- extra_compile_args=["-O3"],
- ),
- ]
- # As of Python 3.6, CCompiler has a `has_flag` method.
- # cf http://bugs.python.org/issue26689
- def has_flag(compiler, flagname):
- """Return a boolean indicating whether a flag name is supported on
- the specified compiler.
- """
- import tempfile
- with tempfile.NamedTemporaryFile('w', suffix='.cpp') as f:
- f.write('int main (int argc, char **argv) { return 0; }')
- try:
- compiler.compile([f.name], extra_postargs=[flagname])
- except setuptools.distutils.errors.CompileError:
- return False
- return True
- def cpp_flag(compiler):
- """Return the -std=c++[11/14] compiler flag.
- The c++14 is preferred over c++11 (when it is available).
- """
- if has_flag(compiler, '-std=c++14'):
- return '-std=c++14'
- elif has_flag(compiler, '-std=c++11'):
- return '-std=c++11'
- else:
- raise RuntimeError(
- 'Unsupported compiler -- at least C++11 support '
- 'is needed!'
- )
- class BuildExt(build_ext):
- """A custom build extension for adding compiler-specific options."""
- c_opts = {
- 'msvc': ['/EHsc'],
- 'unix': [],
- }
- if sys.platform == 'darwin':
- c_opts['unix'] += ['-stdlib=libc++', '-mmacosx-version-min=10.7']
- def build_extensions(self):
- ct = self.compiler.compiler_type
- opts = self.c_opts.get(ct, [])
- if ct == 'unix':
- opts.append('-DVERSION_INFO="%s"' % self.distribution.get_version())
- opts.append(cpp_flag(self.compiler))
- if has_flag(self.compiler, '-fvisibility=hidden'):
- opts.append('-fvisibility=hidden')
- elif ct == 'msvc':
- opts.append(
- '/DVERSION_INFO=\\"%s\\"' % self.distribution.get_version()
- )
- for ext in self.extensions:
- ext.extra_compile_args = opts
- build_ext.build_extensions(self)
- setup(
- name='fastTextpy',
- version=__version__,
- author='Christian Puhrsch',
- author_email='[email protected]',
- description='fastText Python bindings',
- long_description='',
- ext_modules=ext_modules,
- url='https://github.com/facebookresearch/fastText',
- license='BSD',
- install_requires=['pybind11>=2.2', "setuptools >= 0.7.0"],
- cmdclass={'build_ext': BuildExt},
- packages=[str('fastText')],
- package_dir={str(''): str('python')},
- zip_safe=False
- )
|