CMakeLists.txt 9.5 KB

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