-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve setup.py to make gcc/cython compilation optional
- Loading branch information
1 parent
7d5a399
commit 5893ae8
Showing
3 changed files
with
40 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,41 @@ | ||
from pathlib import Path | ||
from setuptools import setup, Extension | ||
import Cython.Build | ||
import os | ||
import shutil | ||
|
||
# read the contents of your README file | ||
# Read the contents of your README file | ||
this_directory = Path(__file__).parent | ||
long_description = (this_directory / "readme.md").read_text() | ||
|
||
version = '0.0.6' | ||
version = '0.0.7' | ||
|
||
ext = Extension(name="UltraDict", sources=["UltraDict.py"]) | ||
# Function to detect if a C compiler is available | ||
def has_compiler(): | ||
# Check if gcc or clang is available | ||
return shutil.which('gcc') or shutil.which('clang') | ||
|
||
# Decide whether to build C extensions or skip based on the availability of a compiler | ||
def get_extension_modules(): | ||
# If a C compiler is not available, skip the extension | ||
if not has_compiler(): | ||
print("No C compiler detected. Skipping C extension building.") | ||
return [] | ||
|
||
# C compiler available, attempt to use Cython or pre-generated C code | ||
try: | ||
from Cython.Build import cythonize | ||
ext = Extension(name="UltraDict", sources=["UltraDict.py"]) | ||
return cythonize(ext, compiler_directives={'language_level': "3"}) | ||
except ImportError: | ||
# Fall back to pre-generated C file | ||
if os.path.exists("UltraDict.c"): | ||
ext = Extension(name="UltraDict", sources=["UltraDict.c"]) | ||
return [ext] | ||
print("No Cython or C source file available. Skipping C extension building.") | ||
return [] | ||
|
||
# Get the extension modules (empty list if no compilation) | ||
ext_modules = get_extension_modules() | ||
|
||
setup( | ||
name='UltraDict', | ||
|
@@ -19,11 +46,10 @@ | |
author='Ronny Rentner', | ||
author_email='[email protected]', | ||
url='https://github.com/ronny-rentner/UltraDict', | ||
cmdclass={'build_ext': Cython.Build.build_ext}, | ||
package_dir={'UltraDict': '.', 'UltraDict.pymutex': 'pymutex'}, | ||
packages=['UltraDict', 'UltraDict.pymutex'], | ||
zip_safe=False, | ||
ext_modules=Cython.Build.cythonize(ext, compiler_directives={'language_level' : "3"}), | ||
setup_requires=['cython>=0.24.1'], | ||
ext_modules=ext_modules, # Only add extensions if a compiler is available | ||
setup_requires=['cython>=0.24.1'] if ext_modules else [], | ||
python_requires=">=3.8", | ||
) |