CMakeLists.txt 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. project(ppocr CXX C)
  2. cmake_minimum_required(VERSION 3.14)
  3. option(WITH_MKL "Compile demo with MKL/OpenBlas support, default use MKL." ON)
  4. option(WITH_GPU "Compile demo with GPU/CPU, default use CPU." OFF)
  5. option(WITH_STATIC_LIB "Compile demo with static/shared library, default use static." ON)
  6. option(WITH_TENSORRT "Compile demo with TensorRT." OFF)
  7. SET(PADDLE_LIB "" CACHE PATH "Location of libraries")
  8. SET(OPENCV_DIR "" CACHE PATH "Location of libraries")
  9. SET(CUDA_LIB "" CACHE PATH "Location of libraries")
  10. SET(CUDNN_LIB "" CACHE PATH "Location of libraries")
  11. SET(TENSORRT_DIR "" CACHE PATH "Compile demo with TensorRT")
  12. set(DEMO_NAME "ppocr")
  13. macro(safe_set_static_flag)
  14. foreach(flag_var
  15. CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
  16. CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
  17. if(${flag_var} MATCHES "/MD")
  18. string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
  19. endif(${flag_var} MATCHES "/MD")
  20. endforeach(flag_var)
  21. endmacro()
  22. if (WITH_MKL)
  23. ADD_DEFINITIONS(-DUSE_MKL)
  24. endif()
  25. if(NOT DEFINED PADDLE_LIB)
  26. message(FATAL_ERROR "please set PADDLE_LIB with -DPADDLE_LIB=/path/paddle/lib")
  27. endif()
  28. if(NOT DEFINED OPENCV_DIR)
  29. message(FATAL_ERROR "please set OPENCV_DIR with -DOPENCV_DIR=/path/opencv")
  30. endif()
  31. if (WIN32)
  32. include_directories("${PADDLE_LIB}/paddle/include")
  33. link_directories("${PADDLE_LIB}/paddle/lib")
  34. find_package(OpenCV REQUIRED PATHS ${OPENCV_DIR}/build/ NO_DEFAULT_PATH)
  35. else ()
  36. find_package(OpenCV REQUIRED PATHS ${OPENCV_DIR}/share/OpenCV NO_DEFAULT_PATH)
  37. include_directories("${PADDLE_LIB}/paddle/include")
  38. link_directories("${PADDLE_LIB}/paddle/lib")
  39. endif ()
  40. include_directories(${OpenCV_INCLUDE_DIRS})
  41. if (WIN32)
  42. add_definitions("/DGOOGLE_GLOG_DLL_DECL=")
  43. if(WITH_MKL)
  44. set(FLAG_OPENMP "/openmp")
  45. endif()
  46. set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /bigobj /MTd ${FLAG_OPENMP}")
  47. set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /bigobj /MT ${FLAG_OPENMP}")
  48. set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /bigobj /MTd ${FLAG_OPENMP}")
  49. set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /bigobj /MT ${FLAG_OPENMP}")
  50. if (WITH_STATIC_LIB)
  51. safe_set_static_flag()
  52. add_definitions(-DSTATIC_LIB)
  53. endif()
  54. message("cmake c debug flags " ${CMAKE_C_FLAGS_DEBUG})
  55. message("cmake c release flags " ${CMAKE_C_FLAGS_RELEASE})
  56. message("cmake cxx debug flags " ${CMAKE_CXX_FLAGS_DEBUG})
  57. message("cmake cxx release flags " ${CMAKE_CXX_FLAGS_RELEASE})
  58. else()
  59. if(WITH_MKL)
  60. set(FLAG_OPENMP "-fopenmp")
  61. endif()
  62. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -o3 ${FLAG_OPENMP} -std=c++11")
  63. set(CMAKE_STATIC_LIBRARY_PREFIX "")
  64. message("cmake cxx flags" ${CMAKE_CXX_FLAGS})
  65. endif()
  66. if (WITH_GPU)
  67. if (NOT DEFINED CUDA_LIB OR ${CUDA_LIB} STREQUAL "")
  68. message(FATAL_ERROR "please set CUDA_LIB with -DCUDA_LIB=/path/cuda-8.0/lib64")
  69. endif()
  70. if (NOT WIN32)
  71. if (NOT DEFINED CUDNN_LIB)
  72. message(FATAL_ERROR "please set CUDNN_LIB with -DCUDNN_LIB=/path/cudnn_v7.4/cuda/lib64")
  73. endif()
  74. endif(NOT WIN32)
  75. endif()
  76. include_directories("${PADDLE_LIB}/third_party/install/protobuf/include")
  77. include_directories("${PADDLE_LIB}/third_party/install/glog/include")
  78. include_directories("${PADDLE_LIB}/third_party/install/gflags/include")
  79. include_directories("${PADDLE_LIB}/third_party/install/xxhash/include")
  80. include_directories("${PADDLE_LIB}/third_party/install/zlib/include")
  81. include_directories("${PADDLE_LIB}/third_party/install/onnxruntime/include")
  82. include_directories("${PADDLE_LIB}/third_party/install/paddle2onnx/include")
  83. include_directories("${PADDLE_LIB}/third_party/boost")
  84. include_directories("${PADDLE_LIB}/third_party/eigen3")
  85. include_directories("${CMAKE_SOURCE_DIR}/")
  86. if (NOT WIN32)
  87. if (WITH_TENSORRT AND WITH_GPU)
  88. include_directories("${TENSORRT_DIR}/include")
  89. link_directories("${TENSORRT_DIR}/lib")
  90. endif()
  91. endif(NOT WIN32)
  92. link_directories("${PADDLE_LIB}/third_party/install/zlib/lib")
  93. link_directories("${PADDLE_LIB}/third_party/install/protobuf/lib")
  94. link_directories("${PADDLE_LIB}/third_party/install/glog/lib")
  95. link_directories("${PADDLE_LIB}/third_party/install/gflags/lib")
  96. link_directories("${PADDLE_LIB}/third_party/install/xxhash/lib")
  97. link_directories("${PADDLE_LIB}/third_party/install/onnxruntime/lib")
  98. link_directories("${PADDLE_LIB}/third_party/install/paddle2onnx/lib")
  99. link_directories("${PADDLE_LIB}/paddle/lib")
  100. if(WITH_MKL)
  101. include_directories("${PADDLE_LIB}/third_party/install/mklml/include")
  102. if (WIN32)
  103. set(MATH_LIB ${PADDLE_LIB}/third_party/install/mklml/lib/mklml.lib
  104. ${PADDLE_LIB}/third_party/install/mklml/lib/libiomp5md.lib)
  105. else ()
  106. set(MATH_LIB ${PADDLE_LIB}/third_party/install/mklml/lib/libmklml_intel${CMAKE_SHARED_LIBRARY_SUFFIX}
  107. ${PADDLE_LIB}/third_party/install/mklml/lib/libiomp5${CMAKE_SHARED_LIBRARY_SUFFIX})
  108. execute_process(COMMAND cp -r ${PADDLE_LIB}/third_party/install/mklml/lib/libmklml_intel${CMAKE_SHARED_LIBRARY_SUFFIX} /usr/lib)
  109. endif ()
  110. set(MKLDNN_PATH "${PADDLE_LIB}/third_party/install/mkldnn")
  111. if(EXISTS ${MKLDNN_PATH})
  112. include_directories("${MKLDNN_PATH}/include")
  113. if (WIN32)
  114. set(MKLDNN_LIB ${MKLDNN_PATH}/lib/mkldnn.lib)
  115. else ()
  116. set(MKLDNN_LIB ${MKLDNN_PATH}/lib/libmkldnn.so.0)
  117. endif ()
  118. endif()
  119. else()
  120. if (WIN32)
  121. set(MATH_LIB ${PADDLE_LIB}/third_party/install/openblas/lib/openblas${CMAKE_STATIC_LIBRARY_SUFFIX})
  122. else ()
  123. set(MATH_LIB ${PADDLE_LIB}/third_party/install/openblas/lib/libopenblas${CMAKE_STATIC_LIBRARY_SUFFIX})
  124. endif ()
  125. endif()
  126. # Note: libpaddle_inference_api.so/a must put before libpaddle_inference.so/a
  127. if(WITH_STATIC_LIB)
  128. if(WIN32)
  129. set(DEPS
  130. ${PADDLE_LIB}/paddle/lib/paddle_inference${CMAKE_STATIC_LIBRARY_SUFFIX})
  131. else()
  132. set(DEPS
  133. ${PADDLE_LIB}/paddle/lib/libpaddle_inference${CMAKE_STATIC_LIBRARY_SUFFIX})
  134. endif()
  135. else()
  136. if(WIN32)
  137. set(DEPS
  138. ${PADDLE_LIB}/paddle/lib/paddle_inference${CMAKE_SHARED_LIBRARY_SUFFIX})
  139. else()
  140. set(DEPS
  141. ${PADDLE_LIB}/paddle/lib/libpaddle_inference${CMAKE_SHARED_LIBRARY_SUFFIX})
  142. endif()
  143. endif(WITH_STATIC_LIB)
  144. if (NOT WIN32)
  145. set(DEPS ${DEPS}
  146. ${MATH_LIB} ${MKLDNN_LIB}
  147. glog gflags protobuf z xxhash
  148. )
  149. if(EXISTS "${PADDLE_LIB}/third_party/install/snappystream/lib")
  150. set(DEPS ${DEPS} snappystream)
  151. endif()
  152. if (EXISTS "${PADDLE_LIB}/third_party/install/snappy/lib")
  153. set(DEPS ${DEPS} snappy)
  154. endif()
  155. else()
  156. set(DEPS ${DEPS}
  157. ${MATH_LIB} ${MKLDNN_LIB}
  158. glog gflags_static libprotobuf xxhash)
  159. set(DEPS ${DEPS} libcmt shlwapi)
  160. if (EXISTS "${PADDLE_LIB}/third_party/install/snappy/lib")
  161. set(DEPS ${DEPS} snappy)
  162. endif()
  163. if(EXISTS "${PADDLE_LIB}/third_party/install/snappystream/lib")
  164. set(DEPS ${DEPS} snappystream)
  165. endif()
  166. endif(NOT WIN32)
  167. if(WITH_GPU)
  168. if(NOT WIN32)
  169. if (WITH_TENSORRT)
  170. set(DEPS ${DEPS} ${TENSORRT_DIR}/lib/libnvinfer${CMAKE_SHARED_LIBRARY_SUFFIX})
  171. set(DEPS ${DEPS} ${TENSORRT_DIR}/lib/libnvinfer_plugin${CMAKE_SHARED_LIBRARY_SUFFIX})
  172. endif()
  173. set(DEPS ${DEPS} ${CUDA_LIB}/libcudart${CMAKE_SHARED_LIBRARY_SUFFIX})
  174. set(DEPS ${DEPS} ${CUDNN_LIB}/libcudnn${CMAKE_SHARED_LIBRARY_SUFFIX})
  175. else()
  176. set(DEPS ${DEPS} ${CUDA_LIB}/cudart${CMAKE_STATIC_LIBRARY_SUFFIX} )
  177. set(DEPS ${DEPS} ${CUDA_LIB}/cublas${CMAKE_STATIC_LIBRARY_SUFFIX} )
  178. set(DEPS ${DEPS} ${CUDNN_LIB}/cudnn${CMAKE_STATIC_LIBRARY_SUFFIX})
  179. endif()
  180. endif()
  181. if (NOT WIN32)
  182. set(EXTERNAL_LIB "-ldl -lrt -lgomp -lz -lm -lpthread")
  183. set(DEPS ${DEPS} ${EXTERNAL_LIB})
  184. endif()
  185. set(DEPS ${DEPS} ${OpenCV_LIBS})
  186. include(FetchContent)
  187. include(external-cmake/auto-log.cmake)
  188. include_directories(${FETCHCONTENT_BASE_DIR}/extern_autolog-src)
  189. AUX_SOURCE_DIRECTORY(./src SRCS)
  190. add_executable(${DEMO_NAME} ${SRCS})
  191. target_link_libraries(${DEMO_NAME} ${DEPS})
  192. if (WIN32 AND WITH_MKL)
  193. add_custom_command(TARGET ${DEMO_NAME} POST_BUILD
  194. COMMAND ${CMAKE_COMMAND} -E copy_if_different ${PADDLE_LIB}/third_party/install/mklml/lib/mklml.dll ./mklml.dll
  195. COMMAND ${CMAKE_COMMAND} -E copy_if_different ${PADDLE_LIB}/third_party/install/mklml/lib/libiomp5md.dll ./libiomp5md.dll
  196. COMMAND ${CMAKE_COMMAND} -E copy_if_different ${PADDLE_LIB}/third_party/install/mkldnn/lib/mkldnn.dll ./mkldnn.dll
  197. COMMAND ${CMAKE_COMMAND} -E copy_if_different ${PADDLE_LIB}/third_party/install/mklml/lib/mklml.dll ./release/mklml.dll
  198. COMMAND ${CMAKE_COMMAND} -E copy_if_different ${PADDLE_LIB}/third_party/install/mklml/lib/libiomp5md.dll ./release/libiomp5md.dll
  199. COMMAND ${CMAKE_COMMAND} -E copy_if_different ${PADDLE_LIB}/third_party/install/mkldnn/lib/mkldnn.dll ./release/mkldnn.dll
  200. )
  201. endif()