-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcompile_code.sh
More file actions
111 lines (89 loc) · 3.08 KB
/
compile_code.sh
File metadata and controls
111 lines (89 loc) · 3.08 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
#!/bin/bash
set -euo pipefail
trap 'echo "❌ Error occurred. Exiting..."; exit 1' ERR
# Constants
readonly ROOT_DIRECTORY=$(pwd)
readonly COMPILED_CODE_DIRECTORY="$ROOT_DIRECTORY/compiled_code"
readonly COMPILED_CODE_SOURCE_DIRECTORY="$COMPILED_CODE_DIRECTORY"
# Comma-separated list of excluded Python files (relative to root, e.g., "main.py" or "tools/start.py")
EXCLUDE_FILES=()
log() {
echo -e "\033[1;34m[INFO]\033[0m $*"
}
safe_copy() {
cp -r "$1" "$2" || { echo "Error: Failed to copy $1"; exit 1; }
}
icd_excluded() {
local rel_path="$1"
for excluded in "${EXCLUDE_FILES[@]}"; do
[[ "$rel_path" == "$excluded" ]] && return 0
done
return 1
}
prepare_output_dirs() {
log "Preparing output directories..."
mkdir -p "$COMPILED_CODE_SOURCE_DIRECTORY"
find . -type d ! -path "./compiled_code*" \
! -path "*/.git*" \
! -path "*/__pycache__*" | while read -r dir; do
mkdir -p "$COMPILED_CODE_SOURCE_DIRECTORY/${dir#./}"
done
}
compile_python_files() {
log "Converting Python files to shared objects..."
find . -name '*.py' ! -path "./compiled_code/*" \
! -path "*/.git/*" \
! -path "*/__pycache__/*" | while read -r py_file; do
rel_path="${py_file#./}"
if is_excluded "$rel_path"; then
log "Excluded: $rel_path"
cp "$py_file" "$COMPILED_CODE_SOURCE_DIRECTORY/$rel_path"
continue
fi
if ! grep -q "# cython: language_level=" "$py_file"; then
echo "# cython: language_level=3" | cat - "$py_file" > tmp && mv tmp "$py_file"
fi
c_file="${py_file%.py}.c"
cython "$py_file" -o "$c_file"
output_dir="$(dirname "$COMPILED_CODE_SOURCE_DIRECTORY/$rel_path")"
base_name="$(basename "$py_file" .py)"
gcc -shared -o "$output_dir/$base_name.so" -fPIC $(python3 -m pybind11 --includes) "$c_file"
rm "$c_file"
done
}
is_excluded() {
local rel_path="$1"
for excluded in "${EXCLUDE_FILES[@]}"; do
[[ "$rel_path" == "$excluded" ]] && return 0
done
return 1
}
copy_other_files() {
log "Copying non-Python files..."
find . -type f ! -name '*.py' ! -name '*.c' \
! -path "./compiled_code/*" \
! -path "*/.git/*" \
! -path "*/__pycache__/*" | while read -r file; do
rel_path="${file#./}"
target_path="$COMPILED_CODE_SOURCE_DIRECTORY/$rel_path"
mkdir -p "$(dirname "$target_path")"
cp "$file" "$target_path"
done
}
# Optional cleanup (build artifacts)
cleanup() {
log "Cleaning up temporary files..."
rm -rf build
}
# Time tracking
start_timer() { date +%s.%N; }
elapsed_time() { echo "$(echo "$(date +%s.%N) - $1" | bc)"; }
# ------------------ EXECUTION ------------------
log "🚀 Build started from current directory..."
mkdir -p "$COMPILED_CODE_DIRECTORY"
start=$(start_timer)
prepare_output_dirs
compile_python_files
copy_other_files
log "✅ Build completed in $(elapsed_time "$start") seconds"
log "🎉 Compiled project is in: $COMPILED_CODE_DIRECTORY"