| from setuptools import setup, Extension | |
| import os | |
| # Allow building from .py (via Cython) if present, otherwise from .c files already generated. | |
| use_cython = False | |
| try: | |
| from Cython.Build import cythonize | |
| use_cython = True | |
| except Exception: | |
| use_cython = False | |
| exts = [] | |
| # Determine sources for each extension: prefer .py when cython is available and .py exists, | |
| # otherwise use .c if present. | |
| modules = [ | |
| ("utils.__init__", "utils/__init__"), | |
| ("utils.benchmark_utils", "utils/benchmark_utils"), | |
| ] | |
| for mod_name, base in modules: | |
| py = base + ".py" | |
| c = base + ".c" | |
| if use_cython and os.path.exists(py): | |
| exts.append(Extension(mod_name, [py])) | |
| elif os.path.exists(c): | |
| exts.append(Extension(mod_name, [c])) | |
| elif os.path.exists(py): | |
| # Cython might not be installed but .py sources exist; fall back to pure-Python install (no ext) | |
| # In this case, don't create an Extension; the package will use pure Python files. | |
| pass | |
| else: | |
| # Nothing to compile for this module; that may be fine if a compiled .so already exists. | |
| pass | |
| if use_cython and exts: | |
| setup( | |
| name="my_utils_package", | |
| ext_modules=cythonize( | |
| exts, | |
| compiler_directives={'language_level': "3"} | |
| ) | |
| ) | |
| elif exts: | |
| setup( | |
| name="my_utils_package", | |
| ext_modules=exts | |
| ) | |
| else: | |
| # Nothing to build; create a minimal package metadata so setup.py can be called harmlessly. | |
| setup(name="my_utils_package") | |