-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathm
executable file
·386 lines (327 loc) · 8.97 KB
/
m
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
#!/bin/bash
# -------------------------------------------------------- #
function jump_and_print_path() {
cd -P "$(dirname "$1")" >/dev/null 2>&1 && pwd
}
function get_script_path() {
local _SOURCE
local _PATH
_SOURCE="${BASH_SOURCE[0]}"
# Resolve $_SOURCE until the file is no longer a symlink
while [ -h "$_SOURCE" ]; do
_PATH="$(jump_and_print_path "${_SOURCE}")"
_SOURCE="$(readlink "${_SOURCE}")"
# If $_SOURCE is a relative symlink, we need to
# resolve it relative to the path where the symlink
# file was located
[[ $_SOURCE != /* ]] && _SOURCE="${_PATH}/${_SOURCE}"
done
_PATH="$(jump_and_print_path "$_SOURCE")"
echo "${_PATH}"
}
# Argument: relative path of project directory wrt this
# script directory
function get_project_path() {
local _PATH
local _PROJPATH
_PATH=$(get_script_path)
_PROJPATH=$(realpath "${_PATH}/$1")
echo "${_PROJPATH}"
}
# ======================================================== #
# ---------------------- ARGUMENTS ----------------------- #
# ======================================================== #
function toshortopts() {
local out=
newargs=()
while [ $# -gt 0 ]; do
case "$1" in
--help) printf -v out '%s' '-h' ;;
--verbose) printf -v out '%s' '-v' ;;
--deb) printf -v out '%s' '-d' ;;
--rpm) printf -v out '%s' '-r' ;;
--jobs) printf -v out '%s' '-j' ;;
--parallel) printf -v out '%s' '-J' ;;
--generator) printf -v out '%s' '-G' ;;
--build-type) printf -v out '%s' '-b' ;;
--build-path) printf -v out '%s' '-p' ;;
*) printf -v out '%s' "$1" ;;
esac
shift
newargs+=( "$out" )
done
}
function separate_args() {
local optstring="$1"
local OPTION
shift
opt_args=()
pos_args=()
while [ $# -gt 0 ]; do
unset OPTIND
unset OPTARG
unset OPTION
while getopts ":$optstring" OPTION; do
if [ "$OPTION" != : ]; then
opt_args+=("-$OPTION")
else
OPTARG="-$OPTARG"
fi
if [ ! -z "$OPTARG" ]; then
opt_args+=("$OPTARG")
fi
unset OPTARG
done
shift $((OPTIND - 1)) || true
pos_args+=("$1")
shift || true
done
}
function missing_argument() {
printf 'Error: option %s requires an argument!\n\n' "$@" >&2
usage
}
function unsupported_argument() {
printf 'Error: option %s does not support argument %s!\n\n' "$@" >&2
usage
}
function unrecognized() {
printf 'Error: unrecognized %s %s!\n\n' "$1" "${*:2}" >&2
}
function parse_opt_args() {
local optstring="$1"
local OPTION
shift
unset OPTIND
unset OPTARG
unset OPTION
while getopts "$optstring" OPTION; do
case $OPTION in
h)
pos_args=()
return 0
;;
v)
verbose=ON
;;
d)
package_deb=ON
;;
r)
package_rpm=ON
;;
j)
if [ -z "$OPTARG" ]; then
missing_argument '-j|--jobs'
return 1
fi
;;
J)
jobs=""
;;
b)
if [ -z "$OPTARG" ]; then
missing_argument '-b|--build-type'
return 1
fi
case "$OPTARG" in
debug) build_type='Debug' ;;
release) build_type='Release' ;;
release-wdebug) build_type='RelWithDebInfo' ;;
*)
unsupported_argument '-b|--build-type' "$OPTARG"
return 1
;;
esac
;;
p)
if [ -z "$OPTARG" ]; then
missing_argument '-p|--build-path'
return 1
fi
path_build="$OPTARG"
;;
G)
if [ -z "$OPTARG" ]; then
missing_argument '-G|--generator'
return 1
fi
generator="$OPTARG"
;;
*)
shift $((OPTIND - 1)) || true
unrecognized 'option' "$1"
return 1
;;
esac
unset OPTARG
done
# Too many options/unrecognized options
shift $((OPTIND - 1)) || true
if [ "$#" -gt 0 ]; then
unrecognized 'options' "$@"
return 1
fi
}
function parse_pos_args() {
commands=("$@")
if [ $# -lt 1 ]; then
commands=(usage)
fi
}
# ======================================================== #
# ----------------------- COMMANDS ----------------------- #
# ======================================================== #
function usage() {
cat <<EOF
usage: $0 [options] COMMAND [...COMMANDS]
Runs the specified list of commands using the given arguments
List of options (all optional):
-h, --help Prints this help message and returns
-v, --verbose Prints more info during execution
-d, --deb Enables the generation of the deb package
-r, --rpm Enables the generation of the rpm package
-G, --generator GEN
Uses the provided CMake generator to build the project
-J, --parallel Enables parallel build execution with a default number of
processes
-j, --jobs JOBS
Enables parallel build execution with JOBS processes
-b, --build-type TARGET[=release*|debug|release-wdebug]
Specifies which version of the project to build
-p, --build-path BUILDPATH[=build]
Specifies which path to use to build the project
List of commands:
build (Re-)Build the project
clean Clean the project build directory
configure (Re-)Configures the project
install (Re-)Install the project
package Generates the desired packages
uninstall Removes the installed files from paths
usage Prints this help message and returns
EOF
# TODO(gabrieleara): test
}
function reset_ran() {
ran_build=0
ran_clean=0
ran_configure=0
ran_install=0
ran_package=0
ran_uninstall=0
ran_usage=0
}
function build() {
if [ "$ran_build" = 1 ]; then
return 0
fi
configure
cmake --build "$path_build" --parallel $jobs
ran_build=1
}
function clean() {
rm -rf "$path_build"
reset_ran
}
function configure() {
if [ "$ran_configure" = 1 ]; then
return 0
fi
if [ "$generator" = 'Ninja' ] && ! command -v ninja &> /dev/null
then
# Fallback to Unix Makefiles on Linux
generator='Unix Makefiles'
fi
cmake -S "$path_src" -B "$path_build" \
-G "$generator" \
-DCMAKE_BUILD_TYPE="$build_type" \
-DCMAKE_VERBOSE_MAKEFILE:BOOL="$verbose" \
-DCPACK_ENABLE_DEB="$package_deb" \
-DCPACK_ENABLE_RPM="$package_rpm"
ran_configure=1
}
function install() {
if [ "$ran_install" = 1 ]; then
return 0
fi
build
sudo cmake --build "$path_build" --target install
ran_install=1
}
function uninstall() {
if [ "$ran_uninstall" = 1 ]; then
return 0
fi
# This is a bit counter-intuitive, to uninstall we need to install first!
# What we want actually is the install manifest to be present in the build
# path!
local install_manifest="${path_build}/install_manifest.txt"
if [ ! -f "$install_manifest" ]; then
install
fi
echo "Removing the following files:"
cat "$install_manifest"
echo ""
cat "$install_manifest" | sudo xargs rm -f
ran_uninstall=1
}
function package() {
if [ "$ran_package" = 1 ]; then
return 0
fi
configure
build
(
set -e
cd "$path_build"
sudo cpack
sudo chown -R $USER:$USER .
)
if [ "$package_deb" = 'ON' ]; then
# TODO: sign the deb package
:
fi
ran_package=1
}
(
set -e
# Variables and default values
path_proj="$(get_project_path ".")"
path_src="$path_proj"
path_build="$path_proj/build"
build_type="Release"
package_deb=OFF
package_rpm=OFF
verbose=OFF
jobs=1
generator=Ninja
commands=()
opt_args=()
pos_args=()
optstring='hvJj:drb:p:G:'
OPTERR=0
# Separate optional from positional arguments, then parse them
toshortopts "$@"
separate_args "$optstring" "${newargs[@]}"
parse_opt_args "$optstring" "${opt_args[@]}"
parse_pos_args "${pos_args[@]}"
reset_ran
for c in "${commands[@]}"; do
# Trim the variable
c="$(echo $c | xargs)"
case "$c" in
build) build ;;
clean) clean ;;
configure) configure ;;
install) install ;;
package) package ;;
uninstall) uninstall ;;
usage) usage ;;
*)
unrecognized 'command' "$c"
usage
false
;;
esac
done
)