PX4 Makefile分析解读

参考文章:PX4源码的Makefile详细理解 http://blog.csdn.net/linkcian/article/details/79152724 感谢原文作者

主要分析 make px4fmu-v2_default编译流程,其余make命令不做解读

# Enforce the presence of the GIT repository
#
# We depend on our submodules, so we have to prevent attempts to
# compile without it being present.
# 获取目录下.git文件列表
ifeq ($(wildcard .git),)
    $(error YOU HAVE TO USE GIT TO DOWNLOAD THIS REPOSITORY. ABORTING.)
endif

#  explicity set default build target
all: posix_sitl_default

# Parsing
# --------------------------------------------------------------------
# assume 1st argument passed is the main target, the
# rest are arguments to pass to the makefile generated
# by cmake in the subdirectory
# 若$(MAKECMDGOALS)=px4fmu-v2_default upload,取第一个单词是px4fmu-v2_default
FIRST_ARG := $(firstword $(MAKECMDGOALS))
# 取px4fmu-v2_default upload中第2个至最后一个单词,即upload
ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS))
# 如果j未赋值,则赋为4,这是并行命令数量
j ?= 4

NINJA_BIN := ninja
ifndef NO_NINJA_BUILD
	NINJA_BUILD := $(shell $(NINJA_BIN) --version 2>/dev/null)

	ifndef NINJA_BUILD
		NINJA_BIN := ninja-build
		NINJA_BUILD := $(shell $(NINJA_BIN) --version 2>/dev/null)
	endif
endif

ifdef NINJA_BUILD
	PX4_CMAKE_GENERATOR := Ninja
	PX4_MAKE := $(NINJA_BIN)

	ifdef VERBOSE
		PX4_MAKE_ARGS := -v
	else
		PX4_MAKE_ARGS :=
	endif
else
	ifdef SYSTEMROOT
		# Windows
		PX4_CMAKE_GENERATOR := "MSYS\ Makefiles"
	else
		PX4_CMAKE_GENERATOR := "Unix\ Makefiles"
	endif
	# 就是make命令
	PX4_MAKE = $(MAKE)
	# 同一时刻允许并行执行4行命令,禁止所有关于目录信息的打印
	PX4_MAKE_ARGS = -j$(j) --no-print-directory
endif

# firmware所在文件夹
SRC_DIR := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))

# check if replay env variable is set & set build dir accordingly
# 检查是否定义relay环境变量,并更改build目录的后缀:BUILD_DIR_SUFFIX,实际上无后缀
ifdef replay
	BUILD_DIR_SUFFIX := _replay
else
	BUILD_DIR_SUFFIX :=
endif

# NuttX verbose output
ifdef VN
	export PX4_NUTTX_BUILD_VERBOSE=1
	export V=1
endif

# NuttX verbose patches output
ifdef VNP
	export PX4_NUTTX_PATCHES_VERBOSE=1
endif

# additional config parameters passed to cmake
# 添加额外的配置参数
CMAKE_ARGS :=
ifdef EXTERNAL_MODULES_LOCATION
	CMAKE_ARGS := -DEXTERNAL_MODULES_LOCATION:STRING=$(EXTERNAL_MODULES_LOCATION)
endif

ifdef PX4_CMAKE_BUILD_TYPE
	CMAKE_ARGS += -DCMAKE_BUILD_TYPE=${PX4_CMAKE_BUILD_TYPE}
endif

# Functions
# --------------------------------------------------------------------
# describe how to build a cmake config
# 描述如何build一个cmake配置文件, 定义了cmke-build命令规则,并未执行
define cmake-build
# $@代表目标即px4fmu-v2_default,BUILD_DIR=build_px4fmu-v2_default
# $(eval text)意思是执行text的内容;@表示执行前不回显,+使命令行可以通过指定-n、-q或-t选项来执行
+@$(eval BUILD_DIR = $(SRC_DIR)/build_$@$(BUILD_DIR_SUFFIX))
# shell语法 -e 判断文件是否存在,&&表示如果前面运行成功则运行下一句
+@if [ $(PX4_CMAKE_GENERATOR) = "Ninja" ] && [ -e $(BUILD_DIR)/Makefile ]; then rm -rf $(BUILD_DIR); fi
# 如果不存在CMakeCache.txt文件,则创建build_px4fmu-v2_default文件夹并进入 ,-G指定编译器,$(2)为$(SRC_DIR)路径,$(1)为nuttx_px4fmu-v2_default,在后面定义,若cmake失败则删除文件夹
+@if [ ! -e $(BUILD_DIR)/CMakeCache.txt ]; then mkdir -p $(BUILD_DIR) && cd $(BUILD_DIR) && cmake $(2)  -G"$(PX4_CMAKE_GENERATOR)" -DCONFIG=$(1) $(CMAKE_ARGS) || (rm -rf $(BUILD_DIR)); fi
# 执行upload参数 make -j4 --no-print-directory upload
+@(cd $(BUILD_DIR) && $(PX4_MAKE) $(PX4_MAKE_ARGS) $(ARGS))
endef

define colorecho
+@tput setaf 6
+@echo $1
+@tput sgr0
endef

# Get a list of all config targets.
# 获取cmake/configs下所有.cmake文件(文件名不含_common,_sdflight_)
ALL_CONFIG_TARGETS := $(basename $(shell find "$(SRC_DIR)/cmake/configs" ! -name '*_common*' ! -name '*_sdflight_*' -name '*.cmake' -print | sed  -e 's:^.*/::' | sort))
# Strip off leading nuttx_
# 去除nuttx_前缀,老版本中是不含有有nuttx_前缀的,这样做是为了兼容老版本,使编译命令不变
NUTTX_CONFIG_TARGETS := $(patsubst nuttx_%,%,$(filter nuttx_%,$(ALL_CONFIG_TARGETS)))

# ADD CONFIGS HERE
# --------------------------------------------------------------------
#  Do not put any spaces between function arguments.

# All targets.
# 直接用cmake/configs下的文件名运行,如nuttx_px4fmu-v2_default
$(ALL_CONFIG_TARGETS):
	$(call cmake-build,$@,$(SRC_DIR))

# Abbreviated config targets.

# nuttx_ is left off by default; provide a rule to allow that.
# 这里专门针对nuttx_前缀的命令,目的是兼容老版本命令
# $@为目标集合,如果输入为NUTTX_CONFIG_TARGETS中的一个,则执行这个目标的编译命令,如px4fmu-v2_default
# cmake-build即为前面定义的规则,nuttx_$@即为$(1),$(SRC_DIR)即为$(2)
$(NUTTX_CONFIG_TARGETS):
	$(call cmake-build,nuttx_$@,$(SRC_DIR))

后续命令与编译相关不大,不再做分析。

总结一下:在运行make px4fmu-v2_default命令后,首先进入Firmware/Makefile文件,提取源码路径、px4fmu-v2_default参数,与cmake/configs中的文件名做比较,符合时则创建build_px4fmu-v2_default文件夹,执行cmake编译命令。


你可能感兴趣的:(pixhawk)