CMakeLists.txt 9.6 KB

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