forked from Soldann/MORB_SLAM
-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.sh
executable file
·179 lines (163 loc) · 5.08 KB
/
build.sh
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
#!/bin/bash
# https://stackoverflow.com/questions/24112727/relative-paths-based-on-file-location-instead-of-current-working-directory
parent_path=$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P )
cd "$parent_path" # change directories so working directory is where the script is
check_install_packages() {
MISSING_PACKAGES=""
set +e
for PACKAGE_NAME in "$@"; do
# check if the package is installed
dpkg -s $PACKAGE_NAME &> /dev/null
if [ $? -eq 0 ]; then
# echo "$PACKAGE_NAME is already installed."
pass=""
else
MISSING_PACKAGES="$MISSING_PACKAGES $PACKAGE_NAME"
fi
done
set -e
if [ -z "$MISSING_PACKAGES" ]; then
pass=""
# echo "All apt package dependencies are already installed."
else
echo "Installing missing apt packages: $MISSING_PACKAGES"
sudo apt update
sudo apt install -y $MISSING_PACKAGES
fi
}
check_pip(){
check_install_packages python3-pip
# python3 -c "import pkg_resources; pkg_resources.get_distribution('$package_name')" 1>/dev/null
MISSING_PACKAGES=""
set +e
for PACKAGE_NAME in "$@"; do
# check if the package is installed
python3 -c "import pkg_resources; pkg_resources.get_distribution('$PACKAGE_NAME')" 1>/dev/null 2>&1
if [ $? -eq 0 ]; then
# echo "$PACKAGE_NAME is already installed."
pass=""
else
MISSING_PACKAGES="$MISSING_PACKAGES $PACKAGE_NAME"
fi
done
set -e
if [ -z "$MISSING_PACKAGES" ]; then
pass=""
# echo "All apt package dependencies are already installed."
else
echo "Installing missing pip packages: $MISSING_PACKAGES"
# sudo apt update
python3 -m pip install $MISSING_PACKAGES
fi
}
do_vcpkg_prereqs(){
check_install_packages bison autoconf libtool curl pkg-config libxi-dev libxtst-dev libxrandr-dev nasm libgl-dev libxmu-dev libglu1-mesa-dev libudev-dev
check_pip jinja2
set +e
}
# Initialize lists
cmake_args=()
j_arg="-j$(nproc)"
g_arg=""
vcpkg_arg=""
# Iterate over arguments
for arg in "$@"; do
if [[ $arg =~ ^-j[0-9]+$ ]]; then
j_arg="$arg"
elif [[ $arg =~ ^-G[0-9a-zA-Z]+$ ]]; then
g_arg="$arg"
elif [[ $arg =~ ^-DCMAKE_TOOLCHAIN_FILE ]]; then
vcpkg_arg="$arg"
else
cmake_args+=("$arg")
fi
done
# Check if the g_arg is empty (non-user specified generator)
if [ -z "$g_arg" ]; then
# select ninja if available, make otherwise
if which ninja >/dev/null 2>&1; then
g_arg="-GNinja"
elif which make >/dev/null 2>&1; then
g_arg="-GMake"
else
echo "Please either install Ninja (preffered), Make, or specify an installed Generator?"
echo " For ninja `sudo apt install ninja-build`"
echo " For make `sudo apt install build-essential`"
exit -1
fi
fi
if [ -f "vcpkgLoc.txt" ]; then
vcpkg_arg=$(awk '{$1=$1};1' vcpkgLoc.txt) # this reads the first line and removes leading & trailing whitespace
if [ ! -f "${vcpkg_arg}" ]; then
vcpkg_arg=""
echo "The vcpkg toolchain file was removed! Replace it or delete ${parent_path}/vcpkgLoc.txt and try again"
exit -1
fi
fi
if [ ! -f "Vocabulary/ORBvoc.txt" ]; then
cd Vocabulary
echo "Extracting vocabulary..."
tar -xf ORBvoc.txt.tar.gz
echo "ORB Vocabulary extracted"
cd $parent_path
else
echo "ORB Vocabulary already extracted"
fi
configCompleteFile='config-finished.bool'
# https://unix.stackexchange.com/questions/31414/how-can-i-pass-a-command-line-argument-into-a-shell-script
if [ ! -d "build" ] || [ ! -f "build/${configCompleteFile}" ]; then
echo 'Performing first time configuration...'
echo "Workers: ${j_arg} Generator: ${g_arg}"
if test -n "$vcpkg_arg"; then
echo "With VCPKG"
if [[ "$vcpkg_arg" =~ ^-DCMAKE_TOOLCHAIN_FILE ]]; then
pass=""
else
vcpkg_arg="-DCMAKE_TOOLCHAIN_FILE=$vcpkg_arg"
fi
do_vcpkg_prereqs
fi
echo "User Flags: ${cmake_args[@]}"
mkdir build 2> /dev/null
cd build
# https://unix.stackexchange.com/questions/31414/how-can-i-pass-a-command-line-argument-into-a-shell-script
if test -n "$vcpkg_arg"; then
cmake .. ${g_arg} "${vcpkg_arg}" "${cmake_args[@]}" # pass arguments on to cmake
else
cmake .. ${g_arg} "${cmake_args[@]}" # pass arguments on to cmake
fi
if [ $? -ne 0 ]; then
rm "${configCompleteFile}" 2> /dev/null
cd ..
echo "Configuration was not successful"
exit 1
fi
touch "${configCompleteFile}"
echo -e " Generator: ${g_arg}\nUser Flags: ${cmake_args[@]}" > "${configCompleteFile}"
else
echo 'Already configured'
cd build
config=$(cat "${configCompleteFile}")
echo "Workers: ${j_arg}${config}"
fi
if [ $? -ne 0 ]; then
cd ..
echo "Configuration was not successful"
exit 1
fi
if test -n "$vcpkg_arg"; then
do_vcpkg_prereqs
fi
echo "Building..."
cmake --build . -- ${j_arg}
if [ $? -ne 0 ]; then
cd ..
echo "Build was not successful"
exit 2
fi
sudo cmake --install .
if [ $? -ne 0 ]; then
cd ..
echo "Install was not successful"
exit 3
fi