-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.m4
More file actions
171 lines (153 loc) · 6.57 KB
/
config.m4
File metadata and controls
171 lines (153 loc) · 6.57 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
dnl Copyright (c) 2024-2026 Bruce Dou <doubaokun@gmail.com>
dnl
dnl Permission is hereby granted, free of charge, to any person obtaining a
dnl copy of this software and associated documentation files (the "Software"),
dnl to deal in the Software without restriction, including without limitation
dnl the rights to use, copy, modify, merge, publish, distribute, sublicense,
dnl and/or sell copies of the Software, and to permit persons to whom the
dnl Software is furnished to do so, subject to the following conditions:
dnl
dnl The above copyright notice and this permission notice shall be included in
dnl all copies or substantial portions of the Software.
dnl
dnl THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
dnl IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
dnl FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
dnl AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
dnl LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
dnl FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
dnl DEALINGS IN THE SOFTWARE.
dnl MemVector PHP Extension — config.m4 v5 (C++17 + Shared Memory + HNSW)
dnl Uses C++ std::atomic, mmap, shm_open.
PHP_ARG_ENABLE([memvector],
[whether to enable MemVector support],
[AS_HELP_STRING([--enable-memvector], [Enable MemVector AI vector store])])
PHP_ARG_ENABLE([memvector-avx2],
[whether to use AVX2 SIMD],
[AS_HELP_STRING([--enable-memvector-avx2], [Enable AVX2 SIMD (default: auto-detect)])],
[auto], [no])
PHP_ARG_WITH([llama],
[for llama.cpp support],
[AS_HELP_STRING([--with-llama@<:@=DIR@:>@], [Enable llama.cpp embedding support])],
[no], [no])
if test "$PHP_MEMVECTOR" != "no"; then
dnl ── Require C++ compiler ─────────────────────────────────────
PHP_REQUIRE_CXX()
dnl ── C++17 std::atomic check (required) ───────────────────────
AC_LANG_PUSH([C++])
AC_MSG_CHECKING([for C++17 std::atomic])
save_CXXFLAGS="$CXXFLAGS"
CXXFLAGS="$CXXFLAGS -std=c++17"
AC_COMPILE_IFELSE(
[AC_LANG_PROGRAM([[#include <atomic>]],
[[std::atomic<int> x{0}; x.fetch_add(1);]])],
[AC_MSG_RESULT([yes])],
[AC_MSG_ERROR([MemVector requires C++17 std::atomic (GCC >= 7, Clang >= 5)])]
)
CXXFLAGS="$save_CXXFLAGS"
dnl ── sys/mman.h check (mmap) ───────────────────────────────────
AC_CHECK_HEADERS([sys/mman.h sys/stat.h fcntl.h math.h], [], [
AC_MSG_ERROR([MemVector requires sys/mman.h and math.h])
])
dnl ── shm_open detection ────────────────────────────────────────
dnl macOS has shm_open in libc, Linux needs -lrt
AC_MSG_CHECKING([for shm_open])
save_LIBS="$LIBS"
AC_LINK_IFELSE(
[AC_LANG_PROGRAM([[
#include <sys/mman.h>
#include <fcntl.h>
]], [[
int fd = shm_open("/test", O_RDWR | O_CREAT, 0660);
(void)fd;
]])],
[
AC_MSG_RESULT([yes (libc)])
AC_DEFINE([HAVE_SHM_OPEN], [1], [shm_open available])
],
[
dnl Try with -lrt (Linux)
LIBS="$LIBS -lrt"
AC_LINK_IFELSE(
[AC_LANG_PROGRAM([[
#include <sys/mman.h>
#include <fcntl.h>
]], [[
int fd = shm_open("/test", O_RDWR | O_CREAT, 0660);
(void)fd;
]])],
[
AC_MSG_RESULT([yes (librt)])
AC_DEFINE([HAVE_SHM_OPEN], [1], [shm_open available])
PHP_ADD_LIBRARY(rt,, MEMVECTOR_SHARED_LIBADD)
],
[
AC_MSG_RESULT([no])
AC_MSG_WARN([shm_open not found — shared memory mode will be disabled])
]
)
]
)
LIBS="$save_LIBS"
dnl ── AVX2 detection ───────────────────────────────────────────
MEMVECTOR_SIMD_FLAGS=""
if test "$PHP_MEMVECTOR_AVX2" != "no"; then
AC_MSG_CHECKING([for AVX2 + FMA])
save_CXXFLAGS="$CXXFLAGS"
CXXFLAGS="$CXXFLAGS -mavx2 -mfma"
AC_COMPILE_IFELSE(
[AC_LANG_PROGRAM([[#include <immintrin.h>]],
[[__m256 x = _mm256_setzero_ps(); (void)x;]])],
[
AC_MSG_RESULT([yes])
MEMVECTOR_SIMD_FLAGS="-mavx2 -mfma"
AC_DEFINE([HAVE_AVX2], [1], [AVX2 available])
],
[AC_MSG_RESULT([no]); MEMVECTOR_SIMD_FLAGS=""]
)
CXXFLAGS="$save_CXXFLAGS"
fi
AC_LANG_POP([C++])
dnl ── llama.cpp detection ─────────────────────────────────────────
if test "$PHP_LLAMA" != "no"; then
AC_LANG_PUSH([C++])
if test "$PHP_LLAMA" != "yes"; then
LLAMA_SEARCH_DIR="$PHP_LLAMA"
else
LLAMA_SEARCH_DIR="/usr/local /usr"
fi
LLAMA_FOUND="no"
for dir in $LLAMA_SEARCH_DIR; do
if test -f "$dir/include/llama.h"; then
LLAMA_INCDIR="$dir/include"
LLAMA_LIBDIR="$dir/lib"
LLAMA_FOUND="yes"
break
fi
done
if test "$LLAMA_FOUND" = "yes"; then
PHP_ADD_INCLUDE($LLAMA_INCDIR)
PHP_ADD_LIBRARY_WITH_PATH(llama, $LLAMA_LIBDIR, MEMVECTOR_SHARED_LIBADD)
AC_DEFINE([HAVE_LLAMA], [1], [llama.cpp available])
AC_MSG_RESULT([llama.cpp found in $LLAMA_INCDIR])
else
AC_MSG_ERROR([llama.cpp not found. Install llama.cpp or specify --with-llama=DIR])
fi
AC_LANG_POP([C++])
fi
dnl ── Libraries: libm + libstdc++ ───────────────────────────────
PHP_ADD_LIBRARY(m,, MEMVECTOR_SHARED_LIBADD)
PHP_ADD_LIBRARY(stdc++,, MEMVECTOR_SHARED_LIBADD)
PHP_SUBST(MEMVECTOR_SHARED_LIBADD)
dnl ── Compile flags ─────────────────────────────────────────────
dnl -std=c++17 : enables std::atomic, constexpr, etc.
dnl -O3 : optimises atomic loops and SIMD kernels
dnl -fno-strict-aliasing : safe for mmap struct overlay
MEMVECTOR_CXXFLAGS="-O3 -std=c++17 -fno-strict-aliasing $MEMVECTOR_SIMD_FLAGS"
MEMVECTOR_SOURCES="src/memvector.cpp src/php_memvector.cpp"
if test "$PHP_LLAMA" != "no"; then
MEMVECTOR_SOURCES="$MEMVECTOR_SOURCES src/php_memvector_embedding.cpp src/php_memvector_reranker.cpp"
fi
PHP_NEW_EXTENSION(memvector, $MEMVECTOR_SOURCES, $ext_shared,, [$MEMVECTOR_CXXFLAGS])
PHP_INSTALL_HEADERS([ext/memvector], [src/php_memvector.h])
fi