-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
252 lines (222 loc) · 8.42 KB
/
setup.py
File metadata and controls
252 lines (222 loc) · 8.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
"""
FasterAPI - Setup
Builds the C++ native library and Cython extensions, packages with Python modules.
"""
import os
import subprocess
import sys
from pathlib import Path
from setuptools import Extension, find_packages, setup
from setuptools.command.build_ext import build_ext
# Check if Cython is available
try:
from Cython.Build import cythonize
HAS_CYTHON = True
except ImportError:
HAS_CYTHON = False
print("Warning: Cython not available. MCP proxy bindings will not be built.")
class CMakeBuildExt(build_ext):
"""Custom build command that uses CMake to build the C++ extension."""
def run(self):
"""Build C++ library via CMake, then build Cython extensions."""
# Build C++ library first
self.build_cpp_library()
# Then build Cython extensions if available
if HAS_CYTHON:
super().run()
def build_cpp_library(self):
"""Build C++ library via CMake."""
build_dir = Path(self.build_temp).absolute()
source_dir = Path(__file__).parent.absolute()
# Target directory for native libraries
native_dir = source_dir / "fasterapi" / "_native"
native_dir.mkdir(parents=True, exist_ok=True)
# Create build directory
build_dir.mkdir(parents=True, exist_ok=True)
# Run CMake configure
print(f"Configuring CMake in {build_dir}")
cmake_args = [
f"-DCMAKE_BUILD_TYPE=Release",
f"-DFA_BUILD_MCP=OFF", # Disable MCP (has exception issues)
f"-DFA_BUILD_PG=ON", # Enable PostgreSQL
f"-DFA_BUILD_HTTP=ON", # Enable HTTP
f"-DFA_BUILD_BENCHMARKS=OFF", # Disable benchmarks for faster build
]
subprocess.check_call(
["cmake", "-G", "Ninja", str(source_dir), "-Wno-dev"] + cmake_args,
cwd=build_dir,
)
# Run Ninja build
print("Building C++ library with Ninja")
subprocess.check_call(
["ninja", "fasterapi_http", "fasterapi_pg"], cwd=build_dir
)
# Copy libraries to fasterapi/_native/
import shutil
# Copy MCP library
for pattern in ["libfasterapi_mcp.*", "fasterapi_mcp.*"]:
for lib_file in build_dir.rglob(pattern):
if lib_file.is_file() and not lib_file.suffix in [".a", ".lib"]:
print(f"Copying {lib_file.name} to {native_dir}")
shutil.copy2(lib_file, native_dir / lib_file.name)
# Copy PG library
for pattern in ["libfasterapi_pg.*", "fasterapi_pg.*"]:
for lib_file in build_dir.rglob(pattern):
if lib_file.is_file() and not lib_file.suffix in [".a", ".lib"]:
print(f"Copying {lib_file.name} to {native_dir}")
shutil.copy2(lib_file, native_dir / lib_file.name)
# Copy HTTP library
for pattern in ["libfasterapi_http.*", "fasterapi_http.*"]:
for lib_file in build_dir.rglob(pattern):
if lib_file.is_file() and not lib_file.suffix in [".a", ".lib"]:
print(f"Copying {lib_file.name} to {native_dir}")
shutil.copy2(lib_file, native_dir / lib_file.name)
# Copy CoroIO library
for pattern in ["libcoroio.*", "coroio.*"]:
for lib_file in build_dir.rglob(pattern):
if lib_file.is_file() and not lib_file.suffix in [".a", ".lib"]:
print(f"Copying {lib_file.name} to {native_dir}")
shutil.copy2(lib_file, native_dir / lib_file.name)
# Platform-specific rpath for finding native libraries at runtime
if sys.platform == "darwin":
# macOS: use -rpath flag (not -Wl,-rpath which doesn't work with Apple clang)
# @loader_path is relative to the .so file location
rpath_link_args = ["-rpath", "@loader_path/_native"]
elif sys.platform.startswith("linux"):
# Linux: $ORIGIN is relative to the .so file location
rpath_link_args = ["-Wl,-rpath,$ORIGIN/_native"]
else:
rpath_link_args = []
# Cython extensions
extensions = []
if HAS_CYTHON:
# Binary kwargs decoder (pure Cython - no C++ dependencies)
# Provides ~26x faster kwargs deserialization vs JSON
extensions.append(
Extension(
"fasterapi.core.binary_kwargs",
sources=["fasterapi/core/binary_kwargs.pyx"],
language="c", # Pure C for maximum performance
extra_compile_args=["-O3"],
)
)
# HTTP Server bindings (Cython - high performance)
extensions.append(
Extension(
"fasterapi.http.server_cy",
sources=["fasterapi/http/server_cy.pyx"],
include_dirs=[".", "src/cpp", "external/coroio"],
library_dirs=[
"fasterapi/_native",
"build/lib",
"build/external/coroio/coroio",
],
libraries=["fasterapi_http", "coroio"],
language="c++",
extra_compile_args=[
"-std=c++20",
"-fexceptions",
], # CoroIO needs exceptions
extra_link_args=rpath_link_args, # Find native libs at runtime
)
)
# FastAPI-compatible native bindings (Cython - high performance)
extensions.append(
Extension(
"fasterapi._fastapi_native",
sources=["fasterapi/_fastapi_native.pyx"],
include_dirs=[".", "src/cpp", "external/simdjson/include"],
library_dirs=["fasterapi/_native", "build/lib"],
libraries=["fasterapi_http"],
language="c++",
extra_compile_args=[
"-std=c++20",
"-fexceptions",
], # Cython needs exceptions for error handling
extra_link_args=rpath_link_args, # Find native libs at runtime
)
)
# MCP Proxy bindings - DISABLED temporarily due to NULL pointer issues
# extensions.append(
# Extension(
# "fasterapi.mcp.proxy_bindings",
# sources=["fasterapi/mcp/proxy_bindings.pyx"],
# include_dirs=["src/cpp"],
# library_dirs=["fasterapi/_native"],
# libraries=["fasterapi_mcp"],
# language="c++",
# extra_compile_args=["-std=c++20"],
# )
# )
# Cythonize extensions
extensions = cythonize(
extensions, compiler_directives={"language_level": 3, "embedsignature": True}
)
setup(
name="fasterapi",
version="0.2.0",
description="High-performance web framework with PostgreSQL and MCP support",
long_description=open("README.md").read() if Path("README.md").exists() else "",
long_description_content_type="text/markdown",
author="FasterAPI Contributors",
url="https://github.com/bengamble/FasterAPI",
license="MIT",
python_requires=">=3.8",
packages=find_packages(exclude=["tests", "tests.*", "benchmarks", "benchmarks.*"]),
package_data={
"fasterapi": ["_native/*"],
"fasterapi.mcp": ["*.pyx", "*.pxd"],
},
include_package_data=True,
ext_modules=extensions,
cmdclass={
"build_ext": CMakeBuildExt,
},
install_requires=[
"pydantic>=2.0",
],
extras_require={
"dev": [
"pytest>=7.0",
"pytest-cov>=4.0",
"pytest-asyncio>=0.21",
"cython>=3.0",
],
"pg": [
"psycopg[binary]>=3.0",
"asyncpg>=0.27",
],
"mcp": [
"cython>=3.0",
],
"all": [
"cython>=3.0",
"psycopg[binary]>=3.0",
"asyncpg>=0.27",
"pytest>=7.0",
"pytest-cov>=4.0",
"pytest-asyncio>=0.21",
],
},
entry_points={
"console_scripts": [
"fasterapi-mcp-proxy=fasterapi.mcp.cli:main",
],
},
classifiers=[
"Development Status :: 3 - Alpha",
"Intended Audience :: Developers",
"License :: OSI Approved :: MIT License",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: C++",
"Programming Language :: Cython",
"Topic :: Software Development :: Libraries :: Application Frameworks",
"Topic :: Internet :: WWW/HTTP",
"Topic :: Database",
],
)