-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sh
executable file
·327 lines (281 loc) · 8.78 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
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
#!/usr/bin/env bash
# show help message
show_usage () {
cat << EOF
usage: $(basename "$0") [ -h | --long-options]
-h, --help shows this message
--debug enable debug logs
--with-tests include unit-tests in build
--docs build library documentation
--lint lint source code
--clear remove build artifacts
--test test watchdog library
EOF
}
# configure bash environment
set -o errexit -o pipefail -o noclobber -o nounset
# declare project structure
ARG_VERBOSE=0
WATCHDOG_ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# initialize logging
__log () {
if [ $# -lt 3 ]; then return 1; fi
printf "\e[1;$2m%-10s\e[m %s\\n" "$1" "${@:3}"
}
log_debug () { [[ "${ARG_VERBOSE}" -ne 1 ]] || __log 'debug' '0' "$@"; }
log_info () { __log 'info' '34' "$@"; }
log_warning () { __log 'warn' '33' "$@"; }
log_error () { __log 'error' '31' "$@"; return 1; }
# this script expects bash v4.4 or higher
if [ "${BASH_VERSINFO[0]}" -lt 4 ] || ( [ "${BASH_VERSINFO[0]}" -eq 4 ] && \
[ "${BASH_VERSINFO[1]}" -lt 4 ] ); then
log_warning "you are using bash version ${BASH_VERSION}"
log_error "this script requires bash version 4.4 or higher"
fi
# helper funcitons
has_function () {
[ "$(type -t "$1")" == "function" ]
}
count_keys_if_set () {
if [ $# -ne 1 ]; then return 1; fi
local sum=0
declare -A arr=${1#*=}
for k in "${!arr[@]}"; do
if [ "${arr[$k]}" -eq 1 ]; then
sum=$((sum + 1))
fi
done
echo $sum
}
is_command_installed () {
if [ $# -eq 0 ]; then return 1; fi
for cmd in "$@"; do
if ! hash "$cmd" 2>/dev/null; then
log_warning "command $cmd is not installed"
return 1
fi
done
return 0
}
check_prerequisite_commands () {
# shellcheck disable=SC2190
arr=("$@")
for i in "${arr[@]}"; do
if ! is_command_installed "$i"; then
log_error "cannot build component with missing prerequisites"
fi
done
log_debug "all prerequisite packages are installed"
}
remove_dir_if_exists () {
if [ $# -ne 1 ] && [ $# -ne 2 ]; then return 1; fi
if [ -d "$1" ]; then
if [ $# -eq 2 ]; then log_info "removing $2"; fi
log_debug "removing directory $1"
rm -rf "$1"
log_info "removed directory $1"
fi
}
remove_file_if_exists () {
if [ $# -ne 1 ] && [ $# -ne 2 ]; then return 1; fi
if [ -f "$1" ]; then
if [ $# -eq 2 ]; then log_info "removing $2"; fi
log_debug "removing $1"
rm "$1"
log_info "removed $1"
fi
}
cmake_option () {
if [ $# -ne 1 ]; then return 1; fi
[ "${BUILD_OPTIONS["$1"]}" -eq 1 ] && echo "ON" || echo "OFF"
}
# more specific helper functions
validate_arguments () {
if [ $# -ne 2 ]; then return 1; fi
declare -A modes=${1#*=}
local count_modes
count_modes="$(count_keys_if_set "$(declare -p modes)")"
if [ "$count_modes" -eq 0 ]; then
log_error "build mode is not specified"
elif [ "$count_modes" -gt 1 ]; then
log_error "only one build mode can be specified"
fi
declare -A options=${2#*=}
local count_options
count_options="$(count_keys_if_set "$(declare -p options)")"
if [ "${modes["build"]}" -eq 0 ] && [ "$count_options" -ne 0 ]; then
log_error "build mode does not support specified options"
fi
}
build_components () {
if [ $# -ne 2 ]; then return 1; fi
declare -A modes=${1#*=}
for K in "${!modes[@]}"; do
if [ "${modes[$K]}" -eq 0 ]; then continue; fi
local func_name="build_${K//'-'/'_'}"
if ! has_function "${func_name}"; then
log_error "build mode not supported"
fi
($func_name "$2")
done
}
# build recipes
build_build () {
if [ $# -ne 1 ]; then return 1; fi
declare -A options=${1#*=}
if [ "${#options[@]}" -ne 0 ]; then
log_debug "building with the following options:"
for K in "${!options[@]}"; do
if [ "${options[$K]}" -eq 1 ]; then
log_debug " - $K"
fi
done
fi
check_prerequisite_commands "cmake" "conan"
local dir_source="${WATCHDOG_ROOT_DIR}"
local dir_build="${dir_source}/local/build"
local dir_install="${dir_source}/local/dist"
mkdir -p "${dir_build}"
mkdir -p "${dir_install}"
if case $OSTYPE in linux* | darwin*) false;; *) true;; esac; then
log_error "recipe not implemented for windows."
fi
local buildtype="Release"
# buildtype=$([ "${options["with-coverage"]}" -eq 1 ] \
# && echo "Debug" || echo "Release")
local cmake_generator="Unix Makefiles"
if is_command_installed "ninja"; then
cmake_generator="Ninja"
fi
local cmake_config_ccache_args=()
if is_command_installed "ccache"; then
log_debug "using ccache"
cmake_config_ccache_args+=(
-DCMAKE_C_COMPILER_LAUNCHER=ccache
-DCMAKE_CXX_COMPILER_LAUNCHER=ccache
)
fi
local cmake_config_general_args=(
-B"${dir_build}"
-H"${dir_source}"
-G"${cmake_generator}"
-DCMAKE_BUILD_TYPE="${buildtype}"
"${cmake_config_ccache_args[@]}"
)
local cmake_config_watchdog_args=(
-DWATCHDOG_BUILD_TESTS="$(cmake_option "with-tests")"
)
log_info "fetching dependencies"
# we specify option `with_tests` to force conan to pull dependencies for
# all targets.
if [ ! -f "${dir_build}/conaninfo.txt" ]; then
conan install -o with_tests=True --install-folder "${dir_build}" \
"${dir_source}/conanfile.py" --build=missing
fi
log_info "building with local toolchain"
cmake "${cmake_config_general_args[@]}" "${cmake_config_watchdog_args[@]}"
cmake --build "${dir_build}" --parallel
cmake --install "${dir_build}" --prefix "${dir_install}"
log_info "build process complete"
if [[ ${buildtype} == "Release" ]]; then
local dir_export_pkg="${dir_build}/conan-export-pkg"
mkdir -p "${dir_export_pkg}"
conan export-pkg -if "${dir_build}" \
-bf "${dir_export_pkg}" \
-f "${dir_source}"
log_info "created conan package"
fi
}
build_lint () {
if [ $# -ne 1 ]; then return 1; fi
check_prerequisite_commands "clang-format"
local dir_source="${WATCHDOG_ROOT_DIR}"
find "${dir_source}" \( -name "*.cpp" -o -name "*.hpp" -o -name "*.h" \) \
-exec clang-format -i {} +
log_info "ran clang-format on source code"
}
build_clear () {
if [ $# -ne 1 ]; then return 1; fi
local dir_source="${WATCHDOG_ROOT_DIR}"
remove_dir_if_exists "${dir_source}/local/build"
remove_dir_if_exists "${dir_source}/local/dist"
remove_dir_if_exists "${dir_source}/local/docs"
remove_dir_if_exists "${dir_source}/local/tests"
}
build_docs () {
if [ $# -ne 1 ]; then return 1; fi
local dir_source="${WATCHDOG_ROOT_DIR}"
local dir_dst="${dir_source}/local/docs/html"
remove_dir_if_exists "$dir_dst"
log_info "building documentation using local toolchain"
check_prerequisite_commands "doxygen"
mkdir -p "$(dirname "${dir_dst}")"
doxygen "${WATCHDOG_ROOT_DIR}/docs/Doxyfile"
if [ -d "$dir_dst" ]; then
log_info "built documentation using local toolchain"
return 0
fi
log_error "failed to build documentation"
}
build_test () {
if [ $# -ne 1 ]; then return 1; fi
local dir_source="${WATCHDOG_ROOT_DIR}"
cd "${dir_source}/local/build" && ctest "${dir_source}" -C Release && cd "$(pwd)"
log_info "ran unit-tests"
}
# check command line arguments
ARG_HELP=0
declare -A BUILD_MODES=(
["build"]=1
["docs"]=0
["clear"]=0
["lint"]=0
["test"]=0
)
declare -A BUILD_OPTIONS=(
["with-tests"]=0
)
for arg in "$@"; do
case $arg in
"")
;;
"-h" | "help" | "--help")
ARG_HELP=1
;;
"--debug")
ARG_VERBOSE=1
;;
"--build")
;;
"--docs")
BUILD_MODES["build"]=0
BUILD_MODES["docs"]=1
;;
"--clear")
BUILD_MODES["build"]=0
BUILD_MODES["clear"]=1
;;
"--lint")
BUILD_MODES["build"]=0
BUILD_MODES["lint"]=1
;;
"--test")
BUILD_MODES["build"]=0
BUILD_MODES["test"]=1
;;
"--with-tests")
BUILD_OPTIONS["with-tests"]=1
;;
*)
log_warning "invalid argument $arg"
show_usage
exit
;;
esac
done
if [[ ${ARG_HELP} -eq 1 ]]; then
show_usage
exit
fi
validate_arguments "$(declare -p BUILD_MODES)" "$(declare -p BUILD_OPTIONS)"
build_components "$(declare -p BUILD_MODES)" "$(declare -p BUILD_OPTIONS)"