Skip to content

Commit 5c65c44

Browse files
committed
add setup.py to compile c extension
1 parent fdb5775 commit 5c65c44

1 file changed

Lines changed: 43 additions & 0 deletions

File tree

setup.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"""Build configuration for optional C extension.
2+
3+
The C extension provides accelerated array operations for the pure Python
4+
backend. If compilation fails (no C compiler), the package installs
5+
normally and falls back to pure Python.
6+
"""
7+
8+
import os
9+
10+
from setuptools import Extension, setup
11+
from setuptools.command.build_ext import build_ext
12+
13+
_c_source = os.path.join(
14+
"src",
15+
"gradient_free_optimizers",
16+
"_array_backend",
17+
"_fast_ops.c",
18+
)
19+
20+
21+
class OptionalBuildExt(build_ext):
22+
"""Build C extensions, silently skip on failure."""
23+
24+
def build_extension(self, ext): # noqa: D102
25+
try:
26+
super().build_extension(ext)
27+
except Exception as e:
28+
print(
29+
f"WARNING: Could not build C extension '{ext.name}': {e}\n"
30+
f"Falling back to pure Python array backend."
31+
)
32+
33+
34+
setup(
35+
cmdclass={"build_ext": OptionalBuildExt},
36+
ext_modules=[
37+
Extension(
38+
"gradient_free_optimizers._array_backend._fast_ops",
39+
sources=[_c_source],
40+
extra_compile_args=["-O2"],
41+
),
42+
],
43+
)

0 commit comments

Comments
 (0)