|
| 1 | +set(the_description "Modern Deep Learning module") |
| 2 | + |
| 3 | +if(${CMAKE_VERSION} VERSION_LESS 3.2) |
| 4 | + message(STATUS "Module opencv_dnn_modern disabled because CMake version is less than 3.2") |
| 5 | + ocv_module_disable(dnn_modern) |
| 6 | + return() |
| 7 | +endif() |
| 8 | + |
| 9 | +cmake_policy(SET CMP0028 OLD) |
| 10 | + |
| 11 | +# Using cmake scripts and modules |
| 12 | +list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake) |
| 13 | + |
| 14 | +# ---------------------------------------------------------------------------- |
| 15 | +# MODULE REQUIREMENTS |
| 16 | +# ---------------------------------------------------------------------------- |
| 17 | + |
| 18 | +find_package(TinyDNN QUIET) |
| 19 | + |
| 20 | +include(CheckCXXCompilerFlag) |
| 21 | +CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11) |
| 22 | + |
| 23 | +if(NOT TinyDNN_FOUND) |
| 24 | + message(STATUS "Module opencv_dnn_modern disabled because tiny-dnn was not found") |
| 25 | + ocv_module_disable(dnn_modern) |
| 26 | + return() |
| 27 | +elseif(NOT COMPILER_SUPPORTS_CXX11) |
| 28 | + message(STATUS "Module opencv_dnn_modern disabled because your compiler does not support C++11") |
| 29 | + ocv_module_disable(dnn_modern) |
| 30 | + return() |
| 31 | +elseif(APPLE_FRAMEWORK OR ANDROID) |
| 32 | + message(STATUS "Module opencv_dnn_modern disabled because you are not under Linux or Win") |
| 33 | + ocv_module_disable(dnn_modern) |
| 34 | + return() |
| 35 | +endif() |
| 36 | + |
| 37 | +# ---------------------------------------------------------------------------- |
| 38 | +# OPTIMIZATION OPTIONS |
| 39 | +# ---------------------------------------------------------------------------- |
| 40 | + |
| 41 | +option(TINYDNN_USE_SSE "Build tiny-dnn with SSE library support" ON) |
| 42 | +option(TINYDNN_USE_AVX "Build tiny-dnn with AVX library support" ON) |
| 43 | +option(TINYDNN_USE_TBB "Build tiny-dnn with TBB library support" OFF) |
| 44 | +option(TINYDNN_USE_OMP "Build tiny-dnn with OMP library support" OFF) |
| 45 | +option(TINYDNN_USE_NNPACK "Build tiny-dnn with NNPACK library support" OFF) |
| 46 | + |
| 47 | +if(TINYDNN_USE_TBB AND HAVE_TBB) |
| 48 | + add_definitions(-DCNN_USE_TBB) |
| 49 | +elseif(NOT TINYDNN_USE_TBB AND |
| 50 | + TINYDNN_USE_OMP AND HAVE_OPENMP) |
| 51 | + add_definitions(-DCNN_USE_OMP) |
| 52 | +endif() |
| 53 | + |
| 54 | +if(TINYDNN_USE_NNPACK) |
| 55 | + find_package(NNPACK REQUIRED) |
| 56 | + add_definitions(-DCNN_USE_NNPACK) |
| 57 | + include_directories(SYSTEM ${NNPACK_INCLUDE_DIR}) |
| 58 | + include_directories(SYSTEM ${NNPACK_INCLUDE_DIR}/../third-party/pthreadpool/include) |
| 59 | + list(APPEND REQUIRED_LIBRARIES ${NNPACK_LIB}) |
| 60 | +endif() |
| 61 | + |
| 62 | +# we need to disable seializer unless we import cereal |
| 63 | +add_definitions(-DCNN_NO_SERIALIZATION) |
| 64 | + |
| 65 | +# NOTE: In case that proto files already exist, |
| 66 | +# this is not needed anymore. |
| 67 | +find_package(Protobuf) |
| 68 | +list(APPEND REQUIRED_LIBRARIES ${PROTOBUF_LIBRARIES}) |
| 69 | + |
| 70 | +#### |
| 71 | +# Setup the compiler options |
| 72 | + |
| 73 | +# set c++ standard to c++11. |
| 74 | +# Note: not working on CMake 2.8. We assume that user has |
| 75 | +# a compiler with C++11 support. |
| 76 | + |
| 77 | +set(CMAKE_CXX_STANDARD 11) |
| 78 | +set(CMAKE_CXX_STANDARD_REQUIRED ON) |
| 79 | +message(STATUS "C++11 support has been enabled by default.") |
| 80 | + |
| 81 | +# Unix |
| 82 | +if(CMAKE_COMPILER_IS_GNUCXX OR MINGW OR |
| 83 | + CMAKE_CXX_COMPILER_ID MATCHES "Clang") |
| 84 | + include(CheckCXXCompilerFlag) |
| 85 | + check_cxx_compiler_flag("-msse3" COMPILER_HAS_SSE_FLAG) |
| 86 | + check_cxx_compiler_flag("-mavx" COMPILER_HAS_AVX_FLAG) |
| 87 | + check_cxx_compiler_flag("-mavx2" COMPILER_HAS_AVX2_FLAG) |
| 88 | + check_cxx_compiler_flag("-mfma" COMPILER_HAS_AVX2_FLAG) |
| 89 | + |
| 90 | + # set Streaming SIMD Extension (SSE) instructions |
| 91 | + if(USE_SSE AND COMPILER_HAS_SSE_FLAG) |
| 92 | + add_definitions(-DCNN_USE_SSE) |
| 93 | + set(EXTRA_C_FLAGS "${EXTRA_C_FLAGS} -msse3") |
| 94 | + endif(USE_SSE AND COMPILER_HAS_SSE_FLAG) |
| 95 | + # set Advanced Vector Extensions (AVX) |
| 96 | + if(USE_AVX AND COMPILER_HAS_AVX_FLAG) |
| 97 | + add_definitions(-DCNN_USE_AVX) |
| 98 | + set(EXTRA_C_FLAGS "${EXTRA_C_FLAGS} -mavx") |
| 99 | + endif(USE_AVX AND COMPILER_HAS_AVX_FLAG) |
| 100 | + # set Advanced Vector Extensions 2 (AVX2) |
| 101 | + if(USE_AVX2 AND COMPILER_HAS_AVX2_FLAG) |
| 102 | + add_definitions(-DCNN_USE_AVX2) |
| 103 | + set(EXTRA_C_FLAGS "${EXTRA_C_FLAGS} -mavx2 -mfma -march=core-avx2") |
| 104 | + endif(USE_AVX2 AND COMPILER_HAS_AVX2_FLAG) |
| 105 | + |
| 106 | + # include extra flags to the compiler |
| 107 | + # TODO: add info about those flags. |
| 108 | + set(EXTRA_C_FLAGS "${EXTRA_C_FLAGS} -Wall -Wpedantic -Wno-narrowing") |
| 109 | + set(EXTRA_C_FLAGS_RELEASE "${EXTRA_C_FLAGS_RELEASE} -O3") |
| 110 | + set(EXTRA_C_FLAGS_DEBUG "${EXTRA_C_FLAGS_DEBUG} -g3 -pthread") |
| 111 | +elseif(MSVC) |
| 112 | + if(USE_SSE) |
| 113 | + add_definitions(-DCNN_USE_SSE) |
| 114 | + set(EXTRA_C_FLAGS "${EXTRA_C_FLAGS} /arch:SSE2") |
| 115 | + endif(USE_SSE) |
| 116 | + if(USE_AVX) |
| 117 | + add_definitions(-DCNN_USE_AVX) |
| 118 | + set(EXTRA_C_FLAGS "${EXTRA_C_FLAGS} /arch:AVX") |
| 119 | + endif(USE_AVX) |
| 120 | + if(USE_AVX2) |
| 121 | + add_definitions(-DCNN_USE_AVX2) |
| 122 | + set(EXTRA_C_FLAGS "${EXTRA_C_FLAGS} /arch:AVX2") |
| 123 | + endif(USE_AVX2) |
| 124 | + # include specific flags for release and debug modes. |
| 125 | + set(EXTRA_C_FLAGS_RELEASE "${EXTRA_C_FLAGS_RELEASE} |
| 126 | + /Ox /Oi /Ot /Oy /GL /fp:fast /GS-") |
| 127 | + set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /LTCG") |
| 128 | + set(EXTRA_C_FLAGS_DEBUG "${EXTRA_C_FLAGS_DEBUG}") |
| 129 | + set(EXTRA_C_FLAGS "${EXTRA_C_FLAGS} /W4 /bigobj") |
| 130 | + # this is fine |
| 131 | + add_definitions(-D _CRT_SECURE_NO_WARNINGS) |
| 132 | + add_definitions(-D _SCL_SECURE_NO_WARNINGS) |
| 133 | + # prolly powerless with header-only project |
| 134 | + set(EXTRA_C_FLAGS "${EXTRA_C_FLAGS} /MP") |
| 135 | +endif() |
| 136 | + |
| 137 | +# ---------------------------------------------------------------------------- |
| 138 | +# DNN-MODERN MODULE |
| 139 | +# ---------------------------------------------------------------------------- |
| 140 | + |
| 141 | +ocv_define_module(dnn_modern opencv_core opencv_imgproc opencv_imgcodecs WRAP python) |
| 142 | +ocv_target_link_libraries(${the_module} ${REQUIRED_LIBRARIES}) |
| 143 | +ocv_target_include_directories(${the_module} ${TINYDNN_INCLUDE_DIRS}) |
| 144 | +target_compile_options(${the_module} PRIVATE "-Wno-error=non-virtual-dtor") |
0 commit comments