• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright © 2020 Arm Ltd and Contributors. All rights reserved.
2# SPDX-License-Identifier: MIT
3
4set(OPENCV_VERSION 4.0.0)
5set(FFMPEG_VERSION 4.2.1)
6set(LIBX264_VERSION stable)
7
8set(OPENCV_LIB OpenCV${OPENCV_VERSION})
9set(FFMPEG_LIB ffmpeg${FFMPEG_VERSION})
10set(X264_LIB   x264${LIBX264_VERSION})
11
12set(OPENCV_NAMES
13    libopencv_core.so.${OPENCV_VERSION}
14    libopencv_imgproc.so.${OPENCV_VERSION}
15    libopencv_imgcodecs.so.${OPENCV_VERSION}
16    libopencv_videoio.so.${OPENCV_VERSION}
17    libopencv_video.so.${OPENCV_VERSION}
18    libopencv_highgui.so.${OPENCV_VERSION})
19
20set(OPENCV_LIBS)
21set(FFMPEG_LIBS)
22
23foreach(opencv_lib ${OPENCV_NAMES})
24    find_library(OPENCV_${opencv_lib}
25        NAMES
26            ${opencv_lib}
27        HINTS
28            ${OPENCV_LIB_DIR}
29        PATHS
30            ${OPENCV_LIB_DIR}
31        PATH_SUFFIXES
32            "lib"
33            "lib64")
34    if(OPENCV_${opencv_lib})
35        message("Found library ${OPENCV_${opencv_lib}}")
36        list(APPEND OPENCV_LIBS ${OPENCV_${opencv_lib}})
37        get_filename_component(OPENCV_LIB_DIR ${OPENCV_${opencv_lib}} DIRECTORY)
38        get_filename_component(OPENCV_ROOT_DIR ${OPENCV_LIB_DIR} DIRECTORY)
39        set(OPENCV_INCLUDE_DIR ${OPENCV_ROOT_DIR}/include/opencv4)
40    endif()
41endforeach()
42
43if(OPENCV_LIBS)
44    message("OpenCV libraries found")
45    set(OPENCV_LIBS_FOUND TRUE)
46else()
47    set(OPENCV_ROOT_DIR ${DEPENDENCIES_DIR}/opencv)
48    set(OPENCV_DEPENDENCIES_ARGS)
49    set(OPENCV_EXTRA_LINKER_ARGS)
50    set(OPENCV_PKGCONFIG)
51
52    if(CMAKE_CROSSCOMPILING)
53        set(FFMPEG_ROOT_DIR ${DEPENDENCIES_DIR}/ffmpeg)
54        set(LIBX264_ROOT_DIR ${DEPENDENCIES_DIR}/x264)
55
56        if (CMAKE_BUILD_TYPE STREQUAL Debug)
57            set(CONFIGURE_DEBUG --enable-debug)
58            set(OPENCV_DEBUG "-DBUILD_WITH_DEBUG_INFO=ON")
59        endif()
60
61
62        ExternalProject_Add(${X264_LIB}
63            URL "https://code.videolan.org/videolan/x264/-/archive/${LIBX264_VERSION}/x264-${LIBX264_VERSION}.tar.gz"
64            DOWNLOAD_DIR ${LIBX264_ROOT_DIR}
65            PREFIX ${LIBX264_ROOT_DIR}
66            CONFIGURE_COMMAND <SOURCE_DIR>/configure
67            --host=${GNU_MACHINE}
68            --enable-static
69            --enable-shared
70            --cross-prefix=${CROSS_PREFIX}
71            --prefix=${CMAKE_BINARY_DIR}
72            --extra-ldflags=-static-libstdc++
73            --extra-cflags=-fPIC
74            ${CONFIGURE_DEBUG}
75            INSTALL_DIR ${CMAKE_BINARY_DIR}
76            BUILD_COMMAND $(MAKE)
77            INSTALL_COMMAND $(MAKE) install
78            )
79
80        set(FFMPEG_Config
81            --enable-shared
82            --enable-cross-compile
83            --cross-prefix=${CROSS_PREFIX}
84            --arch=${CMAKE_SYSTEM_PROCESSOR}
85            --target-os=linux
86            --prefix=${CMAKE_BINARY_DIR}
87            --enable-gpl
88            --enable-nonfree
89            --enable-libx264
90            --extra-cflags=-I${CMAKE_BINARY_DIR}/include
91            --extra-cflags=-fPIC
92            --extra-ldflags=-L${CMAKE_BINARY_DIR}/lib
93            --extra-libs=-ldl
94            --extra-libs=-static-libstdc++
95        )
96
97        ExternalProject_Add(${FFMPEG_LIB}
98            URL "https://github.com/FFmpeg/FFmpeg/archive/n${FFMPEG_VERSION}.tar.gz"
99            URL_HASH MD5=05792c611d1e3ebdf2c7003ff4467390
100            DOWNLOAD_DIR ${FFMPEG_ROOT_DIR}
101            PREFIX ${FFMPEG_ROOT_DIR}
102            CONFIGURE_COMMAND <SOURCE_DIR>/configure ${FFMPEG_Config} ${CONFIGURE_DEBUG}
103            INSTALL_DIR ${CMAKE_BINARY_DIR}
104            BUILD_COMMAND $(MAKE) VERBOSE=1
105            INSTALL_COMMAND $(MAKE) install
106        )
107
108        set(OPENCV_DEPENDENCIES_ARGS "-static-libstdc++ -Wl,-rpath,${CMAKE_BINARY_DIR}/lib")
109        set(OPENCV_EXTRA_LINKER_ARGS "-DOPENCV_EXTRA_EXE_LINKER_FLAGS=${OPENCV_DEPENDENCIES_ARGS}")
110
111        set(OPENCV_PKGCONFIG "PKG_CONFIG_LIBDIR=${CMAKE_BINARY_DIR}/lib/pkgconfig")
112
113        set(FFMPEG_NAMES
114            libavcodec.so
115            libavformat.so
116            libavutil.so
117            libswscale.so
118            )
119
120        foreach(ffmpeg_lib ${FFMPEG_NAMES})
121            add_library(FFMPEG_${ffmpeg_lib} SHARED IMPORTED)
122            set_target_properties(FFMPEG_${ffmpeg_lib} PROPERTIES IMPORTED_LOCATION ${CMAKE_BINARY_DIR}/lib/${ffmpeg_lib})
123            list(APPEND OPENCV_LIBS FFMPEG_${ffmpeg_lib})
124        endforeach()
125
126        add_library(X264_lib264.so SHARED IMPORTED)
127        set_target_properties(X264_lib264.so PROPERTIES IMPORTED_LOCATION ${CMAKE_BINARY_DIR}/lib/libx264.so)
128        list(APPEND OPENCV_LIBS X264_lib264.so)
129    endif()
130
131    set(OPENCV_CMAKE_ARGS
132        -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
133        -DCMAKE_C_FLAGS=-fPIC
134        -DCMAKE_CXX_FLAGS=-fPIC
135        -DWITH_GTK=OFF
136        -DWITH_JPEG=ON
137        -DWITH_IPP=OFF
138        -DBUILD_opencv_java_bindings_generator=OFF
139        -DBUILD_opencv_ml=OFF
140        -DBUILD_opencv_objdetect=OFF
141        -DBUILD_opencv_photo=OFF
142        -DBUILD_opencv_python_bindings_generator=OFF
143        -DBUILD_opencv_stitching=OFF
144        -DBUILD_opencv_gapi=OFF
145        -DBUILD_opencv_features2d=OFF
146        -DBUILD_opencv_dnn=OFF
147        -DBUILD_opencv_flann=OFF
148        -DBUILD_opencv_calib3d=OFF
149        -DBUILD_opencv_python2=OFF
150        -DBUILD_opencv_python3=OFF
151        -DBUILD_opencv_java=OFF
152        -DBUILD_opencv_js=OFF
153        -DBUILD_opencv_ts=OFF
154        -DBUILD_JPEG=ON
155        -DBUILD_JPEG_TURBO_DISABLE=ON
156        -DBUILD_PNG=ON
157        -DBUILD_TIFF=ON
158        -DZLIB_FOUND=OFF
159        -DBUILD_ZLIB=ON
160        -DBUILD_PERF_TESTS=OFF
161        -DBUILD_TESTS=OFF
162        -DBUILD_DOCS=OFF
163        -DBUILD_opencv_apps=OFF
164        -DBUILD_EXAMPLES=OFF
165        -DWITH_V4L=ON
166        -DWITH_LIBV4L=OFF
167        -DWITH_FFMPEG=ON
168        -DCMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR}
169        -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
170        -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
171        -DCMAKE_INSTALL_RPATH=\$ORIGIN:\$ORIGIN/lib:\$ORIGIN/../lib
172        -DCMAKE_SHARED_LINKER_FLAGS=-static-libstdc++
173        ${OPENCV_DEBUG}
174        )
175
176    ExternalProject_Add(${OPENCV_LIB}
177        URL "https://codeload.github.com/opencv/opencv/tar.gz/${OPENCV_VERSION}"
178        URL_HASH MD5=f051c1ff7b327b60123d71b53801b316
179        DOWNLOAD_DIR ${OPENCV_ROOT_DIR}
180        PREFIX ${OPENCV_ROOT_DIR}
181        CONFIGURE_COMMAND ${OPENCV_PKGCONFIG}
182        ${CMAKE_COMMAND} ${OPENCV_CMAKE_ARGS} ${OPENCV_EXTRA_ARGS}
183        ${OPENCV_EXTRA_LINKER_ARGS} ${OPENCV_ROOT_DIR}/src/${OPENCV_LIB}
184        INSTALL_DIR ${CMAKE_BINARY_DIR}
185        BUILD_COMMAND $(MAKE)
186        INSTALL_COMMAND $(MAKE) install
187        )
188
189    if(CMAKE_CROSSCOMPILING)
190        ExternalProject_Add_StepDependencies(${FFMPEG_LIB} build ${X264_LIB})
191        ExternalProject_Add_StepDependencies(${OPENCV_LIB} build ${FFMPEG_LIB})
192    endif()
193
194    set(OPENCV_INCLUDE_DIR ${CMAKE_BINARY_DIR}/include/opencv4)
195    set(OPENCV_LIB_DIR ${CMAKE_BINARY_DIR}/lib)
196
197    foreach(opencv_lib ${OPENCV_NAMES})
198        add_library(OPENCV_${opencv_lib} SHARED IMPORTED)
199        set_target_properties(OPENCV_${opencv_lib} PROPERTIES IMPORTED_LOCATION ${OPENCV_LIB_DIR}/${opencv_lib})
200        list(APPEND OPENCV_LIBS OPENCV_${opencv_lib})
201    endforeach()
202
203endif()