CMakeLists.txt 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. # 定义cmake的最低版本
  2. cmake_minimum_required(VERSION 3.19)
  3. # 检查vcpkg文件夹是否存在
  4. if(EXISTS "${CMAKE_SOURCE_DIR}/vcpkg")
  5. message("Find directory: " "${CMAKE_SOURCE_DIR}/vcpkg")
  6. else()
  7. message("Can't find directory: " "${CMAKE_SOURCE_DIR}/vcpkg")
  8. execute_process(
  9. COMMAND git clone https://github.com/microsoft/vcpkg.git
  10. WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
  11. RESULT_VARIABLE vcpkg_ret
  12. )
  13. if (vcpkg_ret EQUAL 0)
  14. message("clone vcpkg success... ")
  15. else ()
  16. message("clone vcpkg failed... ")
  17. return()
  18. endif ()
  19. endif()
  20. # 查看vcpkg版本
  21. execute_process(
  22. COMMAND "${CMAKE_SOURCE_DIR}/vcpkg/vcpkg.exe" --version
  23. RESULT_VARIABLE result
  24. OUTPUT_VARIABLE output
  25. )
  26. # cmake 无法在执行project之前获取系统类型,所以只能通过一个个安装尝试
  27. if(NOT result EQUAL 0)
  28. message("Can't get vcpkg version, run vcpkg install script file.")
  29. if (EXISTS "${CMAKE_SOURCE_DIR}/vcpkg/bootstrap-vcpkg.sh")
  30. message("Find script ${CMAKE_SOURCE_DIR}/vcpkg/bootstrap-vcpkg.sh, try to install...")
  31. execute_process(
  32. COMMAND "${CMAKE_SOURCE_DIR}/vcpkg/bootstrap-vcpkg.sh"
  33. RESULT_VARIABLE install_result
  34. OUTPUT_VARIABLE install_output
  35. )
  36. endif ()
  37. message("vcpkg sh script install result: ${install_result}")
  38. if (NOT install_result EQUAL 0 AND EXISTS "${CMAKE_SOURCE_DIR}/vcpkg/bootstrap-vcpkg.bat")
  39. message("${CMAKE_SOURCE_DIR}/vcpkg/bootstrap-vcpkg.sh can't install vcpkg, try to use bat script...")
  40. message("Find script ${CMAKE_SOURCE_DIR}/vcpkg/bootstrap-vcpkg.bat, try to install...")
  41. execute_process(
  42. COMMAND "${CMAKE_SOURCE_DIR}/vcpkg/bootstrap-vcpkg.bat"
  43. RESULT_VARIABLE install_result
  44. OUTPUT_VARIABLE install_output
  45. )
  46. message(${install_result})
  47. endif ()
  48. endif()
  49. # vcpkg 环境变量定义,必须在project之前定义
  50. set(VCPKG_ROOT "${CMAKE_SOURCE_DIR}/vcpkg")
  51. set(CMAKE_TOOLCHAIN_FILE "${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake")
  52. # 定义工程名称
  53. project(Project)
  54. set(CMAKE_CXX_STANDARD 17)
  55. option(MeasureTest "Enable test" OFF)
  56. option(GFLAGS "Enable Google Flags." ON)
  57. option(DataToCloud "Enable collect data to mqtt server." ON)
  58. option(AlgAddTest "Enable test alg module." OFF)
  59. option(ClampSafety "Enable clamp safety." OFF)
  60. option(WatchDog "Enable watch dog." ON)
  61. option(MeasureNode "Enable Measure node." ON)
  62. option(LidarICP "Enable LidarICP" OFF)
  63. option(ExecutorTest "Enable ExecutorTest" OFF)
  64. option(Cloud2Mat "Enable ExecutorTest" OFF)
  65. # 指定安装目录
  66. if (MeasureTest)
  67. set(CMAKE_INSTALL_PREFIX
  68. ${PROJECT_SOURCE_DIR}/build/out
  69. )
  70. message("ON test, set install path to ${PROJECT_SOURCE_DIR}/build/out")
  71. else ()
  72. set(CMAKE_INSTALL_PREFIX
  73. # /out
  74. ${PROJECT_SOURCE_DIR}/build/out
  75. )
  76. message("OFF test, set install path to ${PROJECT_SOURCE_DIR}/build/out")
  77. add_definitions(-DETC_PATH="${CMAKE_INSTALL_PREFIX}")
  78. endif ()
  79. SET (CMAKE_EXPORT_COMPILE_COMMANDS ON)
  80. set (CMAKE_BUILD_TYPE "RELEASE")
  81. # 打印工程定级目录(非必要)
  82. message("project path: " ${PROJECT_SOURCE_DIR})
  83. #execute_process(COMMAND bash ${PROJECT_SOURCE_DIR}/proto.sh ${PROJECT_SOURCE_DIR})
  84. execute_process(COMMAND ${PROJECT_SOURCE_DIR}/proto.bat ${PROJECT_SOURCE_DIR})
  85. # 第三方库
  86. find_package(PkgConfig REQUIRED)
  87. pkg_check_modules(nanomsg REQUIRED nanomsg)
  88. pkg_check_modules(YAML_CPP REQUIRED yaml-cpp)
  89. find_path(YAML_CPP_INCLUDE_DIR
  90. NAMES yaml_cpp.h
  91. PATHS ${YAML_CPP_INCLUDE_DIRS})
  92. find_library(YAML_CPP_LIBRARY
  93. NAMES YAML_CPP
  94. PATHS ${YAML_CPP_LIBRARY_DIRS})
  95. if (NOT ${YAML_CPP_VERSION} VERSION_LESS "0.5")
  96. add_definitions(-DHAVE_NEW_YAMLCPP)
  97. endif (NOT ${YAML_CPP_VERSION} VERSION_LESS "0.5")
  98. add_subdirectory(thirdpart/rs_driver)
  99. # Don't search with REQUIRED as we can continue without gflags.
  100. find_package(gflags 2.2.0)
  101. if (gflags_FOUND)
  102. if (TARGET gflags)
  103. message("-- Found Google Flags (gflags) version ${gflags_VERSION}: ${gflags_DIR}")
  104. message("-- Found Google Flags (gflags) version ${gflags_VERSION}: ${GFLAGS_LIBRARIES}")
  105. else()
  106. message("-- Detected version of gflags: ${gflags_VERSION} does not define "
  107. "expected gflags CMake target which should be exported by gflags 2.2+. "
  108. "Building without gflags.")
  109. update_cache_variable(GFLAGS OFF)
  110. endif()
  111. else (gflags_FOUND)
  112. message("-- Did not find Google Flags (gflags), Building without gflags.")
  113. update_cache_variable(GFLAGS OFF)
  114. endif (gflags_FOUND)
  115. find_package(PCL REQUIRED)
  116. find_package(Eigen3 REQUIRED)
  117. find_package(Ceres REQUIRED)
  118. find_package(OpenCV REQUIRED)
  119. find_package(rs_driver REQUIRED)
  120. find_package(Protobuf REQUIRED)
  121. find_package(rabbitmq-c CONFIG REQUIRED)
  122. include_directories(
  123. ${rs_driver_INCLUDE_DIRS}
  124. ${OpenCV_INCLUDE_DIRS}
  125. ${EIGEN3_INCLUDE_DIRS}
  126. ${CERES_INCLUDE_DIR}
  127. ${PCL_INCLUDE_DIRS}
  128. ${PROJECT_SOURCE_DIR}/include
  129. ${PROJECT_SOURCE_DIR}/Modules
  130. )
  131. # 添加子目录CMAKE
  132. add_subdirectory(include)
  133. if(DataToCloud)
  134. add_subdirectory(Modules/DataToCloud)
  135. endif ()
  136. if(AlgAddTest)
  137. add_subdirectory(Modules/AlgAddTest)
  138. endif ()
  139. if(ClampSafety)
  140. add_subdirectory(Modules/ClampSafety)
  141. endif ()
  142. if(WatchDog)
  143. add_subdirectory(Modules/WatchDog)
  144. endif ()
  145. if(MeasureNode)
  146. include_directories(${PROJECT_SOURCE_DIR}/Modules/MeasureNode)
  147. add_subdirectory(Modules/MeasureNode)
  148. endif ()
  149. if(LidarICP)
  150. include_directories(${PROJECT_SOURCE_DIR}/Modules/LidarICP)
  151. add_subdirectory(Modules/LidarICP)
  152. endif ()
  153. if(ExecutorTest)
  154. include_directories(${PROJECT_SOURCE_DIR}/Modules/ExecutorTest)
  155. add_subdirectory(Modules/ExecutorTest)
  156. endif ()
  157. if(Cloud2Mat)
  158. add_subdirectory(Modules/Cloud2Mat)
  159. endif ()
  160. # install(DIRECTORY ${PROJECT_SOURCE_DIR}/etc
  161. # DESTINATION ${CMAKE_INSTALL_PREFIX}
  162. # )