setup.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. # -*- coding: utf-8 -*-
  2. import os
  3. import sys
  4. import glob
  5. import subprocess
  6. from setuptools import setup, Extension
  7. from setuptools.command.build_ext import build_ext
  8. # Convert distutils Windows platform specifiers to CMake -A arguments
  9. PLAT_TO_CMAKE = {
  10. "win32": "Win32",
  11. "win-amd64": "x64",
  12. "win-arm32": "ARM",
  13. "win-arm64": "ARM64",
  14. }
  15. # A CMakeExtension needs a sourcedir instead of a file list.
  16. # The name must be the _single_ output extension from the CMake build.
  17. # If you need multiple extensions, see scikit-build.
  18. class CMakeExtension(Extension):
  19. def __init__(self, name, sourcedir=""):
  20. Extension.__init__(self, name, sources=[])
  21. self.sourcedir = os.path.abspath(sourcedir)
  22. class CMakeBuild(build_ext):
  23. def build_extension(self, ext):
  24. extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name)))
  25. # required for auto-detection of auxiliary "native" libs
  26. if not extdir.endswith(os.path.sep):
  27. extdir += os.path.sep
  28. # cfg = "Debug" if self.debug else "Release"
  29. cfg = "Release"
  30. # CMake lets you override the generator - we need to check this.
  31. # Can be set with Conda-Build, for example.
  32. cmake_generator = os.environ.get("CMAKE_GENERATOR", "")
  33. # Set Python_EXECUTABLE instead if you use PYBIND11_FINDPYTHON
  34. # EXAMPLE_VERSION_INFO shows you how to pass a value into the C++ code
  35. # from Python.
  36. cmake_args = [
  37. "-DCMAKE_LIBRARY_OUTPUT_DIRECTORY={}".format(extdir),
  38. "-DPYTHON_EXECUTABLE={}".format(sys.executable),
  39. "-DEXAMPLE_VERSION_INFO={}".format(self.distribution.get_version()),
  40. "-DCMAKE_BUILD_TYPE={}".format(cfg), # not used on MSVC, but no harm,
  41. # "-DBUILD_VGICP_CUDA=ON",
  42. "-DBUILD_PYTHON_BINDINGS=ON",
  43. ]
  44. build_args = []
  45. if self.compiler.compiler_type != "msvc":
  46. # Using Ninja-build since it a) is available as a wheel and b)
  47. # multithreads automatically. MSVC would require all variables be
  48. # exported for Ninja to pick it up, which is a little tricky to do.
  49. # Users can override the generator with CMAKE_GENERATOR in CMake
  50. # 3.15+.
  51. if not cmake_generator:
  52. pass
  53. # cmake_args += ["-GNinja"]
  54. else:
  55. # Single config generators are handled "normally"
  56. single_config = any(x in cmake_generator for x in {"NMake", "Ninja"})
  57. # CMake allows an arch-in-generator style for backward compatibility
  58. contains_arch = any(x in cmake_generator for x in {"ARM", "Win64"})
  59. # Specify the arch if using MSVC generator, but only if it doesn't
  60. # contain a backward-compatibility arch spec already in the
  61. # generator name.
  62. if not single_config and not contains_arch:
  63. cmake_args += ["-A", PLAT_TO_CMAKE[self.plat_name]]
  64. # Multi-config generators have a different way to specify configs
  65. if not single_config:
  66. cmake_args += [
  67. "-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{}={}".format(cfg.upper(), extdir)
  68. ]
  69. build_args += ["--config", cfg]
  70. # Set CMAKE_BUILD_PARALLEL_LEVEL to control the parallel build level
  71. # across all generators.
  72. if "CMAKE_BUILD_PARALLEL_LEVEL" not in os.environ:
  73. # self.parallel is a Python 3 only way to set parallel jobs by hand
  74. # using -j in the build_ext call, not supported by pip or PyPA-build.
  75. if hasattr(self, "parallel") and self.parallel:
  76. # CMake 3.12+ only.
  77. build_args += ["-j{}".format(self.parallel)]
  78. if not os.path.exists(self.build_temp):
  79. os.makedirs(self.build_temp)
  80. subprocess.check_call(
  81. ["cmake", ext.sourcedir] + cmake_args, cwd=self.build_temp
  82. )
  83. subprocess.check_call(
  84. ["cmake", "--build", "."] + build_args, cwd=self.build_temp
  85. )
  86. # The information here can also be placed in setup.cfg - better separation of
  87. # logic and declaration, and simpler if you include description/version in a file.
  88. setup(
  89. name="pygicp",
  90. version="0.0.1",
  91. author="k.koide",
  92. author_email="k.koide@aist.go.jp",
  93. description="A collection of GICP-based point cloud registration algorithms",
  94. long_description="",
  95. ext_modules=[CMakeExtension("pygicp")],
  96. cmdclass={"build_ext": CMakeBuild},
  97. zip_safe=False,
  98. )