1
0

setup.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #!/usr/bin/env python
  2. # Copyright (c) 2017-present, Facebook, Inc.
  3. # All rights reserved.
  4. #
  5. # This source code is licensed under the BSD-style license found in the
  6. # LICENSE file in the root directory of this source tree. An additional grant
  7. # of patent rights can be found in the PATENTS file in the same directory.
  8. from __future__ import absolute_import
  9. from __future__ import division
  10. from __future__ import print_function
  11. from __future__ import unicode_literals
  12. from setuptools import setup, Extension
  13. from setuptools.command.build_ext import build_ext
  14. import sys
  15. import setuptools
  16. import os
  17. __version__ = '0.0.2'
  18. FASTTEXT_SRC = "src"
  19. # Based on https://github.com/pybind/python_example
  20. class get_pybind_include(object):
  21. """Helper class to determine the pybind11 include path
  22. The purpose of this class is to postpone importing pybind11
  23. until it is actually installed, so that the ``get_include()``
  24. method can be invoked. """
  25. def __init__(self, user=False):
  26. self.user = user
  27. def __str__(self):
  28. import pybind11
  29. return pybind11.get_include(self.user)
  30. fasttext_src_files = map(str, os.listdir(FASTTEXT_SRC))
  31. fasttext_src_cc = list(filter(lambda x: x.endswith('.cc'), fasttext_src_files))
  32. fasttext_src_cc = list(
  33. map(lambda x: str(os.path.join(FASTTEXT_SRC, x)), fasttext_src_cc)
  34. )
  35. ext_modules = [
  36. Extension(
  37. str('fasttext_pybind'),
  38. [
  39. str('python/fastText/pybind/fasttext_pybind.cc'),
  40. ] + fasttext_src_cc,
  41. include_dirs=[
  42. # Path to pybind11 headers
  43. get_pybind_include(),
  44. get_pybind_include(user=True),
  45. # Path to fasttext source code
  46. FASTTEXT_SRC,
  47. ],
  48. language='c++',
  49. extra_compile_args=["-O3"],
  50. ),
  51. ]
  52. # As of Python 3.6, CCompiler has a `has_flag` method.
  53. # cf http://bugs.python.org/issue26689
  54. def has_flag(compiler, flagname):
  55. """Return a boolean indicating whether a flag name is supported on
  56. the specified compiler.
  57. """
  58. import tempfile
  59. with tempfile.NamedTemporaryFile('w', suffix='.cpp') as f:
  60. f.write('int main (int argc, char **argv) { return 0; }')
  61. try:
  62. compiler.compile([f.name], extra_postargs=[flagname])
  63. except setuptools.distutils.errors.CompileError:
  64. return False
  65. return True
  66. def cpp_flag(compiler):
  67. """Return the -std=c++[11/14] compiler flag.
  68. The c++14 is preferred over c++11 (when it is available).
  69. """
  70. if has_flag(compiler, '-std=c++14'):
  71. return '-std=c++14'
  72. elif has_flag(compiler, '-std=c++11'):
  73. return '-std=c++11'
  74. else:
  75. raise RuntimeError(
  76. 'Unsupported compiler -- at least C++11 support '
  77. 'is needed!'
  78. )
  79. class BuildExt(build_ext):
  80. """A custom build extension for adding compiler-specific options."""
  81. c_opts = {
  82. 'msvc': ['/EHsc'],
  83. 'unix': [],
  84. }
  85. if sys.platform == 'darwin':
  86. c_opts['unix'] += ['-stdlib=libc++', '-mmacosx-version-min=10.7']
  87. def build_extensions(self):
  88. ct = self.compiler.compiler_type
  89. opts = self.c_opts.get(ct, [])
  90. if ct == 'unix':
  91. opts.append('-DVERSION_INFO="%s"' % self.distribution.get_version())
  92. opts.append(cpp_flag(self.compiler))
  93. if has_flag(self.compiler, '-fvisibility=hidden'):
  94. opts.append('-fvisibility=hidden')
  95. elif ct == 'msvc':
  96. opts.append(
  97. '/DVERSION_INFO=\\"%s\\"' % self.distribution.get_version()
  98. )
  99. for ext in self.extensions:
  100. ext.extra_compile_args = opts
  101. build_ext.build_extensions(self)
  102. setup(
  103. name='fastTextpy',
  104. version=__version__,
  105. author='Christian Puhrsch',
  106. author_email='[email protected]',
  107. description='fastText Python bindings',
  108. long_description='',
  109. ext_modules=ext_modules,
  110. url='https://github.com/facebookresearch/fastText',
  111. license='BSD',
  112. install_requires=['pybind11>=2.2', "setuptools >= 0.7.0"],
  113. cmdclass={'build_ext': BuildExt},
  114. packages=[str('fastText')],
  115. package_dir={str(''): str('python')},
  116. zip_safe=False
  117. )