forked from mdegans/nano_build_opencv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_opencv.sh
executable file
·148 lines (126 loc) · 3.74 KB
/
build_opencv.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
#!/usr/bin/env bash
# 2019 Michael de Gans
set -e
# change default constants here:
readonly PREFIX=/usr/local # install prefix, (can be ~/.local for a user install)
readonly DEFAULT_VERSION=4.2.0 # controls the default version (gets reset by the first argument)
readonly JOBS=1 # controls the number of jobs
# (recommend leaving JOBS to 1 since each `cc1plus` process towards the end of
# the build consumes close to 1.7G memory) If you have an external drive connected
# as swap you can increase this and parts will build faster, while others might
# build slower. This is currently untested
cleanup () {
# https://stackoverflow.com/questions/226703/how-do-i-prompt-for-yes-no-cancel-input-in-a-linux-shell-script
while true ; do
echo "Do you wish to remove temporary build files in /tmp/build_opencv ? "
if ! [[ "$1" -eq "--test-warning" ]] ; then
echo "(Doing so may make running tests on the build later impossible)"
fi
read -p "Y/N " yn
case ${yn} in
[Yy]* ) rm -rf /tmp/build_opencv ; break;;
[Nn]* ) exit ;;
* ) echo "Please answer yes or no." ;;
esac
done
}
setup () {
cd /tmp
if [[ -d "build_opencv" ]] ; then
echo "It appears an existing build exists in /tmp/build_opencv"
cleanup
fi
mkdir build_opencv
cd build_opencv
}
git_source () {
echo "Getting version '$1' of OpenCV"
git clone --branch "$1" https://github.com/opencv/opencv.git
git clone --branch "$1" https://github.com/opencv/opencv_contrib.git
}
install_dependencies () {
# open-cv has a lot of dependencies, but most can be found in the default
# package repository or should already be installed (eg. CUDA).
echo "Installing build dependencies."
sudo apt-get update
sudo apt-get dist-upgrade -y --autoremove
sudo apt-get install -y \
build-essential \
cmake \
git \
libavcodec-dev \
libavresample-dev \
libavformat-dev \
libdc1394-22-dev \
libgstreamer1.0-dev \
libgtk-3-dev \
libjpeg-dev \
libpng-dev \
libswscale-dev \
libtbb-dev \
libtbb2 \
libtiff-dev \
libv4l-dev \
pkg-config \
python-dev \
python-numpy \
python3-dev \
python3-numpy
}
configure () {
local CMAKEFLAGS="
-D BUILD_EXAMPLES=OFF
-D BUILD_opencv_python2=ON
-D BUILD_opencv_python3=ON
-D CMAKE_INSTALL_PREFIX=${PREFIX}
-D OPENCV_EXTRA_MODULES_PATH=/tmp/build_opencv/opencv_contrib/modules
-D WITH_CUDA=ON
-D CUDA_ARCH_BIN="5.3,6.2,7.2"
-D CUDA_ARCH_PTX=""
-D WITH_GSTREAMER=ON
-D WITH_LIBV4L=ON"
if ! [[ "$1" -eq "test" ]] ; then
CMAKEFLAGS="
${CMAKEFLAGS}
-D BUILD_PERF_TESTS=OFF
-D BUILD_TESTS=OFF"
fi
echo "cmake flags: ${CMAKEFLAGS}"
cd opencv
mkdir build
cd build
cmake ${CMAKEFLAGS} ..
}
main () {
local VER=${DEFAULT_VERSION}
# parse arguments
if [[ "$#" -gt 0 ]] ; then
VER="$1" # override the version
fi
if [[ "$#" -gt 1 ]] && [[ "$2" -eq "test" ]] ; then
DO_TEST=1
fi
# prepare for the build:
setup
install_dependencies
git_source ${VER}
if [[ ${DO_TEST} ]] ; then
configure test
else
configure
fi
# start the build
make -j${JOBS}
# ifdef DO_TEST ; then
if [[ ${DO_TEST} ]] ; then
make test # (make and) run the tests
fi
# avoid a sudo make install (and root owned files in ~) if $PREFIX is writable
if [[ -w ${PREFIX} ]] ; then
make install
else
sudo make install
fi
cleanup --test-warning
}
main "$@"