|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import subprocess |
| 4 | +from setuptools import setup, Extension, find_packages |
| 5 | +from setuptools.command.build_ext import build_ext |
| 6 | + |
| 7 | +class CMakeExtension(Extension): |
| 8 | + def __init__(self, name, sourcedir=''): |
| 9 | + # No sources; CMake handles the build. |
| 10 | + super().__init__(name, sources=[]) |
| 11 | + self.sourcedir = os.path.abspath(sourcedir) |
| 12 | + |
| 13 | +class CMakeBuild(build_ext): |
| 14 | + def run(self): |
| 15 | + # Check if CMake is installed |
| 16 | + try: |
| 17 | + subprocess.check_output(['cmake', '--version']) |
| 18 | + except OSError: |
| 19 | + raise RuntimeError("CMake must be installed to build these extensions.") |
| 20 | + for ext in self.extensions: |
| 21 | + self.build_extension(ext) |
| 22 | + |
| 23 | + def build_extension(self, ext): |
| 24 | + # Calculate the directory where the final .pyd will be placed (inside mssql_python) |
| 25 | + extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name))) |
| 26 | + cfg = 'Debug' if self.debug else 'Release' |
| 27 | + cmake_args = [ |
| 28 | + '-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=' + extdir, |
| 29 | + '-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE=' + extdir, |
| 30 | + '-DCMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE=' + extdir, |
| 31 | + '-DPYTHON_EXECUTABLE=' + sys.executable, |
| 32 | + '-DCMAKE_BUILD_TYPE=' + cfg |
| 33 | + ] |
| 34 | + build_args = ['--config', cfg] |
| 35 | + |
| 36 | + if not os.path.exists(self.build_temp): |
| 37 | + os.makedirs(self.build_temp) |
| 38 | + |
| 39 | + # Configure CMake project |
| 40 | + subprocess.check_call(['cmake', ext.sourcedir] + cmake_args, cwd=self.build_temp) |
| 41 | + # Build the target defined in your CMakeLists.txt |
| 42 | + subprocess.check_call(['cmake', '--build', '.', '--target', 'ddbc_bindings'] + build_args, cwd=self.build_temp) |
| 43 | + |
| 44 | +setup( |
| 45 | + name='mssql-python', |
| 46 | + version='0.1.5', |
| 47 | + description='A Python library for interacting with Microsoft SQL Server', |
| 48 | + long_description=open('README.md', encoding='utf-8').read(), |
| 49 | + long_description_content_type='text/markdown', |
| 50 | + author='Microsoft Corporation', |
| 51 | + author_email='pysqldriver@microsoft.com', |
| 52 | + url='https://github.com/microsoft/mssql-python', |
| 53 | + packages=find_packages(), |
| 54 | + package_data={ |
| 55 | + # Include DLL files inside mssql_python |
| 56 | + 'mssql_python': ['libs/*', 'libs/**/*', '*.dll'] |
| 57 | + }, |
| 58 | + include_package_data=True, |
| 59 | + # Requires Python 3.13 |
| 60 | + python_requires='==3.13.*', |
| 61 | + # Naming the extension as mssql_python.ddbc_bindings puts the .pyd directly in mssql_python |
| 62 | + ext_modules=[CMakeExtension('mssql_python.ddbc_bindings', sourcedir='mssql_python/pybind')], |
| 63 | + cmdclass={'build_ext': CMakeBuild}, |
| 64 | + zip_safe=False, |
| 65 | +) |
0 commit comments