简单的概述
make 和 cmake 是linux/UNIX系统下广泛使用的构建编译规则工具,面对复杂庞大的工程,各种源文件和工具文件分布在工程目录下,如何组织和有序地编译和使用这些文件,显然也是一项复杂的任务。Makefile是直接地定义编译规则以及描述目标之间依赖关系。CMakeLists.txt虽然也是具有相同的功能,但是它是对Makefile的抽象化以便更容易地实现工程编译规则的编写。(想更具体了解cmake与make的关系,请google)
PX4下的Makefile
它只是对CMakeLists.txt的一个简单封装,真正定义PX4工程的编译规则由CMakeList.txt文件实现。
这个文件主要完成:
检查cmake版本3.2及以上和解析命令行参数
# Enforce the presence of the GIT repository
#
# We depend on our submodules, so we have to prevent attempts to
# compile without it being present.
ifeq ($(wildcard .git),)
$(error YOU HAVE TO USE GIT TO DOWNLOAD THIS REPOSITORY. ABORTING.)
endif
#上面检查.git文件是否存在
#若cmake版本3.2以下,将退出
CMAKE_VER := $(shell Tools/check_cmake.sh; echo $$?)
ifneq ($(CMAKE_VER),0)
$(warning Not a valid CMake version or CMake not installed.)
$(warning On Ubuntu 16.04, install or upgrade via:)
$(warning )
$(warning 3rd party PPA:)
$(warning sudo add-apt-repository ppa:george-edison55/cmake-3.x -y)
$(warning sudo apt-get update)
$(warning sudo apt-get install cmake)
$(warning )
$(warning Official website:)
$(warning wget https://cmake.org/files/v3.4/cmake-3.4.3-Linux-x86_64.sh)
$(warning chmod +x cmake-3.4.3-Linux-x86_64.sh)
$(warning sudo mkdir /opt/cmake-3.4.3)
$(warning sudo ./cmake-3.4.3-Linux-x86_64.sh --prefix=/opt/cmake-3.4.3 --exclude-subdir)
$(warning export PATH=/opt/cmake-3.4.3/bin:$$PATH)
$(warning )
$(error Fatal)
endif
...
...
# Parsing
# --------------------------------------------------------------------
# assume 1st argument passed is the main target, the
# rest are arguments to pass to the makefile generated
# by cmake in the subdirectory
FIRST_ARG := $(firstword $(MAKECMDGOALS))
ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS))
...
...
定义配置目标以及解析cmake编译规则
# describe how to build a cmake config
define cmake-build
+@$(eval BUILD_DIR = $(SRC_DIR)/build_$@$(BUILD_DIR_SUFFIX))
+@if [ $(PX4_CMAKE_GENERATOR) = "Ninja" ] && [ -e $(BUILD_DIR)/Makefile ]; then rm -rf $(BUILD_DIR); fi
+@if [ ! -e $(BUILD_DIR)/CMakeCache.txt ]; then mkdir -p $(BUILD_DIR) && cd $(BUILD_DIR) && cmake .. -G$(PX4_CMAKE_GENERATOR) -DCONFIG=$(1) $(CMAKE_ARGS) || (cd .. && rm -rf $(BUILD_DIR)); fi
+@echo "PX4 CONFIG: $(BUILD_DIR)"
+@$(PX4_MAKE) -C "$(BUILD_DIR)" $(PX4_MAKE_ARGS) $(ARGS)
endef
...
...
#以make px4fmu-v2_default为例,上面的-DCONFIG=$(1)展开后就是-DCONFIG=nuttx_px4fmu-v2_default
# Get a list of all config targets.
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_CONFIG_TARGETS := $(patsubst nut