Cython routing added

This commit is contained in:
Илья Глазунов
2026-01-31 02:44:50 +03:00
parent fe541778f1
commit eeeccd57da
10 changed files with 1106 additions and 63 deletions
+100 -1
View File
@@ -9,9 +9,86 @@ Or via make:
"""
import os
import subprocess
import sys
from pathlib import Path
def get_pcre2_config():
include_dirs = []
library_dirs = []
libraries = ["pcre2-8"]
try:
cflags = subprocess.check_output(
["pkg-config", "--cflags", "libpcre2-8"],
stderr=subprocess.DEVNULL
).decode().strip()
libs = subprocess.check_output(
["pkg-config", "--libs", "libpcre2-8"],
stderr=subprocess.DEVNULL
).decode().strip()
for flag in cflags.split():
if flag.startswith("-I"):
include_dirs.append(flag[2:])
for flag in libs.split():
if flag.startswith("-L"):
library_dirs.append(flag[2:])
elif flag.startswith("-l"):
lib = flag[2:]
if lib not in libraries:
libraries.append(lib)
return include_dirs, library_dirs, libraries
except (subprocess.CalledProcessError, FileNotFoundError):
pass
try:
cflags = subprocess.check_output(
["pcre2-config", "--cflags"],
stderr=subprocess.DEVNULL
).decode().strip()
libs = subprocess.check_output(
["pcre2-config", "--libs8"],
stderr=subprocess.DEVNULL
).decode().strip()
for flag in cflags.split():
if flag.startswith("-I"):
include_dirs.append(flag[2:])
for flag in libs.split():
if flag.startswith("-L"):
library_dirs.append(flag[2:])
elif flag.startswith("-l"):
lib = flag[2:]
if lib not in libraries:
libraries.append(lib)
return include_dirs, library_dirs, libraries
except (subprocess.CalledProcessError, FileNotFoundError):
pass
# Fallback: try common paths
common_paths = [
"/opt/homebrew", # macOS ARM
"/usr/local", # macOS Intel / Linux
"/usr", # Linux
]
for base in common_paths:
include_path = Path(base) / "include"
lib_path = Path(base) / "lib"
if (include_path / "pcre2.h").exists():
include_dirs.append(str(include_path))
library_dirs.append(str(lib_path))
break
return include_dirs, library_dirs, libraries
def build_extensions():
try:
from Cython.Build import cythonize
@@ -29,6 +106,14 @@ def build_extensions():
print("Install with: pip install setuptools")
return False
pcre2_include, pcre2_libdir, pcre2_libs = get_pcre2_config()
if not pcre2_include:
print("WARNING: PCRE2 not found. Routing module may not compile.")
print("Install PCRE2: brew install pcre2 (macOS) or apt install libpcre2-dev (Linux)")
else:
print(f"Found PCRE2: includes={pcre2_include}, libs={pcre2_libdir}")
extensions = [
Extension(
"pyserve._path_matcher",
@@ -36,6 +121,18 @@ def build_extensions():
extra_compile_args=["-O3", "-ffast-math"],
define_macros=[("NPY_NO_DEPRECATED_API", "NPY_1_7_API_VERSION")],
),
Extension(
"pyserve._routing",
sources=["pyserve/_routing.pyx"],
include_dirs=pcre2_include,
library_dirs=pcre2_libdir,
libraries=pcre2_libs,
extra_compile_args=["-O3", "-ffast-math"],
define_macros=[
("NPY_NO_DEPRECATED_API", "NPY_1_7_API_VERSION"),
("PCRE2_CODE_UNIT_WIDTH", "8"),
],
),
]
ext_modules = cythonize(
@@ -59,7 +156,9 @@ def build_extensions():
cmd.run()
print("\nCython extensions built successfully!")
print(" - pyserve/_path_matcher" + (".pyd" if sys.platform == "win32" else ".so"))
ext_suffix = ".pyd" if sys.platform == "win32" else ".so"
print(f" - pyserve/_path_matcher{ext_suffix}")
print(f" - pyserve/_routing{ext_suffix}")
return True