forked from fan-ziqi/rl_sar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·403 lines (339 loc) · 12.7 KB
/
build.sh
File metadata and controls
executable file
·403 lines (339 loc) · 12.7 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
391
392
393
394
395
396
397
398
399
400
401
402
403
#!/bin/bash
set -e
# Get script directory
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
# Load common utilities
source "${SCRIPT_DIR}/scripts/common.sh"
# ========================
# Configuration
# ========================
# ========================
# Build Functions
# ========================
setup_inference_runtime() {
print_header "[Setting up Inference Runtime]"
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
DOWNLOAD_SCRIPT="${SCRIPT_DIR}/scripts/download_inference_runtime.sh"
if [ -f "$DOWNLOAD_SCRIPT" ]; then
print_info "Checking inference libraries..."
bash "$DOWNLOAD_SCRIPT" || {
print_error "Failed to setup inference libraries"
exit 1
}
print_success "Inference runtime setup completed!"
else
print_warning "Download script not found: $DOWNLOAD_SCRIPT"
fi
}
setup_mujoco() {
print_header "[Setting up MuJoCo]"
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
DOWNLOAD_MUJOCO_SCRIPT="${SCRIPT_DIR}/scripts/download_mujoco.sh"
if [ -f "$DOWNLOAD_MUJOCO_SCRIPT" ]; then
print_info "Checking MuJoCo library..."
bash "$DOWNLOAD_MUJOCO_SCRIPT" || {
print_error "Failed to setup MuJoCo"
exit 1
}
print_success "MuJoCo setup completed!"
else
print_warning "MuJoCo download script not found: $DOWNLOAD_MUJOCO_SCRIPT"
fi
}
setup_robot_descriptions() {
print_header "[Setting up Robot Descriptions]"
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
DOWNLOAD_ROBOT_DESC_SCRIPT="${SCRIPT_DIR}/scripts/download_robot_descriptions.sh"
if [ -f "$DOWNLOAD_ROBOT_DESC_SCRIPT" ]; then
print_info "Checking robot description files..."
bash "$DOWNLOAD_ROBOT_DESC_SCRIPT" || {
print_error "Failed to setup robot descriptions"
exit 1
}
print_success "Robot descriptions setup completed!"
else
print_warning "Robot descriptions download script not found: $DOWNLOAD_ROBOT_DESC_SCRIPT"
fi
}
run_cmake_build() {
print_header "[Running CMake Build]"
print_warning "NOTE: CMake build is for hardware deployment only, not for simulation."
print_separator
cmake src/rl_sar/ -B cmake_build -DUSE_CMAKE=ON
cmake --build cmake_build -j$(nproc 2>/dev/null || echo 4)
print_success "CMake build completed!"
}
run_mujoco_build() {
print_header "[Running MuJoCo Build]"
print_info "Building with MuJoCo simulator support..."
print_separator
cmake src/rl_sar/ -B cmake_build -DUSE_CMAKE=ON -DUSE_MUJOCO=ON
cmake --build cmake_build -j$(nproc 2>/dev/null || echo 4)
print_success "MuJoCo build completed!"
}
run_ros_build() {
local packages=("$@")
local package_list=$(IFS=' '; echo "${packages[*]}")
print_header "[Running ROS Build]"
# Clean existing symlinks
clean_existing_symlinks "${packages[@]}"
# Detect incompatible artifacts
detect_incompatible_build_artifacts
# Create appropriate symlinks
if [ ${#packages[@]} -eq 0 ]; then
create_symlinks_for_all_packages
else
create_symlinks_for_specific_packages "${packages[@]}"
fi
# Execute build
if [ ${#packages[@]} -eq 0 ]; then
if [[ "$ROS_DISTRO" == "noetic" ]]; then
print_header "[Using catkin build]"
print_info "Building all packages..."
catkin build
else
print_header "[Using colcon build]"
print_info "Building all packages..."
colcon build --merge-install --symlink-install
fi
else
if [[ "$ROS_DISTRO" == "noetic" ]]; then
print_header "[Using catkin build]"
print_info "Building specific packages: $package_list"
catkin build $package_list
else
print_header "[Using colcon build]"
print_info "Building specific packages: $package_list"
colcon build --merge-install --symlink-install --packages-select $package_list
fi
fi
print_success "ROS build completed!"
}
# ========================
# Clean Functions
# ========================
clean_workspace() {
local packages=("$@")
print_header "[Cleaning Workspace]"
# Show what will be cleaned
print_info "The following will be cleaned:"
if [ ${#packages[@]} -eq 0 ]; then
echo " - All package.xml symlinks in directory src/"
else
echo " - Package.xml symlinks for: ${packages[*]}"
fi
echo " - directory build/"
echo " - directory cmake_build/"
echo " - directory devel/"
echo " - directory install/"
echo " - directory log/"
echo " - directory logs/"
echo " - directory .catkin_tools/"
# Ask for confirmation
if [ ${#packages[@]} -eq 0 ]; then
if ! ask_confirmation "Are you sure you want to clean ALL symlinks and build artifacts?"; then
print_warning "Clean operation cancelled."
exit 0
fi
else
if ! ask_confirmation "Are you sure you want to clean symlinks for specified packages and build artifacts?"; then
print_warning "Clean operation cancelled."
exit 0
fi
fi
# Remove package.xml symlinks
if [ ${#packages[@]} -eq 0 ]; then
print_info "Removing all package.xml symlinks..."
find src -name "package.xml" -type l -delete
print_success "Removed all symlinks"
else
print_info "Removing symlinks for specific packages..."
for package_name in "${packages[@]}"; do
package_dir=$(find src -name "$package_name" -type d | head -n 1)
if [ -n "$package_dir" ]; then
if [ -L "$package_dir/package.xml" ]; then
rm -f "$package_dir/package.xml"
print_success "Removed symlink from $package_name"
else
print_warning "No symlink found for $package_name"
fi
else
print_error "Package '$package_name' not found in src directory"
fi
done
fi
# Clean build artifacts
print_info "Cleaning build artifacts..."
rm -rf build/ cmake_build/ devel/ install/ log/ logs/ .catkin_tools/
print_success "Clean completed!"
}
clean_existing_symlinks() {
local packages=("$@")
print_header "[Cleaning Existing Symlinks]"
if [ ${#packages[@]} -eq 0 ]; then
print_info "Removing all existing package.xml symlinks..."
find src -name "package.xml" -type l -delete
print_success "Removed all existing symlinks"
else
print_info "Removing existing symlinks for specified packages..."
removed_packages=()
for package_name in "${packages[@]}"; do
package_dir=$(find src -name "$package_name" -type d | head -n 1)
if [ -n "$package_dir" ] && [ -L "$package_dir/package.xml" ]; then
rm -f "$package_dir/package.xml"
removed_packages+=("$package_name")
fi
done
if [ ${#removed_packages[@]} -gt 0 ]; then
print_success "Removed existing symlinks from: ${removed_packages[*]}"
else
print_warning "No existing symlinks found"
fi
fi
}
# ========================
# ROS Specific Functions
# ========================
detect_incompatible_build_artifacts() {
print_header "[Checking for Incompatible Build Artifacts]"
local needs_cleanup=false
# Check for ROS1 artifacts when using ROS2
if [[ "$ROS_DISTRO" != "noetic" ]]; then
if [ -d "devel" ] || [ -d ".catkin_tools" ]; then
print_warning "Found ROS1 build artifacts (devel/ or .catkin_tools/) while using ROS2. Cleaning workspace..."
needs_cleanup=true
fi
fi
# Check for ROS2 artifacts when using ROS1
if [[ "$ROS_DISTRO" == "noetic" ]]; then
if [ -d "install" ] || [ -d "log" ]; then
print_warning "Found ROS2 build artifacts (install/ or log/) while using ROS1. Cleaning workspace..."
needs_cleanup=true
fi
fi
if [ "$needs_cleanup" = true ]; then
clean_workspace
else
print_success "No incompatible build artifacts found"
fi
}
create_symlinks_for_package() {
local package_dir="$1"
local package_name=$(basename "$package_dir")
if [ -d "$package_dir" ]; then
if [ -f "$package_dir/package.ros1.xml" ] && [ -f "$package_dir/package.ros2.xml" ]; then
[ -e "$package_dir/package.xml" ] && rm -f "$package_dir/package.xml"
if [[ "$ROS_DISTRO" == "noetic" ]]; then
ln -s package.ros1.xml "$package_dir/package.xml"
return 0
elif [[ "$ROS_DISTRO" == "foxy" || "$ROS_DISTRO" == "humble" ]]; then
ln -s package.ros2.xml "$package_dir/package.xml"
return 0
else
print_error "Unknown ROS version: $ROS_DISTRO"
return 1
fi
fi
fi
return 1
}
create_symlinks_for_all_packages() {
print_header "[Creating Symlinks for All Packages]"
created_packages=()
while IFS= read -r -d '' package_dir; do
package_dir=$(dirname "$package_dir")
package_name=$(basename "$package_dir")
if create_symlinks_for_package "$package_dir"; then
created_packages+=("$package_name")
fi
done < <(find src -name "package.ros1.xml" -print0)
if [ ${#created_packages[@]} -gt 0 ]; then
print_success "Created symlinks for: ${created_packages[*]}"
else
print_warning "No packages with dual ROS support found"
fi
}
create_symlinks_for_specific_packages() {
local packages=("$@")
print_header "[Creating Symlinks for Specific Packages]"
print_info "Packages to process: ${packages[*]}"
created_packages=()
for package_name in "${packages[@]}"; do
package_dir=$(find src -name "$package_name" -type d | head -n 1)
if [ -n "$package_dir" ] && create_symlinks_for_package "$package_dir"; then
created_packages+=("$package_name")
fi
done
if [ ${#created_packages[@]} -gt 0 ]; then
print_success "Created symlinks for: ${created_packages[*]}"
fi
}
# ========================
# Main Script
# ========================
show_usage() {
print_header "[Build System Usage]"
print_header
echo -e "Usage: $0 [OPTIONS] [PACKAGE_NAMES...]"
echo ""
echo -e "${COLOR_INFO}Options:${COLOR_RESET}"
echo -e " -c, --clean Clean workspace (remove symlinks and build artifacts)"
echo -e " -m, --cmake Build using CMake (for hardware deployment only)"
echo -e " -mj,--mujoco Build with MuJoCo simulator support (CMake only)"
echo -e " -h, --help Show this help message"
echo ""
echo -e "${COLOR_INFO}Examples:${COLOR_RESET}"
echo -e " $0 # Build all ROS packages"
echo -e " $0 package1 package2 # Build specific ROS packages"
echo -e " $0 -c # Clean all symlinks and build artifacts"
echo -e " $0 --clean package1 # Clean specific package and build artifacts"
echo -e " $0 -m # Build with CMake for hardware deployment"
echo -e " $0 -mj # Build with CMake and MuJoCo simulator support"
}
main() {
local packages=()
local clean_mode=false
local cmake_mode=false
local mujoco_mode=false
# Parse command line arguments
while [[ $# -gt 0 ]]; do
case $1 in
-c|--clean) clean_mode=true; shift ;;
-m|--cmake) cmake_mode=true; shift ;;
-mj|--mujoco) cmake_mode=true; mujoco_mode=true; shift ;;
-h|--help) show_usage; exit 0 ;;
--) shift; packages+=("$@"); break ;;
-*) print_error "Unknown option: $1"; show_usage; exit 1 ;;
*) packages+=("$1"); shift ;;
esac
done
# Handle MuJoCo build mode
if [ "$mujoco_mode" = true ]; then
setup_inference_runtime
setup_robot_descriptions
setup_mujoco
run_mujoco_build
exit 0
fi
# Handle CMake build mode
if [ "$cmake_mode" = true ]; then
setup_inference_runtime
run_cmake_build
exit 0
fi
# Handle clean mode
if [ "$clean_mode" = true ]; then
clean_workspace "${packages[@]}"
exit 0
fi
# Handle ROS build
if [ -z "$ROS_DISTRO" ]; then
print_error "ROS environment not detected. Please source your ROS setup.bash first."
print_info "For hardware deployment, use the --cmake option instead."
exit 1
fi
setup_inference_runtime
setup_robot_descriptions
run_ros_build "${packages[@]}"
}
main "$@"