px4的CMakelists.txt阅读
2024-10-21 03:43:36
############################################################################
#
# Copyright (c) PX4 Development Team. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# . Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# . Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# . Neither the name PX4 nor the names of its contributors may be
# used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
############################################################################ #=============================================================================
# CMAKE CODING STANDARD FOR PX4
#
# Structure
# ---------------------------------------------------------------------------
#
# * Common functions should be included in px_base.cmake.
#
# * OS/ board specific fucntions should be include in
# px_impl_${OS}.cmake or px4_impl_${OS}_${BOARD}.cmake.
#
# Formatting
# ---------------------------------------------------------------------------
#
# * Use hard indents to match the px4 source code.
#
# * All function and script arguments are upper case.
#
# * All local variables are lower case.
#
# * All cmake functions are lowercase.
#
# * For else, endif, endfunction, etc, never put the name of the statement
#
# Functions/Macros
# ---------------------------------------------------------------------------
#
# * Use px4_parse_function_args to parse functions and check for required
# arguments. Unless there is only one argument in the function and it is clear.
#
# * Never use macros. They allow overwriting global variables and this
# makes variable declarations hard to locate.
#
# * If a target from add_custom_* is set in a function, explicitly pass it
# as an output argument so that the target name is clear to the user.
#
# * Avoid use of global variables in functions. Functions in a nested
# scope may use global variables, but this makes it difficult to
# resuse functions.
#
# Included CMake Files
# ---------------------------------------------------------------------------
#
# * All variables in config files must have the prefix "config_".
#
# * Never set global variables in an included cmake file,
# you may only define functions. This excludes config and Toolchain files.
# This makes it clear to the user when variables are being set or targets
# are being created.
#
# * Setting a global variable in a CMakeLists.txt file is ok, because
# each CMakeLists.txt file has scope in the current directory and all
# subdirectories, so it is not truly global.
#
# * All toolchain files should be included in the cmake
# directory and named Toolchain-"name".cmake.
#
# Misc
# ---------------------------------------------------------------------------
#
# * If referencing a string variable, don't put it in quotes.
# Don't do "${OS}" STREQUAL "posix",
# instead type ${OS} STREQUAL "posix". This will throw an
# error when ${OS} is not defined instead of silently
# evaluating to false.
#
#============================================================================= if (${CMAKE_VERSION} VERSION_LESS 3.1.) //判断cmake版本不能低于3.1.0
message("Not a valid CMake version")
message("On Ubuntu >= 16.04, install or upgrade via:")
message(" sudo apt-get install cmake")
message("")
message("Official website: https://cmake.org/download/")
message(FATAL_ERROR "Update CMake and try again" )
endif() # Warning: Changing this modifies CMake's internal workings
# and leads to wrong toolchain detection
cmake_minimum_required(VERSION 3.1 FATAL_ERROR) set(PX4_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}") //设置PX4源文件目录,CMAKE_CURRENT_SOURCE_DIR当前的源文件目录
set(PX4_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}") //设置运行程序目录,这个是cmake当前正在build的目录 execute_process( //执行一个或多个子进程
COMMAND Tools/check_submodules.sh //运行了tools/check_submodules.sh
WORKING_DIRECTORY ${PX4_SOURCE_DIR} //execute_process的测试目录WORKING_DIRECTORY上面shell运行目录设为PX4_SOURCE_DIR是上面设置的
) #=============================================================================
# configuration
#
# must come before project to set toolchain set(CONFIG "posix_sitl_default" CACHE STRING "desired configuration") //这里设置了CONFIG为"posix_sitl_default"设置了CACHE STRING的话,可以在cmake-gui中看到名字"desired configurration" string(REPLACE "_" ";" config_args ${CONFIG}) //把CONFIG中的"_"替换成";"并保存到config_args(不知道为什么小写,所以config_args应该是posix;sitl;default
list(GET config_args OS) //把config_args第一个参数放到OS中
list(GET config_args BOARD) //第二个参数放到BORAD
list(GET config_args LABEL) //第三个参数放到LABEL中
set(target_name "${OS}-${BOARD}-${LABEL}") //然后把target设置成OS-BORAD-LABEL。那我觉得用string(REPLACE "_" "-" target_name ${CONFIG})应该也可以实现 file(GLOB_RECURSE configs RELATIVE cmake/configs "cmake/configs/*.cmake") //GLOB_RECURSE可以在所有子目录(cmake/configs)中找到*cmake文件并和cmake/configs组合存到config中?
set_property(CACHE CONFIG PROPERTY STRINGS ${configs}) //这里的CONFIG在文档中是entry不知道干什么的,config设置到STRINGS中? set(THREADS "" CACHE STRING "number of threads to use for external build processes") //THREADS设为4
set(DEBUG_PORT "/dev/ttyACM0" CACHE STRING "debugging port") //DEBG_PORT设为"dev/ttyACM0"
set(EXTERNAL_MODULES_LOCATION "" CACHE STRING "External modules source location") //EXTERNAL_MODULES_LOCATION为"",后面可以翻译为“外部模块源位置?” if (NOT EXTERNAL_MODULES_LOCATION STREQUAL "") //如果EXTERNAL_MODULE_LOCATION不是""
get_filename_component(EXTERNAL_MODULES_LOCATION "${EXTERNAL_MODULES_LOCATION}" ABSOLUTE) //变成绝对路径
endif() list(APPEND CMAKE_MODULE_PATH "${PX4_SOURCE_DIR}/cmake") //把${PX4_SOURCE_DIR/cmake加进 CMAKE_MODULE_PATH中
message(STATUS "CMAKE_MODULE_PATH: ${CMAKE_MODULE_PATH}") //再显示出来CMAKE_MODULE_PATH中
set(config_module "configs/${CONFIG}") //又设置了config_module "configs/${CONFIG}"
include(${config_module}) //这里包含了其他目录下的cmake list文件 include(common/coverage)
include(common/sanitizers) # CMake build type
# Debug Release RelWithDebInfo MinSizeRel Coverage
if (NOT CMAKE_BUILD_TYPE) //如果CMAKE_BUILD_TYPE非空
if (${OS} STREQUAL "nuttx") //如果OS为nuttx
set(PX4_BUILD_TYPE "MinSizeRel") //PX4_BUILD_TYPE设置为MinSizeRe1
elseif (${OS} STREQUAL "bebop") //如果OS为bebop
set(PX4_BUILD_TYPE "MinSizeRel") //PX4_BUILD_TYPE设置为MinSizeRe1
else()
set(PX4_BUILD_TYPE "RelWithDebInfo") //其他设置为RelWithDebInfo
endif() set(CMAKE_BUILD_TYPE ${PX4_BUILD_TYPE} CACHE STRING "Build type" FORCE) //把PX4_BUILD_TYPE写入CMAKE_BUILD_TYPE
endif() set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug;Release;RelWithDebInfo;MinSizeRel;Coverage") //设置CMAKE_BUILD_TYPE条目属性为"Debug;Release;RelWidthDebInfo;MiniSizeRel;Coverage" message(STATUS "Build Type: ${CMAKE_BUILD_TYPE}") #=============================================================================
# git
#
include(common/px4_git) //这里应该包含了common/px4_git.cmake execute_process(
COMMAND git describe --always --tags //运行git describe --always --tags
OUTPUT_VARIABLE git_tag //会保存输出到git_tag
OUTPUT_STRIP_TRAILING_WHITESPACE
WORKING_DIRECTORY ${PX4_SOURCE_DIR} //git工作目录
) execute_process(
COMMAND Tools/tag_to_version.py --root ${PX4_SOURCE_DIR} 运行Tools/tag_tp_version.py --root .....
OUTPUT_VARIABLE git_version
WORKING_DIRECTORY ${PX4_SOURCE_DIR}
) px4_add_git_submodule(TARGET git_cmake_hexagon PATH "cmake/cmake_hexagon") //添加git submodule目标
px4_add_git_submodule(TARGET git_driverframework PATH "src/lib/DriverFramework")
px4_add_git_submodule(TARGET git_ecl PATH "src/lib/ecl")
px4_add_git_submodule(TARGET git_gazebo PATH "Tools/sitl_gazebo")
px4_add_git_submodule(TARGET git_gazebo_flow PATH "Tools/sitl_gazebo/external/OpticalFlow")
px4_add_git_submodule(TARGET git_gazebo_klt PATH "Tools/sitl_gazebo/external/OpticalFlow/external/klt_feature_tracker")
px4_add_git_submodule(TARGET git_gencpp PATH "Tools/gencpp")
px4_add_git_submodule(TARGET git_genmsg PATH "Tools/genmsg")
px4_add_git_submodule(TARGET git_gps_devices PATH "src/drivers/gps/devices")
px4_add_git_submodule(TARGET git_jmavsim PATH "Tools/jMAVSim")
px4_add_git_submodule(TARGET git_matrix PATH "src/lib/matrix")
px4_add_git_submodule(TARGET git_mavlink PATH "mavlink/include/mavlink/v1.0")
px4_add_git_submodule(TARGET git_mavlink2 PATH "mavlink/include/mavlink/v2.0")
px4_add_git_submodule(TARGET git_nuttx PATH "NuttX")
px4_add_git_submodule(TARGET git_uavcan PATH "src/modules/uavcan/libuavcan") px4_create_git_hash_header() #============================================================================= message(STATUS "PX4 VERSION: ${git_tag}")
message(STATUS "CONFIG: ${target_name}") # The URL for the elf file for crash logging //elf文件crash log的URL(uniform resource locator)
if (DEFINED ENV{BUILD_URI}) //确定ENV{BUILD_URI}是否被定义
set(BUILD_URI $ENV{BUILD_URI})
else()
set(BUILD_URI "localhost") //设置BUILD_URI为localhost
endif() add_definitions(-DBUILD_URI=${BUILD_URI}) //定义了一个BUILD_URI宏 # Define GNU standard installation directories //确定GNU标准安装目录
include(GNUInstallDirs) # Add support for external project building //增加外部项目构建支持
include(ExternalProject) # Setup install paths //设置install路径
if (NOT CMAKE_INSTALL_PREFIX) //如果没有CMAKE_INSTALL_PREFIX
if (${OS} STREQUAL "posix") //posix系统
set(CMAKE_INSTALL_PREFIX "/usr" CACHE PATH "Install path prefix" FORCE) //CMAKE_INSTALL_PREFIX是"/user"
endif()
endif()
if (CMAKE_INSTALL_PREFIX) //输出CMAKE_INSTALL_PREFIX
message(STATUS "CMAKE_INSTALL_PREFIX: ${CMAKE_INSTALL_PREFIX}")
endif() #=============================================================================
# require px4 module interface //要求px4模块接口
set(px4_required_interface
px4_os_prebuild_targets
px4_os_add_flags
)
foreach(cmd ${px4_required_interface}) //轮询px4_required_interface
if (NOT COMMAND ${cmd}) //如果里面没有COMMAND指令
message(FATAL_ERROR "${config_module} must implement ${cmd}")
endif()
endforeach() set(px4_required_config config_module_list)
foreach(conf ${px4_required_config}) //又设置了conf,判断参数是否被定义
if (NOT DEFINED ${conf})
message(FATAL_ERROR "cmake/${config_module} must define ${conf}")
endif()
endforeach() # force static lib build //强制静态库build
set(BUILD_SHARED_LIBS OFF) //关闭共享库 #=============================================================================
# ccache //高速编译工具
#
option(CCACHE "Use ccache if available" OFF)
find_program(CCACHE_PROGRAM ccache)
if (CCACHE AND CCACHE_PROGRAM)
message(STATUS "Enabled ccache: ${CCACHE_PROGRAM}")
set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${CCACHE_PROGRAM}")
endif() #=============================================================================
# project definition //项目定义
#
project(px4 CXX C ASM) //项目名px4 语言cxx c asm set(package-contact "px4users@googlegroups.com") //这是联系方式? #=============================================================================
# find programs and packages //寻找程序和包
# # see if catkin was invoked to build this //如果魏国catkin构建这个
if (CATKIN_DEVEL_PREFIX)
message(STATUS "catkin ENABLED")
find_package(catkin REQUIRED)
if (catkin_FOUND)
catkin_package()
else()
message(FATAL_ERROR "catkin not found")
endif()
endif() find_package(PythonInterp REQUIRED)
px4_find_python_module(jinja2 REQUIRED) #=============================================================================
# cmake testing //cmake测试
#
enable_testing()
include(CTest) #=============================================================================
# generate compile command database 生成编译命令数据库
#
set(CMAKE_EXPORT_COMPILE_COMMANDS ON) #=============================================================================
# check required toolchain variables //确认工具链的值
# # PX4 requires c++11 //px4需要c++11
set(CMAKE_CXX_STANDARD )
set(CMAKE_CXX_STANDARD_REQUIRED ON) # PX4 requires c99 //需要c99
set(CMAKE_C_STANDARD )
set(CMAKE_C_STANDARD_REQUIRED ON) set(required_variables CMAKE_C_COMPILER_ID CMAKE_CXX_COMPILER_ID)
foreach(var ${required_variables})
if (NOT ${var})
message(FATAL_ERROR "Toolchain/config must define ${var}")
endif()
endforeach() # print full c compiler version //打印c编译器版本
execute_process(COMMAND ${CMAKE_C_COMPILER} --version
OUTPUT_VARIABLE c_compiler_version
OUTPUT_STRIP_TRAILING_WHITESPACE
)
STRING(REGEX MATCH "[^\n]*" c_compiler_version_short ${c_compiler_version})
message(STATUS "C compiler: ${c_compiler_version_short}") # print full c++ compiler version //打印c++编译器版本
execute_process(COMMAND ${CMAKE_CXX_COMPILER} --version
OUTPUT_VARIABLE cxx_compiler_version
OUTPUT_STRIP_TRAILING_WHITESPACE
)
STRING(REGEX MATCH "[^\n]*" cxx_compiler_version_short ${cxx_compiler_version})
message(STATUS "C++ compiler: ${cxx_compiler_version_short}") #=============================================================================
# external libraries //外部库
#
px4_os_prebuild_targets(OUT prebuild_targets
BOARD ${BOARD}
THREADS ${THREADS}) #=============================================================================
# build flags //构建的flags
#
px4_os_add_flags( //这里没看懂
BOARD ${BOARD}
C_FLAGS c_flags
CXX_FLAGS cxx_flags
OPTIMIZATION_FLAGS optimization_flags
EXE_LINKER_FLAGS exe_linker_flags
INCLUDE_DIRS include_dirs
LINK_DIRS link_dirs
DEFINITIONS definitions) px4_join(OUT CMAKE_EXE_LINKER_FLAGS LIST "${exe_linker_flags}" GLUE " ")
px4_join(OUT CMAKE_C_FLAGS LIST "${c_flags};${optimization_flags}" GLUE " ")
px4_join(OUT CMAKE_CXX_FLAGS LIST "${cxx_flags};${optimization_flags}" GLUE " ") include_directories(${include_dirs}) //包含都文件路径
#message("INCLUDE_DIRS=${include_dirs}")
link_directories(${link_dirs})
add_definitions(${definitions}) //增加定义 #=============================================================================
# message, and airframe generation //消息和生成机身
# include(common/px4_metadata) //包含px4_metadata.cmake add_subdirectory(msg) //增加一个子目录msg目录
px4_generate_messages(TARGET msg_gen
MSG_FILES ${msg_files}
OS ${OS}
INCLUDES ${msg_include_paths}
DEPENDS git_genmsg git_gencpp prebuild_targets
) px4_generate_airframes_xml(BOARD ${BOARD}) #=============================================================================
# DriverFramework
# # List the DriverFramework drivers
if (DEFINED config_df_driver_list)
message("DF Drivers: ${config_df_driver_list}")
endif() set(df_driver_libs)
foreach(driver ${config_df_driver_list}) //把驱动的目录加进去了
add_subdirectory(src/lib/DriverFramework/drivers/${driver})
list(APPEND df_driver_libs df_${driver})
message("Adding DF driver: ${driver}")
endforeach() #=============================================================================
# external projects //外部工程
# set(ep_base ${PX4_BINARY_DIR}/external)
set_property(DIRECTORY PROPERTY EP_BASE ${ep_base}) # add external project install folders to build //增加外部工程安装文件到build
link_directories(${ep_base}/Install/lib)
include_directories(${ep_base}/Install/include)
# add the directories so cmake won't warn //增加目录使cmake不会警告
execute_process(COMMAND cmake -E make_directory ${ep_base}/Install/lib)
execute_process(COMMAND cmake -E make_directory ${ep_base}/Install/include) #=============================================================================
# external modules //外部模块
#
if (NOT EXTERNAL_MODULES_LOCATION STREQUAL "")
message(STATUS "External modules: ${EXTERNAL_MODULES_LOCATION}")
add_subdirectory("${EXTERNAL_MODULES_LOCATION}/src" external_modules_src) set(config_module_list_external_expanded)
foreach(external_module ${config_module_list_external})
list(APPEND config_module_list_external_expanded
${EXTERNAL_MODULES_LOCATION}/src/${external_module})
endforeach()
set(config_module_list
${config_module_list}
${config_module_list_external_expanded}
)
endif() #=============================================================================
# subdirectories //子目录
#
set(module_libraries)
foreach(module ${config_module_list})
string(REGEX MATCH "^[./]" external_module ${module})
if (external_module)
STRING(REGEX REPLACE "//" "/" EXT_MODULE ${module})
STRING(REGEX REPLACE "/" "__" EXT_MODULE_PREFIX ${EXT_MODULE})
add_subdirectory(${module} ${PX4_BINARY_DIR}/${EXT_MODULE_PREFIX})
else()
add_subdirectory(src/${module})
endif()
px4_mangle_name(${module} mangled_name)
list(APPEND module_libraries ${mangled_name})
endforeach() # Keep track of external shared libs required for modules //track模块需要的外部共享库lib
set(module_external_libraries "${module_external_libraries}" CACHE INTERNAL "module_external_libraries") add_subdirectory(src/firmware/${OS}) if (config_io_board)
add_subdirectory(src/modules/px4iofirmware)
endif() #=============================================================================
# generate custom target to print for all executable and module cmake targets //这里开始编译生成了?
#
if (all_posix_cmake_targets)
list(SORT all_posix_cmake_targets)
px4_join(OUT posix_cmake_target_list LIST ${all_posix_cmake_targets} GLUE "\\n")
add_custom_target(list_cmake_targets
COMMAND sh -c "printf \"${posix_cmake_target_list}\\n\""
COMMENT "List of cmake targets that can be matched by PX4_NO_OPTIMIZATION:"
VERBATIM
)
endif() #=============================================================================
# packaging //打包程序
#
# Important to having packaging at end of cmake file. //cmake文件结尾的重要打包工具
#
set(CPACK_PACKAGE_NAME ${PROJECT_NAME}-${CONFIG})
set(CPACK_PACKAGE_VERSION ${git_version})
set(CPACK_PACKAGE_CONTACT ${package-contact})
set(CPACK_DEBIAN_PACKAGE_SHLIBDEPS ON)
set(CPACK_DEBIAN_PACKAGE_SECTION "devel")
set(CPACK_DEBIAN_PACKAGE_PRIORITY "optional")
set(short-description "The px4 autopilot.")
set(CPACK_DEBIAN_PACKAGE_DESCRIPTION ${short-description})
set(CPACK_GENERATOR "ZIP")
set(CPACK_PACKAGE_FILE_NAME "${PROJECT_NAME}-${CONFIG}-${git_tag}")
set(CPACK_SOURCE_PACKAGE_FILE_NAME "${PROJECT_NAME}-${git_version}")
set(CPACK_SOURCE_GENERATOR "ZIP;TBZ2")
set(CPACK_PACKAGING_INSTALL_PREFIX "")
set(CPACK_SET_DESTDIR "OFF")
if ("${CMAKE_SYSTEM}" MATCHES "Linux")
find_program(DPKG_PROGRAM dpkg)
if (EXISTS ${DPKG_PROGRAM})
list (APPEND CPACK_GENERATOR "DEB")
endif()
endif()
include(CPack) # vim: set noet fenc=utf- ff=unix ft=cmake :
最新文章
- bzoj 3110 K大数查询
- Netsuite >; Foreign Currency Revaluation 外币评估
- JS命名空间实例
- Web 安全测试
- 引入Fresco
- API
- Spring MVC 学习资料
- ASP.NET MVC4学习笔记路由系统实现
- Expat Parser解析xml文件
- HDU 5965 扫雷 【模拟】 (2016年中国大学生程序设计竞赛(合肥))
- 编程之美 两个叶子的节点之间 最大距离 变种 leecode
- Java Networking: InetAddress
- 用户管理_组管理_设置主机名_UGO_文件高级权限_ACL权限
- M-移动端的webapp页面布局教程和webapp实战分析
- java.lang.IllegalArgumentException异常 返回值类型的问题
- 编码原则 之 Once and Only Once
- RTK与差分测量的区别
- JSP中客户端跳转与服务器端跳转的区别
- 数据分析面试题之Pandas中的groupby
- List集合遍历整理
热门文章
- php ord()函数 语法
- php str_repeat()函数 语法
- 组合的输出(回溯、dfs)
- 5 August
- The Tree-planting Day and Simple Disjoint Sets
- JS:收集的一些Array及String原型对象的扩展实现代码
- 六. jenkins部署springboot项目(3)--windows环境--远程windows server服务器
- Linux(Ubuntu)常用命令(五)—— vi/vim常用操作
- b/s 起点
- Java + selenium 元素定位(1)之By id/Name/ClassName