| import os |
| import sys |
| import subprocess |
| from setuptools import setup, Extension |
| from setuptools.command.build_ext import build_ext |
| |
| |
| class CMakeExtension(Extension): |
| def __init__(self, name, sourcedir=''): |
| Extension.__init__(self, name, sources=[]) |
| self.sourcedir = os.path.abspath(sourcedir) |
| |
| |
| class CMakeBuild(build_ext): |
| def build_extension(self, ext): |
| extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name))) |
| |
| # required for auto-detection of auxiliary "native" libs |
| if not extdir.endswith(os.path.sep): |
| extdir += os.path.sep |
| |
| cmake_args = [ |
| f'-DCMAKE_LIBRARY_OUTPUT_DIRECTORY={extdir}', |
| f'-DPYTHON_EXECUTABLE={sys.executable}', |
| '-DCMAKE_BUILD_TYPE=Release', |
| ] |
| |
| build_args = ['--config', 'Release'] |
| |
| # Set CMAKE_BUILD_PARALLEL_LEVEL to control the parallel build level |
| # across all generators. |
| if "CMAKE_BUILD_PARALLEL_LEVEL" not in os.environ: |
| if hasattr(self, "parallel") and self.parallel: |
| build_args += [f"-j{self.parallel}"] |
| |
| if not os.path.exists(self.build_temp): |
| os.makedirs(self.build_temp) |
| |
| subprocess.check_call( |
| ['cmake', ext.sourcedir] + cmake_args, cwd=self.build_temp |
| ) |
| subprocess.check_call( |
| ['cmake', '--build', '.'] + build_args, cwd=self.build_temp |
| ) |
| |
| |
| setup( |
| ext_modules=[CMakeExtension('pycasbin_cpp._pycasbin_cpp')], |
| cmdclass={"build_ext": CMakeBuild}, |
| ) |