-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·240 lines (201 loc) · 9.42 KB
/
build.sh
File metadata and controls
executable file
·240 lines (201 loc) · 9.42 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
#!/bin/bash
set -e # Exit on error
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Get the script directory
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
NATIVE_DIR="${SCRIPT_DIR}/third_party/gopher-orch"
BUILD_DIR="${NATIVE_DIR}/build"
# Handle --clean flag (cleans CMake cache but preserves _deps)
if [ "$1" = "--clean" ]; then
echo -e "${YELLOW}Cleaning build artifacts (preserving _deps)...${NC}"
rm -rf "${SCRIPT_DIR}/native"
rm -rf "${SCRIPT_DIR}/dist"
rm -rf "${SCRIPT_DIR}/build"
rm -rf "${SCRIPT_DIR}/*.egg-info"
rm -f "${BUILD_DIR}/CMakeCache.txt"
rm -rf "${BUILD_DIR}/CMakeFiles"
rm -rf "${BUILD_DIR}/lib"
rm -rf "${BUILD_DIR}/bin"
echo -e "${GREEN}✓ Clean complete${NC}"
if [ "$2" != "--build" ]; then
exit 0
fi
fi
echo -e "${GREEN}======================================${NC}"
echo -e "${GREEN}Building gopher-orch Python SDK${NC}"
echo -e "${GREEN}======================================${NC}"
echo ""
# Step 1: Update submodules recursively
echo -e "${YELLOW}Step 1: Updating submodules...${NC}"
# Support custom SSH host for multiple GitHub accounts
# Usage: GITHUB_SSH_HOST=my-ssh-alias ./build.sh
SSH_HOST="${GITHUB_SSH_HOST:-github.com}"
if [ -n "${GITHUB_SSH_HOST}" ]; then
echo -e "${YELLOW} Using custom SSH host: ${GITHUB_SSH_HOST}${NC}"
fi
# Configure SSH URL rewrite for GopherSecurity repos
# Clear any existing rewrites first to avoid conflicts
git config --local --unset-all url."git@${SSH_HOST}:GopherSecurity/".insteadOf 2>/dev/null || true
# Set up URL rewrites - both https and default git@github.com should map to custom SSH host
git config --local --add url."git@${SSH_HOST}:GopherSecurity/".insteadOf "https://github.com/GopherSecurity/"
git config --local --add url."git@${SSH_HOST}:GopherSecurity/".insteadOf "git@github.com:GopherSecurity/"
git config --local submodule.third_party/gopher-orch.url "git@${SSH_HOST}:GopherSecurity/gopher-orch.git"
# Check if submodule directory exists but is empty/broken (missing CMakeLists.txt)
if [ -d "${NATIVE_DIR}" ] && [ ! -f "${NATIVE_DIR}/CMakeLists.txt" ]; then
echo -e "${YELLOW} Submodule directory exists but appears incomplete, reinitializing...${NC}"
# Deinitialize and remove the submodule directory
git submodule deinit -f third_party/gopher-orch 2>/dev/null || true
rm -rf "${NATIVE_DIR}"
rm -rf .git/modules/third_party/gopher-orch 2>/dev/null || true
fi
# Update main submodule
# First try with recorded commit, if that fails (commit doesn't exist), use --remote to get latest
if ! git submodule update --init third_party/gopher-orch 2>/dev/null; then
echo -e "${YELLOW} Recorded commit not found, fetching latest from remote...${NC}"
if ! git submodule update --init --remote third_party/gopher-orch 2>/dev/null; then
echo -e "${RED}Error: Failed to clone gopher-orch submodule${NC}"
echo -e "${YELLOW}If you have multiple GitHub accounts, use:${NC}"
echo -e " GITHUB_SSH_HOST=your-ssh-alias ./build.sh"
exit 1
fi
fi
# Update nested submodule (gopher-mcp inside gopher-orch)
# Note: gopher-orch/.gitmodules has 'update = none' so we must explicitly update
if [ -d "${NATIVE_DIR}" ]; then
cd "${NATIVE_DIR}"
git config --local url."git@${SSH_HOST}:GopherSecurity/".insteadOf "https://github.com/GopherSecurity/"
# Override 'update = none' by using --checkout
git submodule update --init --checkout third_party/gopher-mcp 2>/dev/null || true
# Also update gopher-mcp's nested submodules recursively
if [ -d "third_party/gopher-mcp" ]; then
cd third_party/gopher-mcp
git config --local url."git@${SSH_HOST}:GopherSecurity/".insteadOf "https://github.com/GopherSecurity/"
git submodule update --init --recursive 2>/dev/null || true
fi
cd "${SCRIPT_DIR}"
fi
echo -e "${GREEN}✓ Submodules updated${NC}"
echo ""
# Step 2: Check if gopher-orch exists
if [ ! -d "${NATIVE_DIR}" ]; then
echo -e "${RED}Error: gopher-orch submodule not found at ${NATIVE_DIR}${NC}"
echo -e "${RED}Run: git submodule update --init --recursive${NC}"
exit 1
fi
# Step 3: Build gopher-orch native library
echo -e "${YELLOW}Step 2: Building gopher-orch native library...${NC}"
cd "${NATIVE_DIR}"
# Create build directory
if [ ! -d "${BUILD_DIR}" ]; then
mkdir -p "${BUILD_DIR}"
fi
cd "${BUILD_DIR}"
# Configure with CMake
# BUILD_BUNDLED_SHARED=OFF means we need to copy all dependency libraries separately
echo -e "${YELLOW} Configuring CMake...${NC}"
cmake .. \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX="${SCRIPT_DIR}/native" \
-DBUILD_SHARED_LIBS=ON \
-DBUILD_BUNDLED_SHARED=OFF \
-DBUILD_TESTS=OFF \
-DCMAKE_POSITION_INDEPENDENT_CODE=ON
# Build
echo -e "${YELLOW} Compiling...${NC}"
cmake --build . --config Release -j$(sysctl -n hw.ncpu 2>/dev/null || nproc 2>/dev/null || echo 4)
# Install to native directory
echo -e "${YELLOW} Installing...${NC}"
cmake --install .
# Copy dependency libraries (since BUILD_BUNDLED_SHARED=OFF)
echo -e "${YELLOW} Copying dependency libraries...${NC}"
NATIVE_LIB="${SCRIPT_DIR}/native/lib"
mkdir -p "${NATIVE_LIB}"
# Copy gopher-mcp libraries
cp -P "${BUILD_DIR}"/lib/libgopher-mcp*.dylib "${NATIVE_LIB}/" 2>/dev/null || true
cp -P "${BUILD_DIR}"/lib/libgopher-mcp*.so* "${NATIVE_LIB}/" 2>/dev/null || true
cp -P "${BUILD_DIR}"/lib/libgopher-mcp-event*.dylib "${NATIVE_LIB}/" 2>/dev/null || true
cp -P "${BUILD_DIR}"/lib/libgopher-mcp-event*.so* "${NATIVE_LIB}/" 2>/dev/null || true
cp -P "${BUILD_DIR}"/lib/libgopher-mcp-logging*.dylib "${NATIVE_LIB}/" 2>/dev/null || true
cp -P "${BUILD_DIR}"/lib/libgopher-mcp-logging*.so* "${NATIVE_LIB}/" 2>/dev/null || true
# Copy fmt and llhttp static libraries
cp -P "${BUILD_DIR}"/lib/libfmt*.a "${NATIVE_LIB}/" 2>/dev/null || true
cp -P "${BUILD_DIR}"/lib/libllhttp*.a "${NATIVE_LIB}/" 2>/dev/null || true
cp -P "${BUILD_DIR}"/_deps/fmt-build/libfmt*.a "${NATIVE_LIB}/" 2>/dev/null || true
cd "${SCRIPT_DIR}"
echo -e "${GREEN}✓ Native library built successfully${NC}"
echo ""
# Step 4: Verify build artifacts
echo -e "${YELLOW}Step 3: Verifying native build artifacts...${NC}"
NATIVE_LIB_DIR="${SCRIPT_DIR}/native/lib"
NATIVE_INCLUDE_DIR="${SCRIPT_DIR}/native/include"
if [ -d "${NATIVE_LIB_DIR}" ]; then
echo -e "${GREEN}✓ Libraries installed to: ${NATIVE_LIB_DIR}${NC}"
ls -lh "${NATIVE_LIB_DIR}"/lib*.dylib 2>/dev/null || \
ls -lh "${NATIVE_LIB_DIR}"/lib*.so 2>/dev/null || \
ls -lh "${NATIVE_LIB_DIR}"/*.dll 2>/dev/null || true
else
echo -e "${YELLOW}⚠ Library directory not found: ${NATIVE_LIB_DIR}${NC}"
fi
if [ -d "${NATIVE_INCLUDE_DIR}" ]; then
echo -e "${GREEN}✓ Headers installed to: ${NATIVE_INCLUDE_DIR}${NC}"
else
echo -e "${YELLOW}⚠ Include directory not found: ${NATIVE_INCLUDE_DIR}${NC}"
fi
echo ""
# Step 5: Set up Python environment
echo -e "${YELLOW}Step 4: Setting up Python environment...${NC}"
cd "${SCRIPT_DIR}"
# Check for Python
if ! command -v python3 &> /dev/null; then
echo -e "${RED}Error: Python 3 not found. Please install Python 3.8+ first.${NC}"
echo -e "${YELLOW} macOS: brew install python${NC}"
echo -e "${YELLOW} Linux: sudo apt-get install python3 python3-pip${NC}"
exit 1
fi
# Check Python version
PYTHON_VERSION=$(python3 -c 'import sys; print(f"{sys.version_info.major}.{sys.version_info.minor}")')
PYTHON_MAJOR=$(echo "$PYTHON_VERSION" | cut -d'.' -f1)
PYTHON_MINOR=$(echo "$PYTHON_VERSION" | cut -d'.' -f2)
if [ "$PYTHON_MAJOR" -lt 3 ] || ([ "$PYTHON_MAJOR" -eq 3 ] && [ "$PYTHON_MINOR" -lt 8 ]); then
echo -e "${RED}Error: Python 3.8+ required. Current version: ${PYTHON_VERSION}${NC}"
exit 1
fi
# Install dependencies
echo -e "${YELLOW} Installing pip dependencies...${NC}"
# Check if we're in a virtual environment
if [ -n "$VIRTUAL_ENV" ]; then
pip3 install -e ".[dev]" --quiet 2>/dev/null || pip3 install -e ".[dev]"
else
# Try user install first, then fall back to regular install
pip3 install --user -e ".[dev]" --quiet 2>/dev/null || \
pip3 install --user -e ".[dev]" 2>/dev/null || \
echo -e "${YELLOW} Note: Could not install in editable mode. You can manually run: pip3 install --user -e '.[dev]'${NC}"
fi
echo -e "${GREEN}✓ Python environment set up successfully${NC}"
echo ""
# Step 6: Run tests
echo -e "${YELLOW}Step 5: Running tests...${NC}"
# Use PYTHONPATH to ensure gopher_orch module can be found even without editable install
export PYTHONPATH="${SCRIPT_DIR}:${PYTHONPATH}"
# Try to run pytest, handling different installation scenarios
if python3 -c "import pytest" 2>/dev/null; then
python3 -m pytest tests/ -v && echo -e "${GREEN}✓ Tests passed${NC}" || echo -e "${YELLOW}⚠ Some tests failed${NC}"
elif [ -f "$HOME/Library/Python/3.9/bin/pytest" ]; then
# macOS user-installed pytest
"$HOME/Library/Python/3.9/bin/pytest" tests/ -v && echo -e "${GREEN}✓ Tests passed${NC}" || echo -e "${YELLOW}⚠ Some tests failed${NC}"
else
echo -e "${YELLOW}⚠ pytest not found. Install with: pip3 install --user pytest${NC}"
fi
echo ""
echo -e "${GREEN}======================================${NC}"
echo -e "${GREEN}Build completed successfully!${NC}"
echo -e "${GREEN}======================================${NC}"
echo ""
echo -e "Native libraries: ${YELLOW}${NATIVE_LIB_DIR}${NC}"
echo -e "Native headers: ${YELLOW}${NATIVE_INCLUDE_DIR}${NC}"
echo -e "Run tests: ${YELLOW}python3 -m pytest tests/${NC}"
echo -e "Run example: ${YELLOW}python3 examples/client_example.py${NC}"