forked from martin-ger/esp32_nat_router
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_all_targets.sh
More file actions
executable file
·308 lines (258 loc) · 9.31 KB
/
build_all_targets.sh
File metadata and controls
executable file
·308 lines (258 loc) · 9.31 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
#!/bin/bash
# ESP32 NAT Router - Multi-Target Build Script
# Compiles for all targets defined in BUILD_ORDER sequentially
set -e # Exit on any error
# Build targets in order
BUILD_ORDER=("esp32" "wt32_eth01" "esp32s3" "esp32c5" "esp32c6" "esp32c3")
# Target descriptions
declare -A TARGET_DESC=(
["esp32"]="ESP32 (Original)"
["esp32s3"]="ESP32-S3"
["esp32c6"]="ESP32-C6"
["esp32c3"]="ESP32-C3"
["esp32c5"]="ESP32-C5"
["wt32_eth01"]="WT32-ETH01 (Ethernet)"
)
# IDF chip target for each build target
declare -A TARGET_CHIP=(
["esp32"]="esp32"
["esp32s3"]="esp32s3"
["esp32c6"]="esp32c6"
["esp32c3"]="esp32c3"
["esp32c5"]="esp32c5"
["wt32_eth01"]="esp32"
)
# Extra sdkconfig defaults (semicolon-separated)
declare -A TARGET_SDKCONFIG=(
["wt32_eth01"]="sdkconfig.defaults;sdkconfig.defaults.wt32_eth01"
)
# Custom build directory (empty = default "build")
declare -A TARGET_BUILD_DIR=(
["wt32_eth01"]="build_eth"
)
# Custom sdkconfig file path (empty = default "sdkconfig")
declare -A TARGET_SDKCONFIG_FILE=(
["wt32_eth01"]="sdkconfig.eth"
)
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to print colored output
print_status() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Function to build for specific target
build_target() {
local target=$1
local description=$2
local chip="${TARGET_CHIP[$target]}"
local sdkconfig="${TARGET_SDKCONFIG[$target]}"
local build_dir="${TARGET_BUILD_DIR[$target]}"
local sdkconfig_file="${TARGET_SDKCONFIG_FILE[$target]}"
local build_args=()
print_status "Building for $description ($target)..."
# Use custom build directory if specified
if [ -n "$build_dir" ]; then
build_args+=("-B" "$build_dir")
fi
# Use custom sdkconfig file if specified (avoids conflicts with shared sdkconfig)
if [ -n "$sdkconfig_file" ]; then
build_args+=("-D" "SDKCONFIG=$sdkconfig_file")
fi
# Use custom sdkconfig defaults if specified
if [ -n "$sdkconfig" ]; then
build_args+=("-D" "SDKCONFIG_DEFAULTS=$sdkconfig")
fi
# Set target using idf.py (use build dir args if custom)
idf.py "${build_args[@]}" set-target "$chip"
# Clean previous build artifacts
print_status "Cleaning previous build artifacts..."
idf.py "${build_args[@]}" clean
# Build project
print_status "Starting compilation for $target..."
if idf.py "${build_args[@]}" build; then
# Save binary artifacts to separate directory
save_binary_artifacts "$target" "$description"
print_success "Build completed successfully for $target"
return 0
else
print_error "Build failed for $target"
return 1
fi
}
# Function to save binary artifacts to separate directory
save_binary_artifacts() {
local target=$1
local description=$2
local artifacts_dir="firmware_$target"
print_status "Saving binary artifacts to $artifacts_dir/..."
# Create artifacts directory if it doesn't exist
mkdir -p "$artifacts_dir"
# Find and copy relevant binary files
local build_dir="${TARGET_BUILD_DIR[$target]:-build}"
local files_copied=0
# Main firmware binary
if [ -f "$build_dir/esp32_nat_router.bin" ]; then
cp "$build_dir/esp32_nat_router.bin" "$artifacts_dir/"
print_status " ✓ Copied esp32_nat_router.bin"
((files_copied++))
fi
# Bootloader binary
if [ -f "$build_dir/bootloader/bootloader.bin" ]; then
cp "$build_dir/bootloader/bootloader.bin" "$artifacts_dir/"
print_status " ✓ Copied bootloader.bin"
((files_copied++))
fi
# Partition table
if [ -f "$build_dir/partition_table/partition-table.bin" ]; then
cp "$build_dir/partition_table/partition-table.bin" "$artifacts_dir/"
print_status " ✓ Copied partition-table.bin"
((files_copied++))
fi
# OTA data initial (required for OTA partition layout)
if [ -f "$build_dir/ota_data_initial.bin" ]; then
cp "$build_dir/ota_data_initial.bin" "$artifacts_dir/"
print_status " ✓ Copied ota_data_initial.bin"
((files_copied++))
fi
# Combined firmware (if available)
if [ -f "$build_dir/esp32_nat_router-merged.bin" ]; then
cp "$build_dir/esp32_nat_router-merged.bin" "$artifacts_dir/"
print_status " ✓ Copied esp32_nat_router-merged.bin"
((files_copied++))
fi
# Copy any other .bin files
for bin_file in "$build_dir"/*.bin; do
if [ -f "$bin_file" ]; then
local filename=$(basename "$bin_file")
if [ ! -f "$artifacts_dir/$filename" ]; then
cp "$bin_file" "$artifacts_dir/"
print_status " ✓ Copied $filename"
((files_copied++))
fi
fi
done
# Create a version info file
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
local git_hash=$(git rev-parse --short HEAD 2>/dev/null || echo "unknown")
cat > "$artifacts_dir/build_info.txt" << EOF
ESP32 NAT Router Build Information
=================================
Target: $description ($target)
Build Time: $timestamp
Git Hash: $git_hash
Binary Files: $files_copied
Build Directory: $build_dir
EOF
print_status " ✓ Created build_info.txt"
print_success "Saved $files_copied binary files to $artifacts_dir/"
}
# Function to check if idf.py is available
check_idf_env() {
if ! command -v idf.py &> /dev/null; then
print_error "idf.py not found. ESP-IDF environment not set up properly."
print_error "Please source ESP-IDF export script first:"
print_error " source /path/to/esp-idf/export.sh"
exit 1
fi
}
# Main script execution
main() {
print_status "ESP32 NAT Router Multi-Target Build Script"
print_status "=========================================="
# Check if ESP-IDF environment is set up
check_idf_env
# Store current directory
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
print_status "Working directory: $(pwd)"
# Array to store failed targets
FAILED_TARGETS=()
# Build for each target
for target in "${BUILD_ORDER[@]}"; do
description="${TARGET_DESC[$target]}"
echo ""
print_status "=========================================="
if ! build_target "$target" "$description"; then
FAILED_TARGETS+=("$target")
fi
echo ""
done
# Final summary
print_status "=========================================="
print_status "Build Summary"
print_status "=========================================="
if [ ${#FAILED_TARGETS[@]} -eq 0 ]; then
print_success "All targets built successfully!"
print_status "Binary artifacts are preserved in firmware directories:"
for target in "${BUILD_ORDER[@]}"; do
artifacts_dir="firmware_$target"
if [ -d "$artifacts_dir" ]; then
print_status " - $artifacts_dir/ (preserved)"
fi
done
print_status "Build directories (will be cleaned on next build):"
for target in "${BUILD_ORDER[@]}"; do
if [ -d "build/$target" ]; then
print_status " - build/$target/"
fi
done
else
print_error "Build failed for ${#FAILED_TARGETS[@]} target(s):"
for failed_target in "${FAILED_TARGETS[@]}"; do
print_error " - $failed_target (${TARGET_DESC[$failed_target]})"
done
exit 1
fi
# Show preserved binary sizes
echo ""
print_status "Preserved Binary Sizes:"
print_status "======================="
for target in "${BUILD_ORDER[@]}"; do
artifacts_dir="firmware_$target"
if [ -f "$artifacts_dir/esp32_nat_router.bin" ]; then
size=$(stat -f%z "$artifacts_dir/esp32_nat_router.bin" 2>/dev/null || stat -c%s "$artifacts_dir/esp32_nat_router.bin" 2>/dev/null || echo "unknown")
print_status " $target: $size bytes ($artifacts_dir/esp32_nat_router.bin)"
fi
done
# Show total size of all preserved artifacts
echo ""
total_size=0
for target in "${BUILD_ORDER[@]}"; do
artifacts_dir="firmware_$target"
if [ -d "$artifacts_dir" ]; then
for bin_file in "$artifacts_dir"/*.bin; do
if [ -f "$bin_file" ]; then
size=$(stat -f%z "$bin_file" 2>/dev/null || stat -c%s "$bin_file" 2>/dev/null || echo "0")
total_size=$((total_size + size))
fi
done
fi
done
if [ $total_size -gt 0 ]; then
if command -v numfmt &> /dev/null; then
human_size=$(numfmt --to=iec $total_size)
else
human_size="${total_size} bytes"
fi
print_status "Total preserved artifacts: $human_size"
fi
print_success "Multi-target build script completed!"
print_status "Binary artifacts are preserved in firmware_* directories and will not be cleaned."
}
# Handle script interruption
trap 'print_warning "Script interrupted by user"; exit 1' INT
# Run main function
main "$@"