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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/bin/sh -eu
#
# script to build the AR SDK sample apps
#
# - ask the location for the sample apps
# - install any pre-requisites via package manager
# - build any pre-requisites not available via package manager (if any)
# - build the sample apps
#
# usage:
#
#  build_samples.sh [ -f | --force ] [-c|--cleanup] [-i <i>|--installdir <i>] [-y|--yes]
#
# where:
#  -f | --force : force rebuild or reinstall of pre-requisites
#  -c | --cleanup : clean up build intermediates
#  -i <i> | --installdir <i> : installation directory (default ~/mysamples)
#  -y | --yes : no prompting - use default values
#

# there are exact requirements
_CUDAVER="11.8"
_TRTVER="8.6.1.6"
_CUDNNVER="8.9.1"


usage() { echo "usage: $0 [-i <dir> | --installdir <dir>] [-y|--yes] [-f|--force] [-p|--prereq]"; exit 0;}
die() { echo "$0: ERROR: $@" 1>&2; exit 1; }
progress() { echo "========== $@ =========="; }

check_exist() {
    msg="$1"
    shift
    for i; do if [ -e "$i" ]; then return; fi; done
    die "Could not find $msg: $@"
}

# usually /usr/local/ARSDK/share
SAMPDIR="$(cd ${AR_SDK_SAMPLES_DIR:-`dirname "$0"`}; pwd)"
if [ ! -e "$SAMPDIR/build_samples.sh" ]
then
    die "cannot find samples directory"
fi

if [ -f /etc/os-release ]
then
    . /etc/os-release
    OS="$ID$VERSION_ID"
    case "$OS" in
        centos7 | ubuntu18.04 | ubuntu20.04 | ubuntu22.04 | rocky8.7 | rocky8.8 | debian11 );;
        *) die "unsupported linux version: $OS";;
    esac
else
    die "cannot determine os version"
fi

# process args
TEMP=$(getopt -o 'xfdci:ypb?' --long 'force,debug,cleanup,installdir:,yes,build,prereq,help' -- "$@")
if [ $? -ne 0 ]; then usage; fi
eval set -- "$TEMP"
unset TEMP

# for

_YES="false"
_FORCE="false"
_BUILD_TYPE="Release"
_INSTALLDIR=""
_CLEANUP="false"
_BUILD="true"
_PREREQ="true"
_PREGEN="true"
while true; do
    case "$1" in
        '-x') set -x;;                                      # script debug output
        '-f' | '--force') _FORCE="true";;                   # force build
        '-d' | '--debug') _BUILD_TYPE="Debug";;             # debug build
        '-c' | '--cleanup') _CLEANUP="true";;               # clean build
        '-i' | '--installdir') _INSTALLDIR="$2";shift;;
        '-y' | '--yes') _YES="true";;                       # answer yes to questions
        '-p' | '--prereq') _BUILD="false"; _PREGEN="true";; # pregenerate prerequisites
        '-b' | '--build') _PREREQ="false";;                 # build without checking prerequisites
        '--') shift;break;;
        *) usage;;
    esac
    shift
done


if ! $_YES
then
    echo ""
    echo "===================================================================="
    echo "This script will:"
    if [ "$OS" = "centos7" ]
    then
        echo "- INSTALL pre-requisite packages using yum"
        echo "- BUILD pre-requisite packages (opencv + ffmpeg)"
    else
        echo "- INSTALL pre-requisite packages using apt-get"
    fi
    echo "- build the sample apps to a location of your choice"
    echo ""
    echo "This may install many pre-requisite packages"
    if [ "$OS" = "centos7" ]
    then
        echo "and the the build may take some time"
    fi

    echo ""
    read -p "Ok to continue? (yes/no) " ans
    case "$ans" in
        [yY] | [Yy][Ee][Ss]);;
        *) echo "aborted."; exit 1;;
    esac
fi

if [ -z "$_INSTALLDIR" ] && ! $_YES; then
    echo "Please provide an absolute path for installation directory below, or use the default. "
    read -p "Install to [~/mysamples]: " _INSTALLDIR
fi

_INSTALLDIR="${_INSTALLDIR:-$HOME/mysamples}"
mkdir -p "$_INSTALLDIR/source"

progress "copy samples to $_INSTALLDIR"

echo "cp -r $SAMPDIR/* $_INSTALLDIR/source/"
cp -r $SAMPDIR/* "$_INSTALLDIR/source/"

#copy preinstalled packages if any
if [ -d "$_INSTALLDIR/pregen" ] && [ ! -d "$_INSTALLDIR/extras" ]
then
    progress "copy preinstalled packages to $_INSTALLDIR"
    cp -r "$_INSTALLDIR/pregen/" "$_INSTALLDIR/extras/"
fi

# containers don't require / provide sudo
if [ -x /usr/bin/sudo ]
then
    SUDO="sudo"
else
    SUDO=""
fi

if $_PREREQ
then

    progress "check prerequisities"

    case "$OS" in

        centos7)

            # also epel-release
            PRE_REQ_LIST="
            cmake3
            gcc
            gcc-c++
            git
            gtk2-devel
            jasper-devel
            libdc1394-devel
            libjpeg-turbo-devel
            libpng-devel
            libtiff-devel
            libv4l-devel
            libwebp-devel
            v4l-utils
            yasm

            autoconf
            automake
            libtool

            make
            wget

            mesa-libGLES-devel
            glfw-devel
            "

            if $_FORCE || ! rpm -q epel-release $PRE_REQ_LIST
            then
                echo "installing required packages:"
                set -x
                $SUDO yum -y install epel-release
                $SUDO yum -y install $PRE_REQ_LIST
                set +x
            else
                echo "Required packages are already installed"
            fi

            CMAKE=cmake3

            ;;

        rocky8.7 | rocky8.8 )
            PRE_REQ_LIST="
                git
                cmake
                gcc
                gcc-c++
                make
                python2
                python3
                python3-numpy
                platform-python-devel
                gtk2-devel
                findutils
                diffutils
                libdc1394
                libjpeg-turbo
                libjpeg-turbo-devel

                libv4l-devel

                autoconf
                automake
                libtool
                mesa-libGL-devel
                glfw-devel
            "

            if $_FORCE || ! rpm -q epel-release $PRE_REQ_LIST
            then
                echo "installing required packages:"
                set -xe
                # dnf will fail with missing package
                $SUDO yum -y install epel-release
                $SUDO dnf config-manager --set-enabled powertools
                $SUDO dnf -y install $PRE_REQ_LIST
                set +x
            else
                echo "Required packages are already installed"
            fi

            CMAKE=cmake

            ;;

        ubuntu18.04 | ubuntu20.04 | ubuntu22.04 | debian10 | debian11 )

            PRE_REQ_LIST="
            cmake
            g++
            make
            ffmpeg
            libopencv-dev
            libgles2-mesa-dev
            libglfw3-dev
            "

            if $_FORCE || ! dpkg --verify $PRE_REQ_LIST >/dev/null
            then
                echo "installing required packages:"
                set -x
                $SUDO apt-get update
                if [ -z "$SUDO" ]
                then
                    export DEBIAN_FRONTEND=noninteractive
                    apt-get install -y $PRE_REQ_LIST
                else
                    $SUDO DEBIAN_FRONTEND=noninteractive apt-get install -y $PRE_REQ_LIST
                fi
                set +x
            else
                echo "Required packages are already installed"
            fi

            CMAKE=cmake

            ;;

        *) die "unexpected OS":;

    esac


    cd "$_INSTALLDIR"


    if [ "$OS" = "centos7" ] || [ "$OS" = "rocky8.7" ] || [ "$OS" = "rocky8.8" ]
    then
        progress "build any additional prerequisites"

        set -e

        if [ -e $_INSTALLDIR/extras/bin/nasm ]  && ! $_FORCE
        then

            echo "nasm: already exists"

        else

            mkdir -p $_INSTALLDIR/build/nasm
            cd $_INSTALLDIR/build/nasm

            git clone https://github.com/netwide-assembler/nasm
            cd nasm
            git -c advice.detachedHead=false checkout tags/nasm-2.15.05

            sh autogen.sh
            sh configure --prefix="$_INSTALLDIR/extras"
            touch nasm.1 ndisasm.1 # cheat

            make -j$(nproc)
            make install

            cd $_INSTALLDIR/build
            $_CLEANUP && rm -rf $_INSTALLDIR/build/nasm
        fi

        export PATH="$PATH:$_INSTALLDIR/extras/bin"

        # -------------- x264 --------------

        if [ -e $_INSTALLDIR/extras/lib/libx264.a ] && ! $_FORCE
        then
            echo "libx264: already exists"
        else
            mkdir -p $_INSTALLDIR/build/x264
            cd $_INSTALLDIR/build/x264

            git clone https://git.videolan.org/git/x264.git
            cd x264
            #git checkout <ver>

            ./configure \
                --enable-static \
                --enable-strip \
                --enable-pic \
                --disable-cli \
                --disable-opencl \
                \
                --disable-avs     \
                --disable-swscale \
                --disable-lavf    \
                --disable-ffms    \
                --disable-gpac    \
                --disable-lsmash \
                \
                --prefix="$_INSTALLDIR/extras"

            make -j$(nproc)
            make install

            cd $_INSTALLDIR/build
            $_CLEANUP && rm -rf $_INSTALLDIR/build/x264
        fi

        # -------------- ffmpeg --------------
        if [ -e $_INSTALLDIR/extras/bin/ffmpeg ] && ! $_FORCE
        then
            echo "ffmpeg: already exists"
        else
            mkdir -p $_INSTALLDIR/build/ffmpeg
            cd $_INSTALLDIR/build/ffmpeg

            export PKG_CONFIG_PATH="$_INSTALLDIR/extras/lib/pkgconfig:${PKG_CONFIG_PATH:-}"

            echo "git clone FFmpeg"
            git -c advice.detachedHead=false clone -b n4.3.2 --depth=1 https://github.com/FFmpeg/FFmpeg

            cd FFmpeg
            git -c advice.detachedHead=false checkout tags/n4.3.2

            ./configure \
                --enable-pic \
                --enable-shared \
                --enable-ffmpeg \
                --enable-ffplay \
                --enable-ffprobe \
                --enable-gpl \
                --enable-libx264 \
                --prefix="$_INSTALLDIR/extras" \
                --disable-doc

            make -j$(nproc)
            make install

            cd $_INSTALLDIR/build
            $_CLEANUP && rm -rf $_INSTALLDIR/build/ffmpeg

            #echo "/usr/local/lib" > /etc/ld.so.conf.d/ffmpeg.conf
            #ldconfig
        fi

        # -------------- opencv --------------

        export LD_LIBRARY_PATH="$_INSTALLDIR/extras/lib:/usr/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}"

        if [ -e $_INSTALLDIR/extras/lib64/libopencv_core.so ]  && ! $_FORCE
        then
            echo "opencv: already exists"
        else

            export PKG_CONFIG_PATH="$_INSTALLDIR/extras/lib/pkgconfig:$_INSTALLDIR/extras/lib64/pkgconfig:/usr/lib64/pkgconfig:/usr/share/pkgconfig:${PKG_CONFIG_PATH:-/usr/share/pkgconfig}"
            export PKG_CONFIG_LIBDIR="$_INSTALLDIR/extras/lib:$_INSTALLDIR/extras/lib64:/usr/lib64:${PKG_CONFIG_LIBDIR:-/usr/lib64}"

            cd "$_INSTALLDIR/build"

            echo "git clone opencv"
            git -c advice.detachedHead=false clone https://github.com/opencv/opencv.git

            cd opencv
            git -c advice.detachedHead=false checkout tags/3.4.6

            mkdir build
            cd build

            $CMAKE \
                -D CMAKE_INSTALL_PREFIX="$_INSTALLDIR/extras" \
                -D CMAKE_BUILD_TYPE=RELEASE \
                -D BUILD_opencv_stitching=OFF \
                -D WITH_1394=OFF \
                -D WITH_FFMPEG=ON \
                \
                -D WITH_GSTREAMER=OFF \
                -D WITH_V4L=ON \
                -D WITH_LIB4VL=ON \
                -D OPENCV_SKIP_PYTHON_LOADER=ON \
                -D OPENCV_GENERATE_PKGCONFIG=ON \
                -D ENABLE_CXX11=ON \
                \
                -D BUILD_opencv_apps=OFF \
                -D BUILD_DOCS=OFF \
                -D BUILD_PACKAGE=OFF \
                -D BUILD_PERF_TESTS=OFF \
                -D BUILD_TESTS=OFF \
                \
                -DBUILD_LIST=core,videoio,highgui,imgproc,calib3d \
                ..

            make -j$(nproc)
            make install

            cd $_INSTALLDIR/build
            $_CLEANUP && rm -rf $_INSTALLDIR/build/opencv
        fi

        if $_PREGEN
        then
            cp -R $_INSTALLDIR/extras/ $_INSTALLDIR/pregen/
        fi

    fi

    progress "prerequisites for samples are complete"
fi

if $_BUILD
then

    progress "verify cuda / cudnn / tensorrt"

    if [ -d /usr/local/ARSDK/external/cuda ] && [ -d /usr/local/ARSDK/external/tensorrt ]
    then
        echo "found ARSDK tensorrt/cuda/cudnn"
        # cuda/cudnn
        export LD_LIBRARY_PATH="/usr/local/ARSDK/external/cuda/lib${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}"
        export PATH="/usr/local/ARSDK/external/cuda/bin:$PATH"

        # tensorrt
        export LD_LIBRARY_PATH="/usr/local/ARSDK/external/tensorrt/lib:$LD_LIBRARY_PATH"
        export PATH="/usr/local/ARSDK/external/tensorrt/bin:$PATH"
    else

        # 7.2.2.3 -> 7.2.2.
        _TRT_VER_SHORT=$(echo $_TRTVER | sed 's/^\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)\..*/\1.\2\.\3/')

        check_exist "CUDA" /usr/local/cuda-$_CUDAVER
        check_exist "TensorRT" /usr/local/TensorRT-$_TRTVER/lib/libnvinfer.so.$_TRT_VER_SHORT \
                    /usr/lib64/libnvinfer.so.$_TRT_VER_SHORT \
                    /usr/lib/x86_64-linux-gnu/libnvinfer.so.$_TRT_VER_SHORT
        check_exist "cuDNN" /usr/local/cuda-$_CUDAVER/lib64/libcudnn.so.$_CUDNNVER \
                    /usr/lib64/libcudnn.so.$_CUDNNVER \
                    /usr/lib/x86_64-linux-gnu/libcudnn.so.$_CUDNNVER

        export LD_LIBRARY_PATH="/usr/local/cuda-${_CUDAVER}/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}"
        export PATH="/usr/local/cuda-${_CUDAVER}/bin:$PATH"

        export LD_LIBRARY_PATH="/usr/local/TensorRT-${_TRTVER}/lib:${LD_LIBRARY_PATH}"
        export PATH="/usr/local/TensorRT-${_TRTVER}/bin:$PATH"
    fi

    progress "build sample apps"

    # quit on error
    set -e

    # build samples referencing the read-only source
    cd $_INSTALLDIR

    ${CMAKE:-cmake} \
        -DCMAKE_BUILD_TYPE=$_BUILD_TYPE \
        $_INSTALLDIR/source

    make -k

    # copy easy-run shell scripts to cmake dir
    # need one copy of setup_env.sh
    cp $SAMPDIR/samples/*/run*.sh $_INSTALLDIR
    cp $SAMPDIR/samples/BodyTrack/setup_env.sh $_INSTALLDIR


    echo ""
    echo "the sample apps have been created"
    echo "If you used sudo permissions to execute this build_samples.sh script"
    echo "please use sudo permissions when trying out the samples below as well"
    echo "You can try the samples using:"
    echo "  cd $_INSTALLDIR"
    /bin/ls -1 run*.sh | awk '{print "  ./"$0}'
    echo "or follow the instructions in the documentation found in:"
    echo "  $SAMPDIR/samples/doc"
    echo ""
    echo "you can modify the sources in $_INSTALLDIR/source and rebuild:"
    echo "  cd $_INSTALLDIR"
    echo "  $CMAKE $_INSTALLDIR/source"
    echo "  make"
fi