当我们执行 cd Firmware
进入PX4源码目录, 然后make
的时候,我们会看到一串输出基本如下(第一次编译会有更多的输出)
[ 2%] Built target df_driver_framework
[ 3%] Built target lockstep_scheduler
[ 3%] Built target uorb_headers
[ 3%] Built target mixer_gen
[ 3%] Built target mixer_gen_6dof
[ 3%] Built target git_gps_devices
[ 3%] Built target git_ecl
[ 3%] Generating git version header
[ 3%] Built target git_mavlink_v2
[ 3%] Built target logs_symlink
[ 3%] Built target perf
[ 20%] Built target uorb_msgs
[ 24%] Built target work_queue
[ 24%] Built target tinybson
[ 24%] Built target ecl_airdata
[ 24%] Built target ecl_geo
...
[ 87%] Built target modules__ekf2
[ 89%] Built target modules__logger
[ 90%] Built target modules__sensors
[ 90%] Built target drivers__ledsim
[ 91%] Built target FlightTaskAuto
[ 91%] Built target FlightTaskManualAltitude
[ 91%] Built target FlightTaskAutoMapper
[ 91%] Built target modules__simulator
[ 91%] Built target FlightTaskAutoMapper2
[ 93%] Built target modules__mavlink
[ 93%] Built target FlightTaskManualAltitudeSmooth
[ 94%] Built target FlightTaskManualAltitudeSmoothVel
[ 94%] Built target FlightTaskManualPosition
[ 95%] Built target FlightTaskAutoLineSmoothVel
[ 95%] Built target FlightTaskAutoLine
[ 95%] Built target FlightTaskAutoFollowMe
[ 95%] Built target FlightTaskOrbit
[ 95%] Built target drivers__airspeedsim
[ 95%] Built target drivers__accelsim
[ 95%] Built target drivers__barosim
[ 95%] Built target drivers__gpssim
[ 96%] Built target drivers__gyrosim
[ 96%] Built target FlightTaskManualPositionSmoothVel
[ 96%] Built target FlightTaskManualPositionSmooth
[ 97%] Built target FlightTasks
[ 98%] Built target modules__mc_pos_control
[ 98%] Linking CXX executable ../../bin/px4
[ 98%] Built target px4
[100%] Built target examples__dyn_hello
从输出看来PX4 默认的编译选项设置了很多的编译目标, 从输出中我们可以看到以 modules__
开头的 ekf2,logger,mavlink
等,以 drivers__
开头的 accelsim,barosim,gpssim
等,以及关键的主程序 px4
. 这些不同的编译目标对应着 PX4
飞行栈的不同功能模块. 当我们在终端输入 make
并单击两次 tab 键时时我们得到下面的输出
$ make
Display all 126 possibilities? (y or n)
aerotenna_ocpoc_ubuntu auav_esc35-v1_default eagle_rtps parrot_bebop_default px4_fmu-v4pro px4_sitl_test
airframe_metadata auav_x21 emlid_navio2_cross posix px4_fmu-v4pro_default python_coverage
airmind_mindpx-v2 auav_x21_default emlid_navio2_native posix_sitl_default px4_fmu-v4pro_rtps qgc_firmware
airmind_mindpx-v2_default av_x-v1 excelsior_default px4_cannode-v1 px4_fmu-v4_rtps quick_check
all av_x-v1_default excelsior_rtps px4_cannode-v1_default px4_fmu-v4_stackcheck rostest
all_config_targets beaglebone_blue_cross format px4_esc-v1 px4_fmu-v5 rostest_run
all_default_targets beaglebone_blue_native gazeboclean px4_esc-v1_default px4_fmu-v5_default scan-build
alt_firmware bitcraze_crazyflie help px4fmu_firmware px4_fmu-v5_fixedwing shellcheck_all
atlflight_cmake_hexagon_bundle bitcraze_crazyflie_default intel_aerofc-v1 px4_fmu-v2 px4_fmu-v5_multicopter sizes
atlflight_cmake_hexagon_fastrpc check intel_aerofc-v1_default px4_fmu-v2_default px4_fmu-v5_rover submodulesclean
atlflight_cmake_hexagon_hexagon_sdk check_format intel_aerofc-v1_rtps px4_fmu-v2_fixedwing px4_fmu-v5_rtps submodulesupdate
atlflight_cmake_hexagon_linux_app check_rtps list_config_targets px4_fmu-v2_lpe px4_fmu-v5_stackcheck tests
atlflight_cmake_hexagon_qurt_flags clang-tidy Makefile px4_fmu-v2_multicopter px4_io-v2 tests_avoidance
atlflight_cmake_hexagon_qurt_lib clang-tidy-fix misc_qgc_extra_firmware px4_fmu-v2_rover px4_io-v2_default tests_coverage
atlflight_eagle clang-tidy-quiet module_documentation px4_fmu-v2_test px4_metadata tests_mission
atlflight_eagle_default clean nxp_fmuk66-v3 px4_fmu-v3 px4_raspberrypi_cross tests_mission_coverage
atlflight_eagle_qurt-default coverity_scan nxp_fmuk66-v3_default px4_fmu-v3_default px4_raspberrypi_native tests_offboard
atlflight_excelsior cppcheck omnibus_f4sd px4_fmu-v3_rtps px4_sitl thiemar_s2740vc-v1
atlflight_excelsior_default distclean omnibus_f4sd_default px4_fmu-v3_stackcheck px4_sitl_default thiemar_s2740vc-v1_default
atlflight_excelsior_qurt-default doxygen parameters_metadata px4_fmu-v4 px4_sitl_default-clang uorb_graphs
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
用来在本机进行仿真的编译选项. 面对如此众多的选项, 本文的目的在于理清各个编译选项的具体内部操作.
这里假设读者有一定的编程经验, 这一小节简述 Makefile 的一些基础概念和语法, 主要是参考了GNU make manual 和 GNU make 项目实战. Makefile 可以视为 Make的操作指令文档, 指导 Make 成一系列的操作. Makefile 的语法杂糅了 c 以及 sh 的特点, 其中包括了基本的逻辑判断和变量定义语法. Makefile 中最核心的部分是 编译指导语句
targets: prerequisites
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).
这里为了引用方便, 在每段代码前面都保留了在文件中的行号. 首先前面的以 # 开头的32行是 Copyright 部分, 基本上是 BSD 协议的内容. 之后是 Makefile 代码的正文部分, 首先是检查当前目录是否是 git repo, 以确保源码的依赖关系没有被破坏, 可以在编译的过程中子模块的下载可以顺利进行.
34 # Enforce the presence of the GIT repository
35 #
36 # We depend on our submodules, so we have to prevent attempts to
37 # compile without it being present.
38 ifeq ($(wildcard .git),)
39 $(error YOU HAVE TO USE GIT TO DOWNLOAD THIS REPOSITORY. ABORTING.)
40 endif
接下来是帮助说明, 意思就是不要害怕这个 Makefile, 这个 Makefile 文件主要是用来向 cmake 传参的, 并列出了二次开发可能主要用到的 make px4_fmu-v2_default
等几个指令.
42 # Help
43 # --------------------------------------------------------------------
44 # Don't be afraid of this makefile, it is just passing
45 # arguments to cmake to allow us to keep the wiki pages etc.
46 # that describe how to build the px4 firmware
47 # the same even when using cmake instead of make.
48 #
49 # Example usage:
50 #
51 # make px4_fmu-v2_default (builds)
52 # make px4_fmu-v2_default upload (builds and uploads)
53 # make px4_fmu-v2_default test (builds and tests)
54 #
55 # This tells cmake to build the nuttx px4_fmu-v2 default config in the
56 # directory build/px4_fmu-v2_default and then call make
57 # in that directory with the target upload.```
接下来设置默认的编译目标, make 把遇到的第一个编译目标设置成默认目标, 这里的 target 是 all
, 而prerequisites 是 px4_sitl_default
,同时这里没有 recipes 部分, 对于没有 recipes 的编译目标, make 则总会尝试去更新他的依赖项, 这里是 px4_sitl_default
59 # explicity set default build target
60 all: px4_sitl_default
接下来是利用 subst
函数生成一个空格, subst 函数的用法为 $(subst 模式字符串, 替换字符串, 原始字符串), 在原始字符串中搜索模式字符串并用替换字符串替换.
62 # define a space character to be able to explicitly find it in strings
63 space := $(subst ,, )
这里通过make 的环境变量 $(MAKECMDGOALS)
获取用户制定的编译目标, 并通过 $(firstword $(MAKECMDGOALS))
函数获取第一个值, 用$(wordlist 2,$(words $(MAKECMDGOALS)), $(MAKECMDGOALS))
第一个之外的其他目标值. 例如在终端中输入 > make px4_sitl_default gazebo
时, FIRST_ARG
将获取到px4_sitl_default
, 而ARGS
将获取到 gazebo
65 # Parsing
66 # --------------------------------------------------------------------
67 # assume 1st argument passed is the main target, the
68 # rest are arguments to pass to the makefile generated
69 # by cmake in the subdirectory
70 FIRST_ARG := $(firstword $(MAKECMDGOALS))
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 要轻量级的编译系统.
74 NINJA_BIN := ninja
75 ifndef NO_NINJA_BUILD
76 NINJA_BUILD := $(shell $(NINJA_BIN) --version 2>/dev/null)
77
78 ifndef NINJA_BUILD
79 NINJA_BIN := ninja-build
80 NINJA_BUILD := $(shell $(NINJA_BIN) --version 2>/dev/null)
81 endif
82 endif
83
84 ifdef NINJA_BUILD
85 PX4_CMAKE_GENERATOR := Ninja
86 PX4_MAKE := $(NINJA_BIN)
87
88 ifdef VERBOSE
89 PX4_MAKE_ARGS := -v
90 else
91 PX4_MAKE_ARGS :=
92 endif
93 else
94 ifdef SYSTEMROOT
95 # Windows
96 PX4_CMAKE_GENERATOR := "MSYS\ Makefiles"
97 else
98 PX4_CMAKE_GENERATOR := "Unix\ Makefiles"
99 endif
100 PX4_MAKE = $(MAKE)
101 PX4_MAKE_ARGS = -j$(j) --no-print-directory
102 endif
然后, 获取了源码所在的路径, 首先用 lastword 函数获取了当前的 Makefile 的名称, 然后用realpath
获取了这个 Makefile 的完整路径, 最后调用 shell 的 dirname 命令获取了文件的目录路径.
104 SRC_DIR := $(shell dirname "$(realpath $(lastword $(MAKEFILE_LIST)))")
接下来设置了 $BUILD_DIR_SUFFIX
变量, 上面注释说"依据是否定义了环境变量 replay 来决定build 目录是否加后缀", 为什么要有 replay 目前还不清楚…
106 # check if replay env variable is set & set build dir accordingly
107 ifdef replay
108 BUILD_DIR_SUFFIX := _replay
109 else
110 BUILD_DIR_SUFFIX :=
111 endif
接下来给 cmake 添加了一些选项. 包括是否包括外部模块, 及 cmake 的build 的类型
113 # additional config parameters passed to cmake
114 ifdef EXTERNAL_MODULES_LOCATION
115 CMAKE_ARGS += -DEXTERNAL_MODULES_LOCATION:STRING=$(EXTERNAL_MODULES_LOCATION)
116 endif
117
118 ifdef PX4_CMAKE_BUILD_TYPE
119 CMAKE_ARGS += -DCMAKE_BUILD_TYPE=${PX4_CMAKE_BUILD_TYPE}
120 else
121
122 # Address Sanitizer
123 ifdef PX4_ASAN
124 CMAKE_ARGS += -DCMAKE_BUILD_TYPE=AddressSanitizer
125 endif
126
127 # Memory Sanitizer
128 ifdef PX4_MSAN
129 CMAKE_ARGS += -DCMAKE_BUILD_TYPE=MemorySanitizer
130 endif
131
132 # Thread Sanitizer
133 ifdef PX4_TSAN
134 CMAKE_ARGS += -DCMAKE_BUILD_TYPE=ThreadSanitizer
135 endif
136
137 # Undefined Behavior Sanitizer
138 ifdef PX4_UBSAN
139 CMAKE_ARGS += -DCMAKE_BUILD_TYPE=UndefinedBehaviorSanitizer
140 endif
141
142 endif
接着这里定义了两个函数, 第一个是后面编译各个选项时主要用到的 cmake-build
, 另一个是用来检查之前的cmake
编译生成的 cache
是否和当前的编译选项一致的cmake-cache-check
. 在cmake-build
中调用了cmake-cache-check
来确定是否需要重新生成目录和编译配置文件.
144 # Functions
145 # --------------------------------------------------------------------
146 # describe how to build a cmake config
147 define cmake-build
148 @$(eval BUILD_DIR = "$(SRC_DIR)/build/$(1)")
149 @# check if the desired cmake configuration matches the cache then CMAKE_CACHE_CHECK stays empty
150 @$(call cmake-cache-check)
151 @# make sure to start from scratch when switching from GNU Make to Ninja
152 @if [ $(PX4_CMAKE_GENERATOR) = "Ninja" ] && [ -e $(BUILD_DIR)/Makefile ]; then rm -rf $(BUILD_DIR); fi
153 @# only excplicitly configure the first build, if cache file already exists the makefile will rerun cmake automatically if necessary
154 @if [ ! -e $(BUILD_DIR)/CMakeCache.txt ] || [ $(CMAKE_CACHE_CHECK) ]; then \
155 mkdir -p $(BUILD_DIR) \
156 && cd $(BUILD_DIR) \
157 && cmake "$(SRC_DIR)" -G"$(PX4_CMAKE_GENERATOR)" $(CMAKE_ARGS) \
158 || (rm -rf $(BUILD_DIR)); \
159 fi
160 @# run the build for the specified target
161 @cmake --build $(BUILD_DIR) -- $(PX4_MAKE_ARGS) $(ARGS)
162 endef
163
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
165 define cmake-cache-check
166 @# change to build folder which fails if it doesn't exist and CACHED_CMAKE_OPTIONS stays empty
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)
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' ))
169 @# transform the options in CMAKE_ARGS into the OPTION=VALUE format without -D
170 @$(eval DESIRED_CMAKE_OPTIONS = $(shell echo $(CMAKE_ARGS) | sed -n 's/-D\([^[:blank:]]*=[^[:blank:]]*\)/\1/gp' ))
171 @# find each currently desired option in the already cached ones making sure the complete configured string value is the same
172 @$(eval VERIFIED_CMAKE_OPTIONS = $(foreach option,$(DESIRED_CMAKE_OPTIONS),$(strip $(findstring $(option)$(space),$(CACHED_CMAKE_OPTIONS)))))
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
174 @$(eval CMAKE_CACHE_CHECK = $(if $(findstring $(DESIRED_CMAKE_OPTIONS),$(VERIFIED_CMAKE_OPTIONS)),,y))
175 endef
下面给则定义了一个带颜色echo 的函数 colorecho
177 COLOR_BLUE = \033[0;94m
178 NO_COLOR = \033[m
179
180 define colorecho
181 +@echo -e '${COLOR_BLUE}${1} ${NO_COLOR}'
182 endef
获取所有的飞控的编译文件
184 # Get a list of all config targets boards/*/*.cmake
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)
186
接着定义了所有编译选项的默认编译方式和以_default
结尾的编译选项的编译方式.
191 # All targets.
192 $(ALL_CONFIG_TARGETS):
193 @$(eval PX4_CONFIG = $@)
194 @$(eval CMAKE_ARGS += -DCONFIG=$(PX4_CONFIG))
195 @$(call cmake-build,$(PX4_CONFIG)$(BUILD_DIR_SUFFIX))
196
197 # Filter for only default targets to allow omiting the "_default" postfix
198 CONFIG_TARGETS_DEFAULT := $(patsubst %_default,%,$(filter %_default,$(ALL_CONFIG_TARGETS)))
199 $(CONFIG_TARGETS_DEFAULT):
200 @$(eval PX4_CONFIG = $@_default)
201 @$(eval CMAKE_ARGS += -DCONFIG=$(PX4_CONFIG))
202 @$(call cmake-build,$(PX4_CONFIG)$(BUILD_DIR_SUFFIX))
203
204 all_config_targets: $(ALL_CONFIG_TARGETS)
205 all_default_targets: $(CONFIG_TARGETS_DEFAULT)
定义了一个辅助函数, 和固件编译配置
209 # board reorganization deprecation warnings (2018-11-22)
210 define deprecation_warning
211 $(warning $(1) has been deprecated and will be removed, please use $(2)!)
212 endef
213
214 px4fmu-%_default:
215 $(call deprecation_warning, ${@},$(subst px4fmu,px4_fmu,$@))
216 $(MAKE) $(subst px4fmu,px4_fmu, $@)
217
218 posix_sitl_default:
219 $(call deprecation_warning, ${@},px4_sitl_default)
220 $(MAKE) px4_sitl_default
221
222 # All targets with just dependencies but no recipe must either be marked as phony (or have the special @: as recipe).
223 .PHONY: all posix px4_sitl_default all_config_targets all_default_targets
针对eagle
和 excelsior
的编译配置选项
225 # Multi- config targets.
226 eagle_default: atlflight_eagle_default atlflight_eagle_qurt-default
227 eagle_rtps: atlflight_eagle_rtps atlflight_eagle_qurt-rtps
228
229 excelsior_default: atlflight_excelsior_default atlflight_excelsior_qurt-default
230 excelsior_rtps: atlflight_excelsior_rtps atlflight_excelsior_qurt-rtps
231
232 .PHONY: eagle_default eagle_rtps
233 .PHONY: excelsior_default excelsior_rtps
分别设置 gqc 可以上传固件的编译选项
238 .PHONY: qgc_firmware px4fmu_firmware misc_qgc_extra_firmware alt_firmware check_rtps
239
240 # QGroundControl flashable NuttX firmware
241 qgc_firmware: px4fmu_firmware misc_qgc_extra_firmware
242
243 # px4fmu NuttX firmware
244 px4fmu_firmware: \
245 check_px4_io-v2_default \
246 check_px4_fmu-v2_default \
247 check_px4_fmu-v3_default \
248 check_px4_fmu-v4_default \
249 check_px4_fmu-v4pro_default \
250 check_px4_fmu-v5_default \
251 sizes
252
253 misc_qgc_extra_firmware: \
254 check_nxp_fmuk66-v3_default \
255 check_intel_aerofc-v1_default \
256 check_auav_x21_default \
257 check_bitcraze_crazyflie_default \
258 check_airmind_mindpx-v2_default \
259 check_px4_fmu-v2_lpe \
260 sizes
261
262 # Other NuttX firmware
263 alt_firmware: \
264 check_px4_cannode-v1_default \
265 check_px4_esc-v1_default \
266 check_auav_esc35-v1_default \
267 check_thiemar_s2740vc-v1_default \
268 sizes
269
270 # builds with RTPS
271 check_rtps: \
272 check_px4_fmu-v3_rtps \
273 check_px4_fmu-v4_rtps \
274 check_px4_fmu-v4pro_rtps \
275 check_px4_sitl_rtps \
276 sizes
获取 elf
文件的大小
278 .PHONY: sizes check quick_check check_rtps uorb_graphs
279
280 sizes:
281 @-find build -name *.elf -type f | xargs size 2> /dev/null || :
定义编译测试
283 # All default targets that don't require a special build environment
284 check: check_px4_sitl_default px4fmu_firmware misc_qgc_extra_firmware alt_firmware tests check_format
285
286 # quick_check builds a single nuttx and posix target, runs testing, and checks the style
287 quick_check: check_px4_sitl_test check_px4_fmu-v5_default tests check_format
288
289 check_%:
290 @echo
291 $(call colorecho,'Building' $(subst check_,,$@))
292 @$(MAKE) --no-print-directory $(subst check_,,$@)
293 @echo
创建uorb消息的订阅发布图
295 uorb_graphs:
296 @./Tools/uorb_graph/create_from_startupscript.sh
297 @./Tools/uorb_graph/create.py --src-path src --exclude-path src/examples --file Tools/uorb_graph/graph_full
298 @$(MAKE) --no-print-directory px4_fmu-v2_default uorb_graph
299 @$(MAKE) --no-print-directory px4_fmu-v4_default uorb_graph
300 @$(MAKE) --no-print-directory px4_sitl_default uorb_graph
下面是依据源码生成文档的编译选项
307 # Documentation
308 # --------------------------------------------------------------------
309 .PHONY: parameters_metadata airframe_metadata module_documentation px4_metadata doxygen
310
311 parameters_metadata:
312 @$(MAKE) --no-print-directory px4_sitl_default metadata_parameters
313
314 airframe_metadata:
315 @$(MAKE) --no-print-directory px4_sitl_default metadata_airframes
316
317 module_documentation:
318 @$(MAKE) --no-print-directory px4_sitl_default metadata_module_documentation
319
320 px4_metadata: parameters_metadata airframe_metadata module_documentation
321
322 doxygen:
323 @mkdir -p "$(SRC_DIR)"/build/doxygen
324 @cd "$(SRC_DIR)"/build/doxygen && cmake "$(SRC_DIR)" $(CMAKE_ARGS) -G"$(PX4_CMAKE_GENERATOR)" -DCONFIG=px4_sitl_default -DBUILD_DOXYGEN=ON
325 @$(PX4_MAKE) -C "$(SRC_DIR)"/build/doxygen
326 @touch "$(SRC_DIR)"/build/doxygen/Documentation/.nojekyll
格式化代码
328 # Astyle
329 # --------------------------------------------------------------------
330 .PHONY: check_format format
331
332 check_format:
333 $(call colorecho,'Checking formatting with astyle')
334 @"$(SRC_DIR)"/Tools/astyle/check_code_style_all.sh
335 @cd "$(SRC_DIR)" && git diff --check
336
337 format:
338 $(call colorecho,'Formatting with astyle')
339 @"$(SRC_DIR)"/Tools/astyle/check_code_style_all.sh --fix
一些测试编译配置
41 # Testing
342 # --------------------------------------------------------------------
343 .PHONY: tests tests_coverage tests_mission tests_mission_coverage tests_offboard tests_avoidance
344 .PHONY: rostest python_coverage
345
346 tests:
347 $(eval CMAKE_ARGS += -DCONFIG=px4_sitl_test)
348 $(eval CMAKE_ARGS += -DTESTFILTER=$(TESTFILTER))
349 $(eval ARGS += test_results)
350 $(eval ASAN_OPTIONS += color=always:check_initialization_order=1:detect_stack_use_after_return=1)
351 $(eval UBSAN_OPTIONS += color=always)
352 $(call cmake-build,px4_sitl_test)
353
354 tests_coverage:
355 @$(MAKE) clean
356 @$(MAKE) --no-print-directory px4_sitl_default test_coverage_genhtml PX4_CMAKE_BUILD_TYPE=Coverage
357 @echo "Open "$(SRC_DIR)"/build/px4_sitl_default/coverage-html/index.html to see coverage"
358
359 rostest: px4_sitl_default
360 @$(MAKE) --no-print-directory px4_sitl_default sitl_gazebo
361
362 tests_mission: rostest
363 @"$(SRC_DIR)"/test/rostest_px4_run.sh mavros_posix_tests_missions.test
364
365 rostest_run: px4_sitl_default
366 @$(MAKE) --no-print-directory px4_sitl_default sitl_gazebo
367 @"$(SRC_DIR)"/test/rostest_px4_run.sh $(TEST_FILE) mission:=$(TEST_MISSION) vehicle:=$(TEST_VEHICLE)
368
369 tests_mission_coverage:
370 @$(MAKE) clean
371 @$(MAKE) --no-print-directory px4_sitl_default PX4_CMAKE_BUILD_TYPE=Coverage
372 @$(MAKE) --no-print-directory px4_sitl_default sitl_gazebo PX4_CMAKE_BUILD_TYPE=Coverage
373 @"$(SRC_DIR)"/test/rostest_px4_run.sh mavros_posix_test_mission.test mission:=VTOL_mission_1 vehicle:=standard_vtol
374 @$(MAKE) --no-print-directory px4_sitl_default generate_coverage
375
376 tests_offboard: rostest
377 @"$(SRC_DIR)"/test/rostest_px4_run.sh mavros_posix_tests_offboard_attctl.test
378 @"$(SRC_DIR)"/test/rostest_px4_run.sh mavros_posix_tests_offboard_posctl.test
379
380 tests_avoidance: rostest
381 @"$(SRC_DIR)"/test/rostest_avoidance_run.sh mavros_posix_test_avoidance.test
382
383 python_coverage:
384 @mkdir -p "$(SRC_DIR)"/build/python_coverage
385 @cd "$(SRC_DIR)"/build/python_coverage && cmake "$(SRC_DIR)" $(CMAKE_ARGS) -G"$(PX4_CMAKE_GENERATOR)" -DCONFIG=px4_sitl_default -DPYTHON_COVERAGE=ON
386 @$(PX4_MAKE) -C "$(SRC_DIR)"/build/python_coverage
387 @$(PX4_MAKE) -C "$(SRC_DIR)"/build/python_coverage metadata_airframes
388 @$(PX4_MAKE) -C "$(SRC_DIR)"/build/python_coverage metadata_parameters
389 #@$(PX4_MAKE) -C "$(SRC_DIR)"/build/python_coverage module_documentation # TODO: fix within coverage.py
390 @coverage combine `find . -name .coverage\*`
391 @coverage report -m
静态编译分析
394 # static analyzers (scan-build, clang-tidy, cppcheck)
395 # --------------------------------------------------------------------
396 .PHONY: scan-build px4_sitl_default-clang clang-tidy clang-tidy-fix clang-tidy-quiet
397 .PHONY: cppcheck shellcheck_all validate_module_configs
398
399 scan-build:
400 @export CCC_CC=clang
401 @export CCC_CXX=clang++
402 @rm -rf "$(SRC_DIR)"/build/px4_sitl_default-scan-build
403 @rm -rf "$(SRC_DIR)"/build/scan-build/report_latest
404 @mkdir -p "$(SRC_DIR)"/build/px4_sitl_default-scan-build
405 @cd "$(SRC_DIR)"/build/px4_sitl_default-scan-build && scan-build cmake "$(SRC_DIR)" -GNinja -DCONFIG=px4_sitl_default
406 @scan-build -o "$(SRC_DIR)"/build/scan-build cmake --build "$(SRC_DIR)"/build/px4_sitl_default-scan-build
407 @find "$(SRC_DIR)"/build/scan-build -maxdepth 1 -mindepth 1 -type d -exec cp -r "{}" "$(SRC_DIR)"/build/scan-build/report_latest \;
408
409 px4_sitl_default-clang:
410 @mkdir -p "$(SRC_DIR)"/build/px4_sitl_default-clang
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++
412 @$(PX4_MAKE) -C "$(SRC_DIR)"/build/px4_sitl_default-clang
413
414 clang-tidy: px4_sitl_default-clang
415 @cd "$(SRC_DIR)"/build/px4_sitl_default-clang && "$(SRC_DIR)"/Tools/run-clang-tidy.py -header-filter=".*\.hpp" -j$(j) -p .
416
417 # to automatically fix a single check at a time, eg modernize-redundant-void-arg
418 # % run-clang-tidy-4.0.py -fix -j4 -checks=-\*,modernize-redundant-void-arg -p .
419 clang-tidy-fix: px4_sitl_default-clang
420 @cd "$(SRC_DIR)"/build/px4_sitl_default-clang && "$(SRC_DIR)"/Tools/run-clang-tidy.py -header-filter=".*\.hpp" -j$(j) -fix -p .
421
422 # modified version of run-clang-tidy.py to return error codes and only output relevant results
423 clang-tidy-quiet: px4_sitl_default-clang
424 @cd "$(SRC_DIR)"/build/px4_sitl_default-clang && "$(SRC_DIR)"/Tools/run-clang-tidy.py -header-filter=".*\.hpp" -j$(j) -p .
425
426 # TODO: Fix cppcheck errors then try --enable=warning,performance,portability,style,unusedFunction or --enable=all
427 cppcheck: px4_sitl_default
428 @mkdir -p "$(SRC_DIR)"/build/cppcheck
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
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/
431
432 shellcheck_all:
433 @"$(SRC_DIR)"/Tools/run-shellcheck.sh "$(SRC_DIR)"/ROMFS/px4fmu_common/
434 @make px4_fmu-v5_default shellcheck
435
436 validate_module_configs:
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
438
clean 选项, 主要用来清理上一次的编译结果
439 # Cleanup
440 # --------------------------------------------------------------------
441 .PHONY: clean submodulesclean submodulesupdate gazeboclean distclean
442
443 clean:
444 @rm -rf "$(SRC_DIR)"/build
445
446 submodulesclean:
447 @git submodule foreach --quiet --recursive git clean -ff -x -d
448 @git submodule update --quiet --init --recursive --force || true
449 @git submodule sync --recursive
450 @git submodule update --init --recursive --force
451
452 submodulesupdate:
453 @git submodule update --quiet --init --recursive || true
454 @git submodule sync --recursive
455 @git submodule update --init --recursive
456
457 gazeboclean:
458 @rm -rf ~/.gazebo/*
459
460 distclean: gazeboclean
461 @git submodule deinit -f .
462 @git clean -ff -x -d -e ".project" -e ".cproject" -e ".idea" -e ".settings" -e ".vscode"
帮助文件, 列出所有可以选的编译选项
464 # Help / Error
465 # --------------------------------------------------------------------
466
467 # All other targets are handled by PX4_MAKE. Add a rule here to avoid printing an error.
468 %:
469 $(if $(filter $(FIRST_ARG),$@), \
470 $(error "$@ cannot be the first argument. Use '$(MAKE) help|list_config_targets' to get a list of all possible [configuration] targets."),@#)
471
472 # Print a list of non-config targets (based on http://stackoverflow.com/a/26339924/1487069)
473 help:
474 @echo "Usage: $(MAKE) "
475 @echo "Where is one of:"
476 @$(MAKE) -pRrq -f $(lastword $(MAKEFILE_LIST)) : 2>/dev/null | \
477 awk -v RS= -F: '/^# File/,/^# Finished Make data base/ {if ($$1 !~ "^[#.]") {print $$1}}' | sort | \
478 egrep -v -e '^[^[:alnum:]]' -e '^($(subst $(space),|,$(ALL_CONFIG_TARGETS)))$$' -e '_default$$' -e '^(posix|eagle|Makefile)'
479 @echo
480 @echo "Or, $(MAKE) []"
481 @echo "Use '$(MAKE) list_config_targets' for a list of configuration targets."
482
483 # Print a list of all config targets.
484 list_config_targets:
485 @for targ in $(patsubst %_default,%[_default],$(ALL_CONFIG_TARGETS)); do echo $$targ; done
首先 make 命令读入参数 px4_sitl_default
, 同时读入当前目录下的Makefile
, 在Makefile 中搜寻px4_sitl_default
编译配置条目, 找到了$(ALL_CONFIG_TARGET)
, 这里的$(ALL_CONFIG_TARGET)
包含了所有的编译配置文件, 但是下面的$(CONFIG_TARGETS_DEFAULT)
也包含了 px4_sitl_default
, 基于Makefile 规范, 后面出现的规则会覆盖之前出现的规则, 所以px4_stil_default
的 recipes 为
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
的定义再次附录如下,
147 define cmake-build
148 @$(eval BUILD_DIR = "$(SRC_DIR)/build/$(1)")
149 @# check if the desired cmake configuration matches the cache then CMAKE_CACHE_CHECK stays empty
150 @$(call cmake-cache-check)
151 @# make sure to start from scratch when switching from GNU Make to Ninja
152 @if [ $(PX4_CMAKE_GENERATOR) = "Ninja" ] && [ -e $(BUILD_DIR)/Makefile ]; then rm -rf $(BUILD_DIR); fi
153 @# only excplicitly configure the first build, if cache file already exists the makefile will rerun cmake automatically if necessary
154 @if [ ! -e $(BUILD_DIR)/CMakeCache.txt ] || [ $(CMAKE_CACHE_CHECK) ]; then \
155 mkdir -p $(BUILD_DIR) \
156 && cd $(BUILD_DIR) \
157 && cmake "$(SRC_DIR)" -G"$(PX4_CMAKE_GENERATOR)" $(CMAKE_ARGS) \
158 || (rm -rf $(BUILD_DIR)); \
159 fi
160 @# run the build for the specified target
161 @cmake --build $(BUILD_DIR) -- $(PX4_MAKE_ARGS) $(ARGS)
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)
并替换成了空值. 在执行完这些之后,我们可以执行
$ cd build/px4_sitl_default
$ ninja gazebo
发现结果和make px4_sitl_default gazebo
结果一样, 整个编译过程大概是 Make 调用了 CMake, 然后CMake 调用了Ninja 读取 CMakeLists.txt 文件生成编译配置文件, 然后CMake 再调用 Ninja 进行编译.
>>> 下一步 CMakeLists.txt分析