fix(build): add kconfig support for fortified toolchains

This commit is contained in:
Onur Özkan 2026-04-01 09:23:50 +03:00 committed by GitHub
parent 94580ab1e5
commit 7d392394dd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 40 additions and 7 deletions

View File

@ -240,8 +240,15 @@ if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE ${PX4_BUILD_TYPE} CACHE STRING "Build type" FORCE) set(CMAKE_BUILD_TYPE ${PX4_BUILD_TYPE} CACHE STRING "Build type" FORCE)
endif() endif()
if(CONFIG_BOARD_SUPPORT_FORTIFIED_TOOLCHAIN)
set(PX4_DEBUG_OPT_LEVEL -Og)
message(STATUS "fortified toolchain support enabled: PX4_DEBUG_OPT_LEVEL=${PX4_DEBUG_OPT_LEVEL}")
else()
set(PX4_DEBUG_OPT_LEVEL -O0)
endif()
if((CMAKE_BUILD_TYPE STREQUAL "Debug") OR (CMAKE_BUILD_TYPE STREQUAL "Coverage")) if((CMAKE_BUILD_TYPE STREQUAL "Debug") OR (CMAKE_BUILD_TYPE STREQUAL "Coverage"))
set(MAX_CUSTOM_OPT_LEVEL -O0) set(MAX_CUSTOM_OPT_LEVEL ${PX4_DEBUG_OPT_LEVEL})
elseif(CMAKE_BUILD_TYPE MATCHES "Sanitizer") elseif(CMAKE_BUILD_TYPE MATCHES "Sanitizer")
set(MAX_CUSTOM_OPT_LEVEL -O1) set(MAX_CUSTOM_OPT_LEVEL -O1)
elseif(CMAKE_BUILD_TYPE MATCHES "Release") elseif(CMAKE_BUILD_TYPE MATCHES "Release")

10
Kconfig
View File

@ -67,6 +67,16 @@ menu "Toolchain"
help help
Enables Cmake Release for -O3 optimization Enables Cmake Release for -O3 optimization
config BOARD_SUPPORT_FORTIFIED_TOOLCHAIN
bool "Fortified toolchain support"
default n
help
Enable compatibility with toolchains that define
_FORTIFY_SOURCE.
This switches PX4_DEBUG_OPT_LEVEL from -O0 to -Og. Keep this
disabled unless the fortified toolchain requires optimization.
config BOARD_ROMFSROOT config BOARD_ROMFSROOT
string "ROMFSROOT" string "ROMFSROOT"
default "px4fmu_common" default "px4fmu_common"

View File

@ -52,10 +52,10 @@ endif()
# add code coverage build type # add code coverage build type
if (("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") OR ("${CMAKE_CXX_COMPILER_ID}" MATCHES "AppleClang")) if (("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") OR ("${CMAKE_CXX_COMPILER_ID}" MATCHES "AppleClang"))
set(CMAKE_C_FLAGS_COVERAGE "--coverage -ftest-coverage -fdiagnostics-absolute-paths -O0 -fprofile-arcs -fno-inline-functions" set(CMAKE_C_FLAGS_COVERAGE "--coverage -ftest-coverage -fdiagnostics-absolute-paths ${PX4_DEBUG_OPT_LEVEL} -fprofile-arcs -fno-inline-functions"
CACHE STRING "Flags used by the C compiler during coverage builds" FORCE) CACHE STRING "Flags used by the C compiler during coverage builds" FORCE)
set(CMAKE_CXX_FLAGS_COVERAGE "--coverage -ftest-coverage -fdiagnostics-absolute-paths -O0-fprofile-arcs -fno-inline-functions -fno-elide-constructors" set(CMAKE_CXX_FLAGS_COVERAGE "--coverage -ftest-coverage -fdiagnostics-absolute-paths ${PX4_DEBUG_OPT_LEVEL} -fprofile-arcs -fno-inline-functions -fno-elide-constructors"
CACHE STRING "Flags used by the C++ compiler during coverage builds" FORCE) CACHE STRING "Flags used by the C++ compiler during coverage builds" FORCE)
set(CMAKE_EXE_LINKER_FLAGS_COVERAGE "-ftest-coverage -fdiagnostics-absolute-paths" set(CMAKE_EXE_LINKER_FLAGS_COVERAGE "-ftest-coverage -fdiagnostics-absolute-paths"
@ -63,11 +63,11 @@ if (("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") OR ("${CMAKE_CXX_COMPILER_ID}"
else() else()
# Add -fprofile-abs-path for GCC v8/9 later on # Add -fprofile-abs-path for GCC v8/9 later on
set(CMAKE_C_FLAGS_COVERAGE "--coverage -ftest-coverage -fprofile-arcs -O0 -fno-default-inline -fno-inline" set(CMAKE_C_FLAGS_COVERAGE "--coverage -ftest-coverage -fprofile-arcs ${PX4_DEBUG_OPT_LEVEL} -fno-default-inline -fno-inline"
CACHE STRING "Flags used by the C compiler during coverage builds" FORCE) CACHE STRING "Flags used by the C compiler during coverage builds" FORCE)
# Add -fprofile-abs-path for GCC v8/9 later on # Add -fprofile-abs-path for GCC v8/9 later on
set(CMAKE_CXX_FLAGS_COVERAGE "--coverage -ftest-coverage -fprofile-arcs -O0 -fno-default-inline -fno-inline -fno-elide-constructors" set(CMAKE_CXX_FLAGS_COVERAGE "--coverage -ftest-coverage -fprofile-arcs ${PX4_DEBUG_OPT_LEVEL} -fno-default-inline -fno-inline -fno-elide-constructors"
CACHE STRING "Flags used by the C++ compiler during coverage builds" FORCE) CACHE STRING "Flags used by the C++ compiler during coverage builds" FORCE)
set(CMAKE_EXE_LINKER_FLAGS_COVERAGE "--coverage -ftest-coverage -lgcov" set(CMAKE_EXE_LINKER_FLAGS_COVERAGE "--coverage -ftest-coverage -lgcov"

View File

@ -70,3 +70,19 @@ The command line and GUI interfaces are shown below.
### menuconfig Command Line Interface ### menuconfig Command Line Interface
![menuconfig command line interface](../../assets/hardware/kconfig-guiconfig.png) ![menuconfig command line interface](../../assets/hardware/kconfig-guiconfig.png)
## Fortified Toolchain Compatibility
Some toolchains define `_FORTIFY_SOURCE` by default. Those toolchains generally require some optimization, which means PX4 configurations that use `-O0` may fail.
PX4 keeps the default debug optimization unchanged unless you explicitly opt in. To switch `PX4_DEBUG_OPT_LEVEL` from `-O0` to `-Og`, enable:
- `Toolchain > Fortified toolchain support`
This is the Kconfig symbol:
```sh
CONFIG_BOARD_SUPPORT_FORTIFIED_TOOLCHAIN=y
```
You can set it either in `boardconfig`/`boardguiconfig` or directly in your board's `*.px4board` file.

View File

@ -109,7 +109,7 @@ px4_add_module(
COMPILE_FLAGS COMPILE_FLAGS
#-DCANARD_ASSERT #-DCANARD_ASSERT
-DUINT32_C\(x\)=__UINT32_C\(x\) -DUINT32_C\(x\)=__UINT32_C\(x\)
-O0 ${PX4_DEBUG_OPT_LEVEL}
${DRIVERS_CYPHAL_OPTIONS} ${DRIVERS_CYPHAL_OPTIONS}
INCLUDES INCLUDES
${LIBCANARD_DIR}/libcanard/ ${LIBCANARD_DIR}/libcanard/

View File

@ -57,5 +57,5 @@ target_compile_options(world_magnetic_model PRIVATE ${MAX_CUSTOM_OPT_LEVEL})
if(BUILD_TESTING) if(BUILD_TESTING)
px4_add_unit_gtest(SRC test_geo_lookup.cpp LINKLIBS world_magnetic_model) px4_add_unit_gtest(SRC test_geo_lookup.cpp LINKLIBS world_magnetic_model)
target_compile_options(unit-test_geo_lookup PRIVATE -O0 -Wno-double-promotion) target_compile_options(unit-test_geo_lookup PRIVATE ${PX4_DEBUG_OPT_LEVEL} -Wno-double-promotion)
endif() endif()