前馈科技

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 73799|回复: 0

PX4 CMakeLists解读

[复制链接]

97

主题

97

帖子

539

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
539
发表于 2020-7-8 10:48:10 | 显示全部楼层 |阅读模式
本解读基于PX4 V1.11.0,下面直接上CMakeLists.txt

  1. ############################################################################
  2. #
  3. # Copyright (c) 2017 - 2019 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_${PX4_PLATFORM}.cmake or px4_impl_${PX4_PLATFORM}_${PX4_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. #        reuse 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 "${PX4_PLATFORM}" STREQUAL "posix",
  95. #        instead type ${PX4_PLATFORM} STREQUAL "posix". This will throw an
  96. #        error when ${PX4_PLATFORM} is not defined instead of silently
  97. #        evaluating to false.
  98. #
  99. #=============================================================================

  100. cmake_minimum_required(VERSION 3.2 FATAL_ERROR)

  101. set(PX4_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
  102. set(PX4_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}")

  103. list(APPEND CMAKE_MODULE_PATH ${PX4_SOURCE_DIR}/cmake)
  104. include(px4_parse_function_args)

  105. #=============================================================================
  106. # git
  107. #
  108. include(px4_git)

  109. execute_process(
  110.         COMMAND git describe --always --tags
  111.         OUTPUT_VARIABLE PX4_GIT_TAG
  112.         OUTPUT_STRIP_TRAILING_WHITESPACE
  113.         WORKING_DIRECTORY ${PX4_SOURCE_DIR}
  114.         )
  115. message(STATUS "PX4 version: ${PX4_GIT_TAG}")

  116. define_property(GLOBAL PROPERTY PX4_MODULE_LIBRARIES
  117.                  BRIEF_DOCS "PX4 module libs"
  118.                  FULL_DOCS "List of all PX4 module libraries"
  119.                  )

  120. define_property(GLOBAL PROPERTY PX4_MODULE_PATHS
  121.                  BRIEF_DOCS "PX4 module paths"
  122.                  FULL_DOCS "List of paths to all PX4 modules"
  123.                  )

  124. #=============================================================================
  125. # configuration
  126. #

  127. set(CONFIG "px4_sitl_default" CACHE STRING "desired configuration")

  128. include(px4_add_module)
  129. set(config_module_list)

  130. include(px4_config)
  131. include(px4_add_board)
  132. include(${PX4_CONFIG_FILE})
  133. message(STATUS "PX4 config: ${PX4_CONFIG}")
  134. message(STATUS "PX4 platform: ${PX4_PLATFORM}")

  135. if(${PX4_PLATFORM} STREQUAL "posix")
  136.         if(ENABLE_LOCKSTEP_SCHEDULER)
  137.                 add_definitions(-DENABLE_LOCKSTEP_SCHEDULER)
  138.                 message(STATUS "PX4 lockstep: enabled")
  139.         else()
  140.                 message(STATUS "PX4 lockstep: disabled")
  141.         endif()
  142. endif()

  143. # external modules
  144. set(EXTERNAL_MODULES_LOCATION "" CACHE STRING "External modules source location")

  145. if(NOT EXTERNAL_MODULES_LOCATION STREQUAL "")
  146.         get_filename_component(EXTERNAL_MODULES_LOCATION "${EXTERNAL_MODULES_LOCATION}" ABSOLUTE)
  147. endif()

  148. set_property(GLOBAL PROPERTY PX4_MODULE_CONFIG_FILES)

  149. include(platforms/${PX4_PLATFORM}/cmake/px4_impl_os.cmake)
  150. list(APPEND CMAKE_MODULE_PATH ${PX4_SOURCE_DIR}/platforms/${PX4_PLATFORM}/cmake)

  151. #如果Firmware/platforms/nuttx/cmake/init.cmake存在
  152. if(EXISTS "${PX4_SOURCE_DIR}/platforms/${PX4_PLATFORM}/cmake/init.cmake")
  153.        #插入Firmware/platforms/nuttx/cmake/init.cmake
  154.         include(init)
  155. endif()

  156. # CMake build type (Debug Release RelWithDebInfo MinSizeRel Coverage)
  157. if(NOT CMAKE_BUILD_TYPE)
  158.         if(${PX4_PLATFORM} STREQUAL "nuttx")
  159.                 set(PX4_BUILD_TYPE "MinSizeRel")
  160.         else()
  161.                 set(PX4_BUILD_TYPE "RelWithDebInfo")
  162.         endif()

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

  165. if((CMAKE_BUILD_TYPE STREQUAL "Debug") OR (CMAKE_BUILD_TYPE STREQUAL "Coverage"))
  166.         set(MAX_CUSTOM_OPT_LEVEL -O0)
  167. elseif(CMAKE_BUILD_TYPE MATCHES "Sanitizer")
  168.         set(MAX_CUSTOM_OPT_LEVEL -O1)
  169. else()
  170.         if(px4_constrained_flash_build)
  171.                 set(MAX_CUSTOM_OPT_LEVEL -Os)
  172.         else()
  173.                 set(MAX_CUSTOM_OPT_LEVEL -O2)
  174.         endif()
  175. endif()

  176. set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug;Release;RelWithDebInfo;MinSizeRel;Coverage;AddressSanitizer;UndefinedBehaviorSanitizer")
  177. message(STATUS "cmake build type: ${CMAKE_BUILD_TYPE}")

  178. #=============================================================================
  179. # project definition
  180. #
  181. project(px4 CXX C ASM)

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

  183. set(CMAKE_CXX_STANDARD 14)
  184. set(CMAKE_CXX_STANDARD_REQUIRED ON)
  185. set(CMAKE_C_STANDARD 11)
  186. set(CMAKE_C_STANDARD_REQUIRED ON)
  187. set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

  188. # For the catkin build process, unset build of dynamically-linked binaries
  189. # and do not change CMAKE_RUNTIME_OUTPUT_DIRECTORY
  190. if (NOT CATKIN_DEVEL_PREFIX)
  191.         set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PX4_BINARY_DIR})
  192.         set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${PX4_BINARY_DIR})
  193.         set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${PX4_BINARY_DIR})
  194. else()
  195.         SET(BUILD_SHARED_LIBS OFF)
  196. endif()

  197. #=============================================================================

  198. # gold linker - use if available
  199. include(CMakeDependentOption)
  200. CMAKE_DEPENDENT_OPTION(USE_LD_GOLD
  201.                       "Use GNU gold linker" ON
  202.                       "NOT WIN32;NOT APPLE" OFF
  203. )

  204. if(USE_LD_GOLD)
  205.         execute_process(COMMAND ${CMAKE_C_COMPILER} -fuse-ld=gold -Wl,--version ERROR_QUIET OUTPUT_VARIABLE LD_VERSION)
  206.         if("${LD_VERSION}" MATCHES "GNU gold")
  207.                 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=gold")
  208.                 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fuse-ld=gold")
  209.         else()
  210.                 set(USE_LD_GOLD OFF)
  211.         endif()
  212. endif()

  213. #=============================================================================

  214. # Setup install paths
  215. if(${PX4_PLATFORM} STREQUAL "posix")
  216.         # This makes it possible to dynamically load code which depends on symbols
  217.         # inside the px4 executable.
  218.         set(CMAKE_POSITION_INDEPENDENT_CODE ON)
  219.         set(CMAKE_ENABLE_EXPORTS ON)

  220.         include(coverage)
  221.         include(sanitizers)

  222.         # Define GNU standard installation directories
  223.         include(GNUInstallDirs)

  224.         if (NOT CMAKE_INSTALL_PREFIX)
  225.                 set(CMAKE_INSTALL_PREFIX "/usr" CACHE PATH "Install path prefix" FORCE)
  226.         endif()
  227. endif()

  228. include(ccache)

  229. #=============================================================================
  230. # find programs and packages
  231. #

  232. # see if catkin was invoked to build this
  233. if (CATKIN_DEVEL_PREFIX)
  234.         message(STATUS "catkin ENABLED")
  235.         find_package(catkin REQUIRED)
  236.         if (catkin_FOUND)
  237.                 catkin_package()
  238.         else()
  239.                 message(FATAL_ERROR "catkin not found")
  240.         endif()
  241. endif()

  242. # Python
  243. # If using catkin, Python 2 is found since it points
  244. # to the Python libs installed with the ROS distro
  245. if (NOT CATKIN_DEVEL_PREFIX)
  246.         find_package(PythonInterp 3)
  247.         # We have a custom error message to tell users how to install python3.
  248.         if (NOT PYTHONINTERP_FOUND)
  249.                 message(FATAL_ERROR "Python 3 not found. Please install Python 3:\n"
  250.                         "    Ubuntu: sudo apt install python3 python3-dev python3-pip\n"
  251.                         "    macOS: brew install python")
  252.         endif()
  253. else()
  254.         find_package(PythonInterp REQUIRED)
  255. endif()

  256. option(PYTHON_COVERAGE "Python code coverage" OFF)
  257. if(PYTHON_COVERAGE)
  258.         message(STATUS "python coverage enabled")
  259.         set(PYTHON_EXECUTABLE coverage run -p)
  260. endif()

  261. #=============================================================================
  262. # get chip and chip manufacturer
  263. #
  264. px4_os_determine_build_chip()
  265. if(NOT PX4_CHIP_MANUFACTURER)
  266.         message(FATAL_ERROR "px4_os_determine_build_chip() needs to set PX4_CHIP_MANUFACTURER")
  267. endif()
  268. if(NOT PX4_CHIP)
  269.         message(FATAL_ERROR "px4_os_determine_build_chip() needs to set PX4_CHIP")
  270. endif()

  271. #=============================================================================
  272. # build flags
  273. #
  274. include(px4_add_common_flags)
  275. px4_add_common_flags()
  276. px4_os_add_flags()

  277. #=============================================================================
  278. # board cmake init (optional)
  279. #
  280. if(EXISTS ${PX4_BOARD_DIR}/cmake/init.cmake)
  281.         include(${PX4_BOARD_DIR}/cmake/init.cmake)
  282. endif()

  283. #=============================================================================
  284. # message, and airframe generation
  285. #
  286. include(px4_metadata)

  287. add_subdirectory(msg EXCLUDE_FROM_ALL)

  288. px4_generate_airframes_xml(BOARD ${PX4_BOARD})

  289. #=============================================================================
  290. # external projects
  291. #
  292. set(ep_base ${PX4_BINARY_DIR}/external)
  293. set_property(DIRECTORY PROPERTY EP_BASE ${ep_base})

  294. # add external project install folders to build
  295. # add the directories so cmake won't warn
  296. execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory ${ep_base})
  297. execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory ${ep_base}/Install)
  298. execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory ${ep_base}/Install/lib)
  299. link_directories(${ep_base}/Install/lib)
  300. execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory ${ep_base}/Install/include)
  301. include_directories(${ep_base}/Install/include)

  302. #=============================================================================
  303. # external modules
  304. #
  305. set(external_module_paths)
  306. if (NOT EXTERNAL_MODULES_LOCATION STREQUAL "")
  307.         message(STATUS "External modules: ${EXTERNAL_MODULES_LOCATION}")
  308.         add_subdirectory("${EXTERNAL_MODULES_LOCATION}/src" external_modules)

  309.         foreach(external_module ${config_module_list_external})
  310.                 add_subdirectory(${EXTERNAL_MODULES_LOCATION}/src/${external_module} external_modules/${external_module})
  311.                 list(APPEND external_module_paths ${EXTERNAL_MODULES_LOCATION}/src/${external_module})
  312.         endforeach()
  313. endif()

  314. #=============================================================================
  315. # Testing - Automatic unit and integration testing with CTest
  316. #

  317. # optionally enable cmake testing (supported only on posix)
  318. option(CMAKE_TESTING "Configure test targets" OFF)
  319. if(${PX4_CONFIG} STREQUAL "px4_sitl_test")
  320.         set(CMAKE_TESTING ON)
  321. endif()
  322. if(CMAKE_TESTING)
  323.         include(CTest) # sets BUILD_TESTING variable
  324. endif()

  325. # enable test filtering to run only specific tests with the ctest -R regex functionality
  326. set(TESTFILTER "" CACHE STRING "Filter string for ctest to selectively only run specific tests (ctest -R)")

  327. # if testing is enabled download and configure gtest
  328. list(APPEND CMAKE_MODULE_PATH ${PX4_SOURCE_DIR}/cmake/gtest/)
  329. include(px4_add_gtest)
  330. if(BUILD_TESTING)
  331.         include(gtest)

  332.         add_custom_target(test_results
  333.                         COMMAND GTEST_COLOR=1 ${CMAKE_CTEST_COMMAND} --output-on-failure -T Test -R ${TESTFILTER} USES_TERMINAL
  334.                         DEPENDS
  335.                                 px4
  336.                                 examples__dyn_hello
  337.                                 test_mixer_multirotor
  338.                         USES_TERMINAL
  339.                         COMMENT "Running tests"
  340.                         WORKING_DIRECTORY ${PX4_BINARY_DIR})
  341.         set_target_properties(test_results PROPERTIES EXCLUDE_FROM_ALL TRUE)
  342. endif()


  343. #=============================================================================
  344. # subdirectories
  345. #
  346. add_library(parameters_interface INTERFACE)

  347. include(px4_add_library)
  348. add_subdirectory(src/lib EXCLUDE_FROM_ALL)

  349. add_subdirectory(platforms/${PX4_PLATFORM}/src/px4)
  350. add_subdirectory(platforms EXCLUDE_FROM_ALL)
  351. add_subdirectory(src/modules/uORB EXCLUDE_FROM_ALL) # TODO: platform layer

  352. if(EXISTS "${PX4_BOARD_DIR}/CMakeLists.txt")
  353.         add_subdirectory(${PX4_BOARD_DIR})
  354. endif()

  355. foreach(module ${config_module_list})
  356.         add_subdirectory(src/${module})
  357. endforeach()

  358. # must be the last module before firmware
  359. add_subdirectory(src/lib/parameters EXCLUDE_FROM_ALL)
  360. target_link_libraries(parameters_interface INTERFACE parameters)

  361. # firmware added last to generate the builtin for included modules
  362. add_subdirectory(platforms/${PX4_PLATFORM})

  363. #=============================================================================
  364. # uORB graph generation: add a custom target 'uorb_graph'
  365. #
  366. set(uorb_graph_config ${PX4_BOARD})

  367. set(graph_module_list "")
  368. foreach(module ${config_module_list})
  369.         set(graph_module_list "${graph_module_list}" "--src-path" "src/${module}")
  370. endforeach()

  371. add_custom_command(OUTPUT ${uorb_graph_config}
  372.         COMMAND ${PYTHON_EXECUTABLE} ${PX4_SOURCE_DIR}/Tools/uorb_graph/create.py
  373.                 ${graph_module_list}
  374.                 --exclude-path src/examples
  375.                 --file ${PX4_SOURCE_DIR}/Tools/uorb_graph/graph_${uorb_graph_config}
  376.         WORKING_DIRECTORY ${PX4_SOURCE_DIR}
  377.         COMMENT "Generating uORB graph"
  378. )
  379. add_custom_target(uorb_graph DEPENDS ${uorb_graph_config})


  380. include(doxygen)
  381. include(metadata)
  382. include(package)

  383. # print size
  384. add_custom_target(size
  385.         COMMAND size [        DISCUZ_CODE_1        ]lt;TARGET_FILE:px4>
  386.         DEPENDS px4
  387.         WORKING_DIRECTORY ${PX4_BINARY_DIR}
  388.         USES_TERMINAL
  389.         )
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|计算机控制

GMT+8, 2024-4-24 07:02 , Processed in 0.051367 second(s), 18 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表