From c2875ad6a222aed80d3b38218d9ef94f148e91db Mon Sep 17 00:00:00 2001 From: Matthias Grob Date: Mon, 16 Nov 2026 14:01:38 +0100 Subject: [PATCH] perf(nuttx): build the NuttX libraries with parallel make jobs Each NuttX library and the apps are built by a make invocation that ninja treats as a single opaque custom command, so they compiled one file at a time. Run them through a wrapper that honours a job count given to the toplevel build (e.g. `make px4_fmu-v5x_default -j1`), which reaches it through MAKEFLAGS, and otherwise uses the number of CPUs available to the process. Unlike CMake's logical core count, nproc respects docker --cpuset-cpus. A load limit keeps the concurrently running NuttX makes from oversubscribing the host. Mark the toplevel cmake build recipe as a sub-make with '+'. GNU make before 4.4 (e.g. Ubuntu 22.04/24.04) passes its jobserver as pipe file descriptors, which it otherwise closes for that recipe, leaving the NuttX makes to fall back to serial builds. Mention how to disable parallel builds in the docker docs. Assisted-by: Claude Code (claude-opus-5-5) --- Makefile | 2 +- docs/en/test_and_ci/docker.md | 2 +- platforms/nuttx/NuttX/CMakeLists.txt | 8 ++++++-- platforms/nuttx/NuttX/tools/px4_nuttx_make.sh | 16 ++++++++++++++++ 4 files changed, 24 insertions(+), 4 deletions(-) create mode 100755 platforms/nuttx/NuttX/tools/px4_nuttx_make.sh diff --git a/Makefile b/Makefile index 2afc9ab473e..e87790c93fd 100644 --- a/Makefile +++ b/Makefile @@ -210,7 +210,7 @@ define cmake-build || (rm -rf $(BUILD_DIR)); \ fi @# run the build for the specified target - @cmake --build $(BUILD_DIR) -- $(PX4_MAKE_ARGS) $(ARGS) + +@cmake --build $(BUILD_DIR) -- $(PX4_MAKE_ARGS) $(ARGS) endef # 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 diff --git a/docs/en/test_and_ci/docker.md b/docs/en/test_and_ci/docker.md index 82308fe62f0..c8942eef73f 100644 --- a/docs/en/test_and_ci/docker.md +++ b/docs/en/test_and_ci/docker.md @@ -256,7 +256,7 @@ The bug is not reproducible, so it is likely a hardware or OS problem. c++: internal compiler error: Killed (program cc1plus) ``` -Try disabling parallel builds. +Try disabling parallel builds, e.g. `make px4_fmu-v5x_default -j1`. **Allow Docker Control from the VM Host** diff --git a/platforms/nuttx/NuttX/CMakeLists.txt b/platforms/nuttx/NuttX/CMakeLists.txt index 3f6134d0e37..bc22671b711 100644 --- a/platforms/nuttx/NuttX/CMakeLists.txt +++ b/platforms/nuttx/NuttX/CMakeLists.txt @@ -38,6 +38,10 @@ set(APPS_DIR ${NUTTX_SRC_DIR}/apps) # NuttX build ############################################################################### +# The NuttX libraries and apps are built by make invocations that ninja treats +# as single opaque steps. Run them through a wrapper that adds parallel jobs. +set(NUTTX_MAKE ${CMAKE_CURRENT_SOURCE_DIR}/tools/px4_nuttx_make.sh) + configure_file(${PX4_SOURCE_DIR}/platforms/nuttx/NuttX/Make.defs.in ${CMAKE_CURRENT_BINARY_DIR}/nuttx/Make.defs) set(EXTRA_NUTTX_CONFIG_FILE ${CMAKE_CURRENT_BINARY_DIR}/extra_config_options) @@ -155,7 +159,7 @@ add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/apps/libapps.a COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_CURRENT_BINARY_DIR}/px4.pdat ${APPS_DIR}/builtin/registry/px4.pdat COMMAND ${CMAKE_COMMAND} -E touch_nocreate ${APPS_DIR}/builtin/registry/.updated COMMAND ${CMAKE_COMMAND} -E touch_nocreate ${APPS_DIR}/builtin/builtin_list.c - COMMAND make --no-print-directory --silent TOPDIR="${NUTTX_DIR}" > ${CMAKE_CURRENT_BINARY_DIR}/nuttx_apps.log + COMMAND ${NUTTX_MAKE} --no-print-directory --silent TOPDIR="${NUTTX_DIR}" > ${CMAKE_CURRENT_BINARY_DIR}/nuttx_apps.log COMMAND ${CMAKE_COMMAND} -E copy_if_different ${APPS_DIR}/libapps.a ${CMAKE_CURRENT_BINARY_DIR}/apps/libapps.a DEPENDS ${nuttx_apps_files} nuttx_context ${NUTTX_DIR}/include/nuttx/config.h ${CMAKE_CURRENT_BINARY_DIR}/px4.bdat @@ -190,7 +194,7 @@ function(add_nuttx_dir nuttx_lib nuttx_lib_dir kernel extra target) add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/nuttx/${nuttx_lib_dir}/lib${nuttx_lib}.a BYPRODUCTS ${nuttx_${nuttx_lib}_byproducts} - COMMAND make -C ${nuttx_lib_dir} --no-print-directory --silent ${nuttx_lib_target} TOPDIR="${NUTTX_DIR}" KERNEL=${kernel} EXTRAFLAGS=${extra} ${bindir_arg} + COMMAND ${NUTTX_MAKE} -C ${nuttx_lib_dir} --no-print-directory --silent ${nuttx_lib_target} TOPDIR="${NUTTX_DIR}" KERNEL=${kernel} EXTRAFLAGS=${extra} ${bindir_arg} COMMAND ${CMAKE_COMMAND} -E copy_if_different ${NUTTX_DIR}/${nuttx_lib_dir}/lib${nuttx_lib}.a ${CMAKE_CURRENT_BINARY_DIR}/nuttx/${nuttx_lib_dir}/lib${nuttx_lib}.a DEPENDS ${nuttx_lib_files} diff --git a/platforms/nuttx/NuttX/tools/px4_nuttx_make.sh b/platforms/nuttx/NuttX/tools/px4_nuttx_make.sh new file mode 100755 index 00000000000..d7197f8b1f2 --- /dev/null +++ b/platforms/nuttx/NuttX/tools/px4_nuttx_make.sh @@ -0,0 +1,16 @@ +#!/bin/sh +# Run make for a NuttX library or the NuttX apps with parallel jobs. +# +# If the build was started with an explicit job count, e.g. +# `make px4_fmu-v5x_default -j1`, MAKEFLAGS carries it (and with GNU make 4.4 +# its jobserver) through ninja to here, so honour it. Otherwise use the number +# of CPUs this process may run on, which, unlike the host's core count, +# respects limits like docker --cpuset-cpus. The load limit keeps the NuttX +# makes that ninja runs concurrently from oversubscribing the host. + +case " ${MAKEFLAGS} " in + *" -j"*) exec make "$@" ;; +esac + +jobs=$(nproc 2>/dev/null || getconf _NPROCESSORS_ONLN 2>/dev/null || echo 1) +exec make -j"${jobs}" -l"${jobs}" "$@"