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 #============================================================================= 35 # CMAKE CODING STANDARD FOR PX4 36 # 37 # Structure 38 # --------------------------------------------------------------------------- 39 # 40 # * Common functions should be included in px_base.cmake. 41 # 42 # * OS/ board specific fucntions should be include in 43 # px_impl_${OS}.cmake or px4_impl_${OS}_${BOARD}.cmake. 44 # 45 # Formatting 46 # --------------------------------------------------------------------------- 47 # 48 # * Use hard indents to match the px4 source code. 49 # 50 # * All function and script arguments are upper case. 51 # 52 # * All local variables are lower case. 53 # 54 # * All cmake functions are lowercase. 55 # 56 # * For else, endif, endfunction, etc, never put the name of the statement 57 # 58 # Functions/Macros 59 # --------------------------------------------------------------------------- 60 # 61 # * Use px4_parse_function_args to parse functions and check for required 62 # arguments. Unless there is only one argument in the function and it is clear. 63 # 64 # * Never use macros. They allow overwriting global variables and this 65 # makes variable declarations hard to locate. 66 # 67 # * If a target from add_custom_* is set in a function, explicitly pass it 68 # as an output argument so that the target name is clear to the user. 69 # 70 # * Avoid use of global variables in functions. Functions in a nested 71 # scope may use global variables, but this makes it difficult to 72 # resuse functions. 73 # 74 # Included CMake Files 75 # --------------------------------------------------------------------------- 76 # 77 # * All variables in config files must have the prefix "config_". 78 # 79 # * Never set global variables in an included cmake file, 80 # you may only define functions. This excludes config and Toolchain files. 81 # This makes it clear to the user when variables are being set or targets 82 # are being created. 83 # 84 # * Setting a global variable in a CMakeLists.txt file is ok, because 85 # each CMakeLists.txt file has scope in the current directory and all 86 # subdirectories, so it is not truly global. 87 # 88 # * All toolchain files should be included in the cmake 89 # directory and named Toolchain-"name".cmake. 90 # 91 # Misc 92 # --------------------------------------------------------------------------- 93 # 94 # * If referencing a string variable, don't put it in quotes. 95 # Don't do "${OS}" STREQUAL "posix", 96 # instead type ${OS} STREQUAL "posix". This will throw an 97 # error when ${OS} is not defined instead of silently 98 # evaluating to false. 99 # 100 #============================================================================= 101 102 if (${CMAKE_VERSION} VERSION_LESS 3.1.0) //判断cmake版本不能低于3.1.0 103 message("Not a valid CMake version") 104 message("On Ubuntu >= 16.04, install or upgrade via:") 105 message(" sudo apt-get install cmake") 106 message("") 107 message("Official website: https://cmake.org/download/") 108 message(FATAL_ERROR "Update CMake and try again" ) 109 endif() 110 111 # Warning: Changing this modifies CMake's internal workings 112 # and leads to wrong toolchain detection 113 cmake_minimum_required(VERSION 3.1 FATAL_ERROR) 114 115 set(PX4_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}") //设置PX4源文件目录,CMAKE_CURRENT_SOURCE_DIR当前的源文件目录 116 set(PX4_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}") //设置运行程序目录,这个是cmake当前正在build的目录 117 118 execute_process( //执行一个或多个子进程 119 COMMAND Tools/check_submodules.sh //运行了tools/check_submodules.sh 120 WORKING_DIRECTORY ${PX4_SOURCE_DIR} //execute_process的测试目录WORKING_DIRECTORY上面shell运行目录设为PX4_SOURCE_DIR是上面设置的 121 ) 122 123 #============================================================================= 124 # configuration 125 # 126 # must come before project to set toolchain 127 128 set(CONFIG "posix_sitl_default" CACHE STRING "desired configuration") //这里设置了CONFIG为"posix_sitl_default"设置了CACHE STRING的话,可以在cmake-gui中看到名字"desired configurration" 129 130 string(REPLACE "_" ";" config_args ${CONFIG}) //把CONFIG中的"_"替换成";"并保存到config_args(不知道为什么小写,所以config_args应该是posix;sitl;default 131 list(GET config_args 0 OS) //把config_args第一个参数放到OS中 132 list(GET config_args 1 BOARD) //第二个参数放到BORAD 133 list(GET config_args 2 LABEL) //第三个参数放到LABEL中 134 set(target_name "${OS}-${BOARD}-${LABEL}") //然后把target设置成OS-BORAD-LABEL。那我觉得用string(REPLACE "_" "-" target_name ${CONFIG})应该也可以实现 135 136 file(GLOB_RECURSE configs RELATIVE cmake/configs "cmake/configs/*.cmake") //GLOB_RECURSE可以在所有子目录(cmake/configs)中找到*cmake文件并和cmake/configs组合存到config中? 137 set_property(CACHE CONFIG PROPERTY STRINGS ${configs}) //这里的CONFIG在文档中是entry不知道干什么的,config设置到STRINGS中? 138 139 set(THREADS "4" CACHE STRING "number of threads to use for external build processes") //THREADS设为4 140 set(DEBUG_PORT "/dev/ttyACM0" CACHE STRING "debugging port") //DEBG_PORT设为"dev/ttyACM0" 141 set(EXTERNAL_MODULES_LOCATION "" CACHE STRING "External modules source location") //EXTERNAL_MODULES_LOCATION为"",后面可以翻译为“外部模块源位置?” 142 143 if (NOT EXTERNAL_MODULES_LOCATION STREQUAL "") //如果EXTERNAL_MODULE_LOCATION不是"" 144 get_filename_component(EXTERNAL_MODULES_LOCATION "${EXTERNAL_MODULES_LOCATION}" ABSOLUTE) //变成绝对路径 145 endif() 146 147 list(APPEND CMAKE_MODULE_PATH "${PX4_SOURCE_DIR}/cmake") //把${PX4_SOURCE_DIR/cmake加进 CMAKE_MODULE_PATH中 148 message(STATUS "CMAKE_MODULE_PATH: ${CMAKE_MODULE_PATH}") //再显示出来CMAKE_MODULE_PATH中 149 set(config_module "configs/${CONFIG}") //又设置了config_module "configs/${CONFIG}" 150 include(${config_module}) //这里包含了其他目录下的cmake list文件 151 152 include(common/coverage) 153 include(common/sanitizers) 154 155 # CMake build type 156 # Debug Release RelWithDebInfo MinSizeRel Coverage 157 if (NOT CMAKE_BUILD_TYPE) //如果CMAKE_BUILD_TYPE非空 158 if (${OS} STREQUAL "nuttx") //如果OS为nuttx 159 set(PX4_BUILD_TYPE "MinSizeRel") //PX4_BUILD_TYPE设置为MinSizeRe1 160 elseif (${OS} STREQUAL "bebop") //如果OS为bebop 161 set(PX4_BUILD_TYPE "MinSizeRel") //PX4_BUILD_TYPE设置为MinSizeRe1 162 else() 163 set(PX4_BUILD_TYPE "RelWithDebInfo") //其他设置为RelWithDebInfo 164 endif() 165 166 set(CMAKE_BUILD_TYPE ${PX4_BUILD_TYPE} CACHE STRING "Build type" FORCE) //把PX4_BUILD_TYPE写入CMAKE_BUILD_TYPE 167 endif() 168 169 set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug;Release;RelWithDebInfo;MinSizeRel;Coverage") //设置CMAKE_BUILD_TYPE条目属性为"Debug;Release;RelWidthDebInfo;MiniSizeRel;Coverage" 170 171 message(STATUS "Build Type: ${CMAKE_BUILD_TYPE}") 172 173 174 #============================================================================= 175 # git 176 # 177 include(common/px4_git) //这里应该包含了common/px4_git.cmake 178 179 execute_process( 180 COMMAND git describe --always --tags //运行git describe --always --tags 181 OUTPUT_VARIABLE git_tag //会保存输出到git_tag 182 OUTPUT_STRIP_TRAILING_WHITESPACE 183 WORKING_DIRECTORY ${PX4_SOURCE_DIR} //git工作目录 184 ) 185 186 execute_process( 187 COMMAND Tools/tag_to_version.py --root ${PX4_SOURCE_DIR} 运行Tools/tag_tp_version.py --root ..... 188 OUTPUT_VARIABLE git_version 189 WORKING_DIRECTORY ${PX4_SOURCE_DIR} 190 ) 191 192 px4_add_git_submodule(TARGET git_cmake_hexagon PATH "cmake/cmake_hexagon") //添加git submodule目标 193 px4_add_git_submodule(TARGET git_driverframework PATH "src/lib/DriverFramework") 194 px4_add_git_submodule(TARGET git_ecl PATH "src/lib/ecl") 195 px4_add_git_submodule(TARGET git_gazebo PATH "Tools/sitl_gazebo") 196 px4_add_git_submodule(TARGET git_gazebo_flow PATH "Tools/sitl_gazebo/external/OpticalFlow") 197 px4_add_git_submodule(TARGET git_gazebo_klt PATH "Tools/sitl_gazebo/external/OpticalFlow/external/klt_feature_tracker") 198 px4_add_git_submodule(TARGET git_gencpp PATH "Tools/gencpp") 199 px4_add_git_submodule(TARGET git_genmsg PATH "Tools/genmsg") 200 px4_add_git_submodule(TARGET git_gps_devices PATH "src/drivers/gps/devices") 201 px4_add_git_submodule(TARGET git_jmavsim PATH "Tools/jMAVSim") 202 px4_add_git_submodule(TARGET git_matrix PATH "src/lib/matrix") 203 px4_add_git_submodule(TARGET git_mavlink PATH "mavlink/include/mavlink/v1.0") 204 px4_add_git_submodule(TARGET git_mavlink2 PATH "mavlink/include/mavlink/v2.0") 205 px4_add_git_submodule(TARGET git_nuttx PATH "NuttX") 206 px4_add_git_submodule(TARGET git_uavcan PATH "src/modules/uavcan/libuavcan") 207 208 px4_create_git_hash_header() 209 210 #============================================================================= 211 212 message(STATUS "PX4 VERSION: ${git_tag}") 213 message(STATUS "CONFIG: ${target_name}") 214 215 # The URL for the elf file for crash logging //elf文件crash log的URL(uniform resource locator) 216 if (DEFINED ENV{BUILD_URI}) //确定ENV{BUILD_URI}是否被定义 217 set(BUILD_URI $ENV{BUILD_URI}) 218 else() 219 set(BUILD_URI "localhost") //设置BUILD_URI为localhost 220 endif() 221 222 add_definitions(-DBUILD_URI=${BUILD_URI}) //定义了一个BUILD_URI宏 223 224 # Define GNU standard installation directories //确定GNU标准安装目录 225 include(GNUInstallDirs) 226 227 # Add support for external project building //增加外部项目构建支持 228 include(ExternalProject) 229 230 # Setup install paths //设置install路径 231 if (NOT CMAKE_INSTALL_PREFIX) //如果没有CMAKE_INSTALL_PREFIX 232 if (${OS} STREQUAL "posix") //posix系统 233 set(CMAKE_INSTALL_PREFIX "/usr" CACHE PATH "Install path prefix" FORCE) //CMAKE_INSTALL_PREFIX是"/user" 234 endif() 235 endif() 236 if (CMAKE_INSTALL_PREFIX) //输出CMAKE_INSTALL_PREFIX 237 message(STATUS "CMAKE_INSTALL_PREFIX: ${CMAKE_INSTALL_PREFIX}") 238 endif() 239 240 #============================================================================= 241 # require px4 module interface //要求px4模块接口 242 set(px4_required_interface 243 px4_os_prebuild_targets 244 px4_os_add_flags 245 ) 246 foreach(cmd ${px4_required_interface}) //轮询px4_required_interface 247 if (NOT COMMAND ${cmd}) //如果里面没有COMMAND指令 248 message(FATAL_ERROR "${config_module} must implement ${cmd}") 249 endif() 250 endforeach() 251 252 set(px4_required_config config_module_list) 253 foreach(conf ${px4_required_config}) //又设置了conf,判断参数是否被定义 254 if (NOT DEFINED ${conf}) 255 message(FATAL_ERROR "cmake/${config_module} must define ${conf}") 256 endif() 257 endforeach() 258 259 # force static lib build //强制静态库build 260 set(BUILD_SHARED_LIBS OFF) //关闭共享库 261 262 #============================================================================= 263 # ccache //高速编译工具 264 # 265 option(CCACHE "Use ccache if available" OFF) 266 find_program(CCACHE_PROGRAM ccache) 267 if (CCACHE AND CCACHE_PROGRAM) 268 message(STATUS "Enabled ccache: ${CCACHE_PROGRAM}") 269 set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${CCACHE_PROGRAM}") 270 endif() 271 272 #============================================================================= 273 # project definition //项目定义 274 # 275 project(px4 CXX C ASM) //项目名px4 语言cxx c asm 276 277 set(package-contact "px4users@googlegroups.com") //这是联系方式? 278 279 #============================================================================= 280 # find programs and packages //寻找程序和包 281 # 282 283 # see if catkin was invoked to build this //如果魏国catkin构建这个 284 if (CATKIN_DEVEL_PREFIX) 285 message(STATUS "catkin ENABLED") 286 find_package(catkin REQUIRED) 287 if (catkin_FOUND) 288 catkin_package() 289 else() 290 message(FATAL_ERROR "catkin not found") 291 endif() 292 endif() 293 294 find_package(PythonInterp REQUIRED) 295 px4_find_python_module(jinja2 REQUIRED) 296 297 #============================================================================= 298 # cmake testing //cmake测试 299 # 300 enable_testing() 301 include(CTest) 302 303 #============================================================================= 304 # generate compile command database 生成编译命令数据库 305 # 306 set(CMAKE_EXPORT_COMPILE_COMMANDS ON) 307 308 #============================================================================= 309 # check required toolchain variables //确认工具链的值 310 # 311 312 # PX4 requires c++11 //px4需要c++11 313 set(CMAKE_CXX_STANDARD 11) 314 set(CMAKE_CXX_STANDARD_REQUIRED ON) 315 316 # PX4 requires c99 //需要c99 317 set(CMAKE_C_STANDARD 99) 318 set(CMAKE_C_STANDARD_REQUIRED ON) 319 320 set(required_variables CMAKE_C_COMPILER_ID CMAKE_CXX_COMPILER_ID) 321 foreach(var ${required_variables}) 322 if (NOT ${var}) 323 message(FATAL_ERROR "Toolchain/config must define ${var}") 324 endif() 325 endforeach() 326 327 # print full c compiler version //打印c编译器版本 328 execute_process(COMMAND ${CMAKE_C_COMPILER} --version 329 OUTPUT_VARIABLE c_compiler_version 330 OUTPUT_STRIP_TRAILING_WHITESPACE 331 ) 332 STRING(REGEX MATCH "[^ ]*" c_compiler_version_short ${c_compiler_version}) 333 message(STATUS "C compiler: ${c_compiler_version_short}") 334 335 # print full c++ compiler version //打印c++编译器版本 336 execute_process(COMMAND ${CMAKE_CXX_COMPILER} --version 337 OUTPUT_VARIABLE cxx_compiler_version 338 OUTPUT_STRIP_TRAILING_WHITESPACE 339 ) 340 STRING(REGEX MATCH "[^ ]*" cxx_compiler_version_short ${cxx_compiler_version}) 341 message(STATUS "C++ compiler: ${cxx_compiler_version_short}") 342 343 #============================================================================= 344 # external libraries //外部库 345 # 346 px4_os_prebuild_targets(OUT prebuild_targets 347 BOARD ${BOARD} 348 THREADS ${THREADS}) 349 350 #============================================================================= 351 # build flags //构建的flags 352 # 353 px4_os_add_flags( //这里没看懂 354 BOARD ${BOARD} 355 C_FLAGS c_flags 356 CXX_FLAGS cxx_flags 357 OPTIMIZATION_FLAGS optimization_flags 358 EXE_LINKER_FLAGS exe_linker_flags 359 INCLUDE_DIRS include_dirs 360 LINK_DIRS link_dirs 361 DEFINITIONS definitions) 362 363 px4_join(OUT CMAKE_EXE_LINKER_FLAGS LIST "${exe_linker_flags}" GLUE " ") 364 px4_join(OUT CMAKE_C_FLAGS LIST "${c_flags};${optimization_flags}" GLUE " ") 365 px4_join(OUT CMAKE_CXX_FLAGS LIST "${cxx_flags};${optimization_flags}" GLUE " ") 366 367 include_directories(${include_dirs}) //包含都文件路径 368 #message("INCLUDE_DIRS=${include_dirs}") 369 link_directories(${link_dirs}) 370 add_definitions(${definitions}) //增加定义 371 372 #============================================================================= 373 # message, and airframe generation //消息和生成机身 374 # 375 376 include(common/px4_metadata) //包含px4_metadata.cmake 377 378 add_subdirectory(msg) //增加一个子目录msg目录 379 px4_generate_messages(TARGET msg_gen 380 MSG_FILES ${msg_files} 381 OS ${OS} 382 INCLUDES ${msg_include_paths} 383 DEPENDS git_genmsg git_gencpp prebuild_targets 384 ) 385 386 px4_generate_airframes_xml(BOARD ${BOARD}) 387 388 #============================================================================= 389 # DriverFramework 390 # 391 392 # List the DriverFramework drivers 393 if (DEFINED config_df_driver_list) 394 message("DF Drivers: ${config_df_driver_list}") 395 endif() 396 397 set(df_driver_libs) 398 foreach(driver ${config_df_driver_list}) //把驱动的目录加进去了 399 add_subdirectory(src/lib/DriverFramework/drivers/${driver}) 400 list(APPEND df_driver_libs df_${driver}) 401 message("Adding DF driver: ${driver}") 402 endforeach() 403 404 #============================================================================= 405 # external projects //外部工程 406 # 407 408 set(ep_base ${PX4_BINARY_DIR}/external) 409 set_property(DIRECTORY PROPERTY EP_BASE ${ep_base}) 410 411 # add external project install folders to build //增加外部工程安装文件到build 412 link_directories(${ep_base}/Install/lib) 413 include_directories(${ep_base}/Install/include) 414 # add the directories so cmake won't warn //增加目录使cmake不会警告 415 execute_process(COMMAND cmake -E make_directory ${ep_base}/Install/lib) 416 execute_process(COMMAND cmake -E make_directory ${ep_base}/Install/include) 417 418 #============================================================================= 419 # external modules //外部模块 420 # 421 if (NOT EXTERNAL_MODULES_LOCATION STREQUAL "") 422 message(STATUS "External modules: ${EXTERNAL_MODULES_LOCATION}") 423 add_subdirectory("${EXTERNAL_MODULES_LOCATION}/src" external_modules_src) 424 425 set(config_module_list_external_expanded) 426 foreach(external_module ${config_module_list_external}) 427 list(APPEND config_module_list_external_expanded 428 ${EXTERNAL_MODULES_LOCATION}/src/${external_module}) 429 endforeach() 430 set(config_module_list 431 ${config_module_list} 432 ${config_module_list_external_expanded} 433 ) 434 endif() 435 436 #============================================================================= 437 # subdirectories //子目录 438 # 439 set(module_libraries) 440 foreach(module ${config_module_list}) 441 string(REGEX MATCH "^[./]" external_module ${module}) 442 if (external_module) 443 STRING(REGEX REPLACE "//" "/" EXT_MODULE ${module}) 444 STRING(REGEX REPLACE "/" "__" EXT_MODULE_PREFIX ${EXT_MODULE}) 445 add_subdirectory(${module} ${PX4_BINARY_DIR}/${EXT_MODULE_PREFIX}) 446 else() 447 add_subdirectory(src/${module}) 448 endif() 449 px4_mangle_name(${module} mangled_name) 450 list(APPEND module_libraries ${mangled_name}) 451 endforeach() 452 453 # Keep track of external shared libs required for modules //track模块需要的外部共享库lib 454 set(module_external_libraries "${module_external_libraries}" CACHE INTERNAL "module_external_libraries") 455 456 add_subdirectory(src/firmware/${OS}) 457 458 if (config_io_board) 459 add_subdirectory(src/modules/px4iofirmware) 460 endif() 461 462 #============================================================================= 463 # generate custom target to print for all executable and module cmake targets //这里开始编译生成了? 464 # 465 if (all_posix_cmake_targets) 466 list(SORT all_posix_cmake_targets) 467 px4_join(OUT posix_cmake_target_list LIST ${all_posix_cmake_targets} GLUE "\n") 468 add_custom_target(list_cmake_targets 469 COMMAND sh -c "printf "${posix_cmake_target_list}\n"" 470 COMMENT "List of cmake targets that can be matched by PX4_NO_OPTIMIZATION:" 471 VERBATIM 472 ) 473 endif() 474 475 #============================================================================= 476 # packaging //打包程序 477 # 478 # Important to having packaging at end of cmake file. //cmake文件结尾的重要打包工具 479 # 480 set(CPACK_PACKAGE_NAME ${PROJECT_NAME}-${CONFIG}) 481 set(CPACK_PACKAGE_VERSION ${git_version}) 482 set(CPACK_PACKAGE_CONTACT ${package-contact}) 483 set(CPACK_DEBIAN_PACKAGE_SHLIBDEPS ON) 484 set(CPACK_DEBIAN_PACKAGE_SECTION "devel") 485 set(CPACK_DEBIAN_PACKAGE_PRIORITY "optional") 486 set(short-description "The px4 autopilot.") 487 set(CPACK_DEBIAN_PACKAGE_DESCRIPTION ${short-description}) 488 set(CPACK_GENERATOR "ZIP") 489 set(CPACK_PACKAGE_FILE_NAME "${PROJECT_NAME}-${CONFIG}-${git_tag}") 490 set(CPACK_SOURCE_PACKAGE_FILE_NAME "${PROJECT_NAME}-${git_version}") 491 set(CPACK_SOURCE_GENERATOR "ZIP;TBZ2") 492 set(CPACK_PACKAGING_INSTALL_PREFIX "") 493 set(CPACK_SET_DESTDIR "OFF") 494 if ("${CMAKE_SYSTEM}" MATCHES "Linux") 495 find_program(DPKG_PROGRAM dpkg) 496 if (EXISTS ${DPKG_PROGRAM}) 497 list (APPEND CPACK_GENERATOR "DEB") 498 endif() 499 endif() 500 include(CPack) 501 502 # vim: set noet fenc=utf-8 ff=unix ft=cmake :