CMakeLists.txt 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. project(ocr_system CXX C)
  2. option(WITH_MKL "Compile demo with MKL/OpenBlas support, default use MKL." ON)
  3. option(WITH_GPU "Compile demo with GPU/CPU, default use CPU." OFF)
  4. option(WITH_STATIC_LIB "Compile demo with static/shared library, default use static." ON)
  5. option(WITH_TENSORRT "Compile demo with TensorRT." OFF)
  6. SET(PADDLE_LIB "" CACHE PATH "Location of libraries")
  7. SET(OPENCV_DIR "" CACHE PATH "Location of libraries")
  8. SET(CUDA_LIB "" CACHE PATH "Location of libraries")
  9. SET(CUDNN_LIB "" CACHE PATH "Location of libraries")
  10. SET(TENSORRT_DIR "" CACHE PATH "Compile demo with TensorRT")
  11. set(DEMO_NAME "ocr_system")
  12. macro(safe_set_static_flag)
  13. foreach(flag_var
  14. CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
  15. CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
  16. if(${flag_var} MATCHES "/MD")
  17. string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
  18. endif(${flag_var} MATCHES "/MD")
  19. endforeach(flag_var)
  20. endmacro()
  21. if (WITH_MKL)
  22. ADD_DEFINITIONS(-DUSE_MKL)
  23. endif()
  24. if(NOT DEFINED PADDLE_LIB)
  25. message(FATAL_ERROR "please set PADDLE_LIB with -DPADDLE_LIB=/path/paddle/lib")
  26. endif()
  27. if(NOT DEFINED OPENCV_DIR)
  28. message(FATAL_ERROR "please set OPENCV_DIR with -DOPENCV_DIR=/path/opencv")
  29. endif()
  30. if (WIN32)
  31. include_directories("${PADDLE_LIB}/paddle/fluid/inference")
  32. include_directories("${PADDLE_LIB}/paddle/include")
  33. link_directories("${PADDLE_LIB}/paddle/fluid/inference")
  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/boost")
  82. include_directories("${PADDLE_LIB}/third_party/eigen3")
  83. include_directories("${CMAKE_SOURCE_DIR}/")
  84. if (NOT WIN32)
  85. if (WITH_TENSORRT AND WITH_GPU)
  86. include_directories("${TENSORRT_DIR}/include")
  87. link_directories("${TENSORRT_DIR}/lib")
  88. endif()
  89. endif(NOT WIN32)
  90. link_directories("${PADDLE_LIB}/third_party/install/zlib/lib")
  91. link_directories("${PADDLE_LIB}/third_party/install/protobuf/lib")
  92. link_directories("${PADDLE_LIB}/third_party/install/glog/lib")
  93. link_directories("${PADDLE_LIB}/third_party/install/gflags/lib")
  94. link_directories("${PADDLE_LIB}/third_party/install/xxhash/lib")
  95. link_directories("${PADDLE_LIB}/paddle/lib")
  96. if(WITH_MKL)
  97. include_directories("${PADDLE_LIB}/third_party/install/mklml/include")
  98. if (WIN32)
  99. set(MATH_LIB ${PADDLE_LIB}/third_party/install/mklml/lib/mklml.lib
  100. ${PADDLE_LIB}/third_party/install/mklml/lib/libiomp5md.lib)
  101. else ()
  102. set(MATH_LIB ${PADDLE_LIB}/third_party/install/mklml/lib/libmklml_intel${CMAKE_SHARED_LIBRARY_SUFFIX}
  103. ${PADDLE_LIB}/third_party/install/mklml/lib/libiomp5${CMAKE_SHARED_LIBRARY_SUFFIX})
  104. execute_process(COMMAND cp -r ${PADDLE_LIB}/third_party/install/mklml/lib/libmklml_intel${CMAKE_SHARED_LIBRARY_SUFFIX} /usr/lib)
  105. endif ()
  106. set(MKLDNN_PATH "${PADDLE_LIB}/third_party/install/mkldnn")
  107. if(EXISTS ${MKLDNN_PATH})
  108. include_directories("${MKLDNN_PATH}/include")
  109. if (WIN32)
  110. set(MKLDNN_LIB ${MKLDNN_PATH}/lib/mkldnn.lib)
  111. else ()
  112. set(MKLDNN_LIB ${MKLDNN_PATH}/lib/libmkldnn.so.0)
  113. endif ()
  114. endif()
  115. else()
  116. set(MATH_LIB ${PADDLE_LIB}/third_party/install/openblas/lib/libopenblas${CMAKE_STATIC_LIBRARY_SUFFIX})
  117. endif()
  118. # Note: libpaddle_inference_api.so/a must put before libpaddle_fluid.so/a
  119. if(WITH_STATIC_LIB)
  120. if(WIN32)
  121. set(DEPS
  122. ${PADDLE_LIB}/paddle/lib/paddle_fluid${CMAKE_STATIC_LIBRARY_SUFFIX})
  123. else()
  124. set(DEPS
  125. ${PADDLE_LIB}/paddle/lib/libpaddle_fluid${CMAKE_STATIC_LIBRARY_SUFFIX})
  126. endif()
  127. else()
  128. if(WIN32)
  129. set(DEPS
  130. ${PADDLE_LIB}/paddle/lib/paddle_fluid${CMAKE_SHARED_LIBRARY_SUFFIX})
  131. else()
  132. set(DEPS
  133. ${PADDLE_LIB}/paddle/lib/libpaddle_fluid${CMAKE_SHARED_LIBRARY_SUFFIX})
  134. endif()
  135. endif(WITH_STATIC_LIB)
  136. if (NOT WIN32)
  137. set(DEPS ${DEPS}
  138. ${MATH_LIB} ${MKLDNN_LIB}
  139. glog gflags protobuf z xxhash
  140. )
  141. if(EXISTS "${PADDLE_LIB}/third_party/install/snappystream/lib")
  142. set(DEPS ${DEPS} snappystream)
  143. endif()
  144. if (EXISTS "${PADDLE_LIB}/third_party/install/snappy/lib")
  145. set(DEPS ${DEPS} snappy)
  146. endif()
  147. else()
  148. set(DEPS ${DEPS}
  149. ${MATH_LIB} ${MKLDNN_LIB}
  150. glog gflags_static libprotobuf xxhash)
  151. set(DEPS ${DEPS} libcmt shlwapi)
  152. if (EXISTS "${PADDLE_LIB}/third_party/install/snappy/lib")
  153. set(DEPS ${DEPS} snappy)
  154. endif()
  155. if(EXISTS "${PADDLE_LIB}/third_party/install/snappystream/lib")
  156. set(DEPS ${DEPS} snappystream)
  157. endif()
  158. endif(NOT WIN32)
  159. if(WITH_GPU)
  160. if(NOT WIN32)
  161. if (WITH_TENSORRT)
  162. set(DEPS ${DEPS} ${TENSORRT_DIR}/lib/libnvinfer${CMAKE_SHARED_LIBRARY_SUFFIX})
  163. set(DEPS ${DEPS} ${TENSORRT_DIR}/lib/libnvinfer_plugin${CMAKE_SHARED_LIBRARY_SUFFIX})
  164. endif()
  165. set(DEPS ${DEPS} ${CUDA_LIB}/libcudart${CMAKE_SHARED_LIBRARY_SUFFIX})
  166. set(DEPS ${DEPS} ${CUDNN_LIB}/libcudnn${CMAKE_SHARED_LIBRARY_SUFFIX})
  167. else()
  168. set(DEPS ${DEPS} ${CUDA_LIB}/cudart${CMAKE_STATIC_LIBRARY_SUFFIX} )
  169. set(DEPS ${DEPS} ${CUDA_LIB}/cublas${CMAKE_STATIC_LIBRARY_SUFFIX} )
  170. set(DEPS ${DEPS} ${CUDNN_LIB}/cudnn${CMAKE_STATIC_LIBRARY_SUFFIX})
  171. endif()
  172. endif()
  173. if (NOT WIN32)
  174. set(EXTERNAL_LIB "-ldl -lrt -lgomp -lz -lm -lpthread")
  175. set(DEPS ${DEPS} ${EXTERNAL_LIB})
  176. endif()
  177. set(DEPS ${DEPS} ${OpenCV_LIBS})
  178. AUX_SOURCE_DIRECTORY(./src SRCS)
  179. add_executable(${DEMO_NAME} ${SRCS})
  180. target_link_libraries(${DEMO_NAME} ${DEPS})
  181. if (WIN32 AND WITH_MKL)
  182. add_custom_command(TARGET ${DEMO_NAME} POST_BUILD
  183. COMMAND ${CMAKE_COMMAND} -E copy_if_different ${PADDLE_LIB}/third_party/install/mklml/lib/mklml.dll ./mklml.dll
  184. COMMAND ${CMAKE_COMMAND} -E copy_if_different ${PADDLE_LIB}/third_party/install/mklml/lib/libiomp5md.dll ./libiomp5md.dll
  185. COMMAND ${CMAKE_COMMAND} -E copy_if_different ${PADDLE_LIB}/third_party/install/mkldnn/lib/mkldnn.dll ./mkldnn.dll
  186. COMMAND ${CMAKE_COMMAND} -E copy_if_different ${PADDLE_LIB}/third_party/install/mklml/lib/mklml.dll ./release/mklml.dll
  187. COMMAND ${CMAKE_COMMAND} -E copy_if_different ${PADDLE_LIB}/third_party/install/mklml/lib/libiomp5md.dll ./release/libiomp5md.dll
  188. COMMAND ${CMAKE_COMMAND} -E copy_if_different ${PADDLE_LIB}/third_party/install/mkldnn/lib/mkldnn.dll ./release/mkldnn.dll
  189. )
  190. endif()