-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCMakeLists.txt
185 lines (166 loc) · 6.88 KB
/
CMakeLists.txt
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
set(deps esp_rom vfs lwip pthread newlib)
idf_component_register(
# We need the dummy source file so that the component
# library is not an interface library. This allows to
# get the list of include directories from other components
# via INCLUDE_DIRECTORIES property later on.
SRCS dummy.c
# OpenCV librarires depend on some of the IDF libraries,
# add them here:
PRIV_REQUIRES ${deps})
# Determine compilation flags used for building OpenCV
# Flags inherited from IDF build system and other IDF components:
set(idf_include_directories $<TARGET_PROPERTY:idf::${COMPONENT_NAME},INCLUDE_DIRECTORIES>)
set(includes "-I$<JOIN:${idf_include_directories}, -I>")
# IDF is built with -D_GNU_SOURCE, but we don't actually implement all _GNU_SOURCE
# features. Defining _GNU_SOURCE causes OpenCV to try to use them, which fails.
# Use _DEFAULT_SOURCE instead, it provides enough features to build OpenCV.
set(extra_defines -D_DEFAULT_SOURCE)
# Final flags for C and C++:
set(c_flags "${includes} ${extra_defines}")
set(cxx_flags "${includes} ${extra_defines}")
set(cxx_flags "${includes} ${extra_defines} -DCV_INT32_T_IS_LONG_INT=ON")
set(common_flags "-ggdb -ffunction-sections -fdata-sections")
# Calculate flags for optimizations, assertions, debug info:
if(COMPILER_OPTIMIZATION_ASSERTIONS_DISABLE)
set(assert_flags "-DNDEBUG")
else()
set(assert_flags "-DDEBUG -D_DEBUG")
endif()
if(CONFIG_IDF_TARGET_ARCH_XTENSA)
set(assert_flags "${assert_flags} -mlongcalls -mauto-litpools")
endif()
if(CONFIG_COMPILER_OPTIMIZATION_DEFAULT)
set(opt_flags "-Og ${common_flags} ${assert_flags}")
set(opt_args -DCMAKE_BUILD_TYPE=Debug
-DCMAKE_C_FLAGS_DEBUG=${opt_flags}
-DCMAKE_CXX_FLAGS_DEBUG=${opt_flags})
elseif(CONFIG_COMPILER_OPTIMIZATION_SIZE)
# Normally we should use MinSizeRel build type for this,
# however OpenCV CMake files only handle Debug and Release
# (see e.g. opencv/cmake/OpenCVCompilerOptions.cmake).
# So we redefine the flags for Release instead.
set(opt_flags "-Os ${common_flags} ${assert_flags}")
set(opt_args -DCMAKE_BUILD_TYPE=Release
-DCMAKE_C_FLAGS_RELEASE=${opt_flags}
-DCMAKE_CXX_FLAGS_RELEASE=${opt_flags})
elseif(COMPILER_OPTIMIZATION_PERF)
# Currently OpenCV fails to compile at -O2 level on Xtensa due to a compiler issue,
# details in https://github.com/jcmvbkbc/gcc-xtensa/issues/14.
# Compiling at -O3 happens to be okay.
set(opt_flags "-O3 ${common_flags} ${assert_flags}")
set(opt_args -DCMAKE_BUILD_TYPE=Release
-DCMAKE_C_FLAGS_RELEASE=${opt_flags}
-DCMAKE_CXX_FLAGS_RELEASE=${opt_flags})
elseif(COMPILER_OPTIMIZATION_NONE)
set(opt_flags "-O0 ${common_flags} ${assert_flags}")
set(opt_args -DCMAKE_BUILD_TYPE=Debug
-DCMAKE_C_FLAGS_DEBUG=${opt_flags}
-DCMAKE_CXX_FLAGS_DEBUG=${opt_flags})
else()
message(FATAL_ERROR "Unsupported optimization level")
endif()
# Python to use for OpenCV build
idf_build_get_property(python PYTHON)
include(ExternalProject)
# Build OpenCV in this directory:
set(BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/opencv-build)
# List of OpenCV static libraries we expect to see after the build.
# Obtained manually by looking at ${BINARY_DIR}/install/lib directory.
# In some cases it's important to have right order of libraries here.
set(opencv_libraries
calib3d flann fuzzy
objdetect tracking video videostab features2d
surface_matching
shape stereo stitching structured_light
line_descriptor ml phase_unwrapping photo plot quality rapid
imgcodecs imgproc core
)
set(3rdparty_libraries
zlib
libopenjp2
libjpeg-turbo
libpng
libtiff
libwebp
)
# List of all static libraries to be produced
set(all_libraries)
set(all_targets)
foreach(libname ${opencv_libraries})
set(lib_path ${BINARY_DIR}/install/lib/libopencv_${libname}.a)
list(APPEND all_libraries ${lib_path})
list(APPEND all_targets opencv_${libname})
add_prebuilt_library(opencv_${libname} ${lib_path})
endforeach()
foreach(libname ${3rdparty_libraries})
set(lib_path ${BINARY_DIR}/install/lib/opencv4/3rdparty/lib${libname}.a)
list(APPEND all_libraries ${lib_path})
list(APPEND all_targets opencv_${libname})
add_prebuilt_library(opencv_${libname} ${lib_path})
endforeach()
# Add OpenCV as a subproject.
ExternalProject_Add(opencv_proj
SOURCE_DIR ${COMPONENT_DIR}/opencv
BINARY_DIR ${BINARY_DIR}
BUILD_BYPRODUCTS ${all_libraries}
# These two options are set so that Ninja immediately outputs
# the subproject build to the terminal. Otherwise it looks like the
# build process "hangs" for too long until OpenCV build is complete.
USES_TERMINAL_CONFIGURE TRUE
USES_TERMINAL_BUILD TRUE
# Arguments to pass to OpenCV CMake invocation:
CMAKE_ARGS
-DCMAKE_C_FLAGS=${c_flags}
-DCMAKE_CXX_FLAGS=${cxx_flags}
${opt_args}
-DCMAKE_INSTALL_PREFIX=${BINARY_DIR}/install
-DENABLE_CCACHE=${CCACHE_ENABLE}
-DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE}
-DPYTHON_DEFAULT_EXECUTABLE=${python}
-DCV_CPU_BASELINE_MODE=${CONFIG_IDF_TARGET}
-DCV_DISABLE_OPTIMIZATION=0
-DOPENCV_EXTRA_MODULES_PATH=${CMAKE_CURRENT_LIST_DIR}/opencv_contrib/modules
-DOPENCV_FORCE_3RDPARTY_BUILD=1
-DCMAKE_SYSTEM_PROCESSOR=${CONFIG_IDF_TARGET_ARCH}
-DENABLE_PIC=0
-DTARGET=${CONFIG_IDF_TARGET}
-DWITH_ADE=0
-DWITH_JPEG=1
-DWITH_OPENCL=0
-DWITH_OPENEXR=0
-DWITH_OPENGL=0
-DWITH_OPENMP=0
-DWITH_PNG=1
-DWITH_PROTOBUF=0
-DWITH_TIFF=1
-DWITH_WEBP=1
-DBUILD_EXAMPLES=0
-DBUILD_PACKAGE=1
-DBUILD_PERF_TESTS=0
-DBUILD_PNG=1
-DBUILD_SHARED_LIBS=0
-DBUILD_TESTS=0
-DBUILD_ZLIB=1
-DBUILD_opencv_apps=0
-DBUILD_opencv_bgsegm=0
-DBUILD_opencv_datasets=0
-DBUILD_opencv_imgcodecs=1
-DBUILD_opencv_objdetect=1
-DBUILD_opencv_imgproc=1
-DBUILD_opencv_tracking=1
-DBUILD_opencv_video=1
-DBUILD_opencv_videoio=0
)
# Attach header files to the component library:
set_target_properties(${COMPONENT_LIB} PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${BINARY_DIR}/install/include/opencv4)
# Make sure the subproject is built before the component library:
add_dependencies(${COMPONENT_LIB} opencv_proj)
foreach(opencv_target ${all_targets})
# Attach IDF compoenent dependencies to OpenCV libraries
foreach(dep ${deps})
target_link_libraries(${opencv_target} INTERFACE idf::${dep})
endforeach()
# Attach OpenCV libraries to the component library
target_link_libraries(${COMPONENT_LIB} INTERFACE ${opencv_target})
endforeach()