aboutsummaryrefslogtreecommitdiff
path: root/scripts/get-and-build-libndpi.sh
blob: d1d74905285db7612c76e74b0dd4d92dc4017505 (plain)
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
#!/usr/bin/env bash

set -e

GIT_EXEC="$(command -v git || printf '%s' "")"
WGET_EXEC="$(command -v wget || printf '%s' "")"
UNZIP_EXEC="$(command -v unzip || printf '%s' "")"
MAKE_EXEC="$(command -v make || printf '%s' "")"

if [ -z "${NDPI_COMMIT_HASH}" ]; then
    NDPI_COMMIT_HASH="dev"
    GITHUB_FALLBACK_URL='https://github.com/ntop/nDPI/archive/refs/heads/dev.zip'
else
    GITHUB_FALLBACK_URL="https://github.com/ntop/nDPI/archive/${NDPI_COMMIT_HASH}.zip"
fi

if [ -z "${GIT_EXEC}" -o -z "${WGET_EXEC}" -o -z "${UNZIP_EXEC}" -o -z "${MAKE_EXEC}" ]; then
    printf '%s\n' "Required Executables missing: git, wget, unzip, make" >&2
    exit 1
fi

LOCKFILE="$(realpath "${0}").lock"
touch "${LOCKFILE}"
exec 42< "${LOCKFILE}"
flock -x -n 42 || {
    printf '%s\n' "Could not aquire file lock for ${0}. Already running instance?" >&2;
    exit 1;
}

if [ ! -z "${CC}" ]; then
    HOST_TRIPLET="$(${CC} ${CFLAGS} -dumpmachine)"
fi

MAKEFLAGS="-${MAKEFLAGS}"

cat <<EOF
------ environment variables ------
HOST_TRIPLET=${HOST_TRIPLET}
CC=${CC:-}
CXX=${CXX:-}
AR=${AR:-}
RANLIB=${RANLIB:-}
PKG_CONFIG=${PKG_CONFIG:-}
CFLAGS=${CFLAGS:-}
LDFLAGS=${LDFLAGS:-}
ADDITIONAL_ARGS=${ADDITIONAL_ARGS:-}
MAKE_PROGRAM=${MAKE_PROGRAM:-}
MAKEFLAGS=${MAKEFLAGS}
DEST_INSTALL=${DEST_INSTALL:-}
NDPI_COMMIT_HASH=${NDPI_COMMIT_HASH:-}
FORCE_GIT_UPDATE=${FORCE_GIT_UPDATE:-}
-----------------------------------
EOF

set -x

cd "$(dirname "${0}")/.."

GIT_SUCCESS=0
if [ -d ./.git -o -f ./.git ]; then
    GIT_SUCCESS=1

    if [ ! -z "${FORCE_GIT_UPDATE}" -a "${FORCE_GIT_UPDATE}" != "OFF" ]; then
        git submodule deinit --force -- ./libnDPI || { GIT_SUCCESS=0; true; }
        LINES_CHANGED=0
    else
        LINES_CHANGED="$(git --no-pager diff ./libnDPI 2>/dev/null | wc -l || printf '0')"
    fi

    if [ ${LINES_CHANGED} -eq 0 ]; then
        git submodule update --progress --init ./libnDPI || { GIT_SUCCESS=0; true; }
    else
        set +x
        printf '%s\n' '-----------------------------------'
        printf 'WARNING: %s changes in source tree %s, no GIT update will be done!\n' "${LINES_CHANGED}" "$(realpath $(dirname "${0}")/../libnDPI)"
        printf '%s\n' '-----------------------------------'
        set -x
    fi
fi

if [ ${GIT_SUCCESS} -eq 0 ]; then
    set +x
    printf '%s\n' '-----------------------------------'
    printf 'WARNING: %s is supposed to be a GIT repository. But it is not.\n' "$(realpath $(dirname "${0}")/..)"
    printf '%s\n' 'Can not clone libnDPI as GIT submodule.'
    printf '%s\n' 'Falling back to Github direct download.'
    printf 'URL: %s\n' "${GITHUB_FALLBACK_URL}"
    printf '%s\n' '-----------------------------------'
    set -x
    wget "${GITHUB_FALLBACK_URL}" -O ./libnDPI-github-${NDPI_COMMIT_HASH}.zip
    unzip ./libnDPI-github-${NDPI_COMMIT_HASH}.zip
    rm -rf ./libnDPI
    mv ./nDPI-${NDPI_COMMIT_HASH} ./libnDPI
fi

cd ./libnDPI
test ! -r Makefile || { make distclean || true; }
DEST_INSTALL="${DEST_INSTALL:-$(realpath ./install)}"
MAKE_PROGRAM="${MAKE_PROGRAM:-make -j4}"
HOST_ARG="--host=${HOST_TRIPLET}"
./autogen.sh --enable-option-checking=fatal \
    --prefix="/" \
    --with-only-libndpi ${HOST_ARG} ${ADDITIONAL_ARGS} || { cat config.log | grep -v '^|'; false; }
${MAKE_PROGRAM} ${MAKEFLAGS} install DESTDIR="${DEST_INSTALL}"

rm -f "${LOCKFILE}"