setup.py 3.1 KB

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