当前位置:   article > 正文

PX4编译文件 Makefile 剖析_makefile:39: *** you have to use git to download t

makefile:39: *** you have to use git to download this repository. aborting.

这篇很好的一点就是,make输入后双击tab列出了所有可以执行的命令!!!!!!!

之前一直不理解

make px4_fmu-v4_default

但是我推理了下,既然是make,那应该是首先找到此目录下的makeflie,这个应该是逃不掉的,所以可能关键在makefile里面?

下面截取一些关键部分

这就很好理解 make px4_sitl gazebo 了

转载自:https://blog.csdn.net/lipi37/article/details/102918032

PX4编译文件 Makefile 剖析

lipi37 2019-11-07 19:52:23

2108

收藏 13

分类专栏: 无人机 PX4 文章标签: PX4 飞控

版权

无人机 同时被 2 个专栏收录

4 篇文章 0 订阅

订阅专栏

PX4

7 篇文章 0 订阅

订阅专栏

PX4编译文件 Makefile 剖析

当我们执行 cd Firmware进入PX4源码目录, 然后make 的时候,我们会看到一串输出基本如下(第一次编译会有更多的输出)

  1. [ 2%] Built target df_driver_framework
  2. [ 3%] Built target lockstep_scheduler
  3. [ 3%] Built target uorb_headers
  4. [ 3%] Built target mixer_gen
  5. [ 3%] Built target mixer_gen_6dof
  6. [ 3%] Built target git_gps_devices
  7. [ 3%] Built target git_ecl
  8. [ 3%] Generating git version header
  9. [ 3%] Built target git_mavlink_v2
  10. [ 3%] Built target logs_symlink
  11. [ 3%] Built target perf
  12. [ 20%] Built target uorb_msgs
  13. [ 24%] Built target work_queue
  14. [ 24%] Built target tinybson
  15. [ 24%] Built target ecl_airdata
  16. [ 24%] Built target ecl_geo
  17. ...
  18. [ 87%] Built target modules__ekf2
  19. [ 89%] Built target modules__logger
  20. [ 90%] Built target modules__sensors
  21. [ 90%] Built target drivers__ledsim
  22. [ 91%] Built target FlightTaskAuto
  23. [ 91%] Built target FlightTaskManualAltitude
  24. [ 91%] Built target FlightTaskAutoMapper
  25. [ 91%] Built target modules__simulator
  26. [ 91%] Built target FlightTaskAutoMapper2
  27. [ 93%] Built target modules__mavlink
  28. [ 93%] Built target FlightTaskManualAltitudeSmooth
  29. [ 94%] Built target FlightTaskManualAltitudeSmoothVel
  30. [ 94%] Built target FlightTaskManualPosition
  31. [ 95%] Built target FlightTaskAutoLineSmoothVel
  32. [ 95%] Built target FlightTaskAutoLine
  33. [ 95%] Built target FlightTaskAutoFollowMe
  34. [ 95%] Built target FlightTaskOrbit
  35. [ 95%] Built target drivers__airspeedsim
  36. [ 95%] Built target drivers__accelsim
  37. [ 95%] Built target drivers__barosim
  38. [ 95%] Built target drivers__gpssim
  39. [ 96%] Built target drivers__gyrosim
  40. [ 96%] Built target FlightTaskManualPositionSmoothVel
  41. [ 96%] Built target FlightTaskManualPositionSmooth
  42. [ 97%] Built target FlightTasks
  43. [ 98%] Built target modules__mc_pos_control
  44. [ 98%] Linking CXX executable ../../bin/px4
  45. [ 98%] Built target px4
  46. [100%] Built target examples__dyn_hello

从输出看来PX4 默认的编译选项设置了很多的编译目标, 从输出中我们可以看到以 modules__ 开头的 ekf2,logger,mavlink 等,以 drivers__ 开头的 accelsim,barosim,gpssim 等,以及关键的主程序 px4. 这些不同的编译目标对应着 PX4 飞行栈的不同功能模块. 当我们在终端输入 make 并单击两次 tab 键时时我们得到下面的输出

  1. $ make
  2. Display all 126 possibilities? (y or n)
  3. aerotenna_ocpoc_ubuntu auav_esc35-v1_default eagle_rtps parrot_bebop_default px4_fmu-v4pro px4_sitl_test
  4. airframe_metadata auav_x21 emlid_navio2_cross posix px4_fmu-v4pro_default python_coverage
  5. airmind_mindpx-v2 auav_x21_default emlid_navio2_native posix_sitl_default px4_fmu-v4pro_rtps qgc_firmware
  6. airmind_mindpx-v2_default av_x-v1 excelsior_default px4_cannode-v1 px4_fmu-v4_rtps quick_check
  7. all av_x-v1_default excelsior_rtps px4_cannode-v1_default px4_fmu-v4_stackcheck rostest
  8. all_config_targets beaglebone_blue_cross format px4_esc-v1 px4_fmu-v5 rostest_run
  9. all_default_targets beaglebone_blue_native gazeboclean px4_esc-v1_default px4_fmu-v5_default scan-build
  10. alt_firmware bitcraze_crazyflie help px4fmu_firmware px4_fmu-v5_fixedwing shellcheck_all
  11. atlflight_cmake_hexagon_bundle bitcraze_crazyflie_default intel_aerofc-v1 px4_fmu-v2 px4_fmu-v5_multicopter sizes
  12. atlflight_cmake_hexagon_fastrpc check intel_aerofc-v1_default px4_fmu-v2_default px4_fmu-v5_rover submodulesclean
  13. atlflight_cmake_hexagon_hexagon_sdk check_format intel_aerofc-v1_rtps px4_fmu-v2_fixedwing px4_fmu-v5_rtps submodulesupdate
  14. atlflight_cmake_hexagon_linux_app check_rtps list_config_targets px4_fmu-v2_lpe px4_fmu-v5_stackcheck tests
  15. atlflight_cmake_hexagon_qurt_flags clang-tidy Makefile px4_fmu-v2_multicopter px4_io-v2 tests_avoidance
  16. atlflight_cmake_hexagon_qurt_lib clang-tidy-fix misc_qgc_extra_firmware px4_fmu-v2_rover px4_io-v2_default tests_coverage
  17. atlflight_eagle clang-tidy-quiet module_documentation px4_fmu-v2_test px4_metadata tests_mission
  18. atlflight_eagle_default clean nxp_fmuk66-v3 px4_fmu-v3 px4_raspberrypi_cross tests_mission_coverage
  19. atlflight_eagle_qurt-default coverity_scan nxp_fmuk66-v3_default px4_fmu-v3_default px4_raspberrypi_native tests_offboard
  20. atlflight_excelsior cppcheck omnibus_f4sd px4_fmu-v3_rtps px4_sitl thiemar_s2740vc-v1
  21. atlflight_excelsior_default distclean omnibus_f4sd_default px4_fmu-v3_stackcheck px4_sitl_default thiemar_s2740vc-v1_default
  22. atlflight_excelsior_qurt-default doxygen parameters_metadata px4_fmu-v4 px4_sitl_default-clang uorb_graphs
  23. auav_esc35-v1 eagle_default parrot_bebop px4_fmu-v4_default px4_sitl_rtps validate_module_configs

可以有很多不同的目标, 在PX4 的官方文档中比较详细的介绍了用于Nuttx的 px4_fmu-v*_default (其中 * 可以是2,3,4,5等代表不同的fmu 版本的数字), 以及 px4_sitl_default 用来在本机进行仿真的编译选项. 面对如此众多的选项, 本文的目的在于理清各个编译选项的具体内部操作.

1 PX4 的 Makefile 文件剖析

1.1 Makefile 背景知识

这里假设读者有一定的编程经验, 这一小节简述 Makefile 的一些基础概念和语法, 主要是参考了GNU make manualGNU make 项目实战. Makefile 可以视为 Make的操作指令文档, 指导 Make 成一系列的操作. Makefile 的语法杂糅了 c 以及 sh 的特点, 其中包括了基本的逻辑判断和变量定义语法. Makefile 中最核心的部分是 编译指导语句

  1. targets: prerequisites
  2. recipes

其中的 targets 是编译目标, prerequisites 是编译依赖项 (它可以依赖于其他一些更基础的依赖项), recipes 是要执行的一系列指令, 这些指令传给 shell 来执行. Makefile 要求 recipes 行的开头必须以一个 tab 开头, 同时如果一行太长的话可以通过 \ 符号进行分割, 下一行同样必须以 tab 开始, make 会自动把 \ 及上下的空白字符串(空格和tab) 替换为单个空格. 下面是Makefile 命令速查表.

Makefile 语法说明
:=变量赋值, 会递归展开
= 或者 define变量赋值, 不会递归展开
+=追加, 和 C 语言和 Python 中的 += 类似
error打印错误信息,并退出
wildcard通配符, 例:$(wildcard *.h) 返回匹配到的所有头文件
firstword获取第一个单词, 例$(firstword “abcd jijii”) 返回 abcd
wordlist获取指定从某个索引到到另一个索引的之间的单词, 例:$(wordlist 2, 5, “This is a test sentence”), 返回 “is” “a” “test” “sentencce”
words计算字符串中单词个数
lastword获取字符串中的最后一个单词
shell在 subshell 中执行命令, 例:$(shell echo “Hello world”) ,会输出 Hello world
subst截取字符串
strip去掉空格去掉字符串中开头和结尾的空字符,并将中间的多个连续空字符(如果有的话)合并为一个空字符
realpath获取文件的路径
findstring在字符串B, 中发现A
if类似于 C 中的 ? : 语句

此外, Makefile 中的.Phony 对象定义为Targets that do not refer to filles but are just actions are called phony targets (执行一些命令但不生成目标文件的 编译目标叫做 Phony target).

1.2 PX4 Makefile 文本剖析

这里为了引用方便, 在每段代码前面都保留了在文件中的行号. 首先前面的以 # 开头的32行是 Copyright 部分, 基本上是 BSD 协议的内容. 之后是 Makefile 代码的正文部分, 首先是检查当前目录是否是 git repo, 以确保源码的依赖关系没有被破坏, 可以在编译的过程中子模块的下载可以顺利进行.

  1. 34 # Enforce the presence of the GIT repository
  2. 35 #
  3. 36 # We depend on our submodules, so we have to prevent attempts to
  4. 37 # compile without it being present.
  5. 38 ifeq ($(wildcard .git),)
  6. 39 $(error YOU HAVE TO USE GIT TO DOWNLOAD THIS REPOSITORY. ABORTING.)
  7. 40 endif

接下来是帮助说明, 意思就是不要害怕这个 Makefile, 这个 Makefile 文件主要是用来向 cmake 传参的, 并列出了二次开发可能主要用到的 make px4_fmu-v2_default 等几个指令.

  1. 42 # Help
  2. 43 # --------------------------------------------------------------------
  3. 44 # Don't be afraid of this makefile, it is just passing
  4. 45 # arguments to cmake to allow us to keep the wiki pages etc.
  5. 46 # that describe how to build the px4 firmware
  6. 47 # the same even when using cmake instead of make.
  7. 48 #
  8. 49 # Example usage:
  9. 50 #
  10. 51 # make px4_fmu-v2_default (builds)
  11. 52 # make px4_fmu-v2_default upload (builds and uploads)
  12. 53 # make px4_fmu-v2_default test (builds and tests)
  13. 54 #
  14. 55 # This tells cmake to build the nuttx px4_fmu-v2 default config in the
  15. 56 # directory build/px4_fmu-v2_default and then call make
  16. 57 # in that directory with the target upload.```

接下来设置默认的编译目标, make 把遇到的第一个编译目标设置成默认目标, 这里的 targetall, 而prerequisitespx4_sitl_default,同时这里没有 recipes 部分, 对于没有 recipes 的编译目标, make 则总会尝试去更新他的依赖项, 这里是 px4_sitl_default

  1. 59 # explicity set default build target
  2. 60 all: px4_sitl_default

接下来是利用 subst 函数生成一个空格, subst 函数的用法为 $(subst 模式字符串, 替换字符串, 原始字符串), 在原始字符串中搜索模式字符串并用替换字符串替换.

  1. 62 # define a space character to be able to explicitly find it in strings
  2. 63 space := $(subst ,, )

这里通过make 的环境变量 $(MAKECMDGOALS)获取用户制定的编译目标, 并通过 $(firstword $(MAKECMDGOALS)) 函数获取第一个值, 用$(wordlist 2,$(words $(MAKECMDGOALS)), $(MAKECMDGOALS)) 第一个之外的其他目标值. 例如在终端中输入 > make px4_sitl_default gazebo 时, FIRST_ARG 将获取到px4_sitl_default , 而ARGS 将获取到 gazebo

  1. 65 # Parsing
  2. 66 # --------------------------------------------------------------------
  3. 67 # assume 1st argument passed is the main target, the
  4. 68 # rest are arguments to pass to the makefile generated
  5. 69 # by cmake in the subdirectory
  6. 70 FIRST_ARG := $(firstword $(MAKECMDGOALS))
  7. 71 ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS))

j 选项是设置开多少 jobs 进行 Make 并行执行

 72 j ?= 4

接着设置是否用构建工具 ninja 生成 CMake 文件. ifndf... else ... endif 含义为如果没有定义则执行, 否则执行... ,而ifdef... else... endif 则反之. 由于默认没有设置 NO_NINJA_BUILD 所以会尝试设置编译生成器为Ninja. 如果尝试失败了,则在 windows 上编译生成器目标会设置为 MSYS Makefiles, 在 Linux 上则会编译生成器目标会设置为Unix Makefiles. Ninja 是一个比 Make 要轻量级的编译系统.

  1. 74 NINJA_BIN := ninja
  2. 75 ifndef NO_NINJA_BUILD
  3. 76 NINJA_BUILD := $(shell $(NINJA_BIN) --version 2>/dev/null)
  4. 77
  5. 78 ifndef NINJA_BUILD
  6. 79 NINJA_BIN := ninja-build
  7. 80 NINJA_BUILD := $(shell $(NINJA_BIN) --version 2>/dev/null)
  8. 81 endif
  9. 82 endif
  10. 83
  11. 84 ifdef NINJA_BUILD
  12. 85 PX4_CMAKE_GENERATOR := Ninja
  13. 86 PX4_MAKE := $(NINJA_BIN)
  14. 87
  15. 88 ifdef VERBOSE
  16. 89 PX4_MAKE_ARGS := -v
  17. 90 else
  18. 91 PX4_MAKE_ARGS :=
  19. 92 endif
  20. 93 else
  21. 94 ifdef SYSTEMROOT
  22. 95 # Windows
  23. 96 PX4_CMAKE_GENERATOR := "MSYS\ Makefiles"
  24. 97 else
  25. 98 PX4_CMAKE_GENERATOR := "Unix\ Makefiles"
  26. 99 endif
  27. 100 PX4_MAKE = $(MAKE)
  28. 101 PX4_MAKE_ARGS = -j$(j) --no-print-directory
  29. 102 endif

然后, 获取了源码所在的路径, 首先用 lastword 函数获取了当前的 Makefile 的名称, 然后用realpath 获取了这个 Makefile 的完整路径, 最后调用 shell 的 dirname 命令获取了文件的目录路径.

104 SRC_DIR := $(shell dirname "$(realpath $(lastword $(MAKEFILE_LIST)))")

接下来设置了 $BUILD_DIR_SUFFIX变量, 上面注释说"依据是否定义了环境变量 replay 来决定build 目录是否加后缀", 为什么要有 replay 目前还不清楚…

  1. 106 # check if replay env variable is set & set build dir accordingly
  2. 107 ifdef replay
  3. 108 BUILD_DIR_SUFFIX := _replay
  4. 109 else
  5. 110 BUILD_DIR_SUFFIX :=
  6. 111 endif

接下来给 cmake 添加了一些选项. 包括是否包括外部模块, 及 cmake 的build 的类型

  1. 113 # additional config parameters passed to cmake
  2. 114 ifdef EXTERNAL_MODULES_LOCATION
  3. 115 CMAKE_ARGS += -DEXTERNAL_MODULES_LOCATION:STRING=$(EXTERNAL_MODULES_LOCATION)
  4. 116 endif
  5. 117
  6. 118 ifdef PX4_CMAKE_BUILD_TYPE
  7. 119 CMAKE_ARGS += -DCMAKE_BUILD_TYPE=${PX4_CMAKE_BUILD_TYPE}
  8. 120 else
  9. 121
  10. 122 # Address Sanitizer
  11. 123 ifdef PX4_ASAN
  12. 124 CMAKE_ARGS += -DCMAKE_BUILD_TYPE=AddressSanitizer
  13. 125 endif
  14. 126
  15. 127 # Memory Sanitizer
  16. 128 ifdef PX4_MSAN
  17. 129 CMAKE_ARGS += -DCMAKE_BUILD_TYPE=MemorySanitizer
  18. 130 endif
  19. 131
  20. 132 # Thread Sanitizer
  21. 133 ifdef PX4_TSAN
  22. 134 CMAKE_ARGS += -DCMAKE_BUILD_TYPE=ThreadSanitizer
  23. 135 endif
  24. 136
  25. 137 # Undefined Behavior Sanitizer
  26. 138 ifdef PX4_UBSAN
  27. 139 CMAKE_ARGS += -DCMAKE_BUILD_TYPE=UndefinedBehaviorSanitizer
  28. 140 endif
  29. 141
  30. 142 endif

接着这里定义了两个函数, 第一个是后面编译各个选项时主要用到的 cmake-build, 另一个是用来检查之前的cmake 编译生成的 cache 是否和当前的编译选项一致的cmake-cache-check. 在cmake-build 中调用了cmake-cache-check 来确定是否需要重新生成目录和编译配置文件.

  1. 144 # Functions
  2. 145 # --------------------------------------------------------------------
  3. 146 # describe how to build a cmake config
  4. 147 define cmake-build
  5. 148 @$(eval BUILD_DIR = "$(SRC_DIR)/build/$(1)")
  6. 149 @# check if the desired cmake configuration matches the cache then CMAKE_CACHE_CHECK stays empty
  7. 150 @$(call cmake-cache-check)
  8. 151 @# make sure to start from scratch when switching from GNU Make to Ninja
  9. 152 @if [ $(PX4_CMAKE_GENERATOR) = "Ninja" ] && [ -e $(BUILD_DIR)/Makefile ]; then rm -rf $(BUILD_DIR); fi
  10. 153 @# only excplicitly configure the first build, if cache file already exists the makefile will rerun cmake automatically if necessary
  11. 154 @if [ ! -e $(BUILD_DIR)/CMakeCache.txt ] || [ $(CMAKE_CACHE_CHECK) ]; then \
  12. 155 mkdir -p $(BUILD_DIR) \
  13. 156 && cd $(BUILD_DIR) \
  14. 157 && cmake "$(SRC_DIR)" -G"$(PX4_CMAKE_GENERATOR)" $(CMAKE_ARGS) \
  15. 158 || (rm -rf $(BUILD_DIR)); \
  16. 159 fi
  17. 160 @# run the build for the specified target
  18. 161 @cmake --build $(BUILD_DIR) -- $(PX4_MAKE_ARGS) $(ARGS)
  19. 162 endef
  20. 163
  21. 164 # check if the options we want to build with in CMAKE_ARGS match the ones which are already configured in the cache inside BUILD_DIR
  22. 165 define cmake-cache-check
  23. 166 @# change to build folder which fails if it doesn't exist and CACHED_CMAKE_OPTIONS stays empty
  24. 167 @# fetch all previously configured and cached options from the build folder and transform them into the OPTION=VALUE format without type (e.g. :BOOL)
  25. 168 @$(eval CACHED_CMAKE_OPTIONS = $(shell cd $(BUILD_DIR) 2>/dev/null && cmake -L 2>/dev/null | sed -n 's/\([^[:blank:]]*\):[^[:blank:]]*\(=[^[:blank:]]*\)/\1\2/gp' ))
  26. 169 @# transform the options in CMAKE_ARGS into the OPTION=VALUE format without -D
  27. 170 @$(eval DESIRED_CMAKE_OPTIONS = $(shell echo $(CMAKE_ARGS) | sed -n 's/-D\([^[:blank:]]*=[^[:blank:]]*\)/\1/gp' ))
  28. 171 @# find each currently desired option in the already cached ones making sure the complete configured string value is the same
  29. 172 @$(eval VERIFIED_CMAKE_OPTIONS = $(foreach option,$(DESIRED_CMAKE_OPTIONS),$(strip $(findstring $(option)$(space),$(CACHED_CMAKE_OPTIONS)))))
  30. 173 @# if the complete list of desired options is found in the list of verified options we don't need to reconfigure and CMAKE_CACHE_CHECK stays empty
  31. 174 @$(eval CMAKE_CACHE_CHECK = $(if $(findstring $(DESIRED_CMAKE_OPTIONS),$(VERIFIED_CMAKE_OPTIONS)),,y))
  32. 175 endef

下面给则定义了一个带颜色echo 的函数 colorecho

  1. 177 COLOR_BLUE = \033[0;94m
  2. 178 NO_COLOR = \033[m
  3. 179
  4. 180 define colorecho
  5. 181 +@echo -e '${COLOR_BLUE}${1} ${NO_COLOR}'
  6. 182 endef

获取所有的飞控的编译文件

  1. 184 # Get a list of all config targets boards/*/*.cmake
  2. 185 ALL_CONFIG_TARGETS := $(shell find boards -maxdepth 3 -mindepth 3 ! -name '*common*' ! -name '*sdflight*' -name '*.cmake' -print | sed -e 's/boards\///' | sed -e 's/\.cmake//' | sed -e 's/\//_/g' | sort)
  3. 186

接着定义了所有编译选项的默认编译方式和以_default结尾的编译选项的编译方式.

  1. 191 # All targets.
  2. 192 $(ALL_CONFIG_TARGETS):
  3. 193 @$(eval PX4_CONFIG = $@)
  4. 194 @$(eval CMAKE_ARGS += -DCONFIG=$(PX4_CONFIG))
  5. 195 @$(call cmake-build,$(PX4_CONFIG)$(BUILD_DIR_SUFFIX))
  6. 196
  7. 197 # Filter for only default targets to allow omiting the "_default" postfix
  8. 198 CONFIG_TARGETS_DEFAULT := $(patsubst %_default,%,$(filter %_default,$(ALL_CONFIG_TARGETS)))
  9. 199 $(CONFIG_TARGETS_DEFAULT):
  10. 200 @$(eval PX4_CONFIG = $@_default)
  11. 201 @$(eval CMAKE_ARGS += -DCONFIG=$(PX4_CONFIG))
  12. 202 @$(call cmake-build,$(PX4_CONFIG)$(BUILD_DIR_SUFFIX))
  13. 203
  14. 204 all_config_targets: $(ALL_CONFIG_TARGETS)
  15. 205 all_default_targets: $(CONFIG_TARGETS_DEFAULT)

定义了一个辅助函数, 和固件编译配置

  1. 209 # board reorganization deprecation warnings (2018-11-22)
  2. 210 define deprecation_warning
  3. 211 $(warning $(1) has been deprecated and will be removed, please use $(2)!)
  4. 212 endef
  5. 213
  6. 214 px4fmu-%_default:
  7. 215 $(call deprecation_warning, ${@},$(subst px4fmu,px4_fmu,$@))
  8. 216 $(MAKE) $(subst px4fmu,px4_fmu, $@)
  9. 217
  10. 218 posix_sitl_default:
  11. 219 $(call deprecation_warning, ${@},px4_sitl_default)
  12. 220 $(MAKE) px4_sitl_default
  13. 221
  14. 222 # All targets with just dependencies but no recipe must either be marked as phony (or have the special @: as recipe).
  15. 223 .PHONY: all posix px4_sitl_default all_config_targets all_default_targets

针对eagleexcelsior 的编译配置选项

  1. 225 # Multi- config targets.
  2. 226 eagle_default: atlflight_eagle_default atlflight_eagle_qurt-default
  3. 227 eagle_rtps: atlflight_eagle_rtps atlflight_eagle_qurt-rtps
  4. 228
  5. 229 excelsior_default: atlflight_excelsior_default atlflight_excelsior_qurt-default
  6. 230 excelsior_rtps: atlflight_excelsior_rtps atlflight_excelsior_qurt-rtps
  7. 231
  8. 232 .PHONY: eagle_default eagle_rtps
  9. 233 .PHONY: excelsior_default excelsior_rtps

分别设置 gqc 可以上传固件的编译选项

  1. 238 .PHONY: qgc_firmware px4fmu_firmware misc_qgc_extra_firmware alt_firmware check_rtps
  2. 239
  3. 240 # QGroundControl flashable NuttX firmware
  4. 241 qgc_firmware: px4fmu_firmware misc_qgc_extra_firmware
  5. 242
  6. 243 # px4fmu NuttX firmware
  7. 244 px4fmu_firmware: \
  8. 245 check_px4_io-v2_default \
  9. 246 check_px4_fmu-v2_default \
  10. 247 check_px4_fmu-v3_default \
  11. 248 check_px4_fmu-v4_default \
  12. 249 check_px4_fmu-v4pro_default \
  13. 250 check_px4_fmu-v5_default \
  14. 251 sizes
  15. 252
  16. 253 misc_qgc_extra_firmware: \
  17. 254 check_nxp_fmuk66-v3_default \
  18. 255 check_intel_aerofc-v1_default \
  19. 256 check_auav_x21_default \
  20. 257 check_bitcraze_crazyflie_default \
  21. 258 check_airmind_mindpx-v2_default \
  22. 259 check_px4_fmu-v2_lpe \
  23. 260 sizes
  24. 261
  25. 262 # Other NuttX firmware
  26. 263 alt_firmware: \
  27. 264 check_px4_cannode-v1_default \
  28. 265 check_px4_esc-v1_default \
  29. 266 check_auav_esc35-v1_default \
  30. 267 check_thiemar_s2740vc-v1_default \
  31. 268 sizes
  32. 269
  33. 270 # builds with RTPS
  34. 271 check_rtps: \
  35. 272 check_px4_fmu-v3_rtps \
  36. 273 check_px4_fmu-v4_rtps \
  37. 274 check_px4_fmu-v4pro_rtps \
  38. 275 check_px4_sitl_rtps \
  39. 276 sizes

获取 elf 文件的大小

  1. 278 .PHONY: sizes check quick_check check_rtps uorb_graphs
  2. 279
  3. 280 sizes:
  4. 281 @-find build -name *.elf -type f | xargs size 2> /dev/null || :

定义编译测试

  1. 283 # All default targets that don't require a special build environment
  2. 284 check: check_px4_sitl_default px4fmu_firmware misc_qgc_extra_firmware alt_firmware tests check_format
  3. 285
  4. 286 # quick_check builds a single nuttx and posix target, runs testing, and checks the style
  5. 287 quick_check: check_px4_sitl_test check_px4_fmu-v5_default tests check_format
  6. 288
  7. 289 check_%:
  8. 290 @echo
  9. 291 $(call colorecho,'Building' $(subst check_,,$@))
  10. 292 @$(MAKE) --no-print-directory $(subst check_,,$@)
  11. 293 @echo

创建uorb消息的订阅发布图

  1. 295 uorb_graphs:
  2. 296 @./Tools/uorb_graph/create_from_startupscript.sh
  3. 297 @./Tools/uorb_graph/create.py --src-path src --exclude-path src/examples --file Tools/uorb_graph/graph_full
  4. 298 @$(MAKE) --no-print-directory px4_fmu-v2_default uorb_graph
  5. 299 @$(MAKE) --no-print-directory px4_fmu-v4_default uorb_graph
  6. 300 @$(MAKE) --no-print-directory px4_sitl_default uorb_graph

下面是依据源码生成文档的编译选项

  1. 307 # Documentation
  2. 308 # --------------------------------------------------------------------
  3. 309 .PHONY: parameters_metadata airframe_metadata module_documentation px4_metadata doxygen
  4. 310
  5. 311 parameters_metadata:
  6. 312 @$(MAKE) --no-print-directory px4_sitl_default metadata_parameters
  7. 313
  8. 314 airframe_metadata:
  9. 315 @$(MAKE) --no-print-directory px4_sitl_default metadata_airframes
  10. 316
  11. 317 module_documentation:
  12. 318 @$(MAKE) --no-print-directory px4_sitl_default metadata_module_documentation
  13. 319
  14. 320 px4_metadata: parameters_metadata airframe_metadata module_documentation
  15. 321
  16. 322 doxygen:
  17. 323 @mkdir -p "$(SRC_DIR)"/build/doxygen
  18. 324 @cd "$(SRC_DIR)"/build/doxygen && cmake "$(SRC_DIR)" $(CMAKE_ARGS) -G"$(PX4_CMAKE_GENERATOR)" -DCONFIG=px4_sitl_default -DBUILD_DOXYGEN=ON
  19. 325 @$(PX4_MAKE) -C "$(SRC_DIR)"/build/doxygen
  20. 326 @touch "$(SRC_DIR)"/build/doxygen/Documentation/.nojekyll

格式化代码

  1. 328 # Astyle
  2. 329 # --------------------------------------------------------------------
  3. 330 .PHONY: check_format format
  4. 331
  5. 332 check_format:
  6. 333 $(call colorecho,'Checking formatting with astyle')
  7. 334 @"$(SRC_DIR)"/Tools/astyle/check_code_style_all.sh
  8. 335 @cd "$(SRC_DIR)" && git diff --check
  9. 336
  10. 337 format:
  11. 338 $(call colorecho,'Formatting with astyle')
  12. 339 @"$(SRC_DIR)"/Tools/astyle/check_code_style_all.sh --fix

一些测试编译配置

  1. 41 # Testing
  2. 342 # --------------------------------------------------------------------
  3. 343 .PHONY: tests tests_coverage tests_mission tests_mission_coverage tests_offboard tests_avoidance
  4. 344 .PHONY: rostest python_coverage
  5. 345
  6. 346 tests:
  7. 347 $(eval CMAKE_ARGS += -DCONFIG=px4_sitl_test)
  8. 348 $(eval CMAKE_ARGS += -DTESTFILTER=$(TESTFILTER))
  9. 349 $(eval ARGS += test_results)
  10. 350 $(eval ASAN_OPTIONS += color=always:check_initialization_order=1:detect_stack_use_after_return=1)
  11. 351 $(eval UBSAN_OPTIONS += color=always)
  12. 352 $(call cmake-build,px4_sitl_test)
  13. 353
  14. 354 tests_coverage:
  15. 355 @$(MAKE) clean
  16. 356 @$(MAKE) --no-print-directory px4_sitl_default test_coverage_genhtml PX4_CMAKE_BUILD_TYPE=Coverage
  17. 357 @echo "Open "$(SRC_DIR)"/build/px4_sitl_default/coverage-html/index.html to see coverage"
  18. 358
  19. 359 rostest: px4_sitl_default
  20. 360 @$(MAKE) --no-print-directory px4_sitl_default sitl_gazebo
  21. 361
  22. 362 tests_mission: rostest
  23. 363 @"$(SRC_DIR)"/test/rostest_px4_run.sh mavros_posix_tests_missions.test
  24. 364
  25. 365 rostest_run: px4_sitl_default
  26. 366 @$(MAKE) --no-print-directory px4_sitl_default sitl_gazebo
  27. 367 @"$(SRC_DIR)"/test/rostest_px4_run.sh $(TEST_FILE) mission:=$(TEST_MISSION) vehicle:=$(TEST_VEHICLE)
  28. 368
  29. 369 tests_mission_coverage:
  30. 370 @$(MAKE) clean
  31. 371 @$(MAKE) --no-print-directory px4_sitl_default PX4_CMAKE_BUILD_TYPE=Coverage
  32. 372 @$(MAKE) --no-print-directory px4_sitl_default sitl_gazebo PX4_CMAKE_BUILD_TYPE=Coverage
  33. 373 @"$(SRC_DIR)"/test/rostest_px4_run.sh mavros_posix_test_mission.test mission:=VTOL_mission_1 vehicle:=standard_vtol
  34. 374 @$(MAKE) --no-print-directory px4_sitl_default generate_coverage
  35. 375
  36. 376 tests_offboard: rostest
  37. 377 @"$(SRC_DIR)"/test/rostest_px4_run.sh mavros_posix_tests_offboard_attctl.test
  38. 378 @"$(SRC_DIR)"/test/rostest_px4_run.sh mavros_posix_tests_offboard_posctl.test
  39. 379
  40. 380 tests_avoidance: rostest
  41. 381 @"$(SRC_DIR)"/test/rostest_avoidance_run.sh mavros_posix_test_avoidance.test
  42. 382
  43. 383 python_coverage:
  44. 384 @mkdir -p "$(SRC_DIR)"/build/python_coverage
  45. 385 @cd "$(SRC_DIR)"/build/python_coverage && cmake "$(SRC_DIR)" $(CMAKE_ARGS) -G"$(PX4_CMAKE_GENERATOR)" -DCONFIG=px4_sitl_default -DPYTHON_COVERAGE=ON
  46. 386 @$(PX4_MAKE) -C "$(SRC_DIR)"/build/python_coverage
  47. 387 @$(PX4_MAKE) -C "$(SRC_DIR)"/build/python_coverage metadata_airframes
  48. 388 @$(PX4_MAKE) -C "$(SRC_DIR)"/build/python_coverage metadata_parameters
  49. 389 #@$(PX4_MAKE) -C "$(SRC_DIR)"/build/python_coverage module_documentation # TODO: fix within coverage.py
  50. 390 @coverage combine `find . -name .coverage\*`
  51. 391 @coverage report -m

静态编译分析

  1. 394 # static analyzers (scan-build, clang-tidy, cppcheck)
  2. 395 # --------------------------------------------------------------------
  3. 396 .PHONY: scan-build px4_sitl_default-clang clang-tidy clang-tidy-fix clang-tidy-quiet
  4. 397 .PHONY: cppcheck shellcheck_all validate_module_configs
  5. 398
  6. 399 scan-build:
  7. 400 @export CCC_CC=clang
  8. 401 @export CCC_CXX=clang++
  9. 402 @rm -rf "$(SRC_DIR)"/build/px4_sitl_default-scan-build
  10. 403 @rm -rf "$(SRC_DIR)"/build/scan-build/report_latest
  11. 404 @mkdir -p "$(SRC_DIR)"/build/px4_sitl_default-scan-build
  12. 405 @cd "$(SRC_DIR)"/build/px4_sitl_default-scan-build && scan-build cmake "$(SRC_DIR)" -GNinja -DCONFIG=px4_sitl_default
  13. 406 @scan-build -o "$(SRC_DIR)"/build/scan-build cmake --build "$(SRC_DIR)"/build/px4_sitl_default-scan-build
  14. 407 @find "$(SRC_DIR)"/build/scan-build -maxdepth 1 -mindepth 1 -type d -exec cp -r "{}" "$(SRC_DIR)"/build/scan-build/report_latest \;
  15. 408
  16. 409 px4_sitl_default-clang:
  17. 410 @mkdir -p "$(SRC_DIR)"/build/px4_sitl_default-clang
  18. 411 @cd "$(SRC_DIR)"/build/px4_sitl_default-clang && cmake "$(SRC_DIR)" $(CMAKE_ARGS) -G"$(PX4_CMAKE_GENERATOR)" -DCONFIG=px4_sitl_default -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++
  19. 412 @$(PX4_MAKE) -C "$(SRC_DIR)"/build/px4_sitl_default-clang
  20. 413
  21. 414 clang-tidy: px4_sitl_default-clang
  22. 415 @cd "$(SRC_DIR)"/build/px4_sitl_default-clang && "$(SRC_DIR)"/Tools/run-clang-tidy.py -header-filter=".*\.hpp" -j$(j) -p .
  23. 416
  24. 417 # to automatically fix a single check at a time, eg modernize-redundant-void-arg
  25. 418 # % run-clang-tidy-4.0.py -fix -j4 -checks=-\*,modernize-redundant-void-arg -p .
  26. 419 clang-tidy-fix: px4_sitl_default-clang
  27. 420 @cd "$(SRC_DIR)"/build/px4_sitl_default-clang && "$(SRC_DIR)"/Tools/run-clang-tidy.py -header-filter=".*\.hpp" -j$(j) -fix -p .
  28. 421
  29. 422 # modified version of run-clang-tidy.py to return error codes and only output relevant results
  30. 423 clang-tidy-quiet: px4_sitl_default-clang
  31. 424 @cd "$(SRC_DIR)"/build/px4_sitl_default-clang && "$(SRC_DIR)"/Tools/run-clang-tidy.py -header-filter=".*\.hpp" -j$(j) -p .
  32. 425
  33. 426 # TODO: Fix cppcheck errors then try --enable=warning,performance,portability,style,unusedFunction or --enable=all
  34. 427 cppcheck: px4_sitl_default
  35. 428 @mkdir -p "$(SRC_DIR)"/build/cppcheck
  36. 429 @cppcheck -i"$(SRC_DIR)"/src/examples --enable=performance --std=c++11 --std=c99 --std=posix --project="$(SRC_DIR)"/build/px4_sitl_default/compile_commands.json --xml-version=2 2> "$(SRC_DIR)"/build/cppcheck/cppcheck-result.xml > /dev/null
  37. 430 @cppcheck-htmlreport --source-encoding=ascii --file="$(SRC_DIR)"/build/cppcheck/cppcheck-result.xml --report-dir="$(SRC_DIR)"/build/cppcheck --source-dir="$(SRC_DIR)"/src/
  38. 431
  39. 432 shellcheck_all:
  40. 433 @"$(SRC_DIR)"/Tools/run-shellcheck.sh "$(SRC_DIR)"/ROMFS/px4fmu_common/
  41. 434 @make px4_fmu-v5_default shellcheck
  42. 435
  43. 436 validate_module_configs:
  44. 437 @find "$(SRC_DIR)"/src/modules "$(SRC_DIR)"/src/drivers "$(SRC_DIR)"/src/lib -name *.yaml -type f -print0 | xargs -0 "$(SRC_DIR)"/Tools/validate_yaml.py --schema-file "$(SRC_DIR)"/validation/module_schema.yaml
  45. 438

clean 选项, 主要用来清理上一次的编译结果

  1. 439 # Cleanup
  2. 440 # --------------------------------------------------------------------
  3. 441 .PHONY: clean submodulesclean submodulesupdate gazeboclean distclean
  4. 442
  5. 443 clean:
  6. 444 @rm -rf "$(SRC_DIR)"/build
  7. 445
  8. 446 submodulesclean:
  9. 447 @git submodule foreach --quiet --recursive git clean -ff -x -d
  10. 448 @git submodule update --quiet --init --recursive --force || true
  11. 449 @git submodule sync --recursive
  12. 450 @git submodule update --init --recursive --force
  13. 451
  14. 452 submodulesupdate:
  15. 453 @git submodule update --quiet --init --recursive || true
  16. 454 @git submodule sync --recursive
  17. 455 @git submodule update --init --recursive
  18. 456
  19. 457 gazeboclean:
  20. 458 @rm -rf ~/.gazebo/*
  21. 459
  22. 460 distclean: gazeboclean
  23. 461 @git submodule deinit -f .
  24. 462 @git clean -ff -x -d -e ".project" -e ".cproject" -e ".idea" -e ".settings" -e ".vscode"

帮助文件, 列出所有可以选的编译选项

  1. 464 # Help / Error
  2. 465 # --------------------------------------------------------------------
  3. 466
  4. 467 # All other targets are handled by PX4_MAKE. Add a rule here to avoid printing an error.
  5. 468 %:
  6. 469 $(if $(filter $(FIRST_ARG),$@), \
  7. 470 $(error "$@ cannot be the first argument. Use '$(MAKE) help|list_config_targets' to get a list of all possible [configuration] targets."),@#)
  8. 471
  9. 472 # Print a list of non-config targets (based on http://stackoverflow.com/a/26339924/1487069)
  10. 473 help:
  11. 474 @echo "Usage: $(MAKE) <target>"
  12. 475 @echo "Where <target> is one of:"
  13. 476 @$(MAKE) -pRrq -f $(lastword $(MAKEFILE_LIST)) : 2>/dev/null | \
  14. 477 awk -v RS= -F: '/^# File/,/^# Finished Make data base/ {if ($$1 !~ "^[#.]") {print $$1}}' | sort | \
  15. 478 egrep -v -e '^[^[:alnum:]]' -e '^($(subst $(space),|,$(ALL_CONFIG_TARGETS)))$$' -e '_default$$' -e '^(posix|eagle|Makefile)'
  16. 479 @echo
  17. 480 @echo "Or, $(MAKE) <config_target> [<make_target(s)>]"
  18. 481 @echo "Use '$(MAKE) list_config_targets' for a list of configuration targets."
  19. 482
  20. 483 # Print a list of all config targets.
  21. 484 list_config_targets:
  22. 485 @for targ in $(patsubst %_default,%[_default],$(ALL_CONFIG_TARGETS)); do echo $$targ; done

2 “make px4_sitl_default” 背后发生了什么

首先 make 命令读入参数 px4_sitl_default, 同时读入当前目录下的Makefile, 在Makefile 中搜寻px4_sitl_default 编译配置条目, 找到了$(ALL_CONFIG_TARGET), 这里的$(ALL_CONFIG_TARGET) 包含了所有的编译配置文件, 但是下面的$(CONFIG_TARGETS_DEFAULT) 也包含了 px4_sitl_default, 基于Makefile 规范, 后面出现的规则会覆盖之前出现的规则, 所以px4_stil_defaultrecipes

200 @$(eval PX4_CONFIG = $@_default)

201 @$(eval CMAKE_ARGS += -DCONFIG=$(PX4_CONFIG))

202 @$(call cmake-build,$(PX4_CONFIG)$(BUILD_DIR_SUFFIX))

其中$@ 代表的是工作目标, 这里即px4_sitl, 第一句为设置 PX4_CONFIG 的值为px4_sitl_default, 第二句为添加编译选项-DCONFIG=px4_sitl_default , 第三句为调用 cmake-build 函数, 并传入参数$(PX4_CONFIG)$(BUILD_DIR_SUFFIX), 由于$(BUILD_DIR_SUFFIX) 的值为空, 则传入的参数为px4_sitl_default. cmake_build 的定义再次附录如下,

  1. 147 define cmake-build
  2. 148 @$(eval BUILD_DIR = "$(SRC_DIR)/build/$(1)")
  3. 149 @# check if the desired cmake configuration matches the cache then CMAKE_CACHE_CHECK stays empty
  4. 150 @$(call cmake-cache-check)
  5. 151 @# make sure to start from scratch when switching from GNU Make to Ninja
  6. 152 @if [ $(PX4_CMAKE_GENERATOR) = "Ninja" ] && [ -e $(BUILD_DIR)/Makefile ]; then rm -rf $(BUILD_DIR); fi
  7. 153 @# only excplicitly configure the first build, if cache file already exists the makefile will rerun cmake automatically if necessary
  8. 154 @if [ ! -e $(BUILD_DIR)/CMakeCache.txt ] || [ $(CMAKE_CACHE_CHECK) ]; then \
  9. 155 mkdir -p $(BUILD_DIR) \
  10. 156 && cd $(BUILD_DIR) \
  11. 157 && cmake "$(SRC_DIR)" -G"$(PX4_CMAKE_GENERATOR)" $(CMAKE_ARGS) \
  12. 158 || (rm -rf $(BUILD_DIR)); \
  13. 159 fi
  14. 160 @# run the build for the specified target
  15. 161 @cmake --build $(BUILD_DIR) -- $(PX4_MAKE_ARGS) $(ARGS)
  16. 162 endef

第一句首先设置 BUILD_DIR$(SRC_DIR)/build/$(1), 即path/to/Firmware/build/px4_sitl_default, 其中$(1) 引用了第一个参数, 即px4_sitl_default; 第二句解释了第三句的主要功能是检查是否之前编译过该目标, 如果没有或者和上一次编译该目标的一些编译选项设置的不一样就设置 就创建该BUILD_DIR 这里是 path/to/Firmware/build/px4_sitl_default, 并进入, 然后执行cmake "/path/to/source/" -G"Ninja" -DCONFIG=px4_sitl_default 来读取CMakeLists.txt 生成Ninja 的编译配置文件; 第四句主要是清理编译目标文件; 第五到十一句判断当前是否需要生成编译配置文件; 第十二到第十三句执行cmake --build /path/to/Firmware/build/px4_sitl_default --, 其中 $(PX4_MAKE_ARGS)$(ARGS) 并替换成了空值. 在执行完这些之后,我们可以执行

  1. $ cd build/px4_sitl_default
  2. $ ninja gazebo

发现结果和make px4_sitl_default gazebo 结果一样, 整个编译过程大概是 Make 调用了 CMake, 然后CMake 调用了Ninja 读取 CMakeLists.txt 文件生成编译配置文件, 然后CMake 再调用 Ninja 进行编译.

>>> 下一步 CMakeLists.txt分析

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/797514
推荐阅读
相关标签
  

闽ICP备14008679号