-
Notifications
You must be signed in to change notification settings - Fork 228
Expand file tree
/
Copy pathdocker-build.sh
More file actions
executable file
·343 lines (288 loc) · 11.4 KB
/
docker-build.sh
File metadata and controls
executable file
·343 lines (288 loc) · 11.4 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
#!/bin/bash
#
# Container wrapper for Thingino firmware build
# Provides non-interactive containerized build environment
#
# Usage:
# ./docker-build.sh # Build firmware (fast parallel)
# ./docker-build.sh dev # Debug build (slow serial, stops at errors)
# ./docker-build.sh menuconfig # Run menuconfig in container
# ./docker-build.sh shell # Open interactive shell
# ./docker-build.sh clean # Clean build in container
# ./docker-build.sh ota # Upgrade firmware OTA
#
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
print_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Minimum memory (MiB) for podman machine VM to avoid OOM during builds
PODMAN_MIN_MEMORY_MB=8192
# Detect container engine
if command -v podman >/dev/null 2>&1; then
CONTAINER_ENGINE="podman"
print_info "Using Podman"
elif command -v docker >/dev/null 2>&1; then
CONTAINER_ENGINE="docker"
print_info "Using Docker"
else
print_error "Neither Podman nor Docker found. Please install one of them."
echo
echo "Install Podman:"
echo " sudo apt update && sudo apt install podman"
echo
echo "Or install Docker:"
echo " curl -fsSL https://get.docker.com | sudo sh"
exit 1
fi
# Ensure podman machine has enough memory (macOS/Windows only; Linux rootless has no VM)
if [ "$CONTAINER_ENGINE" = "podman" ] && podman machine list >/dev/null 2>&1; then
PODMAN_MACHINE=$(podman machine list --format "{{.Name}}" 2>/dev/null | head -1)
if [ -n "$PODMAN_MACHINE" ]; then
CURRENT_MEM=$(podman machine inspect "$PODMAN_MACHINE" --format "{{.Resources.Memory}}" 2>/dev/null)
if [ -n "$CURRENT_MEM" ] && [ "$CURRENT_MEM" -lt "$PODMAN_MIN_MEMORY_MB" ]; then
print_info "Podman machine memory is ${CURRENT_MEM} MiB — increasing to ${PODMAN_MIN_MEMORY_MB} MiB to prevent OOM crashes..."
if podman machine set --memory "$PODMAN_MIN_MEMORY_MB" "$PODMAN_MACHINE" 2>/dev/null; then
print_success "Podman machine memory updated to ${PODMAN_MIN_MEMORY_MB} MiB"
else
print_error "Could not set podman machine memory automatically."
echo " Run manually: podman machine set --memory ${PODMAN_MIN_MEMORY_MB} ${PODMAN_MACHINE}"
fi
fi
fi
fi
# Build image if needed
DOCKER_IMAGE="thingino-builder"
DOCKER_TAG="latest"
if ! $CONTAINER_ENGINE images | grep -q "$DOCKER_IMAGE.*$DOCKER_TAG"; then
print_info "Building container image..."
make -f Makefile.docker docker-build CONTAINER_ENGINE="$CONTAINER_ENGINE"
print_success "Container image built"
else
print_info "Container image already exists"
fi
# Function to select camera
select_camera() {
local cameras_dir="configs/cameras${GROUP:+-$GROUP}"
local memo_file=".selected_camera${GROUP:+-$GROUP}"
if [ ! -d "$cameras_dir" ]; then
print_error "Camera configs directory not found: $cameras_dir"
exit 1
fi
# Check if CAMERA is already provided
if [ -n "$CAMERA" ]; then
if [ -d "$cameras_dir/$CAMERA" ]; then
echo "$CAMERA"
return 0
else
print_error "Provided CAMERA='$CAMERA' not found in $cameras_dir" >&2
exit 1
fi
fi
# Get list of cameras
local cameras=($(ls "$cameras_dir" | sort))
if [ ${#cameras[@]} -eq 0 ]; then
print_error "No camera configs found in $cameras_dir"
exit 1
fi
local selected_camera=""
# Check if there's a previous selection
if [ -f "$memo_file" ]; then
local prev_camera=$(cat "$memo_file")
if [ -n "$prev_camera" ] && [ -d "$cameras_dir/$prev_camera" ]; then
echo "" >&2
echo "Previously selected: $prev_camera" >&2
read -p "Use this camera? [Y/n]: " use_prev >&2
if [ -z "$use_prev" ] || [ "$use_prev" = "y" ] || [ "$use_prev" = "Y" ]; then
selected_camera="$prev_camera"
echo "$selected_camera"
return 0
fi
fi
fi
# Try fzf first (best UX) - can be disabled with USE_FZF=0
if [ "${USE_FZF:-1}" = "1" ] && command -v fzf >/dev/null 2>&1; then
print_info "Select camera (type to filter in order, e.g., 't20' shows t20* cameras):" >&2
selected_camera=$(printf '%s\n' "${cameras[@]}" | fzf \
--height=~100% \
--layout=reverse \
--exact \
--prompt="Camera: " \
--header="Select camera configuration (${#cameras[@]} available) - type to filter" \
--preview-window=hidden | sed 's/\x1b[^a-zA-Z]*[a-zA-Z]//g')
# Reset and clear terminal after fzf
tput sgr0 2>/dev/null || true
clear
echo "" >&2
# Try whiptail (used by main Makefile)
elif command -v whiptail >/dev/null 2>&1; then
# Build menu items for whiptail
local menu_items=()
for camera in "${cameras[@]}"; do
menu_items+=("$camera" "")
done
selected_camera=$(whiptail --title "Camera Selection" \
--menu "Select a camera config (${#cameras[@]} available):" \
20 76 12 \
"${menu_items[@]}" \
3>&1 1>&2 2>&3)
# Try dialog as fallback
elif command -v dialog >/dev/null 2>&1; then
# Build menu items for dialog
local menu_items=()
for camera in "${cameras[@]}"; do
menu_items+=("$camera" "")
done
selected_camera=$(dialog --stdout --title "Camera Selection" \
--menu "Select a camera config (${#cameras[@]} available):" \
20 76 12 \
"${menu_items[@]}")
# Fallback to numbered list
else
echo "" >&2
echo "Available cameras (${#cameras[@]} total):" >&2
echo "==========================================" >&2
local i=1
for camera in "${cameras[@]}"; do
printf "%3d) %s\n" $i "$camera" >&2
((i++))
done
echo "" >&2
read -p "Select camera number (1-${#cameras[@]}), or press Enter to cancel: " selection >&2
if [ -z "$selection" ]; then
print_info "Cancelled"
exit 0
fi
if ! [[ "$selection" =~ ^[0-9]+$ ]] || [ "$selection" -lt 1 ] || [ "$selection" -gt ${#cameras[@]} ]; then
print_error "Invalid selection: $selection"
exit 1
fi
selected_camera="${cameras[$((selection-1))]}"
fi
if [ -z "$selected_camera" ]; then
exit 0
fi
# Strip any ANSI color codes that might have been captured
selected_camera=$(echo "$selected_camera" | sed 's/\x1b[^a-zA-Z]*[a-zA-Z]//g')
# Save selection for next time
echo "$selected_camera" > "$memo_file"
echo "$selected_camera"
}
# Parse command
CMD="${1:-build}"
case "$CMD" in
shell)
print_info "Starting interactive shell in container..."
make -f Makefile.docker docker-shell CONTAINER_ENGINE="$CONTAINER_ENGINE"
;;
menuconfig|linux-menuconfig|busybox-menuconfig)
print_info "Running $CMD in container..."
make -f Makefile.docker "docker-$CMD" CONTAINER_ENGINE="$CONTAINER_ENGINE"
;;
clean)
print_info "Running clean build in container..."
make -f Makefile.docker docker-clean-build CONTAINER_ENGINE="$CONTAINER_ENGINE"
;;
cleanbuild)
# Select camera
CAMERA=$(select_camera)
# Strip any ANSI codes that might have been captured
CAMERA=$(echo "$CAMERA" | sed 's/\x1b[^a-zA-Z]*[a-zA-Z]//g')
if [ -z "$CAMERA" ]; then
print_error "No camera selected"
exit 1
fi
print_success "Selected camera: $CAMERA"
print_info "Running CLEAN build (distclean + fast parallel)..."
# Build with selected camera using cleanbuild target
make -f Makefile.docker docker-make CAMERA="$CAMERA" ${GROUP:+GROUP="$GROUP"} MAKECMDGOALS="cleanbuild" CONTAINER_ENGINE="$CONTAINER_ENGINE"
;;
dev)
# Select camera
CAMERA=$(select_camera)
# Strip any ANSI codes that might have been captured
CAMERA=$(echo "$CAMERA" | sed 's/\x1b[^a-zA-Z]*[a-zA-Z]//g')
if [ -z "$CAMERA" ]; then
print_error "No camera selected"
exit 1
fi
print_success "Selected camera: $CAMERA"
print_info "Running SERIAL build for debugging (incremental, stops at errors)..."
# Build with selected camera using dev target (serial build with V=1)
make -f Makefile.docker docker-make CAMERA="$CAMERA" ${GROUP:+GROUP="$GROUP"} MAKECMDGOALS="dev" CONTAINER_ENGINE="$CONTAINER_ENGINE"
;;
ota)
# Select camera
CAMERA=$(select_camera)
# Strip any ANSI codes that might have been captured
CAMERA=$(echo "$CAMERA" | sed 's/\x1b[^a-zA-Z]*[a-zA-Z]//g')
if [ -z "$CAMERA" ]; then
print_error "No camera selected"
exit 1
fi
print_success "Selected camera: $CAMERA"
print_info "Running ota in container..."
# Build with selected camera
make -f Makefile.docker docker-ota CAMERA="$CAMERA" ${GROUP:+GROUP="$GROUP"} CONTAINER_ENGINE="$CONTAINER_ENGINE" "$@"
;;
build|"")
# Select camera
CAMERA=$(select_camera)
# Strip any ANSI codes that might have been captured
CAMERA=$(echo "$CAMERA" | sed 's/\x1b[^a-zA-Z]*[a-zA-Z]//g')
if [ -z "$CAMERA" ]; then
print_error "No camera selected"
exit 1
fi
print_success "Selected camera: $CAMERA"
print_info "Building firmware in container (parallel incremental)..."
# Build with selected camera (uses default 'all' target which is incremental parallel)
make -f Makefile.docker docker-make CAMERA="$CAMERA" ${GROUP:+GROUP="$GROUP"} MAKECMDGOALS="all" CONTAINER_ENGINE="$CONTAINER_ENGINE"
;;
info)
make -f Makefile.docker docker-info CONTAINER_ENGINE="$CONTAINER_ENGINE"
;;
images)
print_info "Locating built firmware images..."
if [ -d "output-stable" ]; then
find output-stable -name "thingino-*.bin" -type f -exec ls -lh {} \;
else
print_error "No output-stable directory found. Have you built firmware yet?"
fi
;;
rebuild-image)
print_info "Rebuilding container image..."
make -f Makefile.docker docker-clean CONTAINER_ENGINE="$CONTAINER_ENGINE"
make -f Makefile.docker docker-build CONTAINER_ENGINE="$CONTAINER_ENGINE"
print_success "Container image rebuilt"
;;
*)
cat << 'EOF' >&2
Unknown command. Available commands:
./docker-build.sh Build firmware (parallel incremental)
./docker-build.sh cleanbuild Clean + build from scratch (parallel)
./docker-build.sh dev Debug build (serial incremental, V=1, stops at errors)
./docker-build.sh shell Interactive shell in container
./docker-build.sh menuconfig Configure build options
./docker-build.sh clean Clean build artifacts
./docker-build.sh info Show container configuration
./docker-build.sh rebuild-image Rebuild the container image
./docker-build.sh ota Upgrade firmware OTA (requires IP=x.x.x.x)
EOF
exit 1
;;
esac
exit 0