|
| 1 | +set(deps esp_rom vfs lwip pthread newlib) |
| 2 | + |
| 3 | +idf_component_register( |
| 4 | + # We need the dummy source file so that the component |
| 5 | + # library is not an interface library. This allows to |
| 6 | + # get the list of include directories from other components |
| 7 | + # via INCLUDE_DIRECTORIES property later on. |
| 8 | + SRCS dummy.c |
| 9 | + # OpenCV librarires depend on some of the IDF libraries, |
| 10 | + # add them here: |
| 11 | + PRIV_REQUIRES ${deps}) |
| 12 | + |
| 13 | +# Determine compilation flags used for building OpenCV |
| 14 | +# Flags inherited from IDF build system and other IDF components: |
| 15 | +set(idf_include_directories $<TARGET_PROPERTY:idf::${COMPONENT_NAME},INCLUDE_DIRECTORIES>) |
| 16 | +set(includes "-I$<JOIN:${idf_include_directories}, -I>") |
| 17 | +# IDF is built with -D_GNU_SOURCE, but we don't actually implement all _GNU_SOURCE |
| 18 | +# features. Defining _GNU_SOURCE causes OpenCV to try to use them, which fails. |
| 19 | +# Use _DEFAULT_SOURCE instead, it provides enough features to build OpenCV. |
| 20 | +set(extra_defines -D_DEFAULT_SOURCE) |
| 21 | +# Final flags for C and C++: |
| 22 | +set(c_flags "${includes} ${extra_defines}") |
| 23 | +set(cxx_flags "${includes} ${extra_defines}") |
| 24 | +set(cxx_flags "${includes} ${extra_defines} -DCV_INT32_T_IS_LONG_INT=ON -Dalloca=__builtin_alloca") |
| 25 | + |
| 26 | +set(common_flags "-ggdb -ffunction-sections -fdata-sections") |
| 27 | + |
| 28 | +# Calculate flags for optimizations, assertions, debug info: |
| 29 | +if(COMPILER_OPTIMIZATION_ASSERTIONS_DISABLE) |
| 30 | + set(assert_flags "-DNDEBUG") |
| 31 | +else() |
| 32 | + set(assert_flags "-DDEBUG -D_DEBUG") |
| 33 | +endif() |
| 34 | + |
| 35 | +if(CONFIG_IDF_TARGET_ARCH_XTENSA) |
| 36 | + set(assert_flags "${assert_flags} -mlongcalls -mauto-litpools") |
| 37 | +endif() |
| 38 | + |
| 39 | +if(CONFIG_COMPILER_OPTIMIZATION_DEFAULT) |
| 40 | + set(opt_c_flags "-Og ${common_flags} ${assert_flags} -Wno-format -Wno-format-security -Wno-undef -Wno-calloc-transposed-args") |
| 41 | + set(opt_cxx_flags "-Og ${common_flags} ${assert_flags} -Wno-undef -Wno-cast-user-defined -Wno-format -Wno-format-security -Wno-cast-function-type -Wno-deprecated-enum-enum-conversion") |
| 42 | + set(opt_args -DCMAKE_BUILD_TYPE=Debug |
| 43 | + -DCMAKE_C_FLAGS_DEBUG=${opt_c_flags} |
| 44 | + -DCMAKE_CXX_FLAGS_DEBUG=${opt_cxx_flags}) |
| 45 | +elseif(CONFIG_COMPILER_OPTIMIZATION_SIZE) |
| 46 | + # Normally we should use MinSizeRel build type for this, |
| 47 | + # however OpenCV CMake files only handle Debug and Release |
| 48 | + # (see e.g. opencv/cmake/OpenCVCompilerOptions.cmake). |
| 49 | + # So we redefine the flags for Release instead. |
| 50 | + set(opt_c_flags "-Os ${common_flags} ${assert_flags} -Wno-format -Wno-format-security -Wno-undef -Wno-calloc-transposed-args") |
| 51 | + set(opt_cxx_flags "-Os ${common_flags} ${assert_flags} -Wno-undef -Wno-cast-user-defined -Wno-format -Wno-format-security -Wno-cast-function-type -Wno-deprecated-enum-enum-conversion") |
| 52 | + set(opt_args -DCMAKE_BUILD_TYPE=Release |
| 53 | + -DCMAKE_C_FLAGS_RELEASE=${opt_c_flags} |
| 54 | + -DCMAKE_CXX_FLAGS_RELEASE=${opt_cxx_flags}) |
| 55 | +elseif(COMPILER_OPTIMIZATION_PERF) |
| 56 | + # Currently OpenCV fails to compile at -O2 level on Xtensa due to a compiler issue, |
| 57 | + # details in https://github.com/jcmvbkbc/gcc-xtensa/issues/14. |
| 58 | + # Compiling at -O3 happens to be okay. |
| 59 | + set(opt_c_flags "-O3 ${common_flags} ${assert_flags} -Wno-format -Wno-format-security -Wno-undef -Wno-calloc-transposed-args") |
| 60 | + set(opt_cxx_flags "-O3 ${common_flags} ${assert_flags} -Wno-undef -Wno-cast-user-defined -Wno-format -Wno-format-security -Wno-cast-function-type -Wno-deprecated-enum-enum-conversion") |
| 61 | + set(opt_args -DCMAKE_BUILD_TYPE=Release |
| 62 | + -DCMAKE_C_FLAGS_RELEASE=${opt_c_flags} |
| 63 | + -DCMAKE_CXX_FLAGS_RELEASE=${opt_cxx_flags}) |
| 64 | +elseif(COMPILER_OPTIMIZATION_NONE) |
| 65 | + set(opt_c_flags "-O0 ${common_flags} ${assert_flags} -Wno-format -Wno-format-security -Wno-undef -Wno-calloc-transposed-args") |
| 66 | + set(opt_cxx_flags "-O0 ${common_flags} ${assert_flags} -Wno-undef -Wno-cast-user-defined -Wno-format -Wno-format-security -Wno-cast-function-type -Wno-deprecated-enum-enum-conversion") |
| 67 | + set(opt_args -DCMAKE_BUILD_TYPE=Debug |
| 68 | + -DCMAKE_C_FLAGS_DEBUG=${opt_c_flags} |
| 69 | + -DCMAKE_CXX_FLAGS_DEBUG=${opt_cxx_flags}) |
| 70 | +else() |
| 71 | + message(FATAL_ERROR "Unsupported optimization level") |
| 72 | +endif() |
| 73 | + |
| 74 | +# Python to use for OpenCV build |
| 75 | +idf_build_get_property(python PYTHON) |
| 76 | + |
| 77 | +include(ExternalProject) |
| 78 | + |
| 79 | +# Build OpenCV in this directory: |
| 80 | +set(BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/opencv-build) |
| 81 | + |
| 82 | +# List of OpenCV static libraries we expect to see after the build. |
| 83 | +# Obtained manually by looking at ${BINARY_DIR}/install/lib directory. |
| 84 | +# In some cases it's important to have right order of libraries here. |
| 85 | +set(opencv_libraries |
| 86 | + calib3d flann fuzzy |
| 87 | + objdetect tracking video videostab features2d |
| 88 | + surface_matching |
| 89 | + shape stereo stitching structured_light |
| 90 | + line_descriptor ml phase_unwrapping photo plot quality rapid |
| 91 | + imgcodecs imgproc core |
| 92 | +) |
| 93 | + |
| 94 | +set(3rdparty_libraries |
| 95 | + zlib |
| 96 | + libopenjp2 |
| 97 | + libjpeg-turbo |
| 98 | + libpng |
| 99 | + libtiff |
| 100 | + libwebp |
| 101 | +) |
| 102 | + |
| 103 | +# List of all static libraries to be produced |
| 104 | +set(all_libraries) |
| 105 | +set(all_targets) |
| 106 | +foreach(libname ${opencv_libraries}) |
| 107 | + set(lib_path ${BINARY_DIR}/install/lib/libopencv_${libname}.a) |
| 108 | + list(APPEND all_libraries ${lib_path}) |
| 109 | + list(APPEND all_targets opencv_${libname}) |
| 110 | + add_prebuilt_library(opencv_${libname} ${lib_path}) |
| 111 | +endforeach() |
| 112 | +foreach(libname ${3rdparty_libraries}) |
| 113 | + set(lib_path ${BINARY_DIR}/install/lib/opencv4/3rdparty/lib${libname}.a) |
| 114 | + list(APPEND all_libraries ${lib_path}) |
| 115 | + list(APPEND all_targets opencv_${libname}) |
| 116 | + add_prebuilt_library(opencv_${libname} ${lib_path}) |
| 117 | +endforeach() |
| 118 | + |
| 119 | +# Add OpenCV as a subproject. |
| 120 | +ExternalProject_Add(opencv_proj |
| 121 | + SOURCE_DIR ${COMPONENT_DIR}/opencv |
| 122 | + BINARY_DIR ${BINARY_DIR} |
| 123 | + BUILD_BYPRODUCTS ${all_libraries} |
| 124 | + # These two options are set so that Ninja immediately outputs |
| 125 | + # the subproject build to the terminal. Otherwise it looks like the |
| 126 | + # build process "hangs" for too long until OpenCV build is complete. |
| 127 | + USES_TERMINAL_CONFIGURE TRUE |
| 128 | + USES_TERMINAL_BUILD TRUE |
| 129 | + # Arguments to pass to OpenCV CMake invocation: |
| 130 | + CMAKE_ARGS |
| 131 | + -DCMAKE_C_FLAGS=${c_flags} |
| 132 | + -DCMAKE_CXX_FLAGS=${cxx_flags} |
| 133 | + ${opt_args} |
| 134 | + -DCMAKE_INSTALL_PREFIX=${BINARY_DIR}/install |
| 135 | + -DENABLE_CCACHE=${CCACHE_ENABLE} |
| 136 | + -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE} |
| 137 | + -DPYTHON_DEFAULT_EXECUTABLE=${python} |
| 138 | + -DCV_CPU_BASELINE_MODE=${CONFIG_IDF_TARGET} |
| 139 | + -DCV_DISABLE_OPTIMIZATION=0 |
| 140 | + -DOPENCV_EXTRA_MODULES_PATH=${CMAKE_CURRENT_LIST_DIR}/opencv_contrib/modules |
| 141 | + -DOPENCV_FORCE_3RDPARTY_BUILD=1 |
| 142 | + -DCMAKE_SYSTEM_PROCESSOR=${CONFIG_IDF_TARGET_ARCH} |
| 143 | + -DENABLE_PIC=0 |
| 144 | + -DTARGET=${CONFIG_IDF_TARGET} |
| 145 | + -DWITH_ADE=0 |
| 146 | + -DWITH_JPEG=1 |
| 147 | + -DWITH_OPENCL=0 |
| 148 | + -DWITH_OPENEXR=0 |
| 149 | + -DWITH_OPENGL=0 |
| 150 | + -DWITH_OPENMP=0 |
| 151 | + -DWITH_PNG=1 |
| 152 | + -DWITH_PROTOBUF=0 |
| 153 | + -DWITH_TIFF=1 |
| 154 | + -DWITH_WEBP=1 |
| 155 | + -DBUILD_EXAMPLES=0 |
| 156 | + -DBUILD_PACKAGE=1 |
| 157 | + -DBUILD_PERF_TESTS=0 |
| 158 | + -DBUILD_PNG=1 |
| 159 | + -DBUILD_SHARED_LIBS=0 |
| 160 | + -DBUILD_TESTS=0 |
| 161 | + -DBUILD_ZLIB=1 |
| 162 | + -DBUILD_opencv_apps=0 |
| 163 | + -DBUILD_opencv_bgsegm=0 |
| 164 | + -DBUILD_opencv_datasets=0 |
| 165 | + -DBUILD_opencv_imgcodecs=1 |
| 166 | + -DBUILD_opencv_objdetect=1 |
| 167 | + -DBUILD_opencv_imgproc=1 |
| 168 | + -DBUILD_opencv_tracking=1 |
| 169 | + -DBUILD_opencv_video=1 |
| 170 | + -DBUILD_opencv_videoio=0 |
| 171 | +) |
| 172 | + |
| 173 | +# Attach header files to the component library: |
| 174 | +set_target_properties(${COMPONENT_LIB} PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${BINARY_DIR}/install/include/opencv4) |
| 175 | + |
| 176 | +# Make sure the subproject is built before the component library: |
| 177 | +add_dependencies(${COMPONENT_LIB} opencv_proj) |
| 178 | + |
| 179 | + |
| 180 | +foreach(opencv_target ${all_targets}) |
| 181 | + # Attach IDF compoenent dependencies to OpenCV libraries |
| 182 | + foreach(dep ${deps}) |
| 183 | + target_link_libraries(${opencv_target} INTERFACE idf::${dep}) |
| 184 | + endforeach() |
| 185 | + # Attach OpenCV libraries to the component library |
| 186 | + target_link_libraries(${COMPONENT_LIB} INTERFACE ${opencv_target}) |
| 187 | +endforeach() |
0 commit comments