setup.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import os
  2. import re
  3. import sys
  4. import platform
  5. import subprocess
  6. from setuptools import setup, Extension
  7. from setuptools.command.build_ext import build_ext
  8. from distutils.version import LooseVersion
  9. from distutils.sysconfig import get_config_var
  10. from distutils.sysconfig import get_python_inc
  11. class CMakeExtension(Extension):
  12. def __init__(self, name, sourcedir=''):
  13. Extension.__init__(self, name, sources=[])
  14. self.sourcedir = os.path.abspath(sourcedir)
  15. class CMakeBuild(build_ext):
  16. def run(self):
  17. try:
  18. out = subprocess.check_output(['cmake', '--version'])
  19. except OSError:
  20. raise RuntimeError("CMake must be installed to build the following extensions: " +
  21. ", ".join(e.name for e in self.extensions))
  22. if platform.system() == "Windows":
  23. cmake_version = LooseVersion(
  24. re.search(r'version\s*([\d.]+)', out.decode()).group(1))
  25. if cmake_version < '3.1.0':
  26. raise RuntimeError("CMake >= 3.1.0 is required on Windows")
  27. for ext in self.extensions:
  28. self.build_extension(ext)
  29. def build_extension(self, ext):
  30. extdir = os.path.abspath(os.path.dirname(
  31. self.get_ext_fullpath(ext.name)))
  32. python_library = str(get_config_var('LIBDIR'))
  33. python_include_directory = str(get_python_inc())
  34. cmake_args = ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=' + extdir,
  35. '-DPYTHON_EXECUTABLE=' + sys.executable,
  36. '-DPYTHON_LIBRARY=' + python_library,
  37. '-DPYTHON_INCLUDE_DIR=' + python_include_directory, ]
  38. cfg = 'Debug' if self.debug else 'Release'
  39. build_args = ['--config', cfg]
  40. if platform.system() == "Windows":
  41. cmake_args += [
  42. '-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{}={}'.format(cfg.upper(), extdir)]
  43. if sys.maxsize > 2**32:
  44. cmake_args += ['-A', 'x64']
  45. build_args += ['--', '/m']
  46. else:
  47. cmake_args += ['-DCMAKE_BUILD_TYPE=' + cfg]
  48. build_args += ['--', '-j2']
  49. env = os.environ.copy()
  50. env['CXXFLAGS'] = '{} -DVERSION_INFO=\\"{}\\"'.format(env.get('CXXFLAGS', ''),
  51. self.distribution.get_version())
  52. if not os.path.exists(self.build_temp):
  53. os.makedirs(self.build_temp)
  54. subprocess.check_call(['cmake', ext.sourcedir] +
  55. cmake_args, cwd=self.build_temp, env=env)
  56. subprocess.check_call(['cmake', '--build', '.'] +
  57. build_args, cwd=self.build_temp)
  58. setup(
  59. name='pyigl',
  60. version='0.0.1',
  61. author='Geometric Computing Lab @ NYU',
  62. author_email='info@geometriccomputing.org',
  63. description='',
  64. long_description='',
  65. ext_modules=[CMakeExtension('pyigl')],
  66. cmdclass=dict(build_ext=CMakeBuild),
  67. zip_safe=False,
  68. )