PX4 CMakeLists.txt分析

本文分析了PX4工程的CMakeLists.txt文件,详细解释了如何通过CMakeLists.txt组织和编译复杂的工程文件。内容包括:检查cmake版本、解析命令行参数、定义配置目标以及解析编译规则。当执行`make px4fmu-v2_default`时,会解析CMakeLists.txt生成编译规则。文章还提到了nuttx系统源码的配置、msg目录的处理、src子目录的构建以及px4io-v2的编译规则,并提供了流程图和依赖关系图。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

简单的概述

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_T
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值