CMakeLists.txt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. cmake_minimum_required(VERSION 3.14)
  2. project(ppocr CXX)
  3. set(DEMO_NAME "ppocr")
  4. set(CMAKE_CXX_STANDARD 11)
  5. set(CMAKE_CXX_STANDARD_REQUIRED True)
  6. option(WITH_MKL "Compile demo with MKL/OpenBlas support, default use MKL." ON)
  7. option(WITH_GPU "Compile demo with GPU/CPU, default use CPU." OFF)
  8. option(WITH_STATIC_LIB "Compile demo with static/shared library, default use static." ON)
  9. option(USE_FREETYPE "Enable FreeType support" OFF)
  10. SET(PADDLE_LIB "" CACHE PATH "Location of libraries")
  11. SET(OPENCV_DIR "" CACHE PATH "Location of libraries")
  12. SET(CUDA_LIB "" CACHE PATH "Location of libraries")
  13. SET(CUDNN_LIB "" CACHE PATH "Location of libraries")
  14. macro(safe_set_static_flag)
  15. foreach(flag_var
  16. CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
  17. CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
  18. if(${${flag_var}} MATCHES "/MD")
  19. string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
  20. endif()
  21. endforeach(flag_var)
  22. endmacro()
  23. if (WITH_MKL)
  24. ADD_DEFINITIONS(-DUSE_MKL)
  25. endif()
  26. if(NOT DEFINED PADDLE_LIB)
  27. message(FATAL_ERROR "please set PADDLE_LIB with -DPADDLE_LIB=/path/paddle/lib")
  28. endif()
  29. if(NOT DEFINED OPENCV_DIR)
  30. message(FATAL_ERROR "please set OPENCV_DIR with -DOPENCV_DIR=/path/opencv")
  31. endif()
  32. if (WIN32)
  33. include_directories("${PADDLE_LIB}/paddle/include")
  34. link_directories("${PADDLE_LIB}/paddle/lib")
  35. set(CMAKE_CONFIGURATION_TYPES "Debug;Release" CACHE STRING "" FORCE)
  36. set(OpenCV_DIR "${OPENCV_DIR}/x64/vc16/lib")
  37. find_package(OpenCV REQUIRED )
  38. if(USE_FREETYPE)
  39. if(NOT "opencv_freetype" IN_LIST OpenCV_LIBS)
  40. message(FATAL_ERROR "OpenCV was not compiled with the freetype module (opencv_freetype) !")
  41. endif()
  42. add_definitions(-DUSE_FREETYPE)
  43. endif()
  44. else ()
  45. set(OpenCV_DIR "${OPENCV_DIR}/lib64/cmake/opencv4")
  46. find_package(OpenCV REQUIRED)
  47. if(USE_FREETYPE)
  48. if(NOT "opencv_freetype" IN_LIST OpenCV_LIBS)
  49. message(FATAL_ERROR "OpenCV was not compiled with the freetype module (opencv_freetype) !")
  50. endif()
  51. add_definitions(-DUSE_FREETYPE)
  52. endif()
  53. include_directories("${PADDLE_LIB}/paddle/include")
  54. link_directories("${PADDLE_LIB}/paddle/lib")
  55. endif ()
  56. include_directories(${OpenCV_INCLUDE_DIRS})
  57. if (WIN32)
  58. add_definitions("/DGOOGLE_GLOG_DLL_DECL=")
  59. if(WITH_MKL)
  60. set(FLAG_OPENMP "/openmp")
  61. endif()
  62. set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /bigobj /MTd ${FLAG_OPENMP}")
  63. set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /bigobj /MT ${FLAG_OPENMP}")
  64. set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /bigobj /MTd ${FLAG_OPENMP}")
  65. set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /bigobj /MT ${FLAG_OPENMP}")
  66. if (WITH_STATIC_LIB)
  67. safe_set_static_flag()
  68. add_definitions(-DSTATIC_LIB)
  69. add_definitions(-DYAML_CPP_STATIC_DEFINE)
  70. endif()
  71. message("cmake c debug flags " ${CMAKE_C_FLAGS_DEBUG})
  72. message("cmake c release flags " ${CMAKE_C_FLAGS_RELEASE})
  73. message("cmake cxx debug flags " ${CMAKE_CXX_FLAGS_DEBUG})
  74. message("cmake cxx release flags " ${CMAKE_CXX_FLAGS_RELEASE})
  75. else()
  76. if(WITH_MKL)
  77. set(FLAG_OPENMP "-fopenmp")
  78. endif()
  79. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -O3 ${FLAG_OPENMP} -std=c++11")
  80. set(CMAKE_STATIC_LIBRARY_PREFIX "")
  81. message("cmake cxx flags" ${CMAKE_CXX_FLAGS})
  82. endif()
  83. if (WITH_GPU)
  84. if (NOT DEFINED CUDA_LIB OR ${CUDA_LIB} STREQUAL "")
  85. message(FATAL_ERROR "please set CUDA_LIB with -DCUDA_LIB=/path/cuda-8.0/lib64")
  86. endif()
  87. if (NOT WIN32)
  88. if (NOT DEFINED CUDNN_LIB)
  89. message(FATAL_ERROR "please set CUDNN_LIB with -DCUDNN_LIB=/path/cudnn_v7.4/cuda/lib64")
  90. endif()
  91. add_definitions(-DWITH_GPU)
  92. endif(NOT WIN32)
  93. endif()
  94. include_directories("${PADDLE_LIB}/third_party/install/protobuf/include")
  95. include_directories("${PADDLE_LIB}/third_party/install/glog/include")
  96. include_directories("${PADDLE_LIB}/third_party/install/gflags/include")
  97. include_directories("${PADDLE_LIB}/third_party/install/xxhash/include")
  98. include_directories("${PADDLE_LIB}/third_party/install/zlib/include")
  99. include_directories("${PADDLE_LIB}/third_party/install/onnxruntime/include")
  100. include_directories("${PADDLE_LIB}/third_party/install/paddle2onnx/include")
  101. include_directories("${PADDLE_LIB}/third_party/install/yaml-cpp/include")
  102. include_directories("${PADDLE_LIB}/third_party/install/openvino/include")
  103. include_directories("${PADDLE_LIB}/third_party/install/tbb/include")
  104. include_directories("${PADDLE_LIB}/third_party/boost")
  105. include_directories("${PADDLE_LIB}/third_party/eigen3")
  106. include_directories("${PADDLE_LIB}/paddle/include/")
  107. include_directories("${CMAKE_SOURCE_DIR}/")
  108. link_directories("${PADDLE_LIB}/third_party/install/zlib/lib")
  109. link_directories("${PADDLE_LIB}/third_party/install/protobuf/lib")
  110. link_directories("${PADDLE_LIB}/third_party/install/glog/lib")
  111. link_directories("${PADDLE_LIB}/third_party/install/gflags/lib")
  112. link_directories("${PADDLE_LIB}/third_party/install/xxhash/lib")
  113. link_directories("${PADDLE_LIB}/third_party/install/onnxruntime/lib")
  114. link_directories("${PADDLE_LIB}/third_party/install/paddle2onnx/lib")
  115. link_directories("${PADDLE_LIB}/third_party/install/yaml-cpp/lib")
  116. link_directories("${PADDLE_LIB}/third_party/install/openvino/intel64")
  117. link_directories("${PADDLE_LIB}/third_party/install/tbb/lib")
  118. link_directories("${PADDLE_LIB}/paddle/lib")
  119. if(WITH_MKL)
  120. include_directories("${PADDLE_LIB}/third_party/install/mklml/include")
  121. if (WIN32)
  122. set(MATH_LIB ${PADDLE_LIB}/third_party/install/mklml/lib/mklml.lib
  123. ${PADDLE_LIB}/third_party/install/mklml/lib/libiomp5md.lib)
  124. else ()
  125. set(MATH_LIB ${PADDLE_LIB}/third_party/install/mklml/lib/libmklml_intel${CMAKE_SHARED_LIBRARY_SUFFIX}
  126. ${PADDLE_LIB}/third_party/install/mklml/lib/libiomp5${CMAKE_SHARED_LIBRARY_SUFFIX})
  127. execute_process(COMMAND cp -r ${PADDLE_LIB}/third_party/install/mklml/lib/libmklml_intel${CMAKE_SHARED_LIBRARY_SUFFIX} /usr/lib)
  128. endif ()
  129. set(MKLDNN_PATH "${PADDLE_LIB}/third_party/install/onednn")
  130. if(EXISTS ${MKLDNN_PATH})
  131. include_directories("${MKLDNN_PATH}/include")
  132. if (WIN32)
  133. set(MKLDNN_LIB ${MKLDNN_PATH}/lib/mkldnn.lib)
  134. else ()
  135. set(MKLDNN_LIB ${MKLDNN_PATH}/lib/libdnnl.so.3)
  136. endif ()
  137. endif()
  138. else()
  139. if (WIN32)
  140. set(MATH_LIB ${PADDLE_LIB}/third_party/install/openblas/lib/openblas${CMAKE_STATIC_LIBRARY_SUFFIX})
  141. else ()
  142. set(MATH_LIB ${PADDLE_LIB}/third_party/install/openblas/lib/libopenblas${CMAKE_STATIC_LIBRARY_SUFFIX})
  143. endif ()
  144. endif()
  145. # Note: libpaddle_inference_api.so/a must put before libpaddle_inference.so/a
  146. if(WITH_STATIC_LIB)
  147. if(WIN32)
  148. set(DEPS
  149. ${PADDLE_LIB}/paddle/lib/paddle_inference${CMAKE_STATIC_LIBRARY_SUFFIX})
  150. else()
  151. set(DEPS
  152. ${PADDLE_LIB}/paddle/lib/libpaddle_inference${CMAKE_STATIC_LIBRARY_SUFFIX})
  153. endif()
  154. else()
  155. if(WIN32)
  156. set(DEPS
  157. ${PADDLE_LIB}/paddle/lib/paddle_inference${CMAKE_SHARED_LIBRARY_SUFFIX})
  158. else()
  159. set(DEPS
  160. ${PADDLE_LIB}/paddle/lib/libpaddle_inference${CMAKE_SHARED_LIBRARY_SUFFIX})
  161. endif()
  162. endif(WITH_STATIC_LIB)
  163. if (NOT WIN32)
  164. set(DEPS ${DEPS}
  165. ${MATH_LIB} ${MKLDNN_LIB}
  166. glog gflags protobuf z xxhash
  167. )
  168. if(EXISTS "${PADDLE_LIB}/third_party/install/snappystream/lib")
  169. set(DEPS ${DEPS} snappystream)
  170. endif()
  171. if (EXISTS "${PADDLE_LIB}/third_party/install/snappy/lib")
  172. set(DEPS ${DEPS} snappy)
  173. endif()
  174. else()
  175. set(DEPS ${DEPS}
  176. ${MATH_LIB} ${MKLDNN_LIB}
  177. glog gflags_static libprotobuf xxhash)
  178. set(DEPS ${DEPS} libcmt shlwapi)
  179. if (EXISTS "${PADDLE_LIB}/third_party/install/snappy/lib")
  180. set(DEPS ${DEPS} snappy)
  181. endif()
  182. if(EXISTS "${PADDLE_LIB}/third_party/install/snappystream/lib")
  183. set(DEPS ${DEPS} snappystream)
  184. endif()
  185. endif(NOT WIN32)
  186. if (EXISTS "${PADDLE_LIB}/third_party/install/yaml-cpp/lib")
  187. set(DEPS ${DEPS} yaml-cpp)
  188. endif()
  189. if(WITH_GPU)
  190. if(NOT WIN32)
  191. set(DEPS ${DEPS} ${CUDA_LIB}/libcudart${CMAKE_SHARED_LIBRARY_SUFFIX})
  192. set(DEPS ${DEPS} ${CUDNN_LIB}/libcudnn${CMAKE_SHARED_LIBRARY_SUFFIX})
  193. else()
  194. set(DEPS ${DEPS} ${CUDA_LIB}/cudart${CMAKE_STATIC_LIBRARY_SUFFIX} )
  195. message($DEPS)
  196. set(DEPS ${DEPS} ${CUDA_LIB}/cublas${CMAKE_STATIC_LIBRARY_SUFFIX} )
  197. set(DEPS ${DEPS} ${CUDNN_LIB}/cudnn${CMAKE_STATIC_LIBRARY_SUFFIX})
  198. endif()
  199. endif()
  200. if (NOT WIN32)
  201. set(EXTERNAL_LIB "-ldl -lrt -lgomp -lz -lm -lpthread")
  202. set(DEPS ${DEPS} ${EXTERNAL_LIB})
  203. endif()
  204. set(THIRD_PARTY_PATH ${CMAKE_CURRENT_LIST_DIR}/third_party)
  205. function(download_and_decompress url filename decompress_dir)
  206. if(NOT EXISTS "${filename}" AND NOT EXISTS "${decompress_dir}")
  207. message("Downloading file from ${url} to ${filename} ...")
  208. file(DOWNLOAD ${url} "${filename}.tmp" SHOW_PROGRESS)
  209. file(RENAME "${filename}.tmp" ${filename})
  210. endif()
  211. if(NOT EXISTS ${decompress_dir})
  212. file(MAKE_DIRECTORY ${decompress_dir})
  213. message("Decompress file ${filename} ...")
  214. execute_process(COMMAND ${CMAKE_COMMAND} -E tar -xf ${filename} WORKING_DIRECTORY ${decompress_dir})
  215. endif()
  216. endfunction()
  217. set(PACKAGE_LIST abseil-cpp clipper_ver6.4.2 nlohmann)
  218. foreach(PKG ${PACKAGE_LIST})
  219. set(PKG_URL "https://paddle-model-ecology.bj.bcebos.com/paddlex/cpp/libs/${PKG}.tgz")
  220. set(PKG_TGZ_PATH "${CMAKE_CURRENT_BINARY_DIR}/${PKG}.tgz")
  221. set(PKG_DST_PATH "${THIRD_PARTY_PATH}/${PKG}")
  222. download_and_decompress(${PKG_URL} ${PKG_TGZ_PATH} ${PKG_DST_PATH})
  223. endforeach()
  224. add_subdirectory(third_party/abseil-cpp)
  225. add_subdirectory(third_party/clipper_ver6.4.2/cpp)
  226. include_directories(${POLYCLIPPING_INCLUDE_DIR})
  227. set(DEPS ${DEPS} ${OpenCV_LIBS})
  228. set(DEPS ${DEPS} absl::statusor)
  229. set(DEPS ${DEPS} polyclipping)
  230. if(UNIX)
  231. find_package(Iconv REQUIRED)
  232. endif()
  233. file(GLOB_RECURSE SRC_LIST "./src/*.cc")
  234. set(SRCS cli.cc )
  235. add_executable(${DEMO_NAME} ${SRCS} ${SRC_LIST} )
  236. target_link_libraries(${DEMO_NAME} ${DEPS} )
  237. if (WIN32 AND WITH_MKL)
  238. add_custom_command(TARGET ${DEMO_NAME} POST_BUILD
  239. COMMAND ${CMAKE_COMMAND} -E copy_if_different ${PADDLE_LIB}/third_party/install/mklml/lib/mklml.dll ./mklml.dll
  240. COMMAND ${CMAKE_COMMAND} -E copy_if_different ${PADDLE_LIB}/third_party/install/mklml/lib/libiomp5md.dll ./libiomp5md.dll
  241. COMMAND ${CMAKE_COMMAND} -E copy_if_different ${PADDLE_LIB}/third_party/install/onednn/lib/mkldnn.dll ./mkldnn.dll
  242. COMMAND ${CMAKE_COMMAND} -E copy_if_different ${PADDLE_LIB}/third_party/install/mklml/lib/mklml.dll ./release/mklml.dll
  243. COMMAND ${CMAKE_COMMAND} -E copy_if_different ${PADDLE_LIB}/third_party/install/mklml/lib/libiomp5md.dll ./release/libiomp5md.dll
  244. COMMAND ${CMAKE_COMMAND} -E copy_if_different ${PADDLE_LIB}/third_party/install/onednn/lib/mkldnn.dll ./release/mkldnn.dll
  245. )
  246. endif()