|
| 1 | +#!/usr/bin/env bash |
| 2 | +# 2019 Michael de Gans |
| 3 | + |
| 4 | +set -e |
| 5 | + |
| 6 | +# change default constants here: |
| 7 | +readonly PREFIX=/usr/local # install prefix, (can be ~/.local for a user install) |
| 8 | +readonly DEFAULT_VERSION=4.4.0 # controls the default version (gets reset by the first argument) |
| 9 | +readonly CPUS=$(nproc) # controls the number of jobs |
| 10 | + |
| 11 | +# better board detection. if it has 6 or more cpus, it probably has a ton of ram too |
| 12 | +if [[ $CPUS -gt 5 ]]; then |
| 13 | + # something with a ton of ram |
| 14 | + JOBS=$CPUS |
| 15 | +else |
| 16 | + JOBS=1 # you can set this to 4 if you have a swap file |
| 17 | + # otherwise a Nano will choke towards the end of the build |
| 18 | +fi |
| 19 | + |
| 20 | +cleanup () { |
| 21 | +# https://stackoverflow.com/questions/226703/how-do-i-prompt-for-yes-no-cancel-input-in-a-linux-shell-script |
| 22 | + while true ; do |
| 23 | + echo "Do you wish to remove temporary build files in /tmp/build_opencv ? " |
| 24 | + if ! [[ "$1" -eq "--test-warning" ]] ; then |
| 25 | + echo "(Doing so may make running tests on the build later impossible)" |
| 26 | + fi |
| 27 | + read -p "Y/N " yn |
| 28 | + case ${yn} in |
| 29 | + [Yy]* ) rm -rf /tmp/build_opencv ; break;; |
| 30 | + [Nn]* ) exit ;; |
| 31 | + * ) echo "Please answer yes or no." ;; |
| 32 | + esac |
| 33 | + done |
| 34 | +} |
| 35 | + |
| 36 | +setup () { |
| 37 | + cd /tmp |
| 38 | + if [[ -d "build_opencv" ]] ; then |
| 39 | + echo "It appears an existing build exists in /tmp/build_opencv" |
| 40 | + cleanup |
| 41 | + fi |
| 42 | + mkdir build_opencv |
| 43 | + cd build_opencv |
| 44 | +} |
| 45 | + |
| 46 | +git_source () { |
| 47 | + echo "Getting version '$1' of OpenCV" |
| 48 | + git clone --depth 1 --branch "$1" https://github.com/opencv/opencv.git |
| 49 | + git clone --depth 1 --branch "$1" https://github.com/opencv/opencv_contrib.git |
| 50 | +} |
| 51 | + |
| 52 | +install_dependencies () { |
| 53 | + # open-cv has a lot of dependencies, but most can be found in the default |
| 54 | + # package repository or should already be installed (eg. CUDA). |
| 55 | + echo "Installing build dependencies." |
| 56 | + sudo apt-get update |
| 57 | + sudo apt-get dist-upgrade -y --autoremove |
| 58 | + sudo apt-get install -y \ |
| 59 | + build-essential \ |
| 60 | + cmake \ |
| 61 | + git \ |
| 62 | + gfortran \ |
| 63 | + libatlas-base-dev \ |
| 64 | + libavcodec-dev \ |
| 65 | + libavformat-dev \ |
| 66 | + libavresample-dev \ |
| 67 | + libcanberra-gtk3-module \ |
| 68 | + libdc1394-22-dev \ |
| 69 | + libeigen3-dev \ |
| 70 | + libglew-dev \ |
| 71 | + libgstreamer-plugins-base1.0-dev \ |
| 72 | + libgstreamer-plugins-good1.0-dev \ |
| 73 | + libgstreamer1.0-dev \ |
| 74 | + libgtk-3-dev \ |
| 75 | + libjpeg-dev \ |
| 76 | + libjpeg8-dev \ |
| 77 | + libjpeg-turbo8-dev \ |
| 78 | + liblapack-dev \ |
| 79 | + liblapacke-dev \ |
| 80 | + libopenblas-dev \ |
| 81 | + libpng-dev \ |
| 82 | + libpostproc-dev \ |
| 83 | + libswscale-dev \ |
| 84 | + libtbb-dev \ |
| 85 | + libtbb2 \ |
| 86 | + libtesseract-dev \ |
| 87 | + libtiff-dev \ |
| 88 | + libv4l-dev \ |
| 89 | + libxine2-dev \ |
| 90 | + libxvidcore-dev \ |
| 91 | + libx264-dev \ |
| 92 | + pkg-config \ |
| 93 | + python-dev \ |
| 94 | + python-numpy \ |
| 95 | + python3-dev \ |
| 96 | + python3-numpy \ |
| 97 | + python3-matplotlib \ |
| 98 | + qv4l2 \ |
| 99 | + v4l-utils \ |
| 100 | + v4l2ucp \ |
| 101 | + zlib1g-dev |
| 102 | +} |
| 103 | + |
| 104 | +configure () { |
| 105 | + local CMAKEFLAGS=" |
| 106 | + -D BUILD_EXAMPLES=OFF |
| 107 | + -D BUILD_opencv_python2=ON |
| 108 | + -D BUILD_opencv_python3=ON |
| 109 | + -D CMAKE_BUILD_TYPE=RELEASE |
| 110 | + -D CMAKE_INSTALL_PREFIX=${PREFIX} |
| 111 | + -D CUDA_ARCH_BIN=5.3,6.2,7.2 |
| 112 | + -D CUDA_ARCH_PTX= |
| 113 | + -D CUDA_FAST_MATH=ON |
| 114 | + -D CUDNN_VERSION='8.0' |
| 115 | + -D EIGEN_INCLUDE_PATH=/usr/include/eigen3 |
| 116 | + -D ENABLE_NEON=ON |
| 117 | + -D OPENCV_DNN_CUDA=ON |
| 118 | + -D OPENCV_ENABLE_NONFREE=ON |
| 119 | + -D OPENCV_EXTRA_MODULES_PATH=/tmp/build_opencv/opencv_contrib/modules |
| 120 | + -D OPENCV_GENERATE_PKGCONFIG=ON |
| 121 | + -D WITH_CUBLAS=ON |
| 122 | + -D WITH_CUDA=ON |
| 123 | + -D WITH_CUDNN=ON |
| 124 | + -D WITH_GSTREAMER=ON |
| 125 | + -D WITH_LIBV4L=ON |
| 126 | + -D WITH_OPENGL=ON" |
| 127 | + |
| 128 | + if [[ "$1" != "test" ]] ; then |
| 129 | + CMAKEFLAGS=" |
| 130 | + ${CMAKEFLAGS} |
| 131 | + -D BUILD_PERF_TESTS=OFF |
| 132 | + -D BUILD_TESTS=OFF" |
| 133 | + fi |
| 134 | + |
| 135 | + echo "cmake flags: ${CMAKEFLAGS}" |
| 136 | + |
| 137 | + cd opencv |
| 138 | + mkdir build |
| 139 | + cd build |
| 140 | + cmake ${CMAKEFLAGS} .. 2>&1 | tee -a configure.log |
| 141 | +} |
| 142 | + |
| 143 | +main () { |
| 144 | + |
| 145 | + local VER=${DEFAULT_VERSION} |
| 146 | + |
| 147 | + # parse arguments |
| 148 | + if [[ "$#" -gt 0 ]] ; then |
| 149 | + VER="$1" # override the version |
| 150 | + fi |
| 151 | + |
| 152 | + if [[ "$#" -gt 1 ]] && [[ "$2" == "test" ]] ; then |
| 153 | + DO_TEST=1 |
| 154 | + fi |
| 155 | + |
| 156 | + # prepare for the build: |
| 157 | + setup |
| 158 | + install_dependencies |
| 159 | + git_source ${VER} |
| 160 | + |
| 161 | + if [[ ${DO_TEST} ]] ; then |
| 162 | + configure test |
| 163 | + else |
| 164 | + configure |
| 165 | + fi |
| 166 | + |
| 167 | + # start the build |
| 168 | + make -j${JOBS} 2>&1 | tee -a build.log |
| 169 | + |
| 170 | + if [[ ${DO_TEST} ]] ; then |
| 171 | + make test 2>&1 | tee -a test.log |
| 172 | + fi |
| 173 | + |
| 174 | + # avoid a sudo make install (and root owned files in ~) if $PREFIX is writable |
| 175 | + if [[ -w ${PREFIX} ]] ; then |
| 176 | + make install 2>&1 | tee -a install.log |
| 177 | + else |
| 178 | + sudo make install 2>&1 | tee -a install.log |
| 179 | + fi |
| 180 | + |
| 181 | + cleanup --test-warning |
| 182 | + |
| 183 | +} |
| 184 | + |
| 185 | +main "$@" |
0 commit comments