-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.sh
More file actions
167 lines (138 loc) · 5.3 KB
/
build.sh
File metadata and controls
167 lines (138 loc) · 5.3 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
#!/bin/bash
# shellcheck disable=SC1091
# Store the current working directory
original_cwd=$(pwd)
# Directories for the core and onboard workspaces
CORE_WS="/home/ubuntu/robosub-ros2/core"
ONBOARD_WS="/home/ubuntu/robosub-ros2/onboard"
# Function to check if a package exists in a workspace
package_exists() {
package_name=$1
workspace_dir=$2
# Run colcon list and check if the package exists
colcon list --base-path "$workspace_dir" | awk '{print $1}' | grep -q "^$package_name$";
}
# Function to remove paths from an environment variable
remove_paths_from_env_var() {
env_var_name=$1
workspace_dir=$2
package_name=$3
# If package_name is specified, remove the install path for that package
# Otherwise, remove the entire workspace directory
if [ -n "$package_name" ]; then
path_to_remove="$workspace_dir/install/$package_name"
else
path_to_remove="$workspace_dir"
fi
# Get the value of the environment variable
env_var_value=$(eval echo \$"$env_var_name")
if [ -n "$env_var_value" ]; then
new_env_var_value=$(echo "$env_var_value" | tr ':' '\n' | grep -v "$path_to_remove" | tr '\n' ':')
new_env_var_value=${new_env_var_value%:} # Remove trailing colon
export "$env_var_name"="$new_env_var_value"
fi
}
# Function to clean workspace (build, install, log)
clean_workspace() {
workspace_dir=$1
package_name=$2
if [ -n "$package_name" ]; then
echo "Cleaning package '$package_name' in workspace: $workspace_dir"
rm -rf "$workspace_dir/build/$package_name" "$workspace_dir/install/$package_name" # Packages don't have a log directory
else
echo "Cleaning workspace: $workspace_dir"
rm -rf "$workspace_dir/build" "$workspace_dir/install" "$workspace_dir/log"
fi
# Remove paths from env variables used by colcon that include the workspace_dir
remove_paths_from_env_var "AMENT_PREFIX_PATH" "$workspace_dir" "$package_name"
remove_paths_from_env_var "CMAKE_PREFIX_PATH" "$workspace_dir" "$package_name"
remove_paths_from_env_var "COLCON_PREFIX_PATH" "$workspace_dir" "$package_name"
# If only cleaning a package, source the workspace setup.bash to update the environment
if [ -n "$package_name" ]; then
source "$workspace_dir/install/setup.bash"
fi
}
# Function to build workspace or package
build_workspace() {
workspace_dir=$1
package_name=$2
debug_mode=$3
# Colcon build must be run from the workspace directory
cd "$workspace_dir" || exit
build_cmd="colcon build"
# Don't use symlink install for core workspace
if [ "$workspace_dir" != "$CORE_WS" ]; then
build_cmd="$build_cmd --symlink-install"
fi
# Add package-specific build if specified
if [ -n "$package_name" ]; then
build_cmd="$build_cmd --packages-select $package_name"
echo "Building package '$package_name' in workspace: $workspace_dir"
else
echo "Building workspace: $workspace_dir"
fi
# Add debug flags if requested
if [ "$debug_mode" == true ]; then
build_cmd="$build_cmd --cmake-args -DCMAKE_BUILD_TYPE=RelWithDebInfo"
echo "Debug mode enabled: Adding RelWithDebInfo build type to CMake."
fi
# Use sequential executor as parallel builds can use too much memory
$build_cmd --executor sequential
# Source the workspace setup.bash to update the environment
source install/setup.bash
}
# Main script logic
debug_mode=false
# Check if --debug is specified and validate against 'clean'
for arg in "$@"; do
if [ "$arg" == "--debug" ]; then
for check_arg in "$@"; do
if [ "$check_arg" == "clean" ]; then
echo "Error: --debug flag cannot be used with the 'clean' command."
return 1
fi
done
debug_mode=true
set -- "${@/--debug/}" # Remove --debug from arguments to avoid interference
break
fi
done
if [ "$1" == "core" ]; then
# Build all packages in core workspace
build_workspace "$CORE_WS" "" "$debug_mode"
elif [ "$1" == "onboard" ]; then
# Build all packages in onboard workspace
build_workspace "$ONBOARD_WS" "" "$debug_mode"
elif [ "$1" == "clean" ]; then
# Clean workspaces
if [ "$2" == "core" ]; then
clean_workspace "$CORE_WS"
elif [ "$2" == "onboard" ]; then
clean_workspace "$ONBOARD_WS"
elif [ -n "$2" ]; then
# Make sure package exists in onboard workspace
if ! package_exists "$2" "$ONBOARD_WS"; then
echo "Error: Package '$2' not found in onboard workspace."
return 1
fi
# Clean a specific package in the onboard workspace
clean_workspace "$ONBOARD_WS" "$2"
else
clean_workspace "$CORE_WS"
clean_workspace "$ONBOARD_WS"
fi
elif [ -n "$1" ]; then
# Make sure package exists in onboard workspace
if ! package_exists "$1" "$ONBOARD_WS"; then
echo "Error: Package '$1' not found in onboard workspace."
return 1
fi
# Build a specific package in the onboard workspace
build_workspace "$ONBOARD_WS" "$1" "$debug_mode"
else
# Build all packages in both core and onboard workspaces
build_workspace "$CORE_WS" "" "$debug_mode"
build_workspace "$ONBOARD_WS" "" "$debug_mode"
fi
# Return to original directory
cd "$original_cwd" || exit