-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·390 lines (332 loc) · 15 KB
/
build.sh
File metadata and controls
executable file
·390 lines (332 loc) · 15 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
#!/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 -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 Ruby SDK${NC}"
echo -e "${GREEN}======================================${NC}"
echo ""
# Step 1: Update submodules recursively
echo -e "${YELLOW}Step 1: Checking submodules...${NC}"
# Support custom SSH host for multiple GitHub accounts
# Usage: GITHUB_SSH_HOST=bettercallsaulj ./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
git config --local url."git@${SSH_HOST}:GopherSecurity/".insteadOf "https://github.com/GopherSecurity/"
git config --local submodule.third_party/gopher-orch.url "git@${SSH_HOST}:GopherSecurity/gopher-orch.git"
# Check if submodule already exists and has content (e.g., manually copied)
if [ -d "${NATIVE_DIR}" ] && [ -f "${NATIVE_DIR}/CMakeLists.txt" ]; then
echo -e "${GREEN}✓ gopher-orch submodule already present${NC}"
else
# Update main submodule
echo -e "${YELLOW} Cloning gopher-orch submodule...${NC}"
if ! git submodule update --init 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/"
# Check if nested submodule already exists
if [ -d "third_party/gopher-mcp" ] && [ -f "third_party/gopher-mcp/CMakeLists.txt" ]; then
echo -e "${GREEN}✓ gopher-mcp nested submodule already present${NC}"
else
# Override 'update = none' by using --checkout
echo -e "${YELLOW} Updating nested gopher-mcp submodule...${NC}"
git submodule update --init --checkout third_party/gopher-mcp 2>/dev/null || true
fi
# 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 ready${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
echo -e "${YELLOW} Configuring CMake...${NC}"
cmake .. \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX="${SCRIPT_DIR}/native" \
-DBUILD_SHARED_LIBS=ON \
-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 (gopher-mcp, fmt) that gopher-orch depends on
echo -e "${YELLOW} Copying dependency libraries...${NC}"
NATIVE_LIB_DIR="${SCRIPT_DIR}/native/lib"
mkdir -p "${NATIVE_LIB_DIR}"
# Copy gopher-mcp libraries
cp -P "${BUILD_DIR}/lib/libgopher-mcp"*.dylib "${NATIVE_LIB_DIR}/" 2>/dev/null || \
cp -P "${BUILD_DIR}/lib/libgopher-mcp"*.so "${NATIVE_LIB_DIR}/" 2>/dev/null || true
# Copy fmt library
cp -P "${BUILD_DIR}/lib/libfmt"*.dylib "${NATIVE_LIB_DIR}/" 2>/dev/null || \
cp -P "${BUILD_DIR}/lib/libfmt"*.so "${NATIVE_LIB_DIR}/" 2>/dev/null || true
# Fix dylib install names on macOS (required for Ruby FFI to find dependencies)
if [[ "$OSTYPE" == "darwin"* ]]; then
echo -e "${YELLOW} Fixing dylib install names for macOS...${NC}"
cd "${NATIVE_LIB_DIR}"
# Find the actual libfmt version (e.g., libfmt.10.dylib or libfmt.11.dylib)
LIBFMT_DYLIB=$(ls libfmt.*.dylib 2>/dev/null | grep -E 'libfmt\.[0-9]+\.dylib$' | head -1)
LIBFMT_VERSION=$(echo "$LIBFMT_DYLIB" | sed 's/libfmt\.\([0-9]*\)\.dylib/\1/')
# Fix libgopher-orch to use @loader_path for dependencies
if [ -f "libgopher-orch.dylib" ]; then
install_name_tool -id "@rpath/libgopher-orch.dylib" libgopher-orch.dylib 2>/dev/null || true
install_name_tool -change "@rpath/libgopher-mcp.dylib" "@loader_path/libgopher-mcp.dylib" libgopher-orch.dylib 2>/dev/null || true
install_name_tool -change "@rpath/libgopher-mcp-event.dylib" "@loader_path/libgopher-mcp-event.dylib" libgopher-orch.dylib 2>/dev/null || true
# Handle different libfmt versions
for v in 10 11 12; do
install_name_tool -change "@rpath/libfmt.${v}.dylib" "@loader_path/libfmt.${v}.dylib" libgopher-orch.dylib 2>/dev/null || true
install_name_tool -change "libfmt.so.${v}" "@loader_path/libfmt.${v}.dylib" libgopher-orch.dylib 2>/dev/null || true
done
fi
# Fix libgopher-mcp
if [ -f "libgopher-mcp.dylib" ]; then
install_name_tool -id "@rpath/libgopher-mcp.dylib" libgopher-mcp.dylib 2>/dev/null || true
install_name_tool -change "@rpath/libgopher-mcp-event.dylib" "@loader_path/libgopher-mcp-event.dylib" libgopher-mcp.dylib 2>/dev/null || true
for v in 10 11 12; do
install_name_tool -change "@rpath/libfmt.${v}.dylib" "@loader_path/libfmt.${v}.dylib" libgopher-mcp.dylib 2>/dev/null || true
install_name_tool -change "libfmt.so.${v}" "@loader_path/libfmt.${v}.dylib" libgopher-mcp.dylib 2>/dev/null || true
done
fi
# Fix libgopher-mcp-event
if [ -f "libgopher-mcp-event.dylib" ]; then
install_name_tool -id "@rpath/libgopher-mcp-event.dylib" libgopher-mcp-event.dylib 2>/dev/null || true
for v in 10 11 12; do
install_name_tool -change "@rpath/libfmt.${v}.dylib" "@loader_path/libfmt.${v}.dylib" libgopher-mcp-event.dylib 2>/dev/null || true
install_name_tool -change "libfmt.so.${v}" "@loader_path/libfmt.${v}.dylib" libgopher-mcp-event.dylib 2>/dev/null || true
done
fi
# Fix libfmt
if [ -n "$LIBFMT_DYLIB" ] && [ -f "$LIBFMT_DYLIB" ]; then
install_name_tool -id "@rpath/$LIBFMT_DYLIB" "$LIBFMT_DYLIB" 2>/dev/null || true
fi
cd "${SCRIPT_DIR}"
fi
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}"/*.dylib 2>/dev/null || ls -lh "${NATIVE_LIB_DIR}"/*.so 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: Check Ruby environment
echo -e "${YELLOW}Step 4: Checking Ruby environment...${NC}"
cd "${SCRIPT_DIR}"
RUBY_AVAILABLE=false
RUBY_READY=true
# Prefer Homebrew Ruby on macOS (it's keg-only so not in PATH by default)
if [[ "$OSTYPE" == "darwin"* ]]; then
HOMEBREW_RUBY="/usr/local/opt/ruby/bin"
HOMEBREW_GEMS="/usr/local/lib/ruby/gems/4.0.0/bin"
# Also check Apple Silicon path
if [ ! -d "$HOMEBREW_RUBY" ]; then
HOMEBREW_RUBY="/opt/homebrew/opt/ruby/bin"
HOMEBREW_GEMS="/opt/homebrew/lib/ruby/gems/4.0.0/bin"
fi
if [ -d "$HOMEBREW_RUBY" ]; then
export PATH="$HOMEBREW_RUBY:$HOMEBREW_GEMS:$PATH"
echo -e "${GREEN}✓ Using Homebrew Ruby${NC}"
fi
fi
# Check for Ruby
if ! command -v ruby &> /dev/null; then
echo -e "${YELLOW}⚠ Ruby not found. Install Ruby to use the SDK:${NC}"
echo -e "${YELLOW} macOS: brew install ruby${NC}"
echo -e "${YELLOW} Linux: sudo apt-get install ruby ruby-dev${NC}"
RUBY_READY=false
else
RUBY_AVAILABLE=true
# Check Ruby version
RUBY_VERSION=$(ruby -v | head -n 1 | cut -d ' ' -f 2)
RUBY_MAJOR=$(echo "$RUBY_VERSION" | cut -d '.' -f 1)
RUBY_MINOR=$(echo "$RUBY_VERSION" | cut -d '.' -f 2)
echo -e "${GREEN}✓ Ruby version: ${RUBY_VERSION}${NC}"
# Check minimum version (>= 2.7)
if [ "$RUBY_MAJOR" -lt 2 ] || ([ "$RUBY_MAJOR" -eq 2 ] && [ "$RUBY_MINOR" -lt 7 ]); then
echo -e "${RED}✗ Ruby version must be >= 2.7 (found ${RUBY_VERSION})${NC}"
echo -e "${YELLOW} Please upgrade Ruby:${NC}"
echo -e "${YELLOW} macOS: brew upgrade ruby${NC}"
echo -e "${YELLOW} Linux: Use rbenv or rvm to install Ruby >= 2.7${NC}"
RUBY_READY=false
fi
fi
# Check for gem command
if [ "$RUBY_AVAILABLE" = true ]; then
if ! command -v gem &> /dev/null; then
echo -e "${YELLOW}⚠ gem command not found${NC}"
RUBY_READY=false
else
echo -e "${GREEN}✓ gem command available${NC}"
fi
fi
# Check for Bundler
if ! command -v bundle &> /dev/null; then
echo -e "${YELLOW}⚠ Bundler not found. Installing...${NC}"
if [ "$RUBY_AVAILABLE" = true ]; then
gem install bundler --quiet 2>/dev/null && echo -e "${GREEN}✓ Bundler installed${NC}" || {
echo -e "${RED}✗ Failed to install Bundler. Install manually:${NC}"
echo -e "${YELLOW} gem install bundler${NC}"
RUBY_READY=false
}
fi
else
BUNDLER_VERSION=$(bundle -v | cut -d ' ' -f 3)
echo -e "${GREEN}✓ Bundler version: ${BUNDLER_VERSION}${NC}"
fi
# Install dependencies if Gemfile exists
if [ "$RUBY_READY" = true ] && [ -f "Gemfile" ]; then
echo -e "${YELLOW} Installing gem dependencies...${NC}"
if bundle install --quiet 2>/dev/null; then
echo -e "${GREEN}✓ Dependencies installed${NC}"
else
echo -e "${YELLOW}⚠ bundle install failed, trying without --quiet...${NC}"
bundle install 2>&1 | tail -5
RUBY_READY=false
fi
fi
# Verify FFI gem is available
if [ "$RUBY_READY" = true ]; then
echo -e "${YELLOW} Verifying FFI gem...${NC}"
if ruby -e "require 'ffi'" 2>/dev/null; then
FFI_VERSION=$(ruby -e "require 'ffi'; puts FFI::VERSION" 2>/dev/null)
echo -e "${GREEN}✓ FFI gem version: ${FFI_VERSION}${NC}"
else
echo -e "${RED}✗ FFI gem not available${NC}"
echo -e "${YELLOW} Install with: gem install ffi${NC}"
RUBY_READY=false
fi
fi
# Verify SDK can be loaded
if [ "$RUBY_READY" = true ]; then
echo -e "${YELLOW} Verifying SDK can be loaded...${NC}"
if ruby -I"${SCRIPT_DIR}/lib" -e "require 'gopher_orch'; puts 'SDK loaded successfully'" 2>/dev/null; then
echo -e "${GREEN}✓ SDK loads successfully${NC}"
# Check if native library is available
if ruby -I"${SCRIPT_DIR}/lib" -e "require 'gopher_orch'; exit(GopherOrch.available? ? 0 : 1)" 2>/dev/null; then
echo -e "${GREEN}✓ Native library is available${NC}"
else
echo -e "${YELLOW}⚠ Native library not yet loadable (may need DYLD_LIBRARY_PATH)${NC}"
fi
else
echo -e "${RED}✗ Failed to load SDK${NC}"
echo -e "${YELLOW} Check for syntax errors in lib/ files${NC}"
RUBY_READY=false
fi
fi
# Summary
echo ""
if [ "$RUBY_READY" = true ]; then
echo -e "${GREEN}✓ Ruby environment is ready${NC}"
else
echo -e "${YELLOW}⚠ Ruby environment has issues (see above)${NC}"
fi
echo ""
# Step 6: Run tests if RSpec is available
echo -e "${YELLOW}Step 5: Running tests...${NC}"
if [ "$RUBY_READY" = false ]; then
echo -e "${YELLOW}⚠ Skipping tests (Ruby environment not ready)${NC}"
elif [ -f "Gemfile" ] && bundle exec rspec --version &> /dev/null; then
bundle exec rspec --format documentation 2>/dev/null && echo -e "${GREEN}✓ Tests passed${NC}" || echo -e "${YELLOW}⚠ Some tests may have failed (native library required)${NC}"
elif command -v rspec &> /dev/null; then
rspec --format documentation 2>/dev/null && echo -e "${GREEN}✓ Tests passed${NC}" || echo -e "${YELLOW}⚠ Some tests may have failed (native library required)${NC}"
else
echo -e "${YELLOW}⚠ RSpec not found, skipping tests${NC}"
fi
echo ""
# Step 7: Build auth example
echo -e "${YELLOW}Step 6: Building auth example...${NC}"
AUTH_EXAMPLE_DIR="${SCRIPT_DIR}/examples/auth"
if [ -d "${AUTH_EXAMPLE_DIR}" ] && [ -f "${AUTH_EXAMPLE_DIR}/Gemfile" ]; then
cd "${AUTH_EXAMPLE_DIR}"
if [ "$RUBY_READY" = true ]; then
echo -e "${YELLOW} Installing auth example dependencies...${NC}"
if bundle install --quiet 2>/dev/null; then
echo -e "${GREEN}✓ Auth example dependencies installed${NC}"
# Run auth example tests
echo -e "${YELLOW} Running auth example tests...${NC}"
if bundle exec rspec --format documentation 2>/dev/null; then
echo -e "${GREEN}✓ Auth example tests passed${NC}"
else
echo -e "${YELLOW}⚠ Some auth example tests may have failed${NC}"
fi
else
echo -e "${YELLOW}⚠ Failed to install auth example dependencies${NC}"
bundle install 2>&1 | tail -5
fi
else
echo -e "${YELLOW}⚠ Skipping auth example (Ruby environment not ready)${NC}"
fi
cd "${SCRIPT_DIR}"
else
echo -e "${YELLOW}⚠ Auth example not found at ${AUTH_EXAMPLE_DIR}${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}bundle exec rspec${NC}"
echo -e "Run example: ${YELLOW}ruby examples/client_example_json.rb${NC}"
echo -e "Run auth server: ${YELLOW}./examples/auth/run_example.sh${NC}"