From 7b4f554f393ba251f2f189d383b68793ca1d2563 Mon Sep 17 00:00:00 2001 From: chao an Date: Thu, 11 Jul 2024 16:50:31 +0800 Subject: [PATCH] cmake: strip file full path to save the code size Cmake build provide absolute paths to compile files. If __FILE__ macros are used in the source code(ASSERT), the binary will be occupied by many invalid paths. This saves some memory, stops leaking user locations in binaries, makes failure logs more deterministic and most importantly makes builds more deterministic. Debuggers usually have a path mapping feature to ensure the files are still found. Test config sabre-6quad/citest: Before: $ size build/nuttx text data bss dec hex filename 279309 908 13652 293869 47bed build/nuttx After: $ size build/nuttx text data bss dec hex filename 269313 908 13652 283873 454e1 build/nuttx Signed-off-by: chao an --- CMakeLists.txt | 15 +++++++++++++++ Kconfig | 16 ++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index b152fe01d2..d9a3832dbd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -173,6 +173,7 @@ if(NOT EXISTS "${NUTTX_APPS_DIR}") message(FATAL_ERROR "Application directory ${NUTTX_APPS_DIR} is not found") endif() +get_filename_component(NUTTX_APPS_DIR ${NUTTX_APPS_DIR} ABSOLUTE) get_filename_component(apps_dir ${NUTTX_APPS_DIR} NAME) set(NUTTX_APPS_BINDIR "${CMAKE_BINARY_DIR}/${apps_dir}") @@ -513,6 +514,20 @@ if(CONFIG_NDEBUG) add_compile_options(-DNDEBUG) endif() +# Cmake build provide absolute paths to compile files. If __FILE__ macros are +# used in the source code(ASSERT), the binary will contain many invalid paths. +# This saves some memory, stops exposing build systems locations in binaries, +# make failure logs more deterministic and most importantly makes builds more +# failure logs more deterministic and most importantly makes builds more +# deterministic. Debuggers usually have a path mapping feature to ensure the +# files are still found. +if(CONFIG_OUTPUT_STRIP_PATHS) + add_compile_options(-fmacro-prefix-map=${NUTTX_DIR}=) + add_compile_options(-fmacro-prefix-map=${NUTTX_APPS_DIR}=) + add_compile_options(-fmacro-prefix-map=${NUTTX_BOARD_ABS_DIR}=) + add_compile_options(-fmacro-prefix-map=${NUTTX_CHIP_ABS_DIR}=) +endif() + add_definitions(-D__NuttX__) set_property( diff --git a/Kconfig b/Kconfig index 0550e05c99..2f9a933011 100644 --- a/Kconfig +++ b/Kconfig @@ -447,6 +447,22 @@ config DFU_PID hex "PID to use for DFU image" endif # DFU_BINARY + +config OUTPUT_STRIP_PATHS + bool "Strip absolute paths from binaries" + default y + depends on ARCH_TOOLCHAIN_GNU + ---help--- + Cmake build provide absolute paths to compile files. If __FILE__ + macros are used in the source code(ASSERT), the binary will contain + many invalid paths. + This saves some memory, stops exposing build systems locations in binaries, + make failure logs more deterministic and most importantly makes builds more + failure logs more deterministic and most importantly makes builds more + deterministic. + Debuggers usually have a path mapping feature to ensure the files are + still found. + endmenu # Binary Output Formats menu "Customize Header Files"