-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsetup.py
More file actions
151 lines (139 loc) · 5.12 KB
/
setup.py
File metadata and controls
151 lines (139 loc) · 5.12 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
import os
import platform
import sys
import warnings
from setuptools import Extension, setup
try:
from Cython.Build import cythonize
HAS_CYTHON = True
except ImportError:
HAS_CYTHON = False
# Detect platform and configure OpenMP flags
def get_openmp_flags():
"""
Get OpenMP compiler and linker flags for the current platform.
Platform behavior:
- macOS: Check for Homebrew libomp, warn if not found (requires: brew install libomp)
- Windows: Disable OpenMP (MSVC /openmp requires proper Visual Studio setup)
- Linux/Unix: Enable OpenMP with -fopenmp flags
"""
if sys.platform == "darwin": # macOS
# Check for Homebrew libomp
brew_prefix = os.environ.get("HOMEBREW_PREFIX", "/opt/homebrew")
if platform.machine() == "x86_64":
brew_prefix = "/usr/local" # Intel Mac
libomp_include = f"{brew_prefix}/opt/libomp/include"
libomp_lib = f"{brew_prefix}/opt/libomp/lib"
if os.path.exists(libomp_include):
return {
"extra_compile_args": ["-Xpreprocessor", "-fopenmp", f"-I{libomp_include}", "-O3"],
"extra_link_args": ["-lomp", f"-L{libomp_lib}"],
}
else:
# No OpenMP available, return empty flags
warnings.warn(
"libomp not found. OpenMP parallelization disabled. "
"Install with: brew install libomp",
UserWarning,
stacklevel=2,
)
return {"extra_compile_args": ["-O3"], "extra_link_args": []}
elif sys.platform == "win32": # Windows
# Windows MSVC doesn't support -fopenmp flag directly
# OpenMP is enabled via /openmp flag, but requires proper MSVC setup
# For now, disable OpenMP on Windows to ensure builds work out-of-the-box
warnings.warn(
"OpenMP parallelization disabled on Windows. "
"For OpenMP support, ensure Visual Studio with OpenMP is installed.",
UserWarning,
stacklevel=2,
)
return {"extra_compile_args": ["/O2"], "extra_link_args": []}
else: # Linux and other Unix-like systems
return {
"extra_compile_args": ["-fopenmp", "-O3"],
"extra_link_args": ["-fopenmp"],
}
openmp_flags = get_openmp_flags()
# Define extensions
extensions = [
# Tokenizers - Cython modules for word segmentation
Extension(
name="myspellchecker.tokenizers.cython.word_segment",
sources=["src/myspellchecker/tokenizers/cython/word_segment.pyx"],
language="c++",
),
Extension(
name="myspellchecker.tokenizers.cython.mmap_reader",
sources=["src/myspellchecker/tokenizers/cython/mmap_reader.pyx"],
language="c++",
),
# Text processing
Extension(
name="myspellchecker.text.normalize_c",
sources=["src/myspellchecker/text/normalize_c.pyx"],
language="c++",
),
# Distance metrics
Extension(
name="myspellchecker.algorithms.distance.edit_distance_c",
sources=["src/myspellchecker/algorithms/distance/edit_distance_c.pyx"],
language="c++",
),
# Data Pipeline
Extension(
name="myspellchecker.data_pipeline.batch_processor",
sources=["src/myspellchecker/data_pipeline/batch_processor.pyx"],
language="c++",
**openmp_flags, # OpenMP for parallel batch processing
),
Extension(
name="myspellchecker.data_pipeline.frequency_counter",
sources=["src/myspellchecker/data_pipeline/frequency_counter.pyx"],
language="c++",
),
Extension(
name="myspellchecker.data_pipeline.repair_c",
sources=["src/myspellchecker/data_pipeline/repair_c.pyx"],
language="c++",
),
Extension(
name="myspellchecker.data_pipeline.ingester_c",
sources=["src/myspellchecker/data_pipeline/ingester_c.pyx"],
language="c++",
),
Extension(
name="myspellchecker.data_pipeline.tsv_reader_c",
sources=["src/myspellchecker/data_pipeline/tsv_reader_c.pyx"],
language="c++",
),
# Core
Extension(
name="myspellchecker.core.syllable_rules_c",
sources=["src/myspellchecker/core/syllable_rules_c.pyx"],
language="c++",
),
# Algorithms
Extension(
name="myspellchecker.algorithms.viterbi_c", # Suffix with _c to avoid name collision
sources=["src/myspellchecker/algorithms/viterbi.pyx"],
language="c++",
),
]
# Minimal setup() call - most configuration is in pyproject.toml
if HAS_CYTHON:
setup(
ext_modules=cythonize(extensions, compiler_directives={"language_level": "3"}),
)
else:
# Pure Python fallback — Cython extensions are optional.
# The library gracefully falls back to pure Python implementations.
# Install Cython for compiled extensions: pip install Cython>=3.0.0
warnings.warn(
"Cython not found. Building without compiled extensions. "
"The library will use pure Python fallbacks (slower). "
"Install Cython for optimized builds: pip install 'Cython>=3.0.0'",
UserWarning,
stacklevel=2,
)
setup()