-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_backend.sh
More file actions
executable file
·429 lines (336 loc) · 12.7 KB
/
build_backend.sh
File metadata and controls
executable file
·429 lines (336 loc) · 12.7 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#!/bin/bash
set -e
set -o pipefail
UI_DIR="ataraxai-ui"
TAURI_RESOURCE_DIR="$UI_DIR/src-tauri/py_src"
BUILD_DIR="build"
DIST_DIR="dist"
VENV_DIR=".venv_build"
LOG_FILE="build.log"
USE_CUDA=0
CUDA_ARCH=""
CMAKE_ARGS_STR=""
SKIP_CLEANUP=false
VERBOSE=false
PARALLEL_JOBS=$(nproc 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null || echo 4)
REQUIRED_PYTHON_VERSION="3.12"
REQUIRED_CMAKE_VERSION="3.20"
RED='\033[0;31m'
YELLOW='\033[1;33m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
NC='\033[0m'
write_log() {
local message="$1"
local level="${2:-INFO}"
local timestamp=$(date "+%Y-%m-%d %H:%M:%S")
local log_message="[$timestamp] [$level] $message"
case "$level" in
ERROR)
echo -e "${RED}${log_message}${NC}" >&2
;;
WARN)
echo -e "${YELLOW}${log_message}${NC}"
;;
SUCCESS)
echo -e "${GREEN}${log_message}${NC}"
;;
*)
echo -e "${log_message}"
;;
esac
echo "$log_message" >> "$LOG_FILE"
}
test_command() {
command -v "$1" &>/dev/null
}
test_version() {
local cmd="$1"
local version_arg="${2:---version}"
if output=$($cmd $version_arg 2>&1); then
write_log "Detected $cmd version: $output" "INFO"
return 0
else
write_log "Could not determine version for $cmd" "WARN"
return 1
fi
}
invoke_command_with_check() {
local description="$1"
shift
local cmd="$@"
write_log "Executing: $description" "INFO"
if [[ "$VERBOSE" == true ]]; then
write_log "Command: $cmd" "INFO"
fi
if ! eval "$cmd"; then
write_log "$description failed with exit code $?" "ERROR"
exit 1
fi
write_log "$description completed successfully" "SUCCESS"
}
show_usage() {
cat << EOF
AtaraxAI Build Script v$SCRIPT_VERSION
Usage: $0 [OPTIONS]
Options:
--use-cuda Enable CUDA support for GPU acceleration
--cuda-arch=<arch> Specify CUDA architecture (e.g., 75, 86, 89)
--skip-cleanup Skip cleaning old build directories
--verbose Enable verbose output
--parallel-jobs=<n> Number of parallel build jobs (default: $PARALLEL_JOBS)
--help Show this help message
Examples:
$0
$0 --use-cuda --cuda-arch=86
$0 --skip-cleanup --verbose
EOF
exit 0
}
cleanup_on_error() {
write_log "════════════════════════════════════════════════════════════" "ERROR"
write_log "Build failed! Cleaning up..." "ERROR"
write_log "════════════════════════════════════════════════════════════" "ERROR"
write_log "Check $LOG_FILE for detailed information" "ERROR"
if [[ -n "$VIRTUAL_ENV" ]]; then
deactivate 2>/dev/null || true
fi
exit 1
}
for arg in "$@"; do
case $arg in
--use-cuda)
USE_CUDA=1
;;
--cuda-arch=*)
CUDA_ARCH="${arg#*=}"
write_log "CUDA architecture set to: $CUDA_ARCH" "INFO"
;;
--skip-cleanup)
SKIP_CLEANUP=true
;;
--verbose)
VERBOSE=true
;;
--parallel-jobs=*)
PARALLEL_JOBS="${arg#*=}"
;;
--help)
show_usage
;;
*)
write_log "Unknown option: $arg" "ERROR"
echo "Use --help for usage information"
exit 1
;;
esac
done
trap cleanup_on_error ERR
: > "$LOG_FILE"
write_log "Build configuration:" "INFO"
write_log " - CUDA: $(if [[ $USE_CUDA -eq 1 ]]; then echo 'Enabled'; else echo 'Disabled'; fi)" "INFO"
if [[ -n "$CUDA_ARCH" ]]; then
write_log " - CUDA Architecture: $CUDA_ARCH" "INFO"
fi
write_log " - Parallel Jobs: $PARALLEL_JOBS" "INFO"
write_log " - Skip Cleanup: $SKIP_CLEANUP" "INFO"
write_log " - Platform: $(uname -s)" "INFO"
write_log "Checking prerequisites..." "INFO"
declare -A required_tools=(
["uv"]="Python package manager (https://github.com/astral-sh/uv)"
["cmake"]="CMake build system (https://cmake.org/)"
)
for tool in "${!required_tools[@]}"; do
if ! test_command "$tool"; then
write_log "Required tool '$tool' not found: ${required_tools[$tool]}" "ERROR"
exit 1
fi
write_log "Found: $tool" "SUCCESS"
done
test_version "cmake" || true
if test_command nproc; then
PARALLEL_JOBS=$(nproc)
elif test_command sysctl; then
PARALLEL_JOBS=$(sysctl -n hw.ncpu)
else
write_log "Could not detect CPU count, using default: $PARALLEL_JOBS" "WARN"
fi
write_log "Checking for existing backend process..." "INFO"
if pkill -f "py_src/api" 2>/dev/null; then
write_log "Stopped existing backend process" "SUCCESS"
sleep 1
else
write_log "No running backend process found" "INFO"
fi
if [[ "$SKIP_CLEANUP" != true ]]; then
write_log "Cleaning old build directories..." "INFO"
cleanup_paths=(
"$BUILD_DIR"
"$DIST_DIR"
"$TAURI_RESOURCE_DIR"
"ataraxai.egg-info"
"_skbuild"
"$VENV_DIR"
)
for path in "${cleanup_paths[@]}"; do
if [[ -e "$path" ]]; then
write_log "Removing: $path" "INFO"
rm -rf "$path"
fi
done
else
write_log "Skipping cleanup (--skip-cleanup flag set)" "WARN"
fi
write_log "Creating build virtual environment..." "INFO"
invoke_command_with_check "Virtual environment creation" \
"uv venv '$VENV_DIR' -p $REQUIRED_PYTHON_VERSION --seed"
ACTIVATE_SCRIPT="$VENV_DIR/bin/activate"
if [[ ! -f "$ACTIVATE_SCRIPT" ]]; then
write_log "Virtual environment activation script not found at: $ACTIVATE_SCRIPT" "ERROR"
exit 1
fi
write_log "Activating virtual environment..." "INFO"
source "$ACTIVATE_SCRIPT"
VENV_PYTHON=$(python -c "import sys; print(sys.prefix)")
if [[ "$VENV_PYTHON" != *"$VENV_DIR"* ]]; then
write_log "Virtual environment activation failed" "ERROR"
exit 1
fi
write_log "Virtual environment activated: $VENV_PYTHON" "SUCCESS"
write_log "Installing build tools..." "INFO"
invoke_command_with_check "Build tools installation" \
"uv pip install --no-cache-dir cmake scikit-build ninja pyinstaller"
write_log "Configuring CMake arguments..." "INFO"
if [[ $USE_CUDA -eq 1 ]]; then
CMAKE_ARGS_STR="-DATARAXAI_USE_CUDA=ON"
if [[ -n "$CUDA_ARCH" ]]; then
CMAKE_ARGS_STR+=" -DCMAKE_CUDA_ARCHITECTURES=${CUDA_ARCH}"
fi
write_log "CUDA support enabled" "INFO"
else
CMAKE_ARGS_STR="-DATARAXAI_USE_CUDA=OFF"
fi
CMAKE_ARGS_STR+=" -DGGML_ARM_I8MM=OFF"
CMAKE_ARGS_STR+=" -DCMAKE_POSITION_INDEPENDENT_CODE=ON"
write_log "CMake arguments: $CMAKE_ARGS_STR" "INFO"
PYTHON_EXECUTABLE=$(which python)
PYTHON_INCLUDE_DIR=$(python -c "import sysconfig; print(sysconfig.get_path('include'))")
ABS_BUILD_DIR=$(pwd)/$BUILD_DIR
write_log "Python executable: $PYTHON_EXECUTABLE" "INFO"
write_log "Python include dir: $PYTHON_INCLUDE_DIR" "INFO"
write_log "Build directory: $ABS_BUILD_DIR" "INFO"
write_log "Configuring CMake project..." "INFO"
invoke_command_with_check "CMake configuration" \
"cmake -S . -B '$ABS_BUILD_DIR' -DPYTHON_EXECUTABLE='$PYTHON_EXECUTABLE' -DPython_INCLUDE_DIR='$PYTHON_INCLUDE_DIR' $CMAKE_ARGS_STR"
write_log "Building C++ extension with $PARALLEL_JOBS parallel jobs..." "INFO"
invoke_command_with_check "C++ extension build" \
"cmake --build '$ABS_BUILD_DIR' --config Release -- -j $PARALLEL_JOBS"
export CMAKE_ARGS="${CMAKE_ARGS_STR}"
write_log "Installing Python package..." "INFO"
invoke_command_with_check "Python package installation" \
"uv pip install --no-cache-dir -e ."
write_log "Locating compiled artifacts..." "INFO"
if [[ ! -d "$ABS_BUILD_DIR" ]]; then
write_log "Build directory does not exist: $ABS_BUILD_DIR" "ERROR"
exit 1
fi
write_log "Searching for C++ extension in: $ABS_BUILD_DIR" "INFO"
CPP_EXTENSION_PATH=$(find "$ABS_BUILD_DIR" -name "hegemonikon_py*.so" -print -quit)
if [[ -z "$CPP_EXTENSION_PATH" ]] || [[ ! -f "$CPP_EXTENSION_PATH" ]]; then
write_log "Could not find compiled C++ extension (hegemonikon_py*.so)" "ERROR"
write_log "Directory contents of $ABS_BUILD_DIR:" "ERROR"
ls -R "$ABS_BUILD_DIR" | tee -a "$LOG_FILE"
exit 1
fi
write_log "Found C++ extension: $CPP_EXTENSION_PATH" "SUCCESS"
write_log "Locating Python shared library..." "INFO"
PYTHON_LIB_PATH=$(python -c "import sysconfig; print(sysconfig.get_config_var('LIBDIR'))" 2>/dev/null || echo "")
PYTHON_SO_NAME=$(python -c "import sysconfig; print(sysconfig.get_config_var('LDLIBRARY'))" 2>/dev/null || echo "")
if [[ -n "$PYTHON_LIB_PATH" ]] && [[ -n "$PYTHON_SO_NAME" ]]; then
PYTHON_SHARED_LIB="$PYTHON_LIB_PATH/$PYTHON_SO_NAME"
write_log "Method 1: Checking $PYTHON_SHARED_LIB" "INFO"
fi
if [[ -z "$PYTHON_SHARED_LIB" ]] || [[ ! -f "$PYTHON_SHARED_LIB" ]]; then
write_log "Method 1 failed, searching in virtual environment..." "INFO"
PYTHON_SHARED_LIB=$(find "$VIRTUAL_ENV/lib" -name "libpython*.so.*" -print -quit 2>/dev/null || echo "")
fi
if [[ -z "$PYTHON_SHARED_LIB" ]] || [[ ! -f "$PYTHON_SHARED_LIB" ]]; then
write_log "Method 2 failed, searching common locations..." "INFO"
for path in /usr/lib /usr/local/lib /opt/homebrew/lib "$HOME/.pyenv/versions/"*/lib; do
if [[ -d "$path" ]]; then
PYTHON_SHARED_LIB=$(find "$path" -name "libpython*.so.*" -o -name "libpython*.dylib" 2>/dev/null | head -n1)
if [[ -n "$PYTHON_SHARED_LIB" ]] && [[ -f "$PYTHON_SHARED_LIB" ]]; then
break
fi
fi
done
fi
if [[ -z "$PYTHON_SHARED_LIB" ]] || [[ ! -f "$PYTHON_SHARED_LIB" ]]; then
write_log "Method 3 failed, using Python to construct path..." "INFO"
PYTHON_VERSION_SHORT=$(python -c "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}')")
BASE_PREFIX=$(python -c "import sys; print(sys.base_prefix)")
for ext in "so.$PYTHON_VERSION_SHORT" "so" "dylib"; do
TEST_PATH="$BASE_PREFIX/lib/libpython$PYTHON_VERSION_SHORT.$ext"
if [[ -f "$TEST_PATH" ]]; then
PYTHON_SHARED_LIB="$TEST_PATH"
break
fi
done
fi
if [[ -z "$PYTHON_SHARED_LIB" ]] || [[ ! -f "$PYTHON_SHARED_LIB" ]]; then
write_log "Could not locate Python shared library (libpythonX.Y.so)" "ERROR"
write_log "Attempted paths:" "ERROR"
write_log " - $PYTHON_LIB_PATH/$PYTHON_SO_NAME" "ERROR"
write_log " - $VIRTUAL_ENV/lib/libpython*.so.*" "ERROR"
exit 1
fi
write_log "Found Python shared library: $PYTHON_SHARED_LIB" "SUCCESS"
write_log "Running PyInstaller..." "INFO"
PYINSTALLER_CMD="pyinstaller --noconfirm \
--onedir \
--name 'api' \
--distpath '$DIST_DIR' \
--add-binary '$CPP_EXTENSION_PATH:ataraxai' \
--add-binary '$PYTHON_SHARED_LIB:_internal' \
--hidden-import 'ataraxai.hegemonikon_py' \
--hidden-import 'chromadb.telemetry.product.posthog' \
--hidden-import 'chromadb.api.rust' \
--collect-submodules fastapi \
--collect-submodules uvicorn \
--collect-submodules ataraxai \
--exclude-module pytest \
--exclude-module mypy \
--exclude-module ruff \
--exclude-module IPython"
if test_command upx; then
PYINSTALLER_CMD+=" --upx-dir=$(dirname $(which upx))"
fi
PYINSTALLER_CMD+=" api.py"
invoke_command_with_check "PyInstaller bundling" "$PYINSTALLER_CMD"
write_log "Copying artifacts to Tauri directories..." "INFO"
ARTIFACT_DIR="$DIST_DIR/api"
if [[ ! -d "$ARTIFACT_DIR" ]]; then
write_log "Artifact directory not found: $ARTIFACT_DIR" "ERROR"
exit 1
fi
mkdir -p "$TAURI_RESOURCE_DIR"
cp -r "$ARTIFACT_DIR"/* "$TAURI_RESOURCE_DIR"/
write_log "Artifacts copied to: $TAURI_RESOURCE_DIR" "SUCCESS"
DEV_TARGET_DIR="$UI_DIR/src-tauri/target/debug/py_src"
mkdir -p "$DEV_TARGET_DIR"
cp -r "$ARTIFACT_DIR"/* "$DEV_TARGET_DIR"/
write_log "Artifacts copied to: $DEV_TARGET_DIR" "SUCCESS"
write_log "════════════════════════════════════════════════════════════" "SUCCESS"
write_log "Build completed successfully!" "SUCCESS"
write_log "════════════════════════════════════════════════════════════" "SUCCESS"
write_log "" "INFO"
write_log "Artifacts locations:" "INFO"
write_log " Production: $TAURI_RESOURCE_DIR" "INFO"
write_log " Development: $DEV_TARGET_DIR" "INFO"
write_log "" "INFO"
write_log "C++ Extension: $CPP_EXTENSION_PATH" "INFO"
write_log "Python Shared Library: $PYTHON_SHARED_LIB" "INFO"
write_log "" "INFO"
write_log "Build log saved to: $LOG_FILE" "INFO"
deactivate
exit 0