forked from nvidia-holoscan/holohub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun
executable file
·582 lines (482 loc) · 17.6 KB
/
run
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
#!/bin/bash
# SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
HOLOHUB_PY_EXE=${HOLOHUB_PY_EXE:-"python3"}
#===========================================================================================
# Utilities
# Compare versions
compare_version () {
if [[ $1 == $2 ]]
then
echo "0"
return 0
fi
local IFS=.
local i ver1=($1) ver2=($2)
# fill empty fields in ver1 with zeros
for ((i=${#ver1[@]}; i<${#ver2[@]}; i++))
do
ver1[i]=0
done
for ((i=0; i<${#ver1[@]}; i++))
do
if [[ -z ${ver2[i]} ]]
then
# fill empty fields in ver2 with zeros
ver2[i]=0
fi
if ((10#${ver1[i]} > 10#${ver2[i]}))
then
echo "1"
return 1
fi
if ((10#${ver1[i]} < 10#${ver2[i]}))
then
echo "2"
return 2
fi
done
echo "0"
return 0
}
run_command() {
local status=0
local cmd="$*"
[ "$(echo -n "$@")" = "" ] && return 1 # return 1 if there is no command available
"$@"
status=$?
return $status
}
#===========================================================================================
# Helper function to install packages
install_cuda_dependencies_package() {
package_name=$1
preferred_version=$2
# Checking if compatible packages are already installed
installed_version=$(apt list --installed ${package_name} 2>/dev/null | grep $package_name)
if [[ $installed_version != "" ]]; then
echo "Package $package_name found with version $installed_version"
return 0
fi
available_version=$(apt list -a ${package_name} 2>/dev/null | grep $preferred_version)
package_version=$(echo $available_version | cut -d' ' -f2)
package_installed=$(echo $available_version | grep "installed")
if [[ $package_version == "" ]]; then
echo "$package_name $preferred_version is not installable."
echo "You might want to try to install a newer version manually and rerun the setup:"
echo " sudo apt install $package_name"
exit 1
elif [[ $package_installed == "" ]]; then
echo "Installing $package_name=$package_version"
apt install --no-install-recommends -y $package_name=$package_version
fi
}
# Setup the environment
# Build HoloHub sample apps
setup_desc() {
echo ""
echo "Install the required dependencies for HoloHub (sample applications)"
echo "Usage: ./run setup"
echo ""
}
setup() {
# Run apt-get update
apt-get update
# Install wget
wget_version=$(dpkg --status wget | grep -Po '^Version: \K[^-]*')
if [[ $wget_version == "" ]]; then
apt-get install wget
fi
# Check version of CMake otherwise upgrade
cmake_version=$(dpkg --status cmake | grep -Po '^Version: \K[^-]*')
ubuntu_codename=$(cat /etc/os-release | grep -Po '^UBUNTU_CODENAME=\K[^ ]*')
cmake_need_upload=$(compare_version ${cmake_version} "3.20.1")
# If we should update cmake
# Install from https://apt.kitware.com/
if [[ $cmake_version == "" ]] || [[ $cmake_need_upload == 2 ]]; then
apt install --no-install-recommends -y gpg
wget -O - https://apt.kitware.com/keys/kitware-archive-latest.asc 2>/dev/null | gpg --dearmor - | tee /usr/share/keyrings/kitware-archive-keyring.gpg >/dev/null
echo "deb [signed-by=/usr/share/keyrings/kitware-archive-keyring.gpg] https://apt.kitware.com/ubuntu/ ${ubuntu_codename} main" | tee /etc/apt/sources.list.d/kitware.list >/dev/null
apt-get update
apt install --no-install-recommends -y cmake cmake-curses-gui
fi
# Install python dev
python3_dev_version=$(dpkg --status python3-dev 2>/dev/null | grep -Po '^Version: \K[^-]*')
python3_min_version=$(compare_version ${python3_dev_version} "3.8.0")
# Python 3.8 to 3.11 are supported
if [[ $python3_dev_version == "" ]] || [[ $python3_min_version == "2" ]]; then
echo "Installing python"
apt install --no-install-recommends -y python3 python3-dev
python3_dev_version=$(dpkg --status python3-dev 2>/dev/null | grep -Po '^Version: \K[^-]*')
python3_min_version=$(compare_version ${python3_dev_version} "3.8.0")
fi
# If python version is too high
python3_max_version=$(compare_version ${python3_dev_version} "3.12.0")
if [[ $python3_max_version == "0" ]] || [[ $python3_max_version == "1" ]]; then
echo "ERROR: Python version ${python3_dev_version} is not supported"
exit
fi
# Install ffmpeg
ffmpeg_version=$(dpkg --status ffmpeg 2>/dev/null | grep -Po '^Version: \K[^-]*')
if [[ $ffmpeg_version == "" ]]; then
echo "Installing ffmpeg"
apt install --no-install-recommends -y ffmpeg
fi
# Install ngc-cli
ngc_version=$(ngc --version 2>/dev/null | grep -Po '^NGC CLI \K[^-]*')
if [[ $ngc_version == "" ]]; then
echo "Installing ngc cli"
if [ $(uname -m) == "aarch64" ]; then
wget --content-disposition https://ngc.nvidia.com/downloads/ngccli_arm64.zip && unzip ngccli_arm64.zip && chmod u+x ngc-cli/ngc
fi
if [ $(uname -m) == "x86_64" ]; then
wget --content-disposition https://ngc.nvidia.com/downloads/ngccli_linux.zip && unzip ngccli_linux.zip && chmod u+x ngc-cli/ngc
fi
ln -s $(pwd)/ngc-cli/ngc /usr/local/bin/
fi
# Install the tensorrt development libs
# Find the newest cudart version installed
for version in 12-1 12-0 11-8 11-7 11-6 11-4
do
cuda_version=$(dpkg --status cuda-cudart-${version} 2>/dev/null | grep -Po '^Version: \K[^-]*')
# Find the version based on cudart
if [[ $cuda_version != "" ]]; then
break
fi
done
short_cuda_version=${cuda_version%.*}
echo "Cuda version found: $short_cuda_version"
# Make sure we do not install any cuda12 packages
install_cuda_dependencies_package libcudnn8 cuda$short_cuda_version
install_cuda_dependencies_package libcudnn8-dev cuda$short_cuda_version
install_cuda_dependencies_package libnvinfer-dev cuda$short_cuda_version
install_cuda_dependencies_package libnvinfer-plugin-dev cuda$short_cuda_version
install_cuda_dependencies_package libnvonnxparsers-dev cuda$short_cuda_version
echo "Setup for HoloHub is ready. Happy Holocoding!"
}
#===========================================================================================
# Lint
install_lint_deps_desc() { echo 'Install lint dependencies
This command will install the dependencies required to run the linting tools.
- Python linting: ruff, isort, black, mypy
- C++ linting: cpplint
- Spelling: codespell
'
}
install_lint_deps() {
# We use $(command) || exit_code=1 to run all linting tools, and exit
# with failure after all commands were executed if any of them failed
local exit_code=0
pushd ${SCRIPT_DIR} > /dev/null
echo "Install Lint Dependencies for Python"
if ! command -v ruff &> /dev/null; then
run_command ${HOLOHUB_PY_EXE} -m pip install ruff || exit_code=1
fi
if ! command -v isort &> /dev/null; then
run_command ${HOLOHUB_PY_EXE} -m pip install isort || exit_code=1
fi
if ! command -v black &> /dev/null; then
run_command ${HOLOHUB_PY_EXE} -m pip install black || exit_code=1
fi
if ! command -v mypy &> /dev/null; then
run_command ${HOLOHUB_PY_EXE} -m pip install mypy || exit_code=1
fi
echo "Install Lint Dependencies for C++"
if ! command -v cpplint &> /dev/null; then
run_command ${HOLOHUB_PY_EXE} -m pip install cpplint || exit_code=1
fi
echo "Install Lint Dependencies for Code Spelling"
if ! command -v codespell &> /dev/null; then
run_command ${HOLOHUB_PY_EXE} -m pip install codespell || exit_code=1
fi
popd > /dev/null
exit $exit_code
}
lint_desc() { echo 'Lint the repository
Python linting: black, isort, ruff
C++ linting: cpplint
Spelling: codespell
Arguments:
$@ - directories to lint (default: .)
'
}
lint() {
local DIR_TO_RUN=${@:-"."}
# We use $(command) || exit_code=1 to run all linting tools, and exit
# with failure after all commands were executed if any of them failed
local exit_code=0
pushd ${SCRIPT_DIR} > /dev/null
echo "Linting Python"
run_command ruff $DIR_TO_RUN || exit_code=1
run_command ${HOLOHUB_PY_EXE} -m isort -c $DIR_TO_RUN || exit_code=1
run_command ${HOLOHUB_PY_EXE} -m black --check $DIR_TO_RUN || exit_code=1
echo "Linting C++"
# We use `grep -v` to hide verbose output that drowns actual errors
# Since we care about the success/failure of cpplitn and not of grep, we:
# 1. use `set -o pipefail` to fail if `cpplint` fails
# 2. use `grep -v ... || true` to ignore whether grep hid any output
run_command set -o pipefail; ${HOLOHUB_PY_EXE} -m cpplint \
--exclude build \
--exclude install \
--exclude build-\* \
--exclude install-\* \
--recursive $DIR_TO_RUN \
| { grep -v "Ignoring\|Done processing" || true; } || exit_code=1
echo "Code spelling"
run_command codespell $DIR_TO_RUN --skip="*.onnx" || exit_code=1
popd > /dev/null
exit $exit_code
}
#===========================================================================================
# Build HoloHub sample apps
build_desc() {
echo ""
echo "Build the application(s) in HoloHub"
echo "Usage: ./run build <application_name | SAMPLE_APPS : default SAMPLE_APPS> [options]"
echo "Options:"
echo " --sdk <path_to_holoscan_SDK> : Provide path to the SDK"
echo " --configure-args <extra_args> : Additional configuration arguments"
echo ""
echo "Prior to the first build use './run setup' to install the required dependencies"
echo ""
}
build() {
# Holoscan SDK location
holoscan_sdk=""
# CMake configuration args
configure_args=""
# Parse the arguments
ARGS=("$@")
local app
local i
local arg
local skipnext=0
for i in "${!ARGS[@]}"; do
arg="${ARGS[i]}"
if [[ $skipnext == "1" ]]; then
skipnext=0
elif [ "$arg" = "--sdk" ]; then
holoscan_sdk="-Dholoscan_ROOT=${ARGS[i+1]}"
echo "Using Holscan SDK in ${ARGS[i+1]}"
skipnext=1
elif [ "$arg" = "--configure-args" ]; then
configure_args="${ARGS[i+1]}"
echo "Adding configuration arguments: ${ARGS[i+1]}"
skipnext=1
elif [[ $arg = -* ]]; then
echo "Unknown option $arg"
exit 1
else
app=$arg
fi
done
echo "Building Holohub"
# Application(s) to build
application="-DBUILD_SAMPLE_APPS=1"
if [[ $app == "SAMPLE_APPS" ]] || [[ $app == "" ]]; then
echo "Building sample applications."
else
echo "building $1 application"
application="-DAPP_$1=1"
fi
# We define the python path to make sure we grab the right one
cmake_extra_args="-DPython3_EXECUTABLE=${HOLOHUB_PY_EXE}"
# We set the data directory to be outside the build directory
cmake_extra_args="$cmake_extra_args $configure_args -DHOLOHUB_DATA_DIR=${SCRIPT_DIR}/data"
# Sets the default path for cuda
export PATH=$PATH:/usr/local/cuda/bin
cmake -S . -B build ${cmake_extra_args} ${holoscan_sdk} ${application}
cmake --build build -j
echo "Holohub build done."
}
# Build HoloHub sample apps
_list_desc() {
echo ""
echo "Display the list of applications to run."
echo "Usage: ./run list"
echo ""
}
list() {
echo ""
echo "***** HoloHub applications ******"
apps=$(find ${SCRIPT_DIR}/applications -name 'metadata.json')
for d in ${apps}; do
local appname=$(dirname $d | grep -Po '^'${SCRIPT_DIR}/applications/'\K[^ ]*')
local language="${appname##*/}"
if [[ ${language} != "cpp" ]] && [[ ${language} != "python" ]]; then
language=""
else
language="(${language})"
fi
filename="${appname%/*}"
# Check in the CMakelists if the application is a sample application
appcmakelist="applications/CMakeLists.txt"
grepargs="\(${filename} \K[^)]+"
result_grep=$(grep -ozP "${grepargs}" $appcmakelist | tr -d '\0')
result=$(echo $result_grep | grep "HOLOSCAN_SAMPLE_APP")
sample_app=""
if [[ ${result} != "" ]]; then
sample_app="[Sample Application]"
fi
echo "- ${filename} ${language} ${sample_app}"
done
echo ""
echo "Run ./run build <app_name> to build a specific app."
echo "or ./run build SAMPLE_APPS to build the sample applications (noted [Sample Application]), other applications should be built individually."
}
# Launch a sample app
launch_desc() {
echo ""
echo "Launch command allowing to run application directly."
echo "Usage: ./run launch <name_of_the_application> [language: cpp|python]"
echo ""
echo "Use './run list' first to list the available applications"
echo "Note that applications might be listed but not built."
echo "Make sure you ./run build the application first"
echo ""
}
launch() {
local appname=$1
# Export the python path (by default use the one used to build)
local holohub_buildir="${SCRIPT_DIR}/build"
local holoscan_sdk_install=$(grep -Po '^holoscan_DIR:PATH=\K[^ ]*' build/CMakeCache.txt)
local holohub_data_dir=$(grep -Po '^HOLOHUB_DATA_DIR:PATH=\K[^ ]*' build/CMakeCache.txt)
local holohub_app_bin="${holohub_buildir}/applications/${appname}"
local holohub_app_source="${SCRIPT_DIR}/applications/${appname}"
local metadata_file="${holohub_app_source}/metadata.json"
if [[ $2 ]]; then
metadata_file="${holohub_app_source}/$2/metadata.json"
holohub_app_bin="${holohub_buildir}/applications/${appname}/$2"
holohub_app_source="${SCRIPT_DIR}/applications/${appname}/$2"
fi
# Check if the metadata file exists
if [ ! -f "$metadata_file" ]; then
echo "The metadata file for this application does not exist."
echo "Did you forget to specify the language?"
exit 1
fi
# Check if the build directory exists (for CPP apps)
if [ ! -d "$holohub_app_bin" ] && [ "$2" == "cpp" ]; then
echo "The build directory for this application does not exist."
echo "Did you forget to './run build $appname' ?"
exit 1
fi
# Use Python to parse json file
json=$(${HOLOHUB_PY_EXE} -c 'import json,sys
f=open("'${metadata_file}'")
obj=json.load(f)
for k, v in obj["application"]["run"].items():
print(str(k)+"=\""+str(v)+"\"")
')
local json_command=$(echo $json | grep -Po 'command="\K[^"]*')
local command=$json_command
# replace <holohub_data_dir> by the data dir
local command=$(echo "${json_command//<holohub_data_dir>/$holohub_data_dir}")
# replace <holohub_app_bin> by the binary app directory
command=$(echo "${command//<holohub_app_bin>/$holohub_app_bin}")
# replace <holohub_app_source> by the source app directory
command=$(echo "${command//<holohub_app_source>/$holohub_app_source}")
# default workdir is bin
local workdir="cd ${holohub_app_bin}"
local json_workdir=$(echo $json | grep -Po 'workdir="\K[^"]*')
if [[ $json_workdir == "holohub_app_source" ]]; then
workdir="cd ${holohub_app_source}"
elif [[ $json_workdir == "holohub_bin" ]]; then
workdir="cd ${holohub_buildir}"
fi
local environment="export PYTHONPATH=${holoscan_sdk_install}/../../../python/lib:${holohub_buildir}/python/lib"
echo "workdir=${workdir}"
echo "command=${command}"
# Run the command
$environment && $workdir && $command
}
parse_args() {
local OPTIND
while getopts 'yh' option;
do
case "${option}" in
y)
ALWAYS_YES="true"
;;
h)
print_usage
exit 1
;;
*)
;;
esac
done
shift $((OPTIND-1))
CMD="$1"
shift
ARGS=("$@")
# Check if the command has `--help`, `-h`, and override the CMD
local i
local arg
for i in "${!ARGS[@]}"; do
arg="${ARGS[i]}"
if [ "$arg" = "--help" ] || [ "$arg" = "-h" ]; then
ARGS=("$CMD")
CMD="help"
break
fi
done
}
print_usage() {
set +x
echo
echo "USAGE: $0 [command] [arguments]..."
echo ""
echo " --help, -h : Print help messages for [command]"
echo " setup : Install HoloHub main required packages"
echo " list : List all the applications"
echo " build [application] [--sdk holoscan_sdk] : Build a specific application. Default is sample apps"
echo " launch <application> <language> : Run the application"
}
print_cmd_help_messages() {
local cmd="$1"
if [ -n "${cmd}" ]; then
if type ${cmd}_desc > /dev/null 2>&1; then
${cmd}_desc
exit 0
else
echo "Command '${cmd}' doesn't exist!"
exit 1
fi
fi
print_usage
return 0
}
main() {
local ret=0
parse_args "$@"
case "$CMD" in
help|"")
print_cmd_help_messages "${ARGS[@]}"
exit 0
;;
*)
if type ${CMD} > /dev/null 2>&1; then
"$CMD" "${ARGS[@]}"
else
print_usage
exit 1
fi
;;
esac
ret=$?
}
main "$@"