|
| 1 | +import io |
| 2 | +from os import path |
| 3 | +import os |
| 4 | +import re |
| 5 | +import sys |
| 6 | +import platform |
| 7 | +import subprocess |
| 8 | + |
| 9 | +from setuptools import setup, Extension |
| 10 | +from setuptools.command.build_ext import build_ext |
| 11 | +from distutils.version import LooseVersion |
| 12 | + |
| 13 | +here = path.abspath(path.dirname(__file__)) |
| 14 | + |
| 15 | + |
| 16 | +def read(*names, **kwargs): |
| 17 | + return io.open( |
| 18 | + path.join(here, *names), encoding=kwargs.get("encoding", "utf8") |
| 19 | + ).read() |
| 20 | + |
| 21 | + |
| 22 | +class CMakeExtension(Extension): |
| 23 | + def __init__(self, name, sourcedir=""): |
| 24 | + Extension.__init__(self, name, sources=[]) |
| 25 | + self.sourcedir = os.path.abspath(sourcedir) |
| 26 | + |
| 27 | + |
| 28 | +class CMakeBuild(build_ext): |
| 29 | + def run(self): |
| 30 | + try: |
| 31 | + out = subprocess.check_output(["cmake", "--version"]) |
| 32 | + except OSError: |
| 33 | + raise RuntimeError( |
| 34 | + "CMake must be installed to build the following extensions: " |
| 35 | + + ", ".join(e.name for e in self.extensions) |
| 36 | + ) |
| 37 | + |
| 38 | + if platform.system() == "Windows": |
| 39 | + cmake_version = LooseVersion( |
| 40 | + re.search(r"version\s*([\d.]+)", out.decode()).group(1) |
| 41 | + ) |
| 42 | + if cmake_version < "3.1.0": |
| 43 | + raise RuntimeError("CMake >= 3.1.0 is required on Windows") |
| 44 | + |
| 45 | + for ext in self.extensions: |
| 46 | + self.build_extension(ext) |
| 47 | + |
| 48 | + def build_extension(self, ext): |
| 49 | + extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name))) |
| 50 | + cmake_args = [ |
| 51 | + "-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=" + extdir, |
| 52 | + "-DPYTHON_EXECUTABLE=" + sys.executable, |
| 53 | + ] |
| 54 | + |
| 55 | + cfg = "Debug" if self.debug else "Release" |
| 56 | + build_args = ["--config", cfg] |
| 57 | + |
| 58 | + if platform.system() == "Windows": |
| 59 | + cmake_args += [ |
| 60 | + "-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{}={}".format(cfg.upper(), extdir) |
| 61 | + ] |
| 62 | + if sys.maxsize > 2**32: |
| 63 | + cmake_args += ["-A", "x64"] |
| 64 | + # build_args += ['--', '/m'] |
| 65 | + else: |
| 66 | + # # For MacOS. |
| 67 | + # # During compiling stage, the python module always links to a temporary generated library which is going to be destroyed. |
| 68 | + # # Then importing the final installed module will return a link error |
| 69 | + # # The following commands will force the module to look up the its dynmaic linked library in the same folder |
| 70 | + # cmake_args += [ |
| 71 | + # '-DCMAKE_INSTALL_RPATH=@loader_path', |
| 72 | + # '-DCMAKE_BUILD_WITH_INSTALL_RPATH:BOOL=ON', |
| 73 | + # '-DCMAKE_INSTALL_RPATH_USE_LINK_PATH:BOOL=OFF'] |
| 74 | + |
| 75 | + cmake_args += ["-DCMAKE_BUILD_TYPE=" + cfg] |
| 76 | + build_args += ["--", "-j2"] |
| 77 | + |
| 78 | + env = os.environ.copy() |
| 79 | + env["CXXFLAGS"] = '{} -DVERSION_INFO=\\"{}\\"'.format( |
| 80 | + env.get("CXXFLAGS", ""), self.distribution.get_version() |
| 81 | + ) |
| 82 | + if not os.path.exists(self.build_temp): |
| 83 | + os.makedirs(self.build_temp) |
| 84 | + subprocess.check_call( |
| 85 | + ["cmake", ext.sourcedir] + cmake_args, cwd=self.build_temp, env=env |
| 86 | + ) |
| 87 | + subprocess.check_call( |
| 88 | + ["cmake", "--build", "."] + build_args, cwd=self.build_temp |
| 89 | + ) |
| 90 | + |
| 91 | + |
| 92 | +long_description = read("README.md") |
| 93 | +requirements = read("requirements.txt").split("\n") |
| 94 | +optional_requirements = {} |
| 95 | + |
| 96 | + |
| 97 | +setup( |
| 98 | + name="compas_libigl", |
| 99 | + packages=["compas_libigl"], |
| 100 | + ext_modules=[CMakeExtension("compas_libigl")], |
| 101 | + cmdclass=dict(build_ext=CMakeBuild), |
| 102 | +) |
0 commit comments