From 9d5d5c4ddabee72febb3ff29933dc09dd0ebe708 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Cab=C3=A9?= Date: Sat, 10 Oct 2026 06:31:23 +0000 Subject: [PATCH] build(cmake): allow building PX4 as a CMake subproject MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Let another CMake project build PX4 with add_subdirectory(), for instance the build system of an RTOS that links the firmware image. A subproject leaves to its parent project what applies to the whole build: - the build type and ccache, which affect every target - the CMake cache: the board configuration is kept in normal variables, so that its CONFIG_* entries do not mix with the parent project's - the firmware image, so the airframe metadata, packaging and size analysis targets are not created PX4's CMake modules are prepended to the module path, so that modules of the parent project with the same name, such as kconfig or ccache, do not replace them. Standalone builds are unchanged. Assisted-by: Claude:opus-5.5 Signed-off-by: Benjamin Cabé --- CMakeLists.txt | 47 ++++++++++++++++++++++++++++++--------------- cmake/kconfig.cmake | 25 +++++++++++++++++++----- 2 files changed, 51 insertions(+), 21 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 233c4bd2bfa..94c9a574557 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -104,7 +104,17 @@ cmake_minimum_required(VERSION 3.10 FATAL_ERROR) set(PX4_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}" CACHE FILEPATH "PX4 source directory" FORCE) set(PX4_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}" CACHE FILEPATH "PX4 binary directory" FORCE) -list(APPEND CMAKE_MODULE_PATH ${PX4_SOURCE_DIR}/cmake) +# PX4 can be built as a subproject, with add_subdirectory() from another +# project, which then owns the build type, the compiler launcher, the CMake +# cache and the firmware image. +if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR) + set(PX4_SUBPROJECT OFF) +else() + set(PX4_SUBPROJECT ON) +endif() + +# prepend, so that the PX4 modules take precedence over those of a parent project +set(CMAKE_MODULE_PATH ${PX4_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH}) include(px4_parse_function_args) #============================================================================= @@ -238,7 +248,7 @@ endif() set_property(GLOBAL PROPERTY PX4_MODULE_CONFIG_FILES) include(platforms/${PX4_PLATFORM}/cmake/px4_impl_os.cmake) -list(APPEND CMAKE_MODULE_PATH ${PX4_SOURCE_DIR}/platforms/${PX4_PLATFORM}/cmake) +set(CMAKE_MODULE_PATH ${PX4_SOURCE_DIR}/platforms/${PX4_PLATFORM}/cmake ${CMAKE_MODULE_PATH}) if(EXISTS "${PX4_SOURCE_DIR}/platforms/${PX4_PLATFORM}/cmake/init.cmake") include(init) @@ -272,7 +282,7 @@ if(APPLE) endif() # CMake build type (Debug Release RelWithDebInfo MinSizeRel Coverage) -if(NOT CMAKE_BUILD_TYPE) +if(NOT CMAKE_BUILD_TYPE AND NOT PX4_SUBPROJECT) if(${PX4_PLATFORM} STREQUAL "nuttx") set(PX4_BUILD_TYPE "MinSizeRel") else() @@ -375,7 +385,9 @@ if(${PX4_PLATFORM} STREQUAL "posix") include(mavsdk_tests) endif() -include(ccache) +if(NOT PX4_SUBPROJECT) + include(ccache) +endif() #============================================================================= # get chip and chip manufacturer @@ -436,7 +448,9 @@ include(px4_metadata) add_subdirectory(msg EXCLUDE_FROM_ALL) -px4_generate_airframes_xml(BOARD ${PX4_BOARD}) +if(NOT PX4_SUBPROJECT) + px4_generate_airframes_xml(BOARD ${PX4_BOARD}) +endif() #============================================================================= # external projects @@ -533,19 +547,20 @@ add_custom_command(OUTPUT ${uorb_graph_config} add_custom_target(uorb_graph DEPENDS ${uorb_graph_config}) -include(bloaty) +if(NOT PX4_SUBPROJECT) + include(bloaty) + include(metadata) + include(package) + include(sbom) -include(metadata) -include(package) -include(sbom) - -# install python requirements using configured python -add_custom_target(install_python_requirements - COMMAND ${PYTHON_EXECUTABLE} -m pip install --break-system-packages --requirement ${PX4_SOURCE_DIR}/Tools/setup/requirements.txt - DEPENDS ${PX4_SOURCE_DIR}/Tools/setup/requirements.txt - USES_TERMINAL -) + # install python requirements using configured python + add_custom_target(install_python_requirements + COMMAND ${PYTHON_EXECUTABLE} -m pip install --break-system-packages --requirement ${PX4_SOURCE_DIR}/Tools/setup/requirements.txt + DEPENDS ${PX4_SOURCE_DIR}/Tools/setup/requirements.txt + USES_TERMINAL + ) +endif() if(EXISTS "${PX4_SOURCE_DIR}/platforms/${PX4_PLATFORM}/cmake/finalize.cmake") include(finalize) diff --git a/cmake/kconfig.cmake b/cmake/kconfig.cmake index 154cae88b62..d11f101fc95 100644 --- a/cmake/kconfig.cmake +++ b/cmake/kconfig.cmake @@ -1,5 +1,12 @@ -set(BOARD_DEFCONFIG ${PX4_CONFIG_FILE} CACHE FILEPATH "path to defconfig" FORCE) -set(BOARD_CONFIG ${PX4_BINARY_DIR}/boardconfig CACHE FILEPATH "path to config" FORCE) +# The board configuration of a subproject is kept in normal variables, out of +# the CMake cache of the parent project. +if(PX4_SUBPROJECT) + set(BOARD_DEFCONFIG ${PX4_CONFIG_FILE}) + set(BOARD_CONFIG ${PX4_BINARY_DIR}/boardconfig) +else() + set(BOARD_DEFCONFIG ${PX4_CONFIG_FILE} CACHE FILEPATH "path to defconfig" FORCE) + set(BOARD_CONFIG ${PX4_BINARY_DIR}/boardconfig CACHE FILEPATH "path to config" FORCE) +endif() execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "import menuconfig" RESULT_VARIABLE ret) if(ret EQUAL "1") @@ -127,14 +134,22 @@ if(EXISTS ${BOARD_DEFCONFIG}) string(REPLACE "\"" "" Value ${Value}) # Set the variable - set(${Name} ${Value} CACHE INTERNAL "BOARD DEFCONFIG: ${Name}" FORCE) + if(PX4_SUBPROJECT) + set(${Name} "${Value}") + else() + set(${Name} ${Value} CACHE INTERNAL "BOARD DEFCONFIG: ${Name}" FORCE) + endif() else() # Find boolean not set string(REGEX MATCH " (CONFIG[^ ]+) is not set" Name ${NameAndValue}) if(${CMAKE_MATCH_1}) - set(${CMAKE_MATCH_1} "" CACHE INTERNAL "BOARD DEFCONFIG: ${CMAKE_MATCH_1}" FORCE) + if(PX4_SUBPROJECT) + set(${CMAKE_MATCH_1} "") + else() + set(${CMAKE_MATCH_1} "" CACHE INTERNAL "BOARD DEFCONFIG: ${CMAKE_MATCH_1}" FORCE) + endif() endif() endif() @@ -287,7 +302,7 @@ if(EXISTS ${BOARD_DEFCONFIG}) if(PLATFORM) # set OS, and append specific platform module path set(PX4_PLATFORM ${PLATFORM} CACHE STRING "PX4 board OS" FORCE) - list(APPEND CMAKE_MODULE_PATH ${PX4_SOURCE_DIR}/platforms/${PX4_PLATFORM}/cmake) + set(CMAKE_MODULE_PATH ${PX4_SOURCE_DIR}/platforms/${PX4_PLATFORM}/cmake ${CMAKE_MODULE_PATH}) # platform-specific include path include_directories(${PX4_SOURCE_DIR}/platforms/${PX4_PLATFORM}/src/px4/common/include)