px4的CMakelists.txt阅读


  1. ############################################################################
  2. #
  3. # Copyright (c) 2017 PX4 Development Team. All rights reserved.
  4. #
  5. # Redistribution and use in source and binary forms, with or without
  6. # modification, are permitted provided that the following conditions
  7. # are met:
  8. #
  9. # 1. Redistributions of source code must retain the above copyright
  10. # notice, this list of conditions and the following disclaimer.
  11. # 2. Redistributions in binary form must reproduce the above copyright
  12. # notice, this list of conditions and the following disclaimer in
  13. # the documentation and/or other materials provided with the
  14. # distribution.
  15. # 3. Neither the name PX4 nor the names of its contributors may be
  16. # used to endorse or promote products derived from this software
  17. # without specific prior written permission.
  18. #
  19. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. # OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27. # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  29. # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. # POSSIBILITY OF SUCH DAMAGE.
  31. #
  32. ############################################################################

  33. #=============================================================================
  34. # CMAKE CODING STANDARD FOR PX4
  35. #
  36. # Structure
  37. # ---------------------------------------------------------------------------
  38. #
  39. # * Common functions should be included in px_base.cmake.
  40. #
  41. # * OS/ board specific fucntions should be include in
  42. #    px_impl_${OS}.cmake or px4_impl_${OS}_${BOARD}.cmake.
  43. #
  44. # Formatting
  45. # ---------------------------------------------------------------------------
  46. #
  47. # * Use hard indents to match the px4 source code.
  48. #
  49. # * All function and script arguments are upper case.
  50. #
  51. # * All local variables are lower case.
  52. #
  53. # * All cmake functions are lowercase.
  54. #
  55. # * For else, endif, endfunction, etc, never put the name of the statement
  56. #
  57. # Functions/Macros
  58. # ---------------------------------------------------------------------------
  59. #
  60. # * Use px4_parse_function_args to parse functions and check for required
  61. # arguments. Unless there is only one argument in the function and it is clear.
  62. #
  63. # * Never use macros. They allow overwriting global variables and this
  64. #    makes variable declarations hard to locate.
  65. #
  66. # * If a target from add_custom_* is set in a function, explicitly pass it
  67. #    as an output argument so that the target name is clear to the user.
  68. #
  69. # * Avoid use of global variables in functions. Functions in a nested
  70. #    scope may use global variables, but this makes it difficult to
  71. #    resuse functions.
  72. #
  73. # Included CMake Files
  74. # ---------------------------------------------------------------------------
  75. #
  76. # * All variables in config files must have the prefix "config_".
  77. #
  78. # * Never set global variables in an included cmake file,
  79. #    you may only define functions. This excludes config and Toolchain files.
  80. #    This makes it clear to the user when variables are being set or targets
  81. #    are being created.
  82. #
  83. # * Setting a global variable in a CMakeLists.txt file is ok, because
  84. #    each CMakeLists.txt file has scope in the current directory and all
  85. #    subdirectories, so it is not truly global.
  86. #
  87. # * All toolchain files should be included in the cmake
  88. #    directory and named Toolchain-"name".cmake.
  89. #
  90. # Misc
  91. # ---------------------------------------------------------------------------
  92. #
  93. # * If referencing a string variable, don't put it in quotes.
  94. #    Don't do "${OS}" STREQUAL "posix",
  95. #    instead type ${OS} STREQUAL "posix". This will throw an
  96. #    error when ${OS} is not defined instead of silently
  97. #    evaluating to false.
  98. #
  99. #=============================================================================

  100. if (${CMAKE_VERSION} VERSION_LESS 3.1.0)                                                                                            //判断cmake版本不能低于3.1.0
  101.     message("Not a valid CMake version")
  102.     message("On Ubuntu >= 16.04, install or upgrade via:")
  103.     message("    sudo apt-get install cmake")
  104.     message("")
  105.     message("Official website: https://cmake.org/download/")
  106.     message(FATAL_ERROR "Update CMake and try again" )
  107. endif()

  108. # Warning: Changing this modifies CMake's internal workings
  109. # and leads to wrong toolchain detection
  110. cmake_minimum_required(VERSION 3.1 FATAL_ERROR)

  111. set(PX4_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}")                                                                                 //设置PX4源文件目录
  112. set(PX4_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}")                                                                                //设置运行程序目录

  113. execute_process(                                                                                                        //执行一个或多个子进程
  114.     COMMAND Tools/check_submodules.sh                                                                                   //运行了tools/check_submodules.sh
  115.     WORKING_DIRECTORY ${PX4_SOURCE_DIR}                                                                                 //工作目录再px4目录下为当前目录
  116.     )

  117. #=============================================================================
  118. # configuration
  119. #
  120. # must come before project to set toolchain

  121. set(CONFIG "posix_sitl_default" CACHE STRING "desired configuration")

  122. string(REPLACE "_" ";" config_args ${CONFIG})
  123. list(GET config_args 0 OS)
  124. list(GET config_args 1 BOARD)
  125. list(GET config_args 2 LABEL)
  126. set(target_name "${OS}-${BOARD}-${LABEL}")

  127. file(GLOB_RECURSE configs RELATIVE cmake/configs "cmake/configs/*.cmake")
  128. set_property(CACHE CONFIG PROPERTY STRINGS ${configs})

  129. set(THREADS "4" CACHE STRING "number of threads to use for external build processes")
  130. set(DEBUG_PORT "/dev/ttyACM0" CACHE STRING "debugging port")
  131. set(EXTERNAL_MODULES_LOCATION "" CACHE STRING "External modules source location")

  132. if (NOT EXTERNAL_MODULES_LOCATION STREQUAL "")
  133.     get_filename_component(EXTERNAL_MODULES_LOCATION "${EXTERNAL_MODULES_LOCATION}" ABSOLUTE)
  134. endif()

  135. list(APPEND CMAKE_MODULE_PATH "${PX4_SOURCE_DIR}/cmake")
  136. message(STATUS "CMAKE_MODULE_PATH: ${CMAKE_MODULE_PATH}")
  137. set(config_module "configs/${CONFIG}")
  138. include(${config_module})

  139. include(common/coverage)
  140. include(common/sanitizers)

  141. # CMake build type
  142. # Debug Release RelWithDebInfo MinSizeRel Coverage
  143. if (NOT CMAKE_BUILD_TYPE)
  144.     if (${OS} STREQUAL "nuttx")
  145.         set(PX4_BUILD_TYPE "MinSizeRel")
  146.     elseif (${OS} STREQUAL "bebop")
  147.         set(PX4_BUILD_TYPE "MinSizeRel")
  148.     else()
  149.         set(PX4_BUILD_TYPE "RelWithDebInfo")
  150.     endif()

  151.     set(CMAKE_BUILD_TYPE ${PX4_BUILD_TYPE} CACHE STRING "Build type" FORCE)
  152. endif()

  153. set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug;Release;RelWithDebInfo;MinSizeRel;Coverage")

  154. message(STATUS "Build Type: ${CMAKE_BUILD_TYPE}")


  155. #=============================================================================
  156. # git
  157. #
  158. include(common/px4_git)

  159. execute_process(
  160.     COMMAND git describe --always --tags
  161.     OUTPUT_VARIABLE git_tag
  162.     OUTPUT_STRIP_TRAILING_WHITESPACE
  163.     WORKING_DIRECTORY ${PX4_SOURCE_DIR}
  164.     )

  165. execute_process(
  166.     COMMAND Tools/tag_to_version.py --root ${PX4_SOURCE_DIR}
  167.     OUTPUT_VARIABLE git_version
  168.     WORKING_DIRECTORY ${PX4_SOURCE_DIR}
  169.     )

  170. px4_add_git_submodule(TARGET git_cmake_hexagon PATH "cmake/cmake_hexagon")
  171. px4_add_git_submodule(TARGET git_driverframework PATH "src/lib/DriverFramework")
  172. px4_add_git_submodule(TARGET git_ecl PATH "src/lib/ecl")
  173. px4_add_git_submodule(TARGET git_gazebo PATH "Tools/sitl_gazebo")
  174. px4_add_git_submodule(TARGET git_gazebo_flow PATH "Tools/sitl_gazebo/external/OpticalFlow")
  175. px4_add_git_submodule(TARGET git_gazebo_klt PATH "Tools/sitl_gazebo/external/OpticalFlow/external/klt_feature_tracker")
  176. px4_add_git_submodule(TARGET git_gencpp PATH "Tools/gencpp")
  177. px4_add_git_submodule(TARGET git_genmsg PATH "Tools/genmsg")
  178. px4_add_git_submodule(TARGET git_gps_devices PATH "src/drivers/gps/devices")
  179. px4_add_git_submodule(TARGET git_jmavsim PATH "Tools/jMAVSim")
  180. px4_add_git_submodule(TARGET git_matrix PATH "src/lib/matrix")
  181. px4_add_git_submodule(TARGET git_mavlink PATH "mavlink/include/mavlink/v1.0")
  182. px4_add_git_submodule(TARGET git_mavlink2 PATH "mavlink/include/mavlink/v2.0")
  183. px4_add_git_submodule(TARGET git_nuttx PATH "NuttX")
  184. px4_add_git_submodule(TARGET git_uavcan PATH "src/modules/uavcan/libuavcan")

  185. px4_create_git_hash_header()

  186. #=============================================================================

  187. message(STATUS "PX4 VERSION: ${git_tag}")
  188. message(STATUS "CONFIG: ${target_name}")

  189. # The URL for the elf file for crash logging
  190. if (DEFINED ENV{BUILD_URI})
  191.     set(BUILD_URI $ENV{BUILD_URI})
  192. else()
  193.     set(BUILD_URI "localhost")
  194. endif()

  195. add_definitions(-DBUILD_URI=${BUILD_URI})

  196. # Define GNU standard installation directories
  197. include(GNUInstallDirs)

  198. # Add support for external project building
  199. include(ExternalProject)

  200. # Setup install paths
  201. if (NOT CMAKE_INSTALL_PREFIX)
  202.     if (${OS} STREQUAL "posix")
  203.         set(CMAKE_INSTALL_PREFIX "/usr" CACHE PATH "Install path prefix" FORCE)
  204.     endif()
  205. endif()
  206. if (CMAKE_INSTALL_PREFIX)
  207.     message(STATUS "CMAKE_INSTALL_PREFIX: ${CMAKE_INSTALL_PREFIX}")
  208. endif()

  209. #=============================================================================
  210. # require px4 module interface
  211. set(px4_required_interface
  212.     px4_os_prebuild_targets
  213.     px4_os_add_flags
  214.     )
  215. foreach(cmd ${px4_required_interface})
  216.     if (NOT COMMAND ${cmd})
  217.         message(FATAL_ERROR "${config_module} must implement ${cmd}")
  218.     endif()
  219. endforeach()

  220. set(px4_required_config config_module_list)
  221. foreach(conf ${px4_required_config})
  222.     if (NOT DEFINED ${conf})
  223.         message(FATAL_ERROR "cmake/${config_module} must define ${conf}")
  224.     endif()
  225. endforeach()

  226. # force static lib build
  227. set(BUILD_SHARED_LIBS OFF)

  228. #=============================================================================
  229. # ccache
  230. #
  231. option(CCACHE "Use ccache if available" OFF)
  232. find_program(CCACHE_PROGRAM ccache)
  233. if (CCACHE AND CCACHE_PROGRAM)
  234.     message(STATUS "Enabled ccache: ${CCACHE_PROGRAM}")
  235.     set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${CCACHE_PROGRAM}")
  236. endif()

  237. #=============================================================================
  238. # project definition
  239. #
  240. project(px4 CXX C ASM)

  241. set(package-contact "px4users@googlegroups.com")

  242. #=============================================================================
  243. # find programs and packages
  244. #

  245. # see if catkin was invoked to build this
  246. if (CATKIN_DEVEL_PREFIX)
  247.     message(STATUS "catkin ENABLED")
  248.     find_package(catkin REQUIRED)
  249.     if (catkin_FOUND)
  250.         catkin_package()
  251.     else()
  252.         message(FATAL_ERROR "catkin not found")
  253.     endif()
  254. endif()

  255. find_package(PythonInterp REQUIRED)
  256. px4_find_python_module(jinja2 REQUIRED)

  257. #=============================================================================
  258. # cmake testing
  259. #
  260. enable_testing()
  261. include(CTest)

  262. #=============================================================================
  263. # generate compile command database
  264. #
  265. set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

  266. #=============================================================================
  267. # check required toolchain variables
  268. #

  269. # PX4 requires c++11
  270. set(CMAKE_CXX_STANDARD 11)
  271. set(CMAKE_CXX_STANDARD_REQUIRED ON)

  272. # PX4 requires c99
  273. set(CMAKE_C_STANDARD 99)
  274. set(CMAKE_C_STANDARD_REQUIRED ON)

  275. set(required_variables CMAKE_C_COMPILER_ID CMAKE_CXX_COMPILER_ID)
  276. foreach(var ${required_variables})
  277.     if (NOT ${var})
  278.         message(FATAL_ERROR "Toolchain/config must define ${var}")
  279.     endif()
  280. endforeach()

  281. # print full c compiler version
  282. execute_process(COMMAND ${CMAKE_C_COMPILER} --version
  283.         OUTPUT_VARIABLE c_compiler_version
  284.         OUTPUT_STRIP_TRAILING_WHITESPACE
  285.         )
  286. STRING(REGEX MATCH "[^\n]*" c_compiler_version_short ${c_compiler_version})
  287. message(STATUS "C compiler: ${c_compiler_version_short}")

  288. # print full c++ compiler version
  289. execute_process(COMMAND ${CMAKE_CXX_COMPILER} --version
  290.         OUTPUT_VARIABLE cxx_compiler_version
  291.         OUTPUT_STRIP_TRAILING_WHITESPACE
  292.         )
  293. STRING(REGEX MATCH "[^\n]*" cxx_compiler_version_short ${cxx_compiler_version})
  294. message(STATUS "C++ compiler: ${cxx_compiler_version_short}")

  295. #=============================================================================
  296. # external libraries
  297. #
  298. px4_os_prebuild_targets(OUT prebuild_targets
  299.     BOARD ${BOARD}
  300.     THREADS ${THREADS})

  301. #=============================================================================
  302. # build flags
  303. #
  304. px4_os_add_flags(
  305.     BOARD ${BOARD}
  306.     C_FLAGS c_flags
  307.     CXX_FLAGS cxx_flags
  308.     OPTIMIZATION_FLAGS optimization_flags
  309.     EXE_LINKER_FLAGS exe_linker_flags
  310.     INCLUDE_DIRS include_dirs
  311.     LINK_DIRS link_dirs
  312.     DEFINITIONS definitions)

  313. px4_join(OUT CMAKE_EXE_LINKER_FLAGS LIST "${exe_linker_flags}" GLUE " ")
  314. px4_join(OUT CMAKE_C_FLAGS LIST "${c_flags};${optimization_flags}" GLUE " ")
  315. px4_join(OUT CMAKE_CXX_FLAGS LIST "${cxx_flags};${optimization_flags}" GLUE " ")

  316. include_directories(${include_dirs})
  317. #message("INCLUDE_DIRS=${include_dirs}")
  318. link_directories(${link_dirs})
  319. add_definitions(${definitions})

  320. #=============================================================================
  321. # message, and airframe generation
  322. #

  323. include(common/px4_metadata)

  324. add_subdirectory(msg)
  325. px4_generate_messages(TARGET msg_gen
  326.     MSG_FILES ${msg_files}
  327.     OS ${OS}
  328.     INCLUDES ${msg_include_paths}
  329.     DEPENDS git_genmsg git_gencpp prebuild_targets
  330.     )

  331. px4_generate_airframes_xml(BOARD ${BOARD})

  332. #=============================================================================
  333. # DriverFramework
  334. #

  335. # List the DriverFramework drivers
  336. if (DEFINED config_df_driver_list)
  337.     message("DF Drivers: ${config_df_driver_list}")
  338. endif()

  339. set(df_driver_libs)
  340. foreach(driver ${config_df_driver_list})
  341.     add_subdirectory(src/lib/DriverFramework/drivers/${driver})
  342.     list(APPEND df_driver_libs df_${driver})
  343.     message("Adding DF driver: ${driver}")
  344. endforeach()

  345. #=============================================================================
  346. # external projects
  347. #

  348. set(ep_base ${PX4_BINARY_DIR}/external)
  349. set_property(DIRECTORY PROPERTY EP_BASE ${ep_base})

  350. # add external project install folders to build
  351. link_directories(${ep_base}/Install/lib)
  352. include_directories(${ep_base}/Install/include)
  353. # add the directories so cmake won't warn
  354. execute_process(COMMAND cmake -E make_directory ${ep_base}/Install/lib)
  355. execute_process(COMMAND cmake -E make_directory ${ep_base}/Install/include)

  356. #=============================================================================
  357. # external modules
  358. #
  359. if (NOT EXTERNAL_MODULES_LOCATION STREQUAL "")
  360.     message(STATUS "External modules: ${EXTERNAL_MODULES_LOCATION}")
  361.     add_subdirectory("${EXTERNAL_MODULES_LOCATION}/src" external_modules_src)

  362.     set(config_module_list_external_expanded)
  363.     foreach(external_module ${config_module_list_external})
  364.         list(APPEND config_module_list_external_expanded
  365.             ${EXTERNAL_MODULES_LOCATION}/src/${external_module})
  366.     endforeach()
  367.     set(config_module_list
  368.         ${config_module_list}
  369.         ${config_module_list_external_expanded}
  370.         )
  371. endif()

  372. #=============================================================================
  373. # subdirectories
  374. #
  375. set(module_libraries)
  376. foreach(module ${config_module_list})
  377.     string(REGEX MATCH "^[./]" external_module ${module})
  378.     if (external_module)
  379.         STRING(REGEX REPLACE "//" "/" EXT_MODULE ${module})
  380.         STRING(REGEX REPLACE "/" "__" EXT_MODULE_PREFIX ${EXT_MODULE})
  381.         add_subdirectory(${module} ${PX4_BINARY_DIR}/${EXT_MODULE_PREFIX})
  382.     else()
  383.         add_subdirectory(src/${module})
  384.     endif()
  385.     px4_mangle_name(${module} mangled_name)
  386.     list(APPEND module_libraries ${mangled_name})
  387. endforeach()

  388. # Keep track of external shared libs required for modules
  389. set(module_external_libraries "${module_external_libraries}" CACHE INTERNAL "module_external_libraries")

  390. add_subdirectory(src/firmware/${OS})

  391. if (config_io_board)
  392.     add_subdirectory(src/modules/px4iofirmware)
  393. endif()

  394. #=============================================================================
  395. # generate custom target to print for all executable and module cmake targets
  396. #
  397. if (all_posix_cmake_targets)
  398.     list(SORT all_posix_cmake_targets)
  399.     px4_join(OUT posix_cmake_target_list LIST ${all_posix_cmake_targets} GLUE "\\n")
  400.     add_custom_target(list_cmake_targets
  401.         COMMAND sh -c "printf \"${posix_cmake_target_list}\\n\""
  402.         COMMENT "List of cmake targets that can be matched by PX4_NO_OPTIMIZATION:"
  403.         VERBATIM
  404.         )
  405. endif()

  406. #=============================================================================
  407. # packaging
  408. #
  409. # Important to having packaging at end of cmake file.
  410. #
  411. set(CPACK_PACKAGE_NAME ${PROJECT_NAME}-${CONFIG})
  412. set(CPACK_PACKAGE_VERSION ${git_version})
  413. set(CPACK_PACKAGE_CONTACT ${package-contact})
  414. set(CPACK_DEBIAN_PACKAGE_SHLIBDEPS ON)
  415. set(CPACK_DEBIAN_PACKAGE_SECTION "devel")
  416. set(CPACK_DEBIAN_PACKAGE_PRIORITY "optional")
  417. set(short-description "The px4 autopilot.")
  418. set(CPACK_DEBIAN_PACKAGE_DESCRIPTION ${short-description})
  419. set(CPACK_GENERATOR "ZIP")
  420. set(CPACK_PACKAGE_FILE_NAME "${PROJECT_NAME}-${CONFIG}-${git_tag}")
  421. set(CPACK_SOURCE_PACKAGE_FILE_NAME "${PROJECT_NAME}-${git_version}")
  422. set(CPACK_SOURCE_GENERATOR "ZIP;TBZ2")
  423. set(CPACK_PACKAGING_INSTALL_PREFIX "")
  424. set(CPACK_SET_DESTDIR "OFF")
  425. if ("${CMAKE_SYSTEM}" MATCHES "Linux")
  426.     find_program(DPKG_PROGRAM dpkg)
  427.     if (EXISTS ${DPKG_PROGRAM})
  428.         list (APPEND CPACK_GENERATOR "DEB")
  429.     endif()
  430. endif()
  431. include(CPack)

  432. # vim: set noet fenc=utf-8 ff=unix ft=cmake :

<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
阅读(1) | 评论(0) | 转发(0) |
0

上一篇:Qt对话框部分学习

下一篇:cmake学习

给主人留下些什么吧!~~
评论热议
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

习惯就好zz

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值